summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-to-text-fragment/find-range-from-text-directive.html
blob: 229f082747b4ca32ce5724e2785269789b2d016f (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!doctype html>
<title>Tests find-a-range-from-a-text-directive algorithm</title>
<meta charset=utf-8>
<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#find-a-range-from-a-text-directive">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/utils.js"></script>
<script src="stash.js"></script>
<!--
  This test suite performs scroll to text navigations to
  find-range-from-text-directive-target.html and then checks the results, which are
  communicated back from the target page via the WPT Stash server (see stash.py).
  This structure is necessary because scroll to text security restrictions
  specifically restrict the navigator from being able to observe the result of
  the navigation, e.g. the target page cannot have a window opener.
-->
<script>
// This test suite specifically exercises the "find a range from a text
// directive" algorithm in the spec.

let test_cases = [
  {
    fragment: '#:~:text=jumped',
    expect_to_scroll: true,
    description: 'Basic smoke test - full word match'
  },
  {
    // Step 2.2.1
    fragment: '#:~:text=u-,mped',
    expect_to_scroll: false,
    description: 'Prefix must start on a word boundary'
  },
  {
    // Step 2.2.1
    fragment: '#:~:text=ju-,mped',
    expect_to_scroll: true,
    description: 'Prefix need not end on a word boundary'
  },
  {
    // Step 2.2.2
    fragment: '#:~:text=null-,The%20quick',
    expect_to_scroll: false,
    description: 'Prefix doesn\'t exist'
  },
  {
    // Step 2.2.3
    fragment: '#:~:text=foo%20foo-,bar',
    expect_to_scroll: true,
    description: 'Multiple overlapping prefixes'
  },
  {
    // Step 2.2.3
    fragment: '#:~:text=a%20a-,b',
    expect_to_scroll: true,
    description: 'Multiple overlapping one letter prefixes'
  },
  {
    // Step 2.2.4
    fragment: '#:~:text=quick%20brown-,brown%20fox',
    expect_to_scroll: false,
    description: 'Prefix overlaps match text'
  },
  {
    // Step 2.2.4
    fragment: '#:~:text=quick%20brown-,fox',
    expect_to_scroll: true,
    description: 'Match text after prefix'
  },
  {
    // Step 2.2.5
    fragment: '#:~:text=Lorem-,Ipsum',
    expect_to_scroll: true,
    description: 'Search invisible content between prefix and match'
  },
  {
    // Step 2.2.6
    fragment: '#:~:text=end%20of%20the%20document-,test',
    expect_to_scroll: false,
    description: 'Prefix appears at end of document'
  },
  {
    // Step 2.2.8
    fragment: '#:~:text=fox-,jum,over',
    expect_to_scroll: false,
    description: '|end| forces |start| to end on word boundary'
  },
  {
    // Step 2.2.8
    fragment: '#:~:text=fox-,jum',
    expect_to_scroll: false,
    description: 'no |end| or suffix forces |start| to end on word boundary'
  },
  {
    // Step 2.2.8
    fragment: '#:~:text=fox-,jum,-ped',
    expect_to_scroll: true,
    description: 'suffix means |start| need not end on word boundary'
  },
  {
    // Step 2.2.9
    fragment: '#:~:text=jum-,ped',
    expect_to_scroll: true,
    description: '|start| doesn\'t need to start on word boundary'
  },
  {
    // Step 2.2.10
    fragment: '#:~:text=jumped-,null',
    expect_to_scroll: false,
    description: 'prefix with non-existent exact match'
  },
  {
    // Step 2.2.10
    fragment: '#:~:text=jumped-,null,lazy',
    expect_to_scroll: false,
    description: 'prefix with non-existent range match'
  },
  {
    // Step 2.2.11
    fragment: '#:~:text=brown-,jumped',
    expect_to_scroll: false,
    description: 'match doesn\'t immediately follow prefix'
  },
  {
    // Step 2.2.11
    fragment: '#:~:text=foo-,bar',
    expect_to_scroll: true,
    description: 'match doesn\'t immediately follow first prefix instance'
  },
  {
    // Step 2.3.1
    fragment: '#:~:text=jum,over',
    expect_to_scroll: false,
    description: 'no-prefix; |end| forces |start| to end on word boundary'
  },
  {
    // Step 2.3.1
    fragment: '#:~:text=jum',
    expect_to_scroll: false,
    description: 'no-prefix; no |end| or suffix forces |start| to end on word boundary'
  },
  {
    // Step 2.3.1
    fragment: '#:~:text=jum,-ped',
    expect_to_scroll: true,
    description: 'no-prefix; suffix means |start| need not end on word boundary'
  },
  {
    // Step 2.3.2
    fragment: '#:~:text=umped',
    expect_to_scroll: false,
    description: '|start| must start on a word boundary'
  },
  {
    // Step 2.3.3
    fragment: '#:~:text=null',
    expect_to_scroll: false,
    description: 'non-existent exact match'
  },
  {
    // Step 2.3.3
    fragment: '#:~:text=null,lazy',
    expect_to_scroll: false,
    description: 'non-existent range match'
  },
  {
    // Step 2.3.4
    fragment: '#:~:text=b%20b,-c',
    expect_to_scroll: true,
    description: 'overlapping exact matches with suffix'
  },
  {
    // Step 2.3.4
    fragment: '#:~:text=foo%20foo,-bar',
    expect_to_scroll: true,
    description: 'overlapping one letter exact matches with suffix'
  },
  {
    // Step 2.5.1
    fragment: '#:~:text=brown,fox',
    expect_to_scroll: true,
    description: 'matching range search'
  },
  {
    // Step 2.4
    fragment: '#:~:text=brown,quick',
    expect_to_scroll: false,
    description: 'inverted range search'
  },
  {
    // Step 2.5.1.1
    fragment: '#:~:text=quick,bro',
    expect_to_scroll: false,
    description: 'no suffix forces |end| to be end bounded'
  },
  {
    // Step 2.5.1.1
    fragment: '#:~:text=quick,bro,-wn',
    expect_to_scroll: true,
    description: 'suffix means |end| need not be end bounded'
  },
  {
    // Step 2.5.1.2
    fragment: '#:~:text=quick,ro,-wn',
    expect_to_scroll: false,
    description: '|end| must be start bounded'
  },
  {
    // Step 2.5.1.2
    fragment: '#:~:text=bro,wn',
    expect_to_scroll: false,
    description: '|end| must be start bounded even if full range is word bounded'
  },
  {
    // Step 2.5.1.3
    fragment: '#:~:text=quick,null',
    expect_to_scroll: false,
    description: 'non-existent |end|'
  },
  {
    // Step 2.5.1.4
    fragment: '#:~:text=quick,jumped,-fox',
    expect_to_scroll: false,
    description: 'Range with preceeding suffix'
  },
  {
    // Step 2.5.3
    fragment: '#:~:text=The-,quick,brown',
    expect_to_scroll: true,
    description: 'Match with no suffix'
  },
  {
    // Step 2.5.4
    fragment: '#:~:text=The-,quick,fox,-brown',
    expect_to_scroll: false,
    description: 'Suffix comes before |end|'
  },
  {
    // Step 2.5.5
    fragment: '#:~:text=Lorem-,Ipsum,Whitespace,-Dipsum',
    expect_to_scroll: true,
    description: 'Search invisible content between |end| and suffix'
  },
  {
    // Step 2.5.6
    fragment: '#:~:text=quick,-bro',
    expect_to_scroll: false,
    description: 'Suffix must be end bounded'
  },
  {
    // Step 2.5.6
    fragment: '#:~:text=qu,-ick',
    expect_to_scroll: true,
    description: 'Suffix need not be start bounded'
  },
  {
    // Step 2.5.7
    fragment: '#:~:text=quick,-null',
    expect_to_scroll: false,
    description: 'Non-existent suffix'
  },
  {
    // Step 2.5.8
    fragment: '#:~:text=quick,-fox',
    expect_to_scroll: false,
    description: 'Content appears between match and suffix'
  },
  {
    // Step 2.5.9
    fragment: '#:~:text=match,-suffix2',
    expect_to_scroll: true,
    description: 'Non-matching suffix in first potential match'
  },
  {
    // Step 2.5.9
    fragment: '#:~:text=prefix-,match,-suffix3',
    expect_to_scroll: true,
    description: 'Non-matching suffix search continues to prefix match'
  },
  {
    // Step 2.5.10
    fragment: '#:~:text=prefix-,match,matchEnd,-suffix5',
    expect_to_scroll: true,
    description: 'Range end matches correct suffix'
  }
];

for (const test_case of test_cases) {
  promise_test(t => new Promise((resolve, reject) => {
    let key = token();

    test_driver.bless('Open a URL with a text fragment directive', () => {
      window.open(`find-range-from-text-directive-target.html?key=${key}${test_case.fragment}`, '_blank', 'noopener');
    });

    fetchResults(key, resolve, reject);
  }).then(data => {
    const expectation_string = test_case.expect_to_scroll ? 'to scroll' : 'not to scroll';
    assert_equals(data.didScroll, test_case.expect_to_scroll,
                  `Expected ${expectation_string}`);
  }), `${test_case.description}.`);
}
</script>