public static int GetSpecificity(this BaseSelector selector) { if (selector is SimpleSelector) { var simpleCode = selector.ToString().ToLowerInvariant(); if (simpleCode.StartsWith(":not(")) { simpleCode = simpleCode.Substring(5, simpleCode.Length - 6); return(GetSpecificity(new SimpleSelector(simpleCode))); } else if (simpleCode.StartsWith("#")) { // ID selector return(1 << 12); } else if (simpleCode.StartsWith("::") || simpleCode == ":after" || simpleCode == ":before" || simpleCode == ":first-letter" || simpleCode == ":first-line" || simpleCode == ":selection") { // pseudo-element return(1 << 4); } else if (simpleCode.StartsWith(".") || simpleCode.StartsWith(":") || simpleCode.StartsWith("[")) { // class, pseudo-class, attribute return(1 << 8); } else if (simpleCode.Equals("*")) { // all selector return(0); } else { // element selector return(1 << 4); } } else { var list = selector as IEnumerable <BaseSelector>; if (list != null) { return((from s in list select GetSpecificity(s)).Aggregate((p, c) => p + c)); } else { var complex = selector as IEnumerable <CombinatorSelector>; if (complex != null) { return((from s in complex select GetSpecificity(s.Selector)).Aggregate((p, c) => p + c)); } else { return(0); } } } }
public static int GetSpecificity(this BaseSelector selector) { switch (selector) { case SimpleSelector _: return(GetSimpleSpecificity(selector.ToString().ToLowerInvariant())); case IEnumerable <BaseSelector> list: return((from s in list select GetSpecificity(s)).Aggregate((p, c) => p + c)); case IEnumerable <CombinatorSelector> complex: return((from s in complex select GetSpecificity(s.Selector)).Aggregate((p, c) => p + c)); default: return(0); } }
string ExtractSimpleSelector(BaseSelector selector) { SimpleSelector simpleSelector = selector as SimpleSelector; if (simpleSelector != null) { return(selector.ToString()); } AggregateSelectorList selectorList = selector as AggregateSelectorList; // Work around a strange parser issue where sometimes simple selectors // are wrapped inside SelectorList with no delimiter if (selectorList != null && selectorList.Delimiter == string.Empty) { return(selectorList.ToString()); } return(string.Empty); }
private string ExtractSimpleSelector(BaseSelector selector) { SimpleSelector simpleSelector = selector as SimpleSelector; string result; if (simpleSelector != null) { result = selector.ToString(); } else { AggregateSelectorList aggregateSelectorList = selector as AggregateSelectorList; if (aggregateSelectorList != null && aggregateSelectorList.Delimiter == string.Empty) { result = aggregateSelectorList.ToString(); } else { result = string.Empty; } } return(result); }
public IEnumerable <Selector> Replacements(string selection) { foreach (var ex in ExtendedBy) { yield return(new Selector(new [] { new Element(null, selection.Replace(BaseSelector.ToString().Trim(), ex.ToString().Trim())) })); } }