Exemple #1
0
        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);
        }
Exemple #2
0
        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");
        }
Exemple #3
0
        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);
        }