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.Html;
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>();
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 isScripting = context.Configuration.IsScripting();
HtmlParserOptions htmlParserOptions = default(HtmlParserOptions);
htmlParserOptions.IsScripting = isScripting;
HtmlParserOptions options2 = htmlParserOptions;
HtmlDocument document = new HtmlDocument(context, options.Source);
HtmlDomBuilder htmlDomBuilder = new HtmlDomBuilder(document);
document.Setup(options);
context.NavigateTo(document);
context.Fire(new HtmlParseEvent(document, false));
await htmlDomBuilder.ParseAsync(options2, cancelToken).ConfigureAwait(false);
context.Fire(new HtmlParseEvent(document, true));
return document;
}
internal static async Task<IDocument> LoadTextAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
{
bool flag2 = default(HtmlParserOptions).IsScripting = context.Configuration.IsScripting();
HtmlDocument document = new HtmlDocument(context, options.Source);
document.Setup(options);
context.NavigateTo(document);
IElement element = document.CreateElement(TagNames.Html);
IElement child = document.CreateElement(TagNames.Head);
IElement element2 = document.CreateElement(TagNames.Body);
IElement pre = document.CreateElement(TagNames.Pre);
document.AppendChild(element);
element.AppendChild(child);
element.AppendChild(element2);
element2.AppendChild(pre);
pre.SetAttribute(AttributeNames.Style, "word-wrap: break-word; white-space: pre-wrap;");
await options.Source.PrefetchAllAsync(cancelToken).ConfigureAwait(false);
pre.TextContent = options.Source.Text;
return document;
}
protected override string GetTitle()
{
IHtmlTitleElement htmlTitleElement = DocumentElement.FindDescendant<IHtmlTitleElement>();
return ((htmlTitleElement != null) ? htmlTitleElement.TextContent.CollapseAndStrip() : null) ?? base.GetTitle();
}
protected override void SetTitle(string value)
{
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;
}
}
}