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.Linq;
namespace AngleSharp.Extensions
{
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) {
IEnumerable<CssProperty> declarations = htmlElement.Style.OfType<CssProperty>();
cssStyleDeclaration.SetDeclarations(declarations);
}
foreach (IElement item in element.GetAncestors().OfType<IElement>()) {
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 IStringList CreateStyleSheetSets(this INode parent)
{
return new StringList(from m in parent.GetStyleSheets()
select m.Title into m
where m != null
select m);
}
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 sheet = linkStyle.Sheet;
if (sheet != null && !sheet.IsDisabled)
yield return sheet;
} 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.Is(prefix))
return item.NamespaceUri;
}
}
}
return null;
}
}
}