public bool parse(string text) { if (string.IsNullOrEmpty(text)) { return(false); } var tokens = new List <string>(); html.split_string(text, tokens, "", " \t>+~", "(["); if (tokens.Count == 0) { return(false); } var right = tokens[tokens.Count - 1].Trim(); var combinator = '\0'; tokens.RemoveAt(tokens.Count - 1); string lastToken; while (tokens.Count != 0 && ((lastToken = tokens[tokens.Count - 1]) == " " || lastToken == "\t" || lastToken == "+" || lastToken == "~" || lastToken == ">")) { if (combinator == ' ' || combinator == 0) { combinator = lastToken[0]; } tokens.RemoveAt(tokens.Count - 1); } var left = tokens.Aggregate(new StringBuilder(), (a, b) => a.Append(b)).ToString().Trim(); if (string.IsNullOrEmpty(right)) { return(false); } _right.parse(right); switch (combinator) { case '>': _combinator = css_combinator.child; break; case '+': _combinator = css_combinator.adjacent_sibling; break; case '~': _combinator = css_combinator.general_sibling; break; default: _combinator = css_combinator.descendant; break; } _left = null; if (!string.IsNullOrEmpty(left)) { _left = new css_selector((media_query_list)null); if (!_left.parse(left)) { return(false); } } return(true); }
public css_selector(css_selector val) { _right = val._right; _left = val._left; _combinator = val._combinator; _specificity = val._specificity; _order = val._order; _media_query = val._media_query; }
public void CssSelectorParseTest() { var selector = new css_selector(); // https://www.w3schools.com/cssref/css_selectors.asp Assert.IsFalse(selector.parse(string.Empty)); Assert.IsTrue(selector.parse("element")); Assert.AreEqual(selector._combinator, css_combinator.descendant); Assert.AreEqual(selector._right._tag, "element"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.IsNull(selector._left); //Assert.IsTrue(selector.parse("element,element")); Assert.AreEqual(selector._combinator, css_combinator.descendant); Assert.AreEqual(selector._right._tag, "element"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.IsTrue(selector.parse(".class1 .class2")); Assert.AreEqual(selector._combinator, css_combinator.descendant); Assert.IsEmpty(selector._right._tag); Assert.AreEqual(selector._right._attrs.Count, 1); Assert.AreEqual(selector._left._right._attrs.Count, 1); Assert.IsTrue(selector.parse("element element")); Assert.AreEqual(selector._combinator, css_combinator.descendant); Assert.AreEqual(selector._right._tag, "element"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.AreEqual(selector._left._right._tag, "element"); Assert.IsTrue(selector.parse("element>element")); Assert.AreEqual(selector._combinator, css_combinator.child); Assert.AreEqual(selector._right._tag, "element"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.AreEqual(selector._left._right._tag, "element"); Assert.IsTrue(selector.parse("element+element")); Assert.AreEqual(selector._combinator, css_combinator.adjacent_sibling); Assert.AreEqual(selector._right._tag, "element"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.AreEqual(selector._left._right._tag, "element"); Assert.IsTrue(selector.parse("element1~element2")); Assert.AreEqual(selector._combinator, css_combinator.general_sibling); Assert.AreEqual(selector._right._tag, "element2"); Assert.AreEqual(selector._right._attrs.Count, 0); Assert.AreEqual(selector._left._right._tag, "element1"); }
bool parse_selectors(string txt, style styles, media_query_list media) { var tokens = new List <string>(); html.split_string(txt.Trim(), tokens, ","); var added_something = false; foreach (var tok in tokens) { var selector = new css_selector(media); selector._style = styles; if (selector.parse(tok.Trim())) { selector.calc_specificity(); add_selector(selector); added_something = true; } } return(added_something); }
public used_selector(css_selector selector, bool used) { _used = used; _selector = selector; }
void add_selector(css_selector selector) { selector._order = _selectors.Count; _selectors.Add(selector); }