Highlight-iteration.html (10941B)
1 <!doctype html> 2 <title>Highlight iteration</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 range1 = new Range(); 10 let container = document.getElementById('testDiv'); 11 let range2 = new StaticRange({startContainer: container, startOffset: 0, endContainer: container, endOffset: 0}); 12 13 let rangeAdditionModeCollection = ["constructor", "add function"]; 14 let iterationInitializationCollection = ["customHighlight[Symbol.iterator]()", "customHighlight.values()", "customHighlight.keys()"]; 15 16 function getIterator(customHighlight, iterationInitialization){ 17 var iterator; 18 if(iterationInitialization === "customHighlight[Symbol.iterator]()"){ 19 iterator = customHighlight[Symbol.iterator](); 20 } 21 else if(iterationInitialization === "customHighlight.values()"){ 22 iterator = customHighlight.values(); 23 } 24 else if(iterationInitialization === "customHighlight.keys()"){ 25 iterator = customHighlight.keys(); 26 } 27 return iterator; 28 } 29 30 // Test .keys, .values, [Symbol.iterator] 31 32 for(let iterationInitialization of iterationInitializationCollection){ 33 test(() => { 34 let customHighlight = new Highlight(); 35 let iterator = getIterator(customHighlight, iterationInitialization); 36 let element = iterator.next(); 37 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 38 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 39 }, 'Highlight can be iterated when it\'s empty initializing the iterator with ' + iterationInitialization); 40 } 41 42 for(let rangeAdditionMode of rangeAdditionModeCollection){ 43 for(let iterationInitialization of iterationInitializationCollection){ 44 test(() => { 45 var customHighlight; 46 if(rangeAdditionMode === "constructor"){ 47 customHighlight = new Highlight(range1); 48 } 49 else if(rangeAdditionMode === "add function"){ 50 customHighlight = new Highlight(); 51 customHighlight.add(range1); 52 } 53 54 let iterator = getIterator(customHighlight, iterationInitialization); 55 let element = iterator.next(); 56 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last range'); 57 assert_equals(element.value, range1, '.next() returns an element with .value corresponding to the first range added to the Highlight'); 58 element = iterator.next(); 59 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 60 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 61 }, 'Highlight can be iterated over all of its ranges initializing the iterator with ' + iterationInitialization + ' and adding a range by passing it to the ' + rangeAdditionMode); 62 63 test(() => { 64 var customHighlight; 65 if(rangeAdditionMode === "constructor"){ 66 customHighlight = new Highlight(range1, range2); 67 } 68 else if(rangeAdditionMode === "add function"){ 69 customHighlight = new Highlight(); 70 customHighlight.add(range1); 71 customHighlight.add(range2); 72 } 73 74 let iterator = getIterator(customHighlight, iterationInitialization); 75 let element = iterator.next(); 76 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last range'); 77 assert_equals(element.value, range1, '.next() returns an element with .value corresponding to the first range added to the Highlight'); 78 element = iterator.next(); 79 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last range'); 80 assert_equals(element.value, range2, '.next() returns an element with .value corresponding to the second range added to the Highlight'); 81 element = iterator.next(); 82 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 83 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 84 }, 'Highlight can be iterated over all of its ranges initializing the iterator with ' + iterationInitialization + ' and adding two ranges by passing them to the ' + rangeAdditionMode); 85 } 86 } 87 88 // Test .entries() 89 90 test(() => { 91 let customHighlight = new Highlight(); 92 let iterator = customHighlight.entries(); 93 let element = iterator.next(); 94 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 95 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 96 }, 'Highlight can be iterated when it\'s empty initializing the iterator with .entries()'); 97 98 for(let rangeAdditionMode of rangeAdditionModeCollection){ 99 test(() => { 100 var customHighlight; 101 if(rangeAdditionMode === "constructor"){ 102 customHighlight = new Highlight(range1); 103 } 104 else if(rangeAdditionMode === "add function"){ 105 customHighlight = new Highlight(); 106 customHighlight.add(range1); 107 } 108 109 let iterator = customHighlight.entries(); 110 let element = iterator.next(); 111 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last range'); 112 assert_equals(element.value[0], range1, '.next() returns an element with .value[0] corresponding to the first range added to the Highlight'); 113 assert_equals(element.value[1], range1, '.next() returns an element with .value[1] corresponding to the first range added to the Highlight'); 114 element = iterator.next(); 115 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 116 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 117 }, 'Highlight can be iterated over all of its ranges initializing the iterator with .entries() and adding a range by passing it to the ' + rangeAdditionMode); 118 119 test(() => { 120 var customHighlight; 121 if(rangeAdditionMode === "constructor"){ 122 customHighlight = new Highlight(range1, range2); 123 } 124 else if(rangeAdditionMode === "add function"){ 125 customHighlight = new Highlight(); 126 customHighlight.add(range1); 127 customHighlight.add(range2); 128 } 129 130 let iterator = customHighlight.entries(); 131 let element = iterator.next(); 132 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight'); 133 assert_equals(element.value[0], range1, '.next() returns an element with .value[0] corresponding to the first range added to the Highlight'); 134 assert_equals(element.value[1], range1, '.next() returns an element with .value[1] corresponding to the first range added to the Highlight'); 135 element = iterator.next(); 136 assert_false(element.done, 'Highlight is iterable and .next() returns an element with .done===false when the iteration didn\'t go past the last highlight'); 137 assert_equals(element.value[0], range2, '.next() returns an element with .value[0] corresponding to the second range added to the Highlight'); 138 assert_equals(element.value[1], range2, '.next() returns an element with .value[1] corresponding to the second range added to the Highlight'); 139 element = iterator.next(); 140 assert_true(element.done, 'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate'); 141 assert_equals(element.value, undefined, 'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.'); 142 }, 'Highlight can be iterated over all of its ranges initializing the iterator with .entries() and adding two ranges by passing them to the ' + rangeAdditionMode); 143 } 144 145 // Test .forEach 146 147 function compareArrays(array1, array2){ 148 if(array1.length != array2.length){ 149 return false; 150 } 151 for(let index=0; index<array1.length; ++index){ 152 if(array1[index] != array2[index]) 153 return false; 154 } 155 return true; 156 } 157 158 test(() => { 159 let customHighlight = new Highlight(); 160 let expectedResult = []; 161 let actualResult = []; 162 customHighlight.forEach((range) => {actualResult.push(range);}); 163 assert_true(compareArrays(actualResult, expectedResult), 'The ranges seen match the ranges added'); 164 }, 'Highlight can be iterated through when it\'s empty using forEach.'); 165 166 for(let rangeAdditionMode of rangeAdditionModeCollection){ 167 test(() => { 168 var customHighlight; 169 if(rangeAdditionMode === "constructor"){ 170 customHighlight = new Highlight(range1); 171 } 172 else if(rangeAdditionMode === "add function"){ 173 customHighlight = new Highlight(); 174 customHighlight.add(range1); 175 } 176 177 let expectedResult = [range1]; 178 let actualResult = []; 179 customHighlight.forEach((range) => {actualResult.push(range);}); 180 assert_true(compareArrays(actualResult, expectedResult), 'The ranges seen match the ranges added'); 181 }, 'Highlight can be iterated through using forEach when it has one range that was added by passing it to the ' + rangeAdditionMode); 182 183 test(() => { 184 var customHighlight; 185 if(rangeAdditionMode === "constructor"){ 186 customHighlight = new Highlight(range1, range2); 187 } 188 else if(rangeAdditionMode === "add function"){ 189 customHighlight = new Highlight(); 190 customHighlight.add(range1); 191 customHighlight.add(range2); 192 } 193 194 let expectedResult = [range1, range2]; 195 let actualResult = []; 196 customHighlight.forEach((range) => {actualResult.push(range);}); 197 assert_true(compareArrays(actualResult, expectedResult), 'The ranges seen match the ranges added'); 198 }, 'Highlight can be iterated through using forEach when it has two ranges that were added by passing them to the ' + rangeAdditionMode); 199 } 200 201 </script>