File: test_find.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 (273 lines) | stat: -rw-r--r-- 8,838 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!doctype html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
const t = async_test("Test window.find / nsFind");

function testFindable(findCount, textToFind, buildDoc, description) {
  if (typeof findCount == "boolean")
    findCount = findCount ? 1 : 0;
  try {
    const iframe = document.querySelector("iframe")
    iframe.contentDocument.documentElement.innerHTML =
      (typeof buildDoc == "string") ? buildDoc : "";

    if (typeof buildDoc == "function")
      buildDoc(iframe.contentDocument);

    iframe.contentWindow.getSelection().removeAllRanges();
    for (let i = findCount; i >= 0; --i) {
      const expectFindable = i != 0;
      assert_equals(
        iframe.contentWindow.find(textToFind),
        expectFindable,
        "Should be " + (expectFindable ? "" : "not ") + "findable: " + description + ", text: " + textToFind + ", iter: " + (findCount - i + 1)
      );
    }

  } catch (ex) {
    assert_unreached(ex);
  }
}

const INLINE_LIKE_DISPLAY_VALUES = [
  "inline",
  "inline-grid",
  "inline-block",
  "inline-flex",
];

const BLOCK_LIKE_DISPLAY_VALUES = [
  "block",
  "flex",
  "grid",
  "list-item",
  "table-column-group",
  "table-column",
  "table-footer-group",
  "table-header-group",
  "table-row-group",
  "table-row",
  "table",
];

