File: test_selectormatcheselement_host.html

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (154 lines) | stat: -rw-r--r-- 5,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for CSSStyleRule::selectorMatchesElement on :host rules</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <div id="host"></div>
  <script>
    /***** Add Shadow DOM *****/
    const topLevelHostEl = document.getElementById("host");
    const topShadow = topLevelHostEl.attachShadow({
      mode: "open"
    });
    const topLevelShadowSectionEl = document.createElement("section");
    topLevelShadowSectionEl.id = "top-level-shadow-section";
    const nestedHostEl = document.createElement("div");
    nestedHostEl.id = "nested-host";

    topLevelShadowSectionEl.append("top shadow dom section",)
    topShadow.append(topLevelShadowSectionEl, nestedHostEl);

    const sharedStyleSheet  = new CSSStyleSheet();
    sharedStyleSheet.replaceSync(`
      section, [test-rule-shared] {
        color: var(--a);
      }
      :host, [test-rule-shared] {
        --a: red;
      }`);

    const topLevelStyleSheet  = new CSSStyleSheet();
    topLevelStyleSheet.replaceSync(`
      :where(section), [test-rule] {
        background-color: var(--b);
      }
      :host, [test-rule] {
        --b: gold;
      }`);

    topShadow.adoptedStyleSheets = [sharedStyleSheet, topLevelStyleSheet];


    const nestedShadow = nestedHostEl.attachShadow({
      mode: "open"
    });
    const nestedShadowSectionEl = document.createElement("section");
    nestedShadowSectionEl.textContent = "nested shadow dom section";
    nestedShadowSectionEl.id = "nested-shadow-dom-section";
    nestedShadow.append(nestedShadowSectionEl);

    const nestedStyleSheet  = new CSSStyleSheet();
    nestedStyleSheet.replaceSync(`
      :is(section), [test-rule-nested] {
        border-color: var(--c);
      }
      :host, [test-rule-nested] {
        --c: lime;
      }`);
    nestedShadow.adoptedStyleSheets = [sharedStyleSheet, nestedStyleSheet];

    /***** Test *****/
    add_task(async () => {
      info("Checking rules on top level shadow section el");
      checkRulesAndSelectors(topLevelShadowSectionEl, [{
        selectorText: `:where(section), [test-rule]`,
        selectorMatches: [true, false]
      },{
        selectorText: `section, [test-rule-shared]`,
        selectorMatches: [true, false]
      }]);

      info("Checking rules on top level host");
      checkRulesAndSelectors(topLevelHostEl, [{
        selectorText: `:host, [test-rule-shared]`,
        selectorMatches: [true, false]
      },{
        selectorText: `:host, [test-rule]`,
        selectorMatches: [true, false]
      }]);

      info("Checking rules on nested shadow section el");
      checkRulesAndSelectors(nestedShadowSectionEl, [{
        selectorText: `section, [test-rule-shared]`,
        selectorMatches: [true, false]
      },{
        selectorText: `:is(section), [test-rule-nested]`,
        selectorMatches: [true, false]
      }]);

      info("Checking rules on nested host");
      checkRulesAndSelectors(nestedHostEl, [{
        selectorText: `:host, [test-rule-shared]`,
        selectorMatches: [true, false]
      },{
        selectorText: `:host, [test-rule-nested]`,
        selectorMatches: [true, false]
      }]);

      info("Check that non-shared rule selectors don't match for non-matching elements");
      const nonSharedTopLevelHostRules = getNonUAMatchingRules(topLevelHostEl)
        .filter(rule => rule.parentStyleSheet !== sharedStyleSheet);
      is(nonSharedTopLevelHostRules.length, 1, "top level host only has 1 non shared rule");
      is(
        nonSharedTopLevelHostRules[0].selectorMatchesElement(0, nestedHostEl),
        false,
        "non-shared top level host rule does not match nested host"
      );

      const nonSharedNestedHostRules = getNonUAMatchingRules(nestedHostEl)
        .filter(rule => rule.parentStyleSheet !== sharedStyleSheet);
      is(nonSharedNestedHostRules.length, 1, "nested host only has 1 non shared rule");
      is(
        nonSharedNestedHostRules[0].selectorMatchesElement(0, topLevelHostEl),
        false,
        "non-shared nested host rule does not match top level host"
      );
    });

    function checkRulesAndSelectors (element, expectedData) {
      const rules = getNonUAMatchingRules(element);
      is(rules.length, expectedData.length, `Got expected number of rules for #${element.id}`);
      for (let i = 0; i < expectedData.length; i++) {
        const rule = rules[i];
        const expected = expectedData[i];
        is(rule.selectorText, expected.selectorText, `Got expected rule at index ${i} for #${element.id}`);
        for (let j = 0; j < expected.selectorMatches.length; j++) {
          const selectorMatch = expected.selectorMatches[j];
          is(
            rule.selectorMatchesElement(j, element),
            selectorMatch,
            `"${rule.selectorText}" selector part at index ${j} ${selectorMatch ? "matches": "does not match"} #${element.id}`
          );
        }
      }
    }

    function getNonUAMatchingRules(element) {
      return SpecialPowers.InspectorUtils.getMatchingCSSRules(element)
        .filter(rule => {
          let selector = "";
          // Accessing selectorText does throw for some rules (these aren't the ones
          // we're interested in, so it's fine)
          try {
            selector = rule.selectorText;
          } catch (e) {}
          return selector.includes("[test-rule");
        })
    }

  </script>
</body>
</html>