StyleSheetExtensions
Defines a set of extension methods for style sheets.
using AngleSharp.Dom;
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Html;
using AngleSharp.Html;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Extensions
{
public static class StyleSheetExtensions
{
private static readonly Dictionary<string, CssRuleType> RuleMapping = new Dictionary<string, CssRuleType> {
{
typeof(ICssCharsetRule).FullName,
CssRuleType.Charset
},
{
typeof(ICssCounterStyleRule).FullName,
CssRuleType.CounterStyle
},
{
typeof(ICssDocumentRule).FullName,
CssRuleType.Document
},
{
typeof(ICssFontFaceRule).FullName,
CssRuleType.FontFace
},
{
typeof(ICssFontFeatureValuesRule).FullName,
CssRuleType.FontFeatureValues
},
{
typeof(ICssImportRule).FullName,
CssRuleType.Import
},
{
typeof(ICssKeyframeRule).FullName,
CssRuleType.Keyframe
},
{
typeof(ICssKeyframesRule).FullName,
CssRuleType.Keyframes
},
{
typeof(ICssMediaRule).FullName,
CssRuleType.Media
},
{
typeof(ICssNamespaceRule).FullName,
CssRuleType.Namespace
},
{
typeof(ICssPageRule).FullName,
CssRuleType.Page
},
{
typeof(ICssStyleRule).FullName,
CssRuleType.Style
},
{
typeof(ICssSupportsRule).FullName,
CssRuleType.Supports
}
};
public static TRule AddNewRule<TRule>(this ICssRuleCreator creator) where TRule : ICssRule
{
string fullName = typeof(TRule).FullName;
CssRuleType value = CssRuleType.Unknown;
if (RuleMapping.TryGetValue(fullName, out value)) {
ICssRule cssRule = creator.AddNewRule(value);
if (cssRule is TRule)
return (TRule)cssRule;
}
return default(TRule);
}
public static ICssStyleRule AddNewStyle(this ICssRuleCreator creator, string selector = null, IDictionary<string, string> declarations = null)
{
ICssStyleRule cssStyleRule = creator.AddNewRule<ICssStyleRule>();
if (!string.IsNullOrEmpty(selector))
cssStyleRule.SelectorText = selector;
if (declarations != null) {
foreach (KeyValuePair<string, string> declaration in declarations) {
cssStyleRule.Style.SetProperty(declaration.Key, declaration.Value, null);
}
return cssStyleRule;
}
return cssStyleRule;
}
public static ICssStyleRule AddNewStyle(this ICssRuleCreator creator, string selector, object declarations)
{
return creator.AddNewStyle(selector, declarations.ToDictionary());
}
public static IEnumerable<TRule> RulesOf<TRule>(this IEnumerable<IStyleSheet> sheets) where TRule : ICssRule
{
if (sheets == null)
throw new ArgumentNullException("sheets");
return (from m in sheets
where !m.IsDisabled
select m).OfType<ICssStyleSheet>().SelectMany((ICssStyleSheet m) => m.Rules).OfType<TRule>();
}
public static IEnumerable<ICssStyleRule> StylesWith(this IEnumerable<IStyleSheet> sheets, ISelector selector)
{
if (selector == null)
throw new ArgumentNullException("selector");
string selectorText = selector.Text;
return from m in sheets.RulesOf<ICssStyleRule>()
where m.SelectorText == selectorText
select m;
}
public static IDocument GetDocument(this IStyleSheet sheet)
{
return sheet?.OwnerNode?.Owner;
}
public static IEnumerable<ICssComment> GetComments(this ICssNode node)
{
return node.GetAll<ICssComment>();
}
public static IEnumerable<ICssNode> GetAllDescendents(this ICssNode node)
{
if (node == null)
throw new ArgumentNullException("node");
return node.Children.SelectMany((ICssNode m) => m.GetAllDescendents());
}
public static IEnumerable<T> GetAll<T>(this ICssNode node) where T : IStyleFormattable
{
if (node == null)
throw new ArgumentNullException("node");
if (node is T)
yield return (T)node;
foreach (T item in Enumerable.SelectMany<ICssNode, T>(node.Children, (Func<ICssNode, IEnumerable<T>>)((ICssNode m) => StyleSheetExtensions.GetAll<T>(m)))) {
yield return item;
}
}
public static bool IsPersistent(this IHtmlLinkElement link)
{
if (link.Relation.Isi(LinkRelNames.StyleSheet))
return link.Title == null;
return false;
}
public static bool IsPreferred(this IHtmlLinkElement link)
{
if (link.Relation.Isi(LinkRelNames.StyleSheet))
return link.Title != null;
return false;
}
public static bool IsAlternate(this IHtmlLinkElement link)
{
ITokenList relationList = link.RelationList;
if (relationList.Contains(LinkRelNames.StyleSheet) && relationList.Contains(LinkRelNames.Alternate))
return link.Title != null;
return false;
}
}
}