Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / test / test_find.html
blob9969d744ada7d06f5532b67750e6cedec62d6184
1 <!doctype html>
2 <meta charset="utf-8">
3 <script src="/resources/testharness.js"></script>
4 <script src="/resources/testharnessreport.js"></script>
5 <body>
6 <script>
7 const t = async_test("Test window.find / nsFind");
9 function testFindable(isFindable, textToFind, buildDoc, description) {
10 try {
11 const iframe = document.querySelector("iframe")
12 iframe.contentDocument.documentElement.innerHTML =
13 (typeof buildDoc == "string") ? buildDoc : "";
15 if (typeof buildDoc == "function")
16 buildDoc(iframe.contentDocument);
18 iframe.contentWindow.getSelection().removeAllRanges();
19 assert_equals(
20 isFindable,
21 iframe.contentWindow.find(textToFind),
22 "Should be " + (isFindable ? "" : "not ") + "findable: " + description + ", text: " + textToFind
24 } catch (ex) {
25 assert_unreached(ex);
29 const INLINE_LIKE_DISPLAY_VALUES = [
30 "inline",
31 "inline-grid",
32 "inline-block",
33 "inline-flex",
36 const BLOCK_LIKE_DISPLAY_VALUES = [
37 "block",
38 "table",
39 "list-item",
40 "grid",
41 "flex",
44 let runTests = t.step_func_done(function() {
45 testFindable(true, "me and me", `
46 me <div style="display: contents">and</div> me
47 `, "display: contents");
49 testFindable(true, "me me", `
50 me <div style="display: none">and</div> me
51 `, "display: none");
53 testFindable(false, "me and me", `
54 me <div style="display: none">and</div> me
55 `, "display: none");
57 for (const display of INLINE_LIKE_DISPLAY_VALUES) {
58 testFindable(true, "me and me", `
59 me <div style="display: ${display}">and</div> me
60 `, "div display: " + display);
61 testFindable(true, "me and me", `
62 me <span style="display: ${display}">and</span> me
63 `, "span display: " + display);
66 for (const display of BLOCK_LIKE_DISPLAY_VALUES) {
67 testFindable(false, "me and me", `
68 me <div style="display: ${display}">and</div> me
69 `, "div display: " + display);
70 testFindable(false, "me and me", `
71 me <span style="display: ${display}">and</span> me
72 `, "span display: " + display);
75 testFindable(false, "me and me", `
76 me <fieldset>and</fieldset> me
77 `);
79 testFindable(true, "This text should be visible", `
80 <div style="visibility: hidden">
81 <div style="visibility: visible">
82 This text should be visible
83 </div>
84 </div>
85 `);
87 testFindable(true, "This text should be visible", `
88 <style>:root { overflow: hidden }</style>
89 <div style="overflow: auto;">
90 <div style="height: 300vh"></div>
91 This text should be visible
92 </div>
93 `);
95 testFindable(true, "foobar", `
96 <body><script style="display: block;">foobar</` + `script></body>
97 `);
100 testFindable(true, "Shadow text", function(document) {
101 let div = document.createElement("div");
102 div.attachShadow({ mode: "open" }).innerHTML = `
103 Wohoo, this is Shadow text, yay!
105 document.documentElement.appendChild(div);
106 }, "In Shadow DOM");
108 testFindable(true, "Shadow text", function(document) {
109 let div = document.createElement("div");
110 div.appendChild(document.createTextNode(
111 "Wohoo, this is Shadow text, yay!"
113 div.attachShadow({ mode: "open" }).innerHTML = `<slot></slot>`;
114 document.documentElement.appendChild(div);
115 }, "Slotted content in Shadow DOM");
117 // TODO(emilio): This should work in an ideal world.
118 testFindable(false, "Shadow text", function(document) {
119 let div = document.createElement("div");
120 div.appendChild(document.createTextNode("text, yay!"));
121 div.attachShadow({ mode: "open" }).innerHTML = `This is Shadow <slot></slot>`;
122 document.documentElement.appendChild(div);
123 }, "Mixed shadow and non-shadow text");
125 testFindable(true, "Shadow", function(document) {
126 document.documentElement.innerHTML = `
127 Sources<span id="host"></span>
128 <div>whatever</div>
130 document.getElementById("host").attachShadow({ mode: "open" }).innerHTML = "Shadow text";
131 }, "Test inside a shadow-root mid-match");
133 testFindable(false, "Outside shadow", function(document) {
134 document.documentElement.innerHTML = `
135 Outside <div id="host"></div> shadow
137 document.getElementById("host").attachShadow({ mode: "open" }).innerHTML = "inside shadow";
138 }, "Block in different subtree");
140 // NOTE(emilio): It is probably doable / worth changing this to return true,
141 // maybe, by relaxing the security checks in the ranges nsFind returns or
142 // such.
144 // See bug 1442466 / bug 1510485 / bug 1505887.
145 testFindable(false, "foo", function(document) {
146 let input = document.createElement("input");
147 input.value = "foo";
148 document.documentElement.appendChild(input);
149 }, "Native anonymous content isn't exposed in window.find");
151 // Same as above, but in this case the check is warranted, we shouldn't
152 // expose this range.
153 testFindable(false, "find me", `
154 <style>div::before { content: "Do find me" }</style>
155 <div></div>
156 `, "Pseudo-element");
158 // Same as above.
159 testFindable(false, "find me", `
160 <img alt="Do find me">
161 `, "Image alt content");
163 // Same as above.
164 testFindable(false, "find me", `
165 <input type="submit" value="Do find me">
166 `, "Submit input value");
168 testFindable(false, "\0", `
169 &#0;
172 testFindable(true, "\0", function(document) {
173 document.documentElement.appendChild(document.createTextNode("\0"));
174 }, "Inserted null characters are findable");
176 testFindable(false, "ab", `a<br>b`, "<br> forces a break even if there's no whitespace in between");
178 testFindable(true, "history.kafka", `
179 <code>database.history&#8203;.kafka.bootstrap.servers</code>
180 `, "ZWSP should be ignored");
183 window.onload = function() {
184 let iframe = document.createElement("iframe");
185 iframe.onload = runTests;
186 iframe.srcdoc = "<!doctype html><html></html>";
187 document.body.appendChild(iframe);
189 </script>
190 </body>