input-events-range-exceptions.tentative.html (4863B)
1 <!DOCTYPE html> 2 <meta charset="UTF-8"> 3 <title>InputEvent.getTargetRanges() should return same array in various timings</title> 4 <div contenteditable id="target"></div> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 "use strict"; 9 10 // https://w3c.github.io/input-events/#events-inputevents is not clear that 11 // the StaticRange-based APIs are internally represented as Range during event 12 // propagation. However, at least Blink does this, and it requires that an 13 // exception thrown when converting a StaticRange to a Range propagates the 14 // exception. 15 16 let target = document.getElementById("target"); 17 18 test(() => { 19 target.innerHTML = "hello<span> </span>world"; 20 let ev = new InputEvent("input", 21 { 22 data: "a", 23 inputType: "insertText", 24 targetRanges: [ 25 new StaticRange({ 26 startContainer: target, 27 startOffset: 0, 28 endContainer: target, 29 endOffset: 3, 30 }), 31 ], 32 }); 33 target.querySelector("span").remove(); 34 target.normalize(); 35 let ranges = ev.getTargetRanges(); 36 assert_equals(ranges.length, 1); 37 assert_equals(ranges[0].startContainer, target); 38 assert_equals(ranges[0].startOffset, 0); 39 assert_equals(ranges[0].endContainer, target); 40 assert_equals(ranges[0].endOffset, 1); 41 }, "valid target ranges in Element don't throw exceptions"); 42 43 test(() => { 44 target.innerHTML = "hello<span> </span>world"; 45 assert_throws_dom( 46 "IndexSizeError", 47 () => { 48 // This should throw: 49 let ev = new InputEvent("input", 50 { 51 data: "a", 52 inputType: "insertText", 53 targetRanges: [ 54 new StaticRange({ 55 startContainer: target, 56 startOffset: 0, 57 endContainer: target, 58 endOffset: 5, 59 }), 60 ], 61 }); 62 // Run this in case it doesn't throw, to make sure it doesn't crash: 63 ev.getTargetRanges()[0].endOffset; 64 }); 65 }, "InputEvent constructor throws with invalid range in Element"); 66 67 test(() => { 68 target.innerHTML = "hello<span> </span>world"; 69 let text = target.firstChild; 70 let ev = new InputEvent("input", 71 { 72 data: "a", 73 inputType: "insertText", 74 targetRanges: [ 75 new StaticRange({ 76 startContainer: text, 77 startOffset: 0, 78 endContainer: text, 79 endOffset: 3, 80 }), 81 ], 82 }); 83 text.replaceData(1, 1, "eee"); 84 let ranges = ev.getTargetRanges(); 85 assert_equals(ranges.length, 1); 86 assert_equals(ranges[0].startContainer, text); 87 assert_equals(ranges[0].startOffset, 0); 88 assert_equals(ranges[0].endContainer, text); 89 assert_equals(ranges[0].endOffset, 5); 90 }, "valid target ranges in Text don't throw exceptions"); 91 92 test(() => { 93 target.innerHTML = "hello<span> </span>world"; 94 let text = target.firstChild; 95 assert_throws_dom( 96 "IndexSizeError", 97 () => { 98 // This should throw: 99 let ev = new InputEvent("input", 100 { 101 data: "a", 102 inputType: "insertText", 103 targetRanges: [ 104 new StaticRange({ 105 startContainer: text, 106 startOffset: 0, 107 endContainer: text, 108 endOffset: 12, 109 }), 110 ], 111 }); 112 // Run this in case it doesn't throw, to make sure it doesn't crash: 113 ev.getTargetRanges()[0].endOffset; 114 }); 115 }, "InputEvent constructor throws with invalid range in Text"); 116 117 </script>