Selection-addRange-same-instance.html (2932B)
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 "use strict"; 9 10 promise_test(async () => { 11 await new Promise(resolve => document.addEventListener("DOMContentLoaded", resolve)); 12 13 getSelection().removeAllRanges(); 14 const range = document.createRange(); 15 range.setStart(document.querySelector("span"), 0); 16 getSelection().addRange(range); 17 getSelection().addRange(range); 18 assert_equals(getSelection().rangeCount, 1, "Adding same collapsed range twice should not clone the range"); 19 assert_equals(getSelection().getRangeAt(0), range, "The first range should be the added range"); 20 assert_equals(getSelection().focusNode, document.querySelector("span"), "Focus node should be the <span>"); 21 assert_equals(getSelection().focusOffset, 0, "Focus offset should be 0"); 22 assert_true(getSelection().isCollapsed, "Selection should be collapsed"); 23 }, "Adding same collapsed range should not change selections"); 24 25 promise_test(async () => { 26 getSelection().removeAllRanges(); 27 const range = document.createRange(); 28 range.selectNodeContents(document.querySelector("div")); 29 getSelection().addRange(range); 30 getSelection().addRange(range); 31 assert_equals(getSelection().rangeCount, 1, "Adding same range twice should not clone the range"); 32 assert_equals(getSelection().getRangeAt(0), range, "The first range should be the added range"); 33 assert_equals(getSelection().anchorNode, document.querySelector("div"), "Anchor node should be the <div>"); 34 assert_equals(getSelection().anchorOffset, 0, "Anchor offset should be 0"); 35 assert_equals(getSelection().focusNode, document.querySelector("div"), "Focus node should be the <div>"); 36 assert_equals(getSelection().focusOffset, 1, "Focus offset should be 1"); 37 }, "Adding non-collapsed range after updating the range should not change selections"); 38 39 promise_test(async () => { 40 getSelection().removeAllRanges(); 41 const range = document.createRange(); 42 range.collapse(document.querySelector("span"), 0); 43 getSelection().addRange(range); 44 range.selectNodeContents(document.querySelector("div")); 45 getSelection().addRange(range); 46 assert_equals(getSelection().rangeCount, 1, "Adding same range twice should not clone the range"); 47 assert_equals(getSelection().getRangeAt(0), range, "The first range should be the added range"); 48 assert_equals(getSelection().anchorNode, document.querySelector("div"), "Anchor node should be the <div>"); 49 assert_equals(getSelection().anchorOffset, 0, "Anchor offset should be 0"); 50 assert_equals(getSelection().focusNode, document.querySelector("div"), "Focus node should be the <div>"); 51 assert_equals(getSelection().focusOffset, 1, "Focus offset should be 1"); 52 }, "Adding same collapsed range after updating the range should not change selections"); 53 </script> 54 </head> 55 <body><div><span></span></div></body> 56 </html>