AngleSharp by Florian Rappl

<PackageReference Include="AngleSharp" Version="0.8.4.1" />

 StyleExtensions

static class StyleExtensions
A set of extension methods for style / related methods.
using AngleSharp.Dom; using AngleSharp.Dom.Collections; using AngleSharp.Dom.Css; using AngleSharp.Dom.Html; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace AngleSharp.Extensions { [DebuggerStepThrough] internal static class StyleExtensions { public static CssStyleDeclaration ComputeDeclarations(this StyleCollection rules, IElement element, string pseudoSelector = null) { CssStyleDeclaration cssStyleDeclaration = new CssStyleDeclaration((string)null); PseudoElement pseudoElement = PseudoElement.Create(element, pseudoSelector); if (pseudoElement != null) element = pseudoElement; cssStyleDeclaration.SetDeclarations(rules.ComputeCascadedStyle(element)); HtmlElement htmlElement = element as HtmlElement; if (htmlElement != null) cssStyleDeclaration.SetDeclarations(htmlElement.Style); IEnumerable<IElement> enumerable = element.GetAncestors().OfType<IElement>(); foreach (IElement item in enumerable) { CssStyleDeclaration style = rules.ComputeCascadedStyle(item); cssStyleDeclaration.UpdateDeclarations(style); } return new CssStyleDeclaration(cssStyleDeclaration.Declarations); } public static IEnumerable<string> GetAllStyleSheetSets(this IStyleSheetList sheets) { List<string> existing = new List<string>(); foreach (IStyleSheet sheet in sheets) { string title = sheet.Title; if (!string.IsNullOrEmpty(title) && !existing.Contains(title)) { existing.Add(title); yield return title; } } } public static IEnumerable<string> GetEnabledStyleSheetSets(this IStyleSheetList sheets) { List<string> list = new List<string>(); foreach (IStyleSheet sheet in sheets) { string title = sheet.Title; if (!string.IsNullOrEmpty(title) && !list.Contains(title) && sheet.IsDisabled) list.Add(title); } return sheets.GetAllStyleSheetSets().Except(list); } public static void EnableStyleSheetSet(this IStyleSheetList sheets, string name) { foreach (IStyleSheet sheet in sheets) { string title = sheet.Title; if (!string.IsNullOrEmpty(title)) sheet.IsDisabled = (title != name); } } public static IEnumerable<IStyleSheet> GetStyleSheets(this INode parent) { foreach (INode childNode in parent.ChildNodes) { if (childNode.NodeType == NodeType.Element) { ILinkStyle linkStyle = childNode as ILinkStyle; if (linkStyle != null) { IStyleSheet sheet2 = linkStyle.Sheet; if (sheet2 != null) yield return sheet2; } else { foreach (IStyleSheet styleSheet in childNode.GetStyleSheets()) { yield return styleSheet; } } } } } public static string LocateNamespace(this IStyleSheetList sheets, string prefix) { foreach (IStyleSheet sheet in sheets) { CssStyleSheet cssStyleSheet = sheet as CssStyleSheet; if (!sheet.IsDisabled && cssStyleSheet != null) { foreach (CssNamespaceRule item in cssStyleSheet.Rules.OfType<CssNamespaceRule>()) { if (item.Prefix == prefix) return item.NamespaceUri; } } } return null; } } }