StyleExtensions
A set of extension methods for style / related methods.
using AngleSharp.Css;
using AngleSharp.DOM;
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 void ExtendWith(this CssPropertyBag bag, CSSStyleDeclaration styling, Priority priority)
{
foreach (CSSProperty declaration in styling.Declarations) {
bag.TryUpdate(declaration, priority);
}
}
public static void InheritFrom(this CssPropertyBag bag, IElement element, IWindow window)
{
IElement parentElement = element.ParentElement;
if (parentElement != null) {
CSSStyleDeclaration cSSStyleDeclaration = window.ComputeDeclarations(parentElement);
foreach (CSSProperty declaration in cSSStyleDeclaration.Declarations) {
CSSProperty cSSProperty = bag[declaration.Name];
if (cSSProperty == null || cSSProperty.IsInherited)
bag.TryUpdate(declaration);
}
}
}
public static CSSStyleDeclaration ComputeDeclarations(this IWindow window, IElement element)
{
CssPropertyBag cssPropertyBag = new CssPropertyBag();
foreach (IStyleSheet styleSheet in window.Document.StyleSheets) {
CSSStyleSheet cSSStyleSheet = styleSheet as CSSStyleSheet;
if (cSSStyleSheet != null && !styleSheet.IsDisabled && styleSheet.Media.Validate(window)) {
CSSRuleList rules = cSSStyleSheet.Rules;
rules.ComputeStyle(cssPropertyBag, window, element);
}
}
HTMLElement hTMLElement = element as HTMLElement;
if (hTMLElement != null)
cssPropertyBag.ExtendWith(hTMLElement.Style, Priority.Inline);
cssPropertyBag.InheritFrom(element, window);
return new CSSStyleDeclaration(cssPropertyBag);
}
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 is IElement) {
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;
}
}
}
}
}
}
}