public css_selector(css_element_selector right) { _right = right; _media_query = null; _combinator = css_combinator.descendant; _order = 0; }
public void CssElementSelectorParseTest() { var selector = new css_element_selector(); // https://www.w3schools.com/cssref/css_selectors.asp selector.parse(".class"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "class"); Assert.AreEqual(selector._attrs[0].attribute, "class"); selector.parse(".class1.class2"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 2); Assert.AreEqual(selector._attrs[0].val, "class1"); Assert.AreEqual(selector._attrs[1].val, "class2"); Assert.AreEqual(selector._attrs[0].attribute, "class"); selector.parse("#id"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "id"); Assert.AreEqual(selector._attrs[0].attribute, "id"); selector.parse("*"); Assert.AreEqual(selector._tag, "*"); Assert.AreEqual(selector._attrs.Count, 0); selector.parse("element"); Assert.AreEqual(selector._tag, "element"); Assert.AreEqual(selector._attrs.Count, 0); selector.parse("[attribute]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.IsNull(selector._attrs[0].val); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.exists); selector.parse("[attribute=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.equal); selector.parse("[attribute~=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.contain_str); selector.parse("[attribute|=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.start_str); selector.parse("[attribute^=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.start_str); selector.parse("[attribute$=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.end_str); selector.parse("[attribute*=value]"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "value"); Assert.AreEqual(selector._attrs[0].attribute, "attribute"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.contain_str); selector.parse(":active"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "active"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse("::after"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "after"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse("::before"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "before"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse(":checked"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "checked"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":default"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "default"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":disabled"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "disabled"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":empty"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "empty"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":enabled"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "enabled"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":first-child"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "first-child"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse("::first-letter"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "first-letter"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse("::first-line"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "first-line"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse(":first-of-type"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "first-of-type"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":focus"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "focus"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":hover"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "hover"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":in-range"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "in-range"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":indeterminate"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "indeterminate"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":invalid"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "invalid"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":lang(language)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "lang(language)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":last-child"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "last-child"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":last-of-type"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "last-of-type"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":link"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "link"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":not(selector)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "not(selector)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":nth-child(n)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "nth-child(n)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":nth-last-child(n)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "nth-last-child(n)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":nth-last-of-type(n)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "nth-last-of-type(n)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":nth-of-type(n)"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "nth-of-type(n)"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":only-of-type"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "only-of-type"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":only-child"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "only-child"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":optional"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "optional"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":out-of-range"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "out-of-range"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse("::placeholder"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "placeholder"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse(":read-only"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "read-only"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":read-write"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "read-write"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":required"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "required"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":root"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "root"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse("::selection"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "selection"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo-el"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_element); selector.parse(":target"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "target"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":valid"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "valid"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); selector.parse(":visited"); Assert.IsEmpty(selector._tag); Assert.AreEqual(selector._attrs.Count, 1); Assert.AreEqual(selector._attrs[0].val, "visited"); Assert.AreEqual(selector._attrs[0].attribute, "pseudo"); Assert.AreEqual(selector._attrs[0].condition, attr_select_condition.pseudo_class); // other selector.parse("tag:psudo#anchor"); Assert.AreEqual(selector._tag, "tag"); Assert.AreEqual(selector._attrs.Count, 2); }
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; }