include cache update
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Solicitors.HtmlParsing;
|
||||
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddHtmlParsing(this IServiceCollection services)
|
||||
|
||||
@@ -33,7 +33,7 @@ internal class HtmlParser : IHtmlParser
|
||||
return ParseHtml(tokenEnumerator);
|
||||
}
|
||||
|
||||
private IEnumerable<string> LexHtml(IEnumerator<char> content)
|
||||
private IEnumerable<string> LexHtml(CharEnumerator content)
|
||||
{
|
||||
var work = "";
|
||||
while (content.MoveNext())
|
||||
@@ -85,14 +85,14 @@ internal class HtmlParser : IHtmlParser
|
||||
}
|
||||
}
|
||||
|
||||
bool IsVoidOrSelfClosingElement(string token)
|
||||
private bool IsVoidOrSelfClosingElement(string token)
|
||||
{
|
||||
if (token.StartsWith("<!") || token.EndsWith("/>"))
|
||||
return true;
|
||||
|
||||
var tagName = token.Split(' ').First().Substring(1);
|
||||
var tagName = token.Split(' ').First()[1..];
|
||||
if (tagName.EndsWith('>'))
|
||||
tagName = tagName.Substring(0, tagName.Length - 1);
|
||||
tagName = tagName[..^1];
|
||||
return _voidElements.Contains(tagName);
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ internal class HtmlParser : IHtmlParser
|
||||
{
|
||||
var attributesString = string.Join(' ', tokenNoBraces.Split(' ').Skip(1));
|
||||
var attString = "";
|
||||
bool inQuote = false;
|
||||
foreach (char c in attributesString)
|
||||
var inQuote = false;
|
||||
foreach (var c in attributesString)
|
||||
{
|
||||
if (!inQuote && c == ' ')
|
||||
{
|
||||
|
||||
@@ -2,19 +2,16 @@ using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Solicitors.HtmlParsing.Models;
|
||||
|
||||
internal class HtmlNode(string tagName, IHtmlAttribute[] attributes)
|
||||
internal class HtmlNode(string name, IHtmlAttribute[] attributes)
|
||||
: IHtmlNode
|
||||
{
|
||||
private readonly IHtmlAttribute[] _attributes = attributes;
|
||||
private readonly string _tagName = tagName;
|
||||
|
||||
public virtual bool TryGetByTagName(
|
||||
string tagName,
|
||||
[NotNullWhen(true)] out IHtmlNode? node,
|
||||
bool noChildren = false)
|
||||
{
|
||||
node = null;
|
||||
if (tagName == _tagName)
|
||||
if (tagName == name)
|
||||
node = this;
|
||||
|
||||
return node is not null;
|
||||
@@ -24,7 +21,7 @@ internal class HtmlNode(string tagName, IHtmlAttribute[] attributes)
|
||||
{
|
||||
get
|
||||
{
|
||||
var classAttribute = _attributes
|
||||
var classAttribute = attributes
|
||||
.Where(a => a is HtmlValueAttribute)
|
||||
.Select(a => (a as HtmlValueAttribute)!)
|
||||
.FirstOrDefault(a => a.Name.Equals("class", StringComparison.CurrentCultureIgnoreCase));
|
||||
@@ -55,7 +52,7 @@ internal class HtmlNode(string tagName, IHtmlAttribute[] attributes)
|
||||
bool noChildren = false)
|
||||
{
|
||||
node = null;
|
||||
if (tagName == _tagName && Classes.Contains(className, StringComparer.InvariantCultureIgnoreCase))
|
||||
if (tagName == name && Classes.Contains(className, StringComparer.InvariantCultureIgnoreCase))
|
||||
node = this;
|
||||
|
||||
return node is not null;
|
||||
@@ -75,7 +72,7 @@ internal class HtmlNode(string tagName, IHtmlAttribute[] attributes)
|
||||
|
||||
public bool HasAttribute(string attributeName)
|
||||
{
|
||||
return _attributes
|
||||
return attributes
|
||||
.Any(a => a.Name.Equals(attributeName, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
@@ -83,7 +80,7 @@ internal class HtmlNode(string tagName, IHtmlAttribute[] attributes)
|
||||
{
|
||||
attributeValue = null;
|
||||
|
||||
var matchingAttribute = _attributes.FirstOrDefault(
|
||||
var matchingAttribute = attributes.FirstOrDefault(
|
||||
a => a.Name.Equals(attributeName, StringComparison.InvariantCultureIgnoreCase)
|
||||
);
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@ namespace Solicitors.HtmlParsing.Models;
|
||||
internal class ParentHtmlNode(string tagName, IHtmlAttribute[] attributes, IHtmlNode[] children)
|
||||
: HtmlNode(tagName, attributes)
|
||||
{
|
||||
private readonly IHtmlNode[] _children = children;
|
||||
|
||||
public override bool TryGetByTagName(
|
||||
string tagName,
|
||||
[NotNullWhen(true)] out IHtmlNode? node,
|
||||
@@ -15,7 +13,7 @@ internal class ParentHtmlNode(string tagName, IHtmlAttribute[] attributes, IHtml
|
||||
node = null;
|
||||
if (!base.TryGetByTagName(tagName, out node, noChildren) && !noChildren)
|
||||
{
|
||||
var nodesToCheck = new Queue<IHtmlNode>(_children);
|
||||
var nodesToCheck = new Queue<IHtmlNode>(children);
|
||||
while (nodesToCheck.TryDequeue(out var child))
|
||||
{
|
||||
if (child.TryGetByTagName(tagName, out var childNode, true))
|
||||
@@ -41,7 +39,7 @@ internal class ParentHtmlNode(string tagName, IHtmlAttribute[] attributes, IHtml
|
||||
node = null;
|
||||
if (!base.TryGetByClass(className, out node, noChildren) && !noChildren)
|
||||
{
|
||||
var nodesToCheck = new Queue<IHtmlNode>(_children);
|
||||
var nodesToCheck = new Queue<IHtmlNode>(children);
|
||||
while (nodesToCheck.TryDequeue(out var child))
|
||||
{
|
||||
if (child.TryGetByClass(className, out var childNode, true))
|
||||
@@ -68,7 +66,7 @@ internal class ParentHtmlNode(string tagName, IHtmlAttribute[] attributes, IHtml
|
||||
node = null;
|
||||
if (!base.TryGetByTagNameAndClass(tagName, className, out node, noChildren) && !noChildren)
|
||||
{
|
||||
var nodesToCheck = new Queue<IHtmlNode>(_children);
|
||||
var nodesToCheck = new Queue<IHtmlNode>(children);
|
||||
while (nodesToCheck.TryDequeue(out var child))
|
||||
{
|
||||
if (child.TryGetByTagNameAndClass(tagName, className, out var childNode, true))
|
||||
@@ -86,9 +84,9 @@ internal class ParentHtmlNode(string tagName, IHtmlAttribute[] attributes, IHtml
|
||||
return node is not null;
|
||||
}
|
||||
|
||||
public override bool TryGetChildren([NotNullWhen(true)] out IEnumerable<IHtmlNode>? children)
|
||||
public override bool TryGetChildren([NotNullWhen(true)] out IEnumerable<IHtmlNode>? children1)
|
||||
{
|
||||
children = _children;
|
||||
return _children.Length != 0;
|
||||
children1 = children;
|
||||
return children.Length != 0;
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,6 @@ namespace Solicitors.HtmlParsing.Models;
|
||||
internal class StringNode(string content)
|
||||
: IHtmlNode
|
||||
{
|
||||
private readonly string _content = content;
|
||||
|
||||
public bool TryGetByTagName(
|
||||
string tagName,
|
||||
[NotNullWhen(true)] out IHtmlNode? node,
|
||||
@@ -37,7 +35,7 @@ internal class StringNode(string content)
|
||||
|
||||
public bool TryGetText([NotNullWhen(true)] out string? text)
|
||||
{
|
||||
text = _content;
|
||||
text = content;
|
||||
return !string.IsNullOrEmpty(text);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user