AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="1.0.1-alpha-242" />

 BrowsingContext

A simple and lightweight browsing context.
using AngleSharp.Browser; using AngleSharp.Browser.Dom; using AngleSharp.Dom; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace AngleSharp { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] public sealed class BrowsingContext : EventTarget, IBrowsingContext, IEventTarget, IDisposable { [System.Runtime.CompilerServices.Nullable(1)] private readonly IEnumerable<object> _originalServices; [System.Runtime.CompilerServices.Nullable(1)] private readonly List<object> _services; private readonly Sandboxes _security; private readonly IBrowsingContext _parent; private readonly IDocument _creator; private readonly IHistory _history; [System.Runtime.CompilerServices.Nullable(1)] private readonly Dictionary<string, WeakReference<IBrowsingContext>> _children; public IDocument Active { get; set; } public IDocument Creator => _creator; [System.Runtime.CompilerServices.Nullable(1)] public IEnumerable<object> OriginalServices { [System.Runtime.CompilerServices.NullableContext(1)] get { return _originalServices; } } public IWindow Current => Active?.DefaultView; public IBrowsingContext Parent => _parent; public IHistory SessionHistory => _history; public Sandboxes Security => _security; public BrowsingContext(IConfiguration configuration = null) : this((configuration ?? Configuration.Default).Services, Sandboxes.None) { } private BrowsingContext(Sandboxes security) { _services = new List<object>(); _originalServices = _services; _security = security; _children = new Dictionary<string, WeakReference<IBrowsingContext>>(); } [System.Runtime.CompilerServices.NullableContext(1)] internal BrowsingContext(IEnumerable<object> services, Sandboxes security) : this(security) { _services.AddRange(services); _originalServices = services; _history = GetService<IHistory>(); } [System.Runtime.CompilerServices.NullableContext(1)] internal BrowsingContext(IBrowsingContext parent, Sandboxes security) : this(parent.OriginalServices, security) { _parent = parent; _creator = _parent.Active; } [System.Runtime.CompilerServices.NullableContext(1)] [return: System.Runtime.CompilerServices.Nullable(2)] public T GetService<T>() where T : class { int count = _services.Count; int num = 0; while (num < count) { object obj = _services[num]; T val = obj as T; if (val == null) { Func<IBrowsingContext, T> func = obj as Func<IBrowsingContext, T>; if (func == null) { num++; continue; } val = func(this); _services[num] = val; } return val; } return null; } [System.Runtime.CompilerServices.NullableContext(1)] public IEnumerable<T> GetServices<T>() where T : class { int count = _services.Count; for (int i = 0; i < count; i++) { object obj = _services[i]; T val = obj as T; if (val == null) { Func<IBrowsingContext, T> func = obj as Func<IBrowsingContext, T>; if (func == null) continue; val = func(this); _services[i] = val; } yield return val; } } [System.Runtime.CompilerServices.NullableContext(1)] public IBrowsingContext CreateChild([System.Runtime.CompilerServices.Nullable(2)] string name, Sandboxes security) { BrowsingContext browsingContext = new BrowsingContext(this, security); if (name != null && name.Length > 0) _children[name] = new WeakReference<IBrowsingContext>(browsingContext); return browsingContext; } [System.Runtime.CompilerServices.NullableContext(1)] [return: System.Runtime.CompilerServices.Nullable(2)] public IBrowsingContext FindChild(string name) { IBrowsingContext target = null; if (!string.IsNullOrEmpty(name) && _children.TryGetValue(name, out WeakReference<IBrowsingContext> value)) value.TryGetTarget(out target); return target; } [System.Runtime.CompilerServices.NullableContext(1)] public static IBrowsingContext New([System.Runtime.CompilerServices.Nullable(2)] IConfiguration configuration = null) { if (configuration == null) configuration = Configuration.Default; return new BrowsingContext(configuration.Services, Sandboxes.None); } [System.Runtime.CompilerServices.NullableContext(1)] public static IBrowsingContext NewFrom<[System.Runtime.CompilerServices.Nullable(2)] TService>(TService instance) { return new BrowsingContext(Configuration.Default.WithOnly(instance).Services, Sandboxes.None); } void IDisposable.Dispose() { Active?.Dispose(); Active = null; } } }