AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.2.3" />

.NET API 371,712 bytes

 HTMLDocument

Represents an HTML document.
using AngleSharp.DOM.Collections; using AngleSharp.DOM.Css; using AngleSharp.Html; using System; using System.IO; namespace AngleSharp.DOM.Html { public sealed class HTMLDocument : Document, IHTMLDocument, IDocument, INode, IQueryElements { private bool embedded; private bool scripting; private HTMLCollection forms; private HTMLCollection scripts; private HTMLCollection images; private HTMLBodyElement body; private HTMLHeadElement head; private HTMLTitleElement title; private static readonly CompoundSelector anchorQuery = CompoundSelector.Create(SimpleSelector.Type("a"), SimpleSelector.AttrAvailable("name")); private static readonly ListSelector embedQuery = ListSelector.Create(SimpleSelector.Type("embed"), SimpleSelector.Type("object"), SimpleSelector.Type("applet")); private static readonly ListSelector linkQuery = ListSelector.Create(CompoundSelector.Create(SimpleSelector.Type("a"), SimpleSelector.AttrAvailable("href")), CompoundSelector.Create(SimpleSelector.Type("area"), SimpleSelector.AttrAvailable("href"))); public HTMLCollection Anchors => _children.QuerySelectorAll(anchorQuery); public HTMLCollection Forms => forms; public HTMLCollection Images => images; public HTMLCollection Scripts => scripts; public HTMLCollection Embeds => _children.QuerySelectorAll(embedQuery); public HTMLCollection Links => _children.QuerySelectorAll(linkQuery); public string Title { get { if (title != null) return title.Text; return string.Empty; } set { if (title == null) { if (documentElement == null) AppendChild(new HTMLHtmlElement()); if (head == null) documentElement.AppendChild(new HTMLHeadElement()); head.AppendChild(new HTMLTitleElement()); } title.Text = value; } } public HTMLHeadElement Head => head; public HTMLBodyElement Body => body; public string CompatMode { get { if (base.QuirksMode != QuirksMode.On) return "CSS1Compat"; return "BackCompat"; } } public Cookie Cookie { get { return null; } set { } } public string Domain => new Uri(base.DocumentURI).Host; public string URL => base.DocumentURI; internal bool IsEmbedded { get { return embedded; } set { embedded = value; } } internal bool IsScripting { get { return scripting; } set { scripting = value; } } internal int ScriptsWaiting => 0; internal int ScriptsAsSoonAsPossible => 0; internal bool IsLoadingDelayed => false; internal bool IsInBrowsingContext => false; internal bool IsToBePrinted { get; set; } public HTMLDocument() { _ns = Namespaces.Html; forms = new HTMLCollection(); scripts = new HTMLCollection(); images = new HTMLCollection(); } public static HTMLDocument LoadFromURL(string url) { HTMLDocument hTMLDocument = new HTMLDocument(); return hTMLDocument.Load(url); } public static HTMLDocument LoadFromSource(string source) { return DocumentBuilder.Html(source); } public HTMLDocument Load(string url) { location = url; Cookie = new Cookie(); for (int i = _children.Length - 1; i >= 0; i++) { RemoveChild(_children[i]); } base.ReadyState = Readiness.Loading; base.QuirksMode = QuirksMode.Off; Stream stream = Builder.Stream(url); SourceManager source = new SourceManager(stream); HtmlParser htmlParser = new HtmlParser(this, source); return htmlParser.Result; } public override Node CloneNode(bool deep = true) { HTMLDocument hTMLDocument = new HTMLDocument(); Node.CopyProperties(this, hTMLDocument, deep); Document.CopyDocumentProperties(this, hTMLDocument, deep); hTMLDocument.embedded = embedded; hTMLDocument.scripting = scripting; return hTMLDocument; } public Node AdoptNode(Node externalNode) { if (externalNode.OwnerDocument != null && externalNode.ParentNode != null) externalNode.ParentNode.RemoveChild(externalNode); externalNode.OwnerDocument = this; return externalNode; } public HTMLDocument Open() { return this; } public HTMLDocument Close() { return this; } public HTMLDocument Write(string content) { return this; } public HTMLDocument WriteLn(string content) { return Write(content + '\n'); } public override Element CreateElement(string tagName) { return HTMLElement.Factory(tagName); } public override CDATASection CreateCDATASection(string data) { throw new DOMException(ErrorCode.NotSupported); } public HTMLCollection GetElementsByName(string name) { HTMLCollection result = new HTMLCollection(); GetElementsByName(_children, name, result); return result; } internal override void ReferenceNode(Node node) { base.ReferenceNode(node); if (node is Element) { if (node is HTMLFormElement) { HTMLFormElement node2 = (HTMLFormElement)node; forms.Add(node2); } else if (node is HTMLImageElement) { HTMLImageElement node3 = (HTMLImageElement)node; images.Add(node3); } else if (node is HTMLScriptElement) { HTMLScriptElement node4 = (HTMLScriptElement)node; scripts.Add(node4); } else if (body == null && node is HTMLBodyElement) { body = (HTMLBodyElement)node; } else if (head == null && node is HTMLHeadElement) { head = (HTMLHeadElement)node; } else if (title == null && node is HTMLTitleElement) { title = (HTMLTitleElement)node; } } } internal override void DereferenceNode(Node node) { base.DereferenceNode(node); if (node is Element) { if (node is HTMLFormElement) { HTMLFormElement node2 = (HTMLFormElement)node; forms.Remove(node2); } else if (node is HTMLImageElement) { HTMLImageElement node3 = (HTMLImageElement)node; images.Remove(node3); } else if (node is HTMLScriptElement) { HTMLScriptElement node4 = (HTMLScriptElement)node; scripts.Remove(node4); } else if (body == node) { body = Document.FindChild<HTMLBodyElement>(documentElement); } else if (head == node) { head = Document.FindChild<HTMLHeadElement>(documentElement); } else if (title == node) { title = Document.FindChild<HTMLTitleElement>(head); } } } internal void SpinEventLoop() { } internal void RaiseDomContentLoaded() { } internal void RaiseLoadedEvent() { } internal void QueueTask(Action action) { } internal void Print() { } private void GetElementsByName(NodeList children, string name, HTMLCollection result) { for (int i = 0; i < _children.Length; i++) { if (_children[i] is HTMLElement) { HTMLElement hTMLElement = (HTMLElement)_children[i]; if (hTMLElement.GetAttribute("name") == name) result.Add(hTMLElement); GetElementsByName(hTMLElement.ChildNodes, name, result); } } } } }