HtmlParserExtensions
Extensions to be used exclusively by the parser or the tokenizer.
using AngleSharp.Dom;
using AngleSharp.Dom.Collections;
using AngleSharp.Extensions;
using AngleSharp.Html;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace AngleSharp.Parser.Html
{
internal static class HtmlParserExtensions
{
public static void SetAttributes(this Element element, List<KeyValuePair<string, string>> attributes)
{
NamedNodeMap attributes2 = element.Attributes;
for (int i = 0; i < attributes.Count; i++) {
KeyValuePair<string, string> keyValuePair = attributes[i];
Attr attr = new Attr(keyValuePair.Key, keyValuePair.Value);
attributes2.FastAddItem(attr);
}
}
public static HtmlTreeMode? SelectMode(this Element element, bool isLast, Stack<HtmlTreeMode> templateModes)
{
string localName = element.LocalName;
if (localName.Is(TagNames.Select))
return HtmlTreeMode.InSelect;
if (TagNames.AllTableCells.Contains(localName))
return isLast ? HtmlTreeMode.InBody : HtmlTreeMode.InCell;
if (localName.Is(TagNames.Tr))
return HtmlTreeMode.InRow;
if (TagNames.AllTableSections.Contains(localName))
return HtmlTreeMode.InTableBody;
if (localName.Is(TagNames.Body))
return HtmlTreeMode.InBody;
if (localName.Is(TagNames.Table))
return HtmlTreeMode.InTable;
if (localName.Is(TagNames.Caption))
return HtmlTreeMode.InCaption;
if (localName.Is(TagNames.Colgroup))
return HtmlTreeMode.InColumnGroup;
if (localName.Is(TagNames.Template))
return templateModes.Peek();
if (localName.Is(TagNames.Html))
return HtmlTreeMode.BeforeHead;
if (localName.Is(TagNames.Head))
return isLast ? HtmlTreeMode.InBody : HtmlTreeMode.InHead;
if (localName.Is(TagNames.Frameset))
return HtmlTreeMode.InFrameset;
if (isLast)
return HtmlTreeMode.InBody;
return null;
}
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.Is(element.NodeName) && element2.NamespaceUri.Is(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));
}
public static QuirksMode GetQuirksMode(this HtmlDoctypeToken doctype)
{
if (doctype.IsFullQuirks)
return QuirksMode.On;
if (doctype.IsLimitedQuirks)
return QuirksMode.Limited;
return QuirksMode.Off;
}
}
}