BrowsingContextExtensions
A set of extensions for the browsing context.
using AngleSharp.Dom;
using AngleSharp.Extensions;
using AngleSharp.Network;
using AngleSharp.Services;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AngleSharp
{
public static class BrowsingContextExtensions
{
public static Task<IDocument> OpenNewAsync(this IBrowsingContext context, string url = null)
{
return context.OpenAsync(delegate(VirtualResponse m) {
m.Address(url);
});
}
public static Task<IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel)
{
if (response == null)
throw new ArgumentNullException("response");
if (context == null)
context = BrowsingContext.New(null);
CreateDocumentOptions options = new CreateDocumentOptions(response, context.Configuration, null);
return context.Configuration.GetFactory<IDocumentFactory>().CreateAsync(context, options, cancel);
}
public static async Task<IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel)
{
if (request == null)
throw new ArgumentNullException("request");
IDocumentLoader loader = context.Loader;
if (loader != null) {
IDownload download = loader.DownloadAsync(request);
IDownload download2 = download;
cancel.Register(download2.Cancel);
using (IResponse response = await download.Task.ConfigureAwait(false)) {
if (response != null)
return await context.OpenAsync(response, cancel).ConfigureAwait(false);
}
}
return await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false);
}
public static Task<IDocument> OpenAsync(this IBrowsingContext context, Url url, CancellationToken cancel)
{
if (url == null)
throw new ArgumentNullException("url");
DocumentRequest documentRequest = DocumentRequest.Get(url, null, null);
if (context != null && context.Active != null)
documentRequest.Referer = context.Active.DocumentUri;
return context.OpenAsync(documentRequest, cancel);
}
public static async Task<IDocument> OpenAsync(this IBrowsingContext context, Action<VirtualResponse> request, CancellationToken cancel)
{
if (request == null)
throw new ArgumentNullException("request");
using (IResponse response = VirtualResponse.Create(request))
return await context.OpenAsync(response, cancel).ConfigureAwait(false);
}
public static Task<IDocument> OpenAsync(this IBrowsingContext context, Action<VirtualResponse> request)
{
return context.OpenAsync(request, CancellationToken.None);
}
public static Task<IDocument> OpenAsync(this IBrowsingContext context, Url url)
{
return context.OpenAsync(url, CancellationToken.None);
}
public static Task<IDocument> OpenAsync(this IBrowsingContext context, string address)
{
if (address == null)
throw new ArgumentNullException("address");
return context.OpenAsync(Url.Create(address), CancellationToken.None);
}
public static void (this IBrowsingContext context, IDocument document)
{
context.SessionHistory?.PushState(document, document.Title, document.Url);
context.Active = document;
}
}
}