AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.16.1-alpha-104" />

 BrowsingContextExtensions

public static class BrowsingContextExtensions
A set of extensions for the browsing context.
using AngleSharp.Browser; using AngleSharp.Browser.Dom.Events; using AngleSharp.Common; using AngleSharp.Css; using AngleSharp.Dom; using AngleSharp.Io; using AngleSharp.Media; using AngleSharp.Scripting; using AngleSharp.Text; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace AngleSharp { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class BrowsingContextExtensions { public static Task<IDocument> OpenNewAsync(this IBrowsingContext context, [System.Runtime.CompilerServices.Nullable(2)] string url = null, CancellationToken cancellation = default(CancellationToken)) { return context.OpenAsync(delegate(VirtualResponse m) { m.Address(url ?? "http://localhost/"); }, cancellation); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel = default(CancellationToken)) { IResponse response2 = response; if (response2 == null) throw new ArgumentNullException("response"); response = response2; if (context == null) context = BrowsingContext.New(null); Encoding defaultEncoding = context.GetDefaultEncoding(); IDocumentFactory factory = context.GetFactory<IDocumentFactory>(); CreateDocumentOptions options = new CreateDocumentOptions(response, defaultEncoding, null); return factory.CreateAsync(context, options, cancel); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel = default(CancellationToken)) { DocumentRequest documentRequest = request; if (documentRequest == null) throw new ArgumentNullException("request"); request = documentRequest; if (context == null) context = BrowsingContext.New(null); return context.NavigateToAsync(request, cancel); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, Url url, CancellationToken cancel = default(CancellationToken)) { Url url2 = url; if (url2 == null) throw new ArgumentNullException("url"); url = url2; return context.OpenAsync(DocumentRequest.Get(url, null, context?.Active?.DocumentUri), cancel); } [AsyncStateMachine(typeof(<OpenAsync>d__4))] public static Task<IDocument> OpenAsync(this IBrowsingContext context, Action<VirtualResponse> request, CancellationToken cancel = default(CancellationToken)) { <OpenAsync>d__4 stateMachine = default(<OpenAsync>d__4); stateMachine.<>t__builder = AsyncTaskMethodBuilder<IDocument>.Create(); stateMachine.context = context; stateMachine.request = request; stateMachine.cancel = cancel; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public static Task<IDocument> OpenAsync(this IBrowsingContext context, string address, CancellationToken cancellation = default(CancellationToken)) { string text = address; if (text == null) throw new ArgumentNullException("address"); address = text; return context.OpenAsync(Url.Create(address), cancellation); } internal static Task<IDocument> NavigateToAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel = default(CancellationToken)) { return context.GetNavigationHandler(request.Target)?.NavigateAsync(request, cancel) ?? Task.FromResult<IDocument>(null); } public static void NavigateTo(this IBrowsingContext context, IDocument document) { context.SessionHistory?.PushState(document, document.Title, document.Url); context.Active = document; } [return: System.Runtime.CompilerServices.Nullable(2)] public static INavigationHandler GetNavigationHandler(this IBrowsingContext context, Url url) { return context.GetServices<INavigationHandler>().FirstOrDefault((INavigationHandler m) => m.SupportsProtocol(url.Scheme)); } public static Encoding GetDefaultEncoding(this IBrowsingContext context) { IEncodingProvider provider = context.GetProvider<IEncodingProvider>(); string language = context.GetLanguage(); return provider?.Suggest(language) ?? Encoding.UTF8; } public static CultureInfo GetCulture(this IBrowsingContext context) { return context.GetService<CultureInfo>() ?? CultureInfo.CurrentUICulture; } public static CultureInfo GetCultureFrom(this IBrowsingContext context, string language) { try { return new CultureInfo(language); } catch (CultureNotFoundException ex) { context.TrackError(ex); return context.GetCulture(); } } public static string GetLanguage(this IBrowsingContext context) { return context.GetCulture().Name; } public static TFactory GetFactory<TFactory>(this IBrowsingContext context) where TFactory : class { return context.GetServices<TFactory>().Single(); } public static TProvider GetProvider<TProvider>(this IBrowsingContext context) where TProvider : class { return context.GetServices<TProvider>().SingleOrDefault(); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] public static IResourceService<TResource> GetResourceService<[System.Runtime.CompilerServices.Nullable(0)] TResource>(this IBrowsingContext context, string type) where TResource : IResourceInfo { foreach (IResourceService<TResource> service in context.GetServices<IResourceService<TResource>>()) { if (service.SupportsType(type)) return service; } return null; } public static string GetCookie(this IBrowsingContext context, Url url) { return context.GetProvider<ICookieProvider>()?.GetCookie(url) ?? string.Empty; } public static void SetCookie(this IBrowsingContext context, Url url, string value) { context.GetProvider<ICookieProvider>()?.SetCookie(url, value); } [return: System.Runtime.CompilerServices.Nullable(2)] public static ISpellCheckService GetSpellCheck(this IBrowsingContext context, string language) { ISpellCheckService spellCheckService = null; IEnumerable<ISpellCheckService> services = context.GetServices<ISpellCheckService>(); CultureInfo cultureFrom = context.GetCultureFrom(language); string twoLetterISOLanguageName = cultureFrom.TwoLetterISOLanguageName; foreach (ISpellCheckService item in services) { CultureInfo culture = item.Culture; if (culture != null) { string twoLetterISOLanguageName2 = culture.TwoLetterISOLanguageName; if (culture.Equals(cultureFrom)) return item; if (spellCheckService == null && twoLetterISOLanguageName2.Is(twoLetterISOLanguageName)) spellCheckService = item; } } return spellCheckService; } [return: System.Runtime.CompilerServices.Nullable(2)] public static IStylingService GetCssStyling(this IBrowsingContext context) { return context.GetStyling(MimeTypeNames.Css); } [return: System.Runtime.CompilerServices.Nullable(2)] public static IStylingService GetStyling(this IBrowsingContext context, string type) { foreach (IStylingService service in context.GetServices<IStylingService>()) { if (service.SupportsType(type)) return service; } return null; } public static bool IsScripting(this IBrowsingContext context) { return context.GetServices<IScriptingService>().Any(); } [return: System.Runtime.CompilerServices.Nullable(2)] public static IScriptingService GetJsScripting(this IBrowsingContext context) { return context.GetScripting(MimeTypeNames.DefaultJavaScript); } [return: System.Runtime.CompilerServices.Nullable(2)] public static IScriptingService GetScripting(this IBrowsingContext context, string type) { foreach (IScriptingService service in context.GetServices<IScriptingService>()) { if (service.SupportsType(type)) return service; } return null; } [return: System.Runtime.CompilerServices.Nullable(2)] public static ICommand GetCommand(this IBrowsingContext context, string commandId) { return context.GetProvider<ICommandProvider>()?.GetCommand(commandId); } public static void TrackError(this IBrowsingContext context, Exception ex) { TrackEvent eventData = new TrackEvent("error", ex); context.Fire(eventData); } public static Task InteractAsync<[System.Runtime.CompilerServices.Nullable(2)] T>(this IBrowsingContext context, string eventName, T data) { InteractivityEvent<T> interactivityEvent = new InteractivityEvent<T>(eventName, data); context.Fire(interactivityEvent); return interactivityEvent.Result ?? Task.FromResult(false); } public static IBrowsingContext ResolveTargetContext(this IBrowsingContext context, [System.Runtime.CompilerServices.Nullable(2)] string target) { bool flag = false; IBrowsingContext browsingContext = context; if (target != null && target.Length > 0) { browsingContext = context.FindChildFor(target); flag = (browsingContext == null); } if (flag) browsingContext = context.CreateChildFor(target); return browsingContext; } public static IBrowsingContext CreateChildFor(this IBrowsingContext context, [System.Runtime.CompilerServices.Nullable(2)] string target) { Sandboxes security = Sandboxes.None; if (target == "_blank") target = null; return context.CreateChild(target, security); } [return: System.Runtime.CompilerServices.Nullable(2)] public static IBrowsingContext FindChildFor(this IBrowsingContext context, string target) { if (string.IsNullOrEmpty(target) || target == "_self") return context; if (target == "_parent") return context.Parent ?? context; if (target == "_top") return context; return context.FindChild(target); } public static IEnumerable<Task> GetDownloads<[System.Runtime.CompilerServices.Nullable(0)] T>(this IBrowsingContext context) where T : INode { IResourceLoader service = context.GetService<IResourceLoader>(); if (service == null) return Array.Empty<Task>(); return from m in service.GetDownloads() where m.Source is T select ((ICancellable<IResponse>)m).Task; } } }