NodeList
Represents a list of Node instances or nodes.
using AngleSharp.Css;
using AngleSharp.DOM.Css;
using AngleSharp.DOM.Html;
using System;
namespace AngleSharp.DOM.Collections
{
[DOM("NodeList")]
public sealed class NodeList : BaseCollection<Node>
{
internal NodeList()
{
}
internal Element QuerySelector(string selectors)
{
Selector selector = CssParser.ParseSelector(selectors, false);
return QuerySelector(this, selector);
}
internal Element QuerySelector(Selector selectors)
{
return QuerySelector(this, selectors);
}
internal T QuerySelector<T>(Selector selectors) where T : Element
{
return QuerySelector(this, selectors) as T;
}
internal HTMLCollection QuerySelectorAll(string selectors)
{
Selector selector = CssParser.ParseSelector(selectors, false);
HTMLCollection result = new HTMLCollection();
QuerySelectorAll(this, selector, result);
return result;
}
internal HTMLCollection QuerySelectorAll(Selector selector)
{
HTMLCollection result = new HTMLCollection();
QuerySelectorAll(this, selector, result);
return result;
}
internal HTMLCollection GetElementsByClassName(string classNames)
{
HTMLCollection result = new HTMLCollection();
string[] array = classNames.SplitSpaces();
if (array.Length > 0)
GetElementsByClassName(this, array, result);
return result;
}
internal HTMLCollection GetElementsByTagName(string tagName)
{
HTMLCollection result = new HTMLCollection();
bool takeAll = tagName == "*";
GetElementsByTagName(this, tagName, result, takeAll);
return result;
}
internal HTMLCollection GetElementsByTagNameNS(string namespaceURI, string localName)
{
HTMLCollection result = new HTMLCollection();
GetElementsByTagNameNS(this, namespaceURI, localName, result);
return result;
}
private static Element QuerySelector(NodeList elements, Selector selector)
{
for (int i = 0; i < elements.Length; i++) {
Element element = elements[i] as Element;
if (element != null) {
if (selector.Match(element))
return element;
if (element.HasChildNodes) {
element = QuerySelector(element.ChildNodes, selector);
if (element != null)
return element;
}
}
}
return null;
}
private static void QuerySelectorAll(NodeList elements, Selector selector, HTMLCollection result)
{
for (int i = 0; i < elements.Length; i++) {
Element element = elements[i] as Element;
if (element != null) {
if (selector.Match(element))
result.Add(element);
if (element.HasChildNodes)
QuerySelectorAll(element.ChildNodes, selector, result);
}
}
}
private static void GetElementsByClassName(NodeList elements, string[] classNames, HTMLCollection result)
{
for (int i = 0; i < elements.Length; i++) {
HTMLElement hTMLElement = elements[i] as HTMLElement;
if (hTMLElement != null) {
if (hTMLElement.ClassList.Contains(classNames))
result.Add(hTMLElement);
if (hTMLElement.ChildElementCount != 0)
GetElementsByClassName(hTMLElement.ChildNodes, classNames, result);
}
}
}
private static void GetElementsByTagName(NodeList elements, string tagName, HTMLCollection result, bool takeAll)
{
for (int i = 0; i < elements.Length; i++) {
Element element = elements[i] as Element;
if (element != null) {
if (takeAll || element.NodeName.Equals(tagName, StringComparison.OrdinalIgnoreCase))
result.Add(element);
if (element.ChildElementCount != 0)
GetElementsByTagName(element.ChildNodes, tagName, result, takeAll);
}
}
}
private static void GetElementsByTagNameNS(NodeList elements, string namespaceURI, string localName, HTMLCollection result)
{
bool flag = localName == "*";
for (int i = 0; i < elements.Length; i++) {
Element element = elements[i] as Element;
if (element != null) {
if (element.NamespaceURI == namespaceURI && (flag || element.LocalName.Equals(localName, StringComparison.OrdinalIgnoreCase)))
result.Add(element);
if (element.ChildElementCount != 0)
GetElementsByTagNameNS(element.ChildNodes, namespaceURI, localName, result);
}
}
}
}
}