AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="1.2.0-beta.449" />

.NET API 957,952 bytes

 MutationObserver

public sealed class MutationObserver
MutationObserver provides developers a way to react to changes in a DOM.
using AngleSharp.Attributes; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace AngleSharp.Dom { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] [DomName("MutationObserver")] public sealed class MutationObserver { [System.Runtime.CompilerServices.NullableContext(0)] internal struct MutationOptions { public bool IsObservingChildNodes; public bool IsObservingSubtree; public bool IsObservingCharacterData; public bool IsObservingAttributes; public bool IsExaminingOldCharacterData; public bool IsExaminingOldAttributeValue; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] public IEnumerable<string> AttributeFilters; public bool IsInvalid { [System.Runtime.CompilerServices.IsReadOnly] get { if (!IsObservingAttributes && !IsObservingCharacterData) return !IsObservingChildNodes; return false; } } } [System.Runtime.CompilerServices.Nullable(0)] private sealed class MutationObserving { private readonly INode _target = target; private readonly MutationOptions _options = options; private readonly List<INode> _transientNodes = new List<INode>(); public INode Target => _target; public MutationOptions Options => _options; public List<INode> TransientNodes => _transientNodes; public MutationObserving(INode target, MutationOptions options) { } } private readonly Queue<IMutationRecord> _records; private readonly MutationCallback _callback; private readonly List<MutationObserving> _observing; [System.Runtime.CompilerServices.Nullable(2)] private MutationObserving this[INode node] { [return: System.Runtime.CompilerServices.Nullable(2)] get { foreach (MutationObserving item in _observing) { if (item.Target == node) return item; } return null; } } [DomConstructor] public MutationObserver(MutationCallback callback) { _records = new Queue<IMutationRecord>(); if (callback == null) throw new ArgumentNullException("callback"); _callback = callback; _observing = new List<MutationObserving>(); } internal void Enqueue(MutationRecord record) { int count = _records.Count; _records.Enqueue(record); } internal void Trigger() { IMutationRecord[] array = _records.ToArray(); _records.Clear(); ClearTransients(); if (array.Length != 0) _callback(array, this); } internal MutationOptions ResolveOptions(INode node) { foreach (MutationObserving item in _observing) { if (item.Target == node || item.TransientNodes.Contains(node)) return item.Options; } return default(MutationOptions); } internal void AddTransient(INode ancestor, INode node) { MutationObserving mutationObserving = this[ancestor]; if (mutationObserving != null && mutationObserving.Options.IsObservingSubtree) mutationObserving.TransientNodes.Add(node); } internal void ClearTransients() { foreach (MutationObserving item in _observing) { item.TransientNodes.Clear(); } } [DomName("disconnect")] public void Disconnect() { foreach (MutationObserving item in _observing) { ((Node)item.Target).Owner.Mutations.Unregister(this); } _records.Clear(); } [DomName("observe")] [DomInitDict(1, false)] public void Connect(INode target, bool childList = false, bool subtree = false, bool? attributes = default(bool?), bool? characterData = default(bool?), bool? attributeOldValue = default(bool?), bool? characterDataOldValue = default(bool?), [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] IEnumerable<string> attributeFilter = null) { Node node = target as Node; if (node != null) { bool valueOrDefault = characterDataOldValue.GetValueOrDefault(); bool valueOrDefault2 = attributeOldValue.GetValueOrDefault(); MutationOptions mutationOptions = default(MutationOptions); mutationOptions.IsObservingChildNodes = childList; mutationOptions.IsObservingSubtree = subtree; mutationOptions.IsExaminingOldCharacterData = valueOrDefault; mutationOptions.IsExaminingOldAttributeValue = valueOrDefault2; mutationOptions.IsObservingCharacterData = characterData.GetValueOrDefault(valueOrDefault); mutationOptions.IsObservingAttributes = (attributes ?? (valueOrDefault2 || attributeFilter != null)); mutationOptions.AttributeFilters = attributeFilter; MutationOptions mutationOptions2 = mutationOptions; if (mutationOptions2.IsExaminingOldAttributeValue && !mutationOptions2.IsObservingAttributes) throw new DomException(DomError.TypeMismatch); if (mutationOptions2.AttributeFilters != null && !mutationOptions2.IsObservingAttributes) throw new DomException(DomError.TypeMismatch); if (mutationOptions2.IsExaminingOldCharacterData && !mutationOptions2.IsObservingCharacterData) throw new DomException(DomError.TypeMismatch); if (mutationOptions2.IsInvalid) throw new DomException(DomError.Syntax); Document document = node as Document; if (document != null) { Node node2 = document.DocumentElement as Node; if (node2 != null) { node = node2; target = node2; } } if (node.Owner == null) throw new DomException(DomError.HierarchyRequest); node.Owner.Mutations.Register(this); MutationObserving mutationObserving = this[target]; if (mutationObserving != null) { mutationObserving.TransientNodes.Clear(); _observing.Remove(mutationObserving); } _observing.Add(new MutationObserving(target, mutationOptions2)); } } [DomName("takeRecords")] public IEnumerable<IMutationRecord> Flush() { while (_records.Count > 0) { yield return _records.Dequeue(); } } } }