HtmlDocument
sealed class HtmlDocument : Document, IHtmlDocument, IDocument, INode, IEventTarget, IMarkupFormattable, IParentNode, IGlobalEventHandlers, IDocumentStyle, INonElementParentNode, IDisposable
Represents a document node that contains only HTML nodes.
using AngleSharp.Dom.Events;
using AngleSharp.Extensions;
using AngleSharp.Network;
using AngleSharp.Parser.Html;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlDocument : Document, IHtmlDocument, IDocument, INode, IEventTarget, IMarkupFormattable, IParentNode, IGlobalEventHandlers, IDocumentStyle, INonElementParentNode, IDisposable
{
public override IElement DocumentElement => this.FindChild<HtmlHtmlElement>();
public override string Title {
get {
IHtmlTitleElement htmlTitleElement = DocumentElement.FindDescendant<IHtmlTitleElement>();
if (htmlTitleElement != null)
return htmlTitleElement.TextContent.CollapseAndStrip();
return string.Empty;
}
set {
IHtmlTitleElement htmlTitleElement = DocumentElement.FindDescendant<IHtmlTitleElement>();
if (htmlTitleElement == null) {
IHtmlHeadElement head = base.Head;
if (head == null)
return;
htmlTitleElement = new HtmlTitleElement(this, null);
head.AppendChild(htmlTitleElement);
}
htmlTitleElement.TextContent = value;
}
}
internal HtmlDocument(IBrowsingContext context, TextSource source)
: base(context ?? BrowsingContext.New(null), source)
{
base.ContentType = MimeTypeNames.Html;
}
internal HtmlDocument(IBrowsingContext context = null)
: this(context, new TextSource(string.Empty))
{
}
public override INode Clone(bool deep = true)
{
TextSource source = new TextSource(base.Source.Text);
HtmlDocument htmlDocument = new HtmlDocument(base.Context, source);
CloneDocument(htmlDocument, deep);
return htmlDocument;
}
internal static async Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
{
bool scripting = context.Configuration.IsScripting();
HtmlParserOptions parserOptions = new HtmlParserOptions {
IsScripting = scripting
};
HtmlDocument document = new HtmlDocument(context, options.Source);
HtmlDomBuilder parser = new HtmlDomBuilder(document) {
Error = (EventHandler<HtmlErrorEvent>)delegate(object _, HtmlErrorEvent error) {
context.Fire(error);
}
};
document.Setup(options);
context.NavigateTo(document);
context.Fire(new HtmlParseEvent(document, false));
await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);
context.Fire(new HtmlParseEvent(document, true));
return document;
}
}
}