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.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 = MimeTypes.Html;
}
internal HtmlDocument(IBrowsingContext context = null)
: this(context, new TextSource(string.Empty))
{
}
public override INode Clone(bool deep = true)
{
HtmlDocument htmlDocument = new HtmlDocument(base.Context, new TextSource(base.Source.Text));
Node.CopyProperties(this, htmlDocument, deep);
Document.CopyDocumentProperties(this, htmlDocument, deep);
return htmlDocument;
}
internal static async Task<HtmlDocument> LoadAsync(IBrowsingContext context, IResponse response, MimeType contentType, TextSource source, CancellationToken cancelToken)
{
HtmlDocument document = new HtmlDocument(context, source);
HtmlParseStartEvent evt = new HtmlParseStartEvent(document);
IConfiguration config = context.Configuration;
IEventAggregator events = config.Events;
HtmlDomBuilder parser = new HtmlDomBuilder(document);
document.ContentType = contentType.Content;
document.Referrer = response.Headers.GetOrDefault(HeaderNames.Referer, string.Empty);
document.DocumentUri = response.Address.Href;
document.Cookie = response.Headers.GetOrDefault(HeaderNames.SetCookie, string.Empty);
document.ReadyState = DocumentReadyState.Loading;
context.NavigateTo(document);
events?.Publish(evt);
HtmlParserOptions options = new HtmlParserOptions {
IsScripting = config.IsScripting()
};
await parser.ParseAsync(options, cancelToken).ConfigureAwait(false);
evt.FireEnd();
return document;
}
}
}