- Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathHighlight-iteration.html
201 lines (179 loc) · 10.7 KB
/
Highlight-iteration.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!doctype html>
<title>Highlight iteration</title>
<linkrel="help" href="https://drafts.csswg.org/css-highlight-api-1/">
<scriptsrc="/resources/testharness.js"></script>
<scriptsrc="/resources/testharnessreport.js"></script>
<divid='testDiv'>abc</div>
<script>
'use strict';
letrange1=newRange();
letcontainer=document.getElementById('testDiv');
letrange2=newStaticRange({startContainer: container,startOffset: 0,endContainer: container,endOffset: 0});
letrangeAdditionModeCollection=["constructor","add function"];
letiterationInitializationCollection=["customHighlight[Symbol.iterator]()","customHighlight.values()","customHighlight.keys()"];
functiongetIterator(customHighlight,iterationInitialization){
variterator;
if(iterationInitialization==="customHighlight[Symbol.iterator]()"){
iterator=customHighlight[Symbol.iterator]();
}
elseif(iterationInitialization==="customHighlight.values()"){
iterator=customHighlight.values();
}
elseif(iterationInitialization==="customHighlight.keys()"){
iterator=customHighlight.keys();
}
returniterator;
}
// Test .keys, .values, [Symbol.iterator]
for(letiterationInitializationofiterationInitializationCollection){
test(()=>{
letcustomHighlight=newHighlight();
letiterator=getIterator(customHighlight,iterationInitialization);
letelement=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated when it\'s empty initializing the iterator with '+iterationInitialization);
}
for(letrangeAdditionModeofrangeAdditionModeCollection){
for(letiterationInitializationofiterationInitializationCollection){
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
}
letiterator=getIterator(customHighlight,iterationInitialization);
letelement=iterator.next();
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');
assert_equals(element.value,range1,'.next() returns an element with .value corresponding to the first range added to the Highlight');
element=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated over all of its ranges initializing the iterator with '+iterationInitialization+' and adding a range by passing it to the '+rangeAdditionMode);
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1,range2);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
customHighlight.add(range2);
}
letiterator=getIterator(customHighlight,iterationInitialization);
letelement=iterator.next();
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');
assert_equals(element.value,range1,'.next() returns an element with .value corresponding to the first range added to the Highlight');
element=iterator.next();
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');
assert_equals(element.value,range2,'.next() returns an element with .value corresponding to the second range added to the Highlight');
element=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated over all of its ranges initializing the iterator with '+iterationInitialization+' and adding two ranges by passing them to the '+rangeAdditionMode);
}
}
// Test .entries()
test(()=>{
letcustomHighlight=newHighlight();
letiterator=customHighlight.entries();
letelement=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated when it\'s empty initializing the iterator with .entries()');
for(letrangeAdditionModeofrangeAdditionModeCollection){
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
}
letiterator=customHighlight.entries();
letelement=iterator.next();
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');
assert_equals(element.value[0],range1,'.next() returns an element with .value[0] corresponding to the first range added to the Highlight');
assert_equals(element.value[1],range1,'.next() returns an element with .value[1] corresponding to the first range added to the Highlight');
element=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated over all of its ranges initializing the iterator with .entries() and adding a range by passing it to the '+rangeAdditionMode);
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1,range2);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
customHighlight.add(range2);
}
letiterator=customHighlight.entries();
letelement=iterator.next();
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');
assert_equals(element.value[0],range1,'.next() returns an element with .value[0] corresponding to the first range added to the Highlight');
assert_equals(element.value[1],range1,'.next() returns an element with .value[1] corresponding to the first range added to the Highlight');
element=iterator.next();
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');
assert_equals(element.value[0],range2,'.next() returns an element with .value[0] corresponding to the second range added to the Highlight');
assert_equals(element.value[1],range2,'.next() returns an element with .value[1] corresponding to the second range added to the Highlight');
element=iterator.next();
assert_true(element.done,'Highlight is iterable and .next() returns an element with .done===true when there are no more ranges to iterate');
assert_equals(element.value,undefined,'Highlight is iterable and .next() returns an element with .value undefined when there are no more ranges to iterate.');
},'Highlight can be iterated over all of its ranges initializing the iterator with .entries() and adding two ranges by passing them to the '+rangeAdditionMode);
}
// Test .forEach
functioncompareArrays(array1,array2){
if(array1.length!=array2.length){
returnfalse;
}
for(letindex=0;index<array1.length;++index){
if(array1[index]!=array2[index])
returnfalse;
}
returntrue;
}
test(()=>{
letcustomHighlight=newHighlight();
letexpectedResult=[];
letactualResult=[];
customHighlight.forEach((range)=>{actualResult.push(range);});
assert_true(compareArrays(actualResult,expectedResult),'The ranges seen match the ranges added');
},'Highlight can be iterated through when it\'s empty using forEach.');
for(letrangeAdditionModeofrangeAdditionModeCollection){
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
}
letexpectedResult=[range1];
letactualResult=[];
customHighlight.forEach((range)=>{actualResult.push(range);});
assert_true(compareArrays(actualResult,expectedResult),'The ranges seen match the ranges added');
},'Highlight can be iterated through using forEach when it has one range that was added by passing it to the '+rangeAdditionMode);
test(()=>{
varcustomHighlight;
if(rangeAdditionMode==="constructor"){
customHighlight=newHighlight(range1,range2);
}
elseif(rangeAdditionMode==="add function"){
customHighlight=newHighlight();
customHighlight.add(range1);
customHighlight.add(range2);
}
letexpectedResult=[range1,range2];
letactualResult=[];
customHighlight.forEach((range)=>{actualResult.push(range);});
assert_true(compareArrays(actualResult,expectedResult),'The ranges seen match the ranges added');
},'Highlight can be iterated through using forEach when it has two ranges that were added by passing them to the '+rangeAdditionMode);
}
</script>