StyleSheetExtensions
Defines a set of extension methods for style sheets.
using AngleSharp.Css;
using AngleSharp.Dom;
using AngleSharp.Dom.Css;
using AngleSharp.Parser.Css;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace AngleSharp.Extensions
{
[DebuggerStepThrough]
public static class StyleSheetExtensions
{
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 IEnumerable<CssComment> GetComments(this CssNode node)
{
List<CssToken> tokens = node.Tokens;
IEnumerable<CssComment> first = from m in tokens
where m.Type == CssTokenType.Comment
select new CssComment(m);
return first.Concat(node.Children.SelectMany((CssNode m) => m.GetComments()));
}
public static IEnumerable<CssNode> GetAllDescendents(this CssNode node)
{
if (node == null)
throw new ArgumentNullException("node");
return node.Children.SelectMany((CssNode m) => m.GetAllDescendents());
}
public static IEnumerable<T> GetAll<T>(this CssNode node) where T : IStyleFormattable
{
if (node == null)
throw new ArgumentNullException("node");
if (node.Entity is T)
yield return (T)node.Entity;
foreach (T item in Enumerable.SelectMany<CssNode, T>((IEnumerable<CssNode>)node.Children, (Func<CssNode, IEnumerable<T>>)((CssNode m) => StyleSheetExtensions.GetAll<T>(m)))) {
yield return item;
}
}
public static CssNode GetAssociatedNode(this CssNode node, IStyleFormattable entity)
{
if (node == null)
throw new ArgumentNullException("node");
if (entity == null)
throw new ArgumentNullException("entity");
CssNode cssNode = null;
if (object.ReferenceEquals(node.Entity, entity))
return node;
foreach (CssNode child in node.Children) {
cssNode = child.GetAssociatedNode(entity);
if (cssNode != null)
return cssNode;
}
return cssNode;
}
public static string GetSource(this CssNode node)
{
if (node == null)
throw new ArgumentNullException("node");
List<CssToken> tokens = node.Tokens;
List<CssNode> children = node.Children;
int num = tokens.Count + children.Count;
string[] array = new string[num];
TextPosition[] array2 = new TextPosition[num];
for (int i = 0; i < tokens.Count; i++) {
array[i] = tokens[i].ToValue();
array2[i] = tokens[i].Position;
}
int num2 = tokens.Count;
int num3 = 0;
while (num2 < num) {
array[num2] = children[num3].GetSource();
array2[num2] = children[num3].Start;
int num4 = num2 - 1;
while (num4 >= 0 && !(array2[num4] < array2[num4 + 1])) {
Swap(array2, num4);
Swap(array, num4);
num4--;
}
num2++;
num3++;
}
return string.Join(string.Empty, array);
}
private static void Swap<T>(T[] array, int i)
{
int num = i + 1;
T val = array[i];
array[i] = array[num];
array[num] = val;
}
}
}