3 <script src=
"/resources/testharness.js"></script>
4 <script src=
"/resources/testharnessreport.js"></script>
7 const t
= async_test("Test window.find / nsFind");
9 function testFindable(isFindable
, textToFind
, buildDoc
, description
) {
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();
21 iframe
.contentWindow
.find(textToFind
),
22 "Should be " + (isFindable
? "" : "not ") + "findable: " + description
+ ", text: " + textToFind
29 const INLINE_LIKE_DISPLAY_VALUES
= [
36 const BLOCK_LIKE_DISPLAY_VALUES
= [
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
53 testFindable(false, "me and me", `
54 me <div style="display: none">and</div> me
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
79 testFindable(true, "This text should be visible", `
80 <div style="visibility: hidden">
81 <div style="visibility: visible">
82 This text should be visible
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
95 testFindable(true, "foobar", `
96 <body><script style="display: block;">foobar</` + `script></body>
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
);
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>
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
144 // See bug 1442466 / bug 1510485 / bug 1505887.
145 testFindable(false, "foo", function(document
) {
146 let input
= document
.createElement("input");
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>
156 `, "Pseudo-element");
159 testFindable(false, "find me", `
160 <img alt="Do find me">
161 `, "Image alt content");
164 testFindable(false, "find me", `
165 <input type="submit" value="Do find me">
166 `, "Submit input value");
168 testFindable(false, "\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​.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
);