DocumentExtensions
Useful methods for document objects.
using AngleSharp.Dom;
using AngleSharp.Dom.Collections;
using AngleSharp.Dom.Html;
using AngleSharp.Network;
using AngleSharp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AngleSharp.Extensions
{
internal static class DocumentExtensions
{
public static void ForEachRange(this Document document, Predicate<AngleSharp.Dom.Collections.Range> condition, Action<AngleSharp.Dom.Collections.Range> action)
{
foreach (AngleSharp.Dom.Collections.Range attachedReference in document.GetAttachedReferences<AngleSharp.Dom.Collections.Range>()) {
if (condition(attachedReference))
action(attachedReference);
}
}
public static void AdoptNode(this IDocument document, INode node)
{
Node node2 = node as Node;
if (node2 == null)
throw new DomException(DomError.NotSupported);
node2.Parent?.RemoveChild(node2, false);
node2.Owner = (document as Document);
}
public static void QueueTask(this Document document, Action action)
{
document.Loop.Enqueue(action, TaskPriority.Normal);
}
public static void QueueMutation(this Document document, MutationRecord record)
{
MutationObserver[] array = document.Mutations.Observers.ToArray();
if (array.Length != 0) {
IEnumerable<INode> inclusiveAncestors = record.Target.GetInclusiveAncestors();
foreach (MutationObserver mutationObserver in array) {
bool? nullable = null;
foreach (INode item in inclusiveAncestors) {
MutationObserver.MutationOptions mutationOptions = mutationObserver.ResolveOptions(item);
if (!mutationOptions.IsInvalid && (item == record.Target || mutationOptions.IsObservingSubtree) && (!record.IsAttribute || mutationOptions.IsObservingAttributes) && (!record.IsAttribute || mutationOptions.AttributeFilters == null || (mutationOptions.AttributeFilters.Contains(record.AttributeName) && record.AttributeNamespace == null)) && (!record.IsCharacterData || mutationOptions.IsObservingCharacterData) && (!record.IsChildList || mutationOptions.IsObservingChildNodes) && (!nullable.HasValue || nullable.Value))
nullable = ((record.IsAttribute && !mutationOptions.IsExaminingOldAttributeValue) || (record.IsCharacterData && !mutationOptions.IsExaminingOldCharacterData));
}
if (nullable.HasValue)
mutationObserver.Enqueue(record.Copy(nullable.Value));
}
document.PerformMicrotaskCheckpoint();
}
}
public static void AddTransientObserver(this Document document, INode node)
{
IEnumerable<INode> ancestors = node.GetAncestors();
IEnumerable<MutationObserver> observers = document.Mutations.Observers;
foreach (INode item in ancestors) {
foreach (MutationObserver item2 in observers) {
item2.AddTransient(item, node);
}
}
}
public static void ApplyManifest(this Document document)
{
if (document.IsInBrowsingContext) {
IHtmlHtmlElement htmlHtmlElement = document.DocumentElement as IHtmlHtmlElement;
if (htmlHtmlElement != null) {
string manifest = htmlHtmlElement.Manifest;
Predicate<string> predicate = (string str) => false;
if (!string.IsNullOrEmpty(manifest))
predicate(manifest);
}
}
}
public static void PerformMicrotaskCheckpoint(this Document document)
{
document.Mutations.ScheduleCallback();
}
public static void ProvideStableState(this Document document)
{
}
public static IEnumerable<Task> GetDownloads<T>(this Document document) where T : INode
{
IResourceLoader loader = document.Loader;
if (loader == null)
return Enumerable.Empty<Task>();
return from m in loader.GetDownloads()
where m.Originator is T
select ((ICancellable<IResponse>)m).Task;
}
public static IEnumerable<Task> GetScriptDownloads(this Document document)
{
return document.GetDownloads<HtmlScriptElement>();
}
public static IEnumerable<Task> GetStyleSheetDownloads(this Document document)
{
return document.GetDownloads<HtmlLinkElement>();
}
public static async Task WaitForReadyAsync(this Document document)
{
await TaskEx.WhenAll(document.GetScriptDownloads().ToArray()).ConfigureAwait(false);
await TaskEx.WhenAll(document.GetStyleSheetDownloads().ToArray()).ConfigureAwait(false);
}
public static IBrowsingContext GetTarget(this Document document, string target)
{
if (string.IsNullOrEmpty(target) || target.Is("_self"))
return document.Context;
if (target.Is("_parent"))
return document.Context.Parent ?? document.Context;
if (target.Is("_top"))
return document.Context;
return document.Options.FindContext(target);
}
public static IBrowsingContext CreateTarget(this Document document, string target)
{
Sandboxes security = Sandboxes.None;
if (target.Is("_blank"))
return document.Options.NewContext(security);
return document.NewContext(target, security);
}
public static IBrowsingContext NewContext(this Document document, string name, Sandboxes security)
{
return document.Options.GetFactory<IContextFactory>().Create(document.Context, name, security);
}
public static IBrowsingContext NewChildContext(this Document document, Sandboxes security)
{
IBrowsingContext browsingContext = document.NewContext(string.Empty, security);
document.AttachReference(browsingContext);
return browsingContext;
}
}
}