public XElementContext(XElement root, XizzleConventions conventions = null) { Conventions = conventions ?? DefaultConventions; RootElement = root; _idDict = new Dictionary<string, XElement>(); _typeDict = new Dictionary<string, HashSet<XElement>>(); _attrDict = new Dictionary<string, HashSet<XElement>>(); foreach (var el in RootElement.DescendantsAndSelf()) { IEnumerable<XAttribute> nameAttributes = el.Attributes() .Where(a => a.Name.LocalName == Conventions.IdAttributeName()); foreach (XAttribute attr in nameAttributes) _idDict[attr.Value] = el; if (!_typeDict.ContainsKey(el.Name.LocalName)) _typeDict[el.Name.LocalName] = new HashSet<XElement>(); _typeDict[el.Name.LocalName].Add(el); foreach (XAttribute a in el.Attributes()) { if (!_attrDict.ContainsKey(a.Name.LocalName)) _attrDict[a.Name.LocalName] = new HashSet<XElement>(); _attrDict[a.Name.LocalName].Add(el); } } }
static XElementContext() { _patterns = new FlexDict { {"GOS"/*Group of Selectors*/, @"^\s*{Selector}(\s*,\s*{Selector})*\s*$"}, {"Selector", @"{SOSS}{CombinatorSOSS}*{PseudoElement}?"}, {"CombinatorSOSS", @"\s*{Combinator}\s*{SOSS}"}, {"SOSS"/*Sequence of Simple Selectors*/, @"({TypeSelector}|{UniversalSelector}){SimpleSelector}*|{SimpleSelector}+"}, {"SimpleSelector", @"{AttributeSelector}|{IDSelector}|{PseudoSelector}"}, {"TypeSelector", @"{Identifier}"}, {"UniversalSelector", @"\*"}, {"AttributeSelector", @"\[\s*{Identifier}(\s*{ComparisonOperator}\s*{AttributeValue})?\s*\]"}, {"IDSelector", @"#{Identifier}"}, {"PseudoSelector", @":{Identifier}{PseudoArgs}?"}, {"PseudoElement", @"::{Identifier}"}, {"PseudoArgs", @"\(({String}|[^)])*\)"}, {"ComparisonOperator", @"[~^$*|!]?="}, {"Combinator", @"[ >+~]"}, {"Identifier", @"-?[a-zA-Z\u00A0-\uFFFF_][a-zA-Z\u00A0-\uFFFF_0-9-]*"}, {"AttributeValue", @"{Identifier}|{String}"}, {"String", @"""((?>[^\\""\r\n]|\\\r\n|\\.)*)""|'((?>[^\\'\r\n]|\\\r\n|\\.)*)'"}, }; _re = new Regex(_patterns["GOS"], RegexOptions.ExplicitCapture | RegexOptions.Singleline); _contexts = new Dictionary<XElement, XElementContext>(); DefaultConventions = new XizzleConventions { IdAttributeName = () => "id" }; }
public static XElementContext Open(this XElement root, XizzleConventions conventions = null) { return XElementContext.Get(root, conventions); }
public static XElementContext Parse(string xml, XizzleConventions conventions = null) { return Get(XElement.Parse(xml), conventions); }
public static XElementContext Load(XmlReader reader, XizzleConventions conventions = null) { return Get(XElement.Load(reader), conventions); }
public static XElementContext Get(XElement root, XizzleConventions conventions = null) { XElementContext context; lock (root) { if (_contexts.ContainsKey(root)) context = _contexts[root]; else { context = new XElementContext(root, conventions); _contexts.Add(root, context); } } return context; }