test_find_bug1601118.html (2087B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <div id="container"> 6 Fission <br/> 7 Some text <br/> 8 Some more text Fission or Fission or even <span>Fi</span>ssion<br/> 9 Fission <br/> 10 more text<br/> 11 <div> 12 in a nested block Fission stuff 13 </div> 14 </div> 15 <script> 16 const kContainer = document.getElementById("container"); 17 const kExpectedCount = 6; 18 19 // We expect surroundContents() to throw in the <span>Fi</span>ssion case. 20 const kExpectedThrewCount = 1; 21 22 // Keep a hang of the original DOM so as to test forwards and backwards navigation. 23 const kContainerClone = kContainer.cloneNode(true); 24 25 function advance(backwards) { 26 if (!window.find("Fiss", /* caseSensitive = */ true, backwards)) 27 return { success: false }; 28 29 let threw = false; 30 try { 31 window.getSelection().getRangeAt(0).surroundContents(document.createElement("mark")); 32 } catch (ex) { 33 threw = true; 34 } 35 36 // Sanity-check 37 assert_equals(window.getSelection().toString(), "Fiss"); 38 return { success: true, threw }; 39 } 40 41 function runTestForDirection(backwards) { 42 let threwCount = 0; 43 for (let i = 0; i < kExpectedCount; ++i) { 44 let result = advance(backwards); 45 assert_true(result.success, `Should've successfully advanced (${i} / ${kExpectedCount}, backwards: ${backwards})`); 46 if (result.threw) 47 threwCount++; 48 } 49 assert_false(advance(backwards).success, `Should've had exactly ${kExpectedCount} matches`); 50 assert_equals(threwCount, kExpectedThrewCount, "Should've thrown the expected number of times"); 51 assert_equals(kContainer.querySelectorAll("mark").length, kExpectedCount - threwCount, "Should've created the expected number of marks"); 52 assert_false(!!kContainer.querySelector("mark mark"), "Shouldn't have created nested marks"); 53 } 54 55 test(function() { 56 runTestForDirection(false); 57 window.getSelection().removeAllRanges(); 58 kContainer.innerHTML = kContainerClone.innerHTML; 59 runTestForDirection(true); 60 }, "window.find() while marking with surroundContents"); 61 </script>