HtmlParserExtensions
Extensions to be used exclusively by the parser or the tokenizer.
using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Extensions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace AngleSharp.Parser.Html
{
[DebuggerStepThrough]
internal static class HtmlParserExtensions
{
public static void SetAttributes(this Element element, List<KeyValuePair<string, string>> attributes)
{
for (int i = 0; i < attributes.Count; i++) {
KeyValuePair<string, string> keyValuePair = attributes[i];
element.Attributes.Add(new Attr(element, keyValuePair.Key, keyValuePair.Value));
element.AttributeChanged(keyValuePair.Key, null, null, true);
}
}
public static int GetCode(this HtmlParseError code)
{
return (int)code;
}
public static void SetUniqueAttributes(this Element element, List<KeyValuePair<string, string>> attributes)
{
for (int num = attributes.Count - 1; num >= 0; num--) {
if (element.HasAttribute(attributes[num].Key))
attributes.RemoveAt(num);
}
element.SetAttributes(attributes);
}
public static void AddFormatting(this List<Element> formatting, Element element)
{
int num = 0;
for (int num2 = formatting.Count - 1; num2 >= 0; num2--) {
Element element2 = formatting[num2];
if (element2 == null)
break;
if (element2.NodeName == element.NodeName && element2.NamespaceUri == element.NamespaceUri && element2.Attributes.AreEqual(element.Attributes) && ++num == 3) {
formatting.RemoveAt(num2);
break;
}
}
formatting.Add(element);
}
public static void ClearFormatting(this List<Element> formatting)
{
while (formatting.Count != 0) {
int index = formatting.Count - 1;
Element element = formatting[index];
formatting.RemoveAt(index);
if (element == null)
break;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddScopeMarker(this List<Element> formatting)
{
formatting.Add(null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddComment(this Node parent, HtmlToken token)
{
parent.AddNode(new Comment(parent.Owner, token.Data));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsTableElement(this INode node)
{
if (!(node is IHtmlTableElement) && !(node is IHtmlTableSectionElement))
return node is IHtmlTableRowElement;
return true;
}
}
}