Highlight-iteration-with-modifications.html (6597B)
1 <!doctype html> 2 <title>Highlight iteration with insertions and deletions inbetween</title> 3 <link rel="help" href="https://drafts.csswg.org/css-highlight-api-1/"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id='testDiv'>abc</div> 7 <script> 8 'use strict'; 9 let container = document.getElementById('testDiv'); 10 let range1 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 1}); 11 let range2 = new Range(); 12 13 // Test insertions using .add 14 test(() => { 15 let customHighlight = new Highlight(); 16 let iterator = customHighlight[Symbol.iterator](); 17 customHighlight.add(range1); 18 let element = iterator.next(); 19 assert_true(element.done, 'The iteration ends although we added a new range after starting the iteration'); 20 assert_equals(element.value, undefined, 'A range added after starting the iteration is not found during the current iteration'); 21 }, 'Highlight iteration is not modified when a new range is added after starting the iteration'); 22 23 test(() => { 24 let customHighlight = new Highlight(range1); 25 let iterator = customHighlight[Symbol.iterator](); 26 customHighlight.add(range2); 27 let element = iterator.next(); 28 assert_false(element.done, 'The iteration doesn\'t end although there was a second range added to the Highlight after starting the iteration'); 29 assert_equals(element.value, range1, 'The range that was pointed to by the iterator is returned although a second range was added after starting the iteration'); 30 element = iterator.next(); 31 assert_true(element.done, 'The iteration ends after going through all the ranges that were in the Highlight when the iteration started although there was a range addition after starting the iteration'); 32 assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends'); 33 }, 'Highlight iteration is not modified when a new range is added after starting the iteration with one range in the Highlight'); 34 35 // Test deletions using .delete 36 test(() => { 37 let customHighlight = new Highlight(range1); 38 let iterator = customHighlight[Symbol.iterator](); 39 customHighlight.delete(range1); 40 let element = iterator.next(); 41 assert_false(element.done, 'The iteration doesn\'t end although the range that was pointed to by the iterator was deleted'); 42 assert_equals(element.value, range1, 'The range that was pointed to by the iterator is returned although it was deleted after starting the iteration'); 43 element = iterator.next(); 44 assert_true(element.done, 'The iteration ends after going through all the ranges although the range that was pointed to by the iterator was deleted after starting the iteration'); 45 assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends'); 46 }, 'Highlight iteration is not modified when the range that was pointed to by the iterator was deleted after starting the iteration'); 47 48 test(() => { 49 let customHighlight = new Highlight(range1, range2); 50 let iterator = customHighlight[Symbol.iterator](); 51 customHighlight.delete(range2); 52 let element = iterator.next(); 53 assert_false(element.done, 'The iteration doesn\'t end when the range following to the one that was pointed to by the iterator was deleted'); 54 assert_equals(element.value, range1, 'The range that was pointed to by the iterator is returned as it should although the next range was deleted immediately after starting the iteration'); 55 element = iterator.next(); 56 assert_false(element.done, 'The iteration doesn\'t end when you call .next twice since the beginning of the iteration although the second range was deleted'); 57 assert_equals(element.value, range2, 'The range that was pointed to by the iterator is returned as it should although the next range was deleted immediately after starting the iteration'); 58 element = iterator.next(); 59 assert_true(element.done, 'The iteration ends after going through all the ranges although the second range was deleted immediately after starting the iteration'); 60 assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends'); 61 }, 'Highlight iteration is not modified when the range that was immediately after the one pointed to by the iterator was deleted after starting the iteration'); 62 63 test(() => { 64 let customHighlight = new Highlight(range1, range2); 65 let iterator = customHighlight[Symbol.iterator](); 66 let element = iterator.next(); 67 assert_false(element.done, 'The iteration doesn\'t end when there are still two ranges to visit'); 68 assert_equals(element.value, range1, 'The range that was pointed to by the iterator is returned as it should'); 69 customHighlight.delete(range1); 70 element = iterator.next(); 71 assert_false(element.done, 'The iteration doesn\'t end when the range previously visited is deleted and there is still a range to visit'); 72 assert_equals(element.value, range2, 'The range that was pointed to by the iterator is returned as it should although the previous range was deleted after calling .next'); 73 element = iterator.next(); 74 assert_true(element.done, 'The iteration ends after going through all the ranges although the first range was deleted after the first call to .next'); 75 assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends'); 76 }, 'Highlight iteration is not modified when a range that was already visited is deleted and there are still ranges to visit'); 77 78 // Test deletions using .clear 79 test(() => { 80 let customHighlight = new Highlight(range1); 81 let iterator = customHighlight[Symbol.iterator](); 82 customHighlight.clear(); 83 let element = iterator.next(); 84 assert_false(element.done, 'The iteration doesn\'t end although the range that was pointed to by the iterator was deleted using .clear()'); 85 assert_equals(element.value, range1, 'The range that was pointed to by the iterator is returned although it was deleted using .clear() after starting the iteration'); 86 element = iterator.next(); 87 assert_true(element.done, 'The iteration ends after going through all the ranges although the range that was pointed to by the iterator was deleted using .clear() after starting the iteration'); 88 assert_equals(element.value, undefined, '.next() returns undefined when the iteration ends'); 89 }, 'Highlight iteration is not modified when the range that was pointed to by the iterator was deleted using .clear() after starting the iteration'); 90 91 </script>