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();
PseudoElement pseudoElement = PseudoElement.Create(element, pseudoSelector);
if (pseudoElement != null)
element = pseudoElement;
cssStyleDeclaration.SetDeclarations(rules.ComputeCascadedStyle(element).Declarations);
IHtmlElement htmlElement = element as IHtmlElement;
if (htmlElement != null)
cssStyleDeclaration.SetDeclarations(htmlElement.Style.OfType<CssProperty>());
IEnumerable<IElement> enumerable = element.GetAncestors().OfType<IElement>();
foreach (IElement item in enumerable) {
CssStyleDeclaration cssStyleDeclaration2 = rules.ComputeCascadedStyle(item);
cssStyleDeclaration.UpdateDeclarations(cssStyleDeclaration2.Declarations);
}
return cssStyleDeclaration;
}
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 IStyleSheetList CreateStyleSheets(this INode parent)
{
return new StyleSheetList(parent.GetStyleSheets());
}
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;
}
}
}