let runTests = t.step_func_done(function() {
  testFindable(true, "me and me", `
    me <div style="display: contents">and</div> me
  `, "display: contents");

  testFindable(true, "me me", `
    me <div style="display: none">and</div> me
  `, "display: none");

  testFindable(false, "me and me", `
    me <div style="display: none">and</div> me
  `, "display: none");

  for (const display of INLINE_LIKE_DISPLAY_VALUES) {
    testFindable(true, "me and me", `
      me <div style="display: ${display}">and</div> me
    `, "div display: " + display);
    testFindable(true, "me and me", `
      me <span style="display: ${display}">and</span> me
    `, "span display: " + display);
  }

  for (const display of BLOCK_LIKE_DISPLAY_VALUES) {
    testFindable(false, "me and me", `
      me <div style="display: ${display}">and</div> me
    `, "div display: " + display);
    testFindable(false, "me and me", `
      me <span style="display: ${display}">and</span> me
    `, "span display: " + display);
  }

  testFindable(false, "me and me", `
    me <fieldset>and</fieldset> me
  `);

  testFindable(true, "This text should be visible", `
    <div style="visibility: hidden">
      <div style="visibility: visible">
        This text should be visible
      </div>
    </div>
  `);

  testFindable(true, "This text should be visible", `
    <style>:root { overflow: hidden }</style>
    <div style="overflow: auto;">
      <div style="height: 300vh"></div>
      This text should be visible
    </div>
  `);

  testFindable(true, "foobar", `
    <body><script style="display: block;">foobar</` + `script></body>
  `);


  testFindable(true, "Shadow text", function(document) {
    let div = document.createElement("div");
    div.attachShadow({ mode: "open" }).innerHTML = `
      Wohoo, this is Shadow text, yay!
    `;
    document.documentElement.appendChild(div);
  }, "In Shadow DOM");

  testFindable(true, "Shadow text", function(document) {
    let div = document.createElement("div");
    div.appendChild(document.createTextNode(
      "Wohoo, this is Shadow text, yay!"
    ));
    div.attachShadow({ mode: "open" }).innerHTML = `<slot></slot>`;
    document.documentElement.appendChild(div);
  }, "Slotted content in Shadow DOM");

  // TODO(emilio): This should work in an ideal world.
  testFindable(false, "Shadow text", function(document) {
    let div = document.createElement("div");
    div.appendChild(document.createTextNode("text, yay!"));
    div.attachShadow({ mode: "open" }).innerHTML = `This is Shadow <slot></slot>`;
    document.documentElement.appendChild(div);
  }, "Mixed shadow and non-shadow text");

  testFindable(true, "Shadow", function(document) {
    document.documentElement.innerHTML = `
      Sources<span id="host"></span>
      <div>whatever</div>
    `;
    document.getElementById("host").attachShadow({ mode: "open" }).innerHTML = "Shadow text";
  }, "Test inside a shadow-root mid-match");

  testFindable(false, "Outside shadow", function(document) {
    document.documentElement.innerHTML = `
      Outside <div id="host"></div> shadow
    `;
    document.getElementById("host").attachShadow({ mode: "open" }).innerHTML = "inside shadow";
  }, "Block in different subtree");

  // NOTE(emilio): It is probably doable / worth changing this to return true,
  // maybe, by relaxing the security checks in the ranges nsFind returns or
  // such.
  //
  // See bug 1442466 / bug 1510485 / bug 1505887.
  testFindable(false, "foo", function(document) {
    let input = document.createElement("input");
    input.value = "foo";
    document.documentElement.appendChild(input);
  }, "Native anonymous content isn't exposed in window.find");

  // Same as above, but in this case the check is warranted, we shouldn't
  // expose this range.
  testFindable(false, "find me", `
    <style>div::before { content: "Do find me" }</style>
    <div></div>
  `, "Pseudo-element");

  // Same as above.
  testFindable(false, "find me", `
    <img alt="Do find me">
  `, "Image alt content");

  // Same as above.
  testFindable(false, "find me", `
    <input type="submit" value="Do find me">
  `, "Submit input value");

  testFindable(false, "\0", `
    &#0;
  `);

  testFindable(true, "\0", function(document) {
    document.documentElement.appendChild(document.createTextNode("\0"));
  }, "Inserted null characters are findable");

  testFindable(false, "ab", `a<br>b`, "<br> forces a break even if there's no whitespace in between");

  testFindable(true, "history.kafka", `
    <code>database.history&#8203;.kafka.bootstrap.servers</code>
  `, "ZWSP should be ignored");

  testFindable(2, " ", "a    b    c", "Collapsed whitespace");

  // TODO(emilio): This might be worth discussing in the spec. For now
  // hard-coding our implementation.
  testFindable(false, "find me", `
    Do you find <span inert>not findable</span> me?
  `, "boundary-crossing inert");

  testFindable(true, "hidden content", `
    <div hidden="until-found">This is hidden content</div>
  `, "hidden=until-found should be findable");

  testFindable(true, "details content", `
    <details>
      <summary>Click to expand</summary>
      <div>This is details content</div>
    </details>
  `, "closed details should be findable");

  promise_test(async function() {
    const iframe = document.querySelector("iframe");
    iframe.contentDocument.documentElement.innerHTML = `
      <div id="hidden" hidden="until-found">findable text</div>
    `;

    const beforematchPromise = new Promise(resolve => {
      iframe.contentDocument.getElementById("hidden").addEventListener("beforematch", () => {
        resolve();
      }, { once: true });
    });

    iframe.contentWindow.getSelection().removeAllRanges();
    const found = iframe.contentWindow.find("findable");

    assert_true(found, "Should find text in hidden=until-found");

    await beforematchPromise;

    await new Promise(resolve => iframe.contentWindow.requestAnimationFrame(resolve));

    const hiddenElement = iframe.contentDocument.getElementById("hidden");
    assert_equals(hiddenElement.hidden, false, "hidden attribute should be removed");
  }, "beforematch event fires and element is revealed for hidden=until-found");

  testFindable(false, "should not be found", `
    <style>#outer { content-visibility: hidden; }</style>
    <div id="outer">
      <div hidden="until-found">This should not be found</div>
    </div>
  `, "hidden=until-found inside content-visibility:hidden should not be findable");

  promise_test(async function() {
    const iframe = document.querySelector("iframe");
    iframe.contentDocument.documentElement.innerHTML = `
      <details id="details">
        <summary>Summary</summary>
        <div>Details text</div>
      </details>
    `;

    iframe.contentWindow.getSelection().removeAllRanges();
    const found = iframe.contentWindow.find("Details text");

    assert_true(found, "Should find text in closed details");

    await new Promise(resolve => iframe.contentWindow.requestAnimationFrame(resolve));

    const detailsElement = iframe.contentDocument.getElementById("details");
    assert_true(detailsElement.open, "details element should be opened after find");
  }, "details element is opened after find");
});

window.onload = function() {
  let iframe = document.createElement("iframe");
  iframe.onload = runTests;
  iframe.srcdoc = "<!doctype html><html></html>";
  document.body.appendChild(iframe);
};
</script>
</body>