Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/DOM/Processors/LinterTest.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Test\Parsoid\Wt2Html\DOM\Processors;
5
6 use PHPUnit\Framework\TestCase;
7 use Wikimedia\Parsoid\Mocks\MockDataAccess;
8 use Wikimedia\Parsoid\Mocks\MockPageConfig;
9 use Wikimedia\Parsoid\Mocks\MockPageContent;
10 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
11 use Wikimedia\Parsoid\Parsoid;
12
13 /**
14 * Test cases for the linter
15 *
16 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter
17 */
18 class LinterTest extends TestCase {
19
20 private function wtToLint(
21 string $wt, array $linterOverrides = [], ?string $title = null
22 ): array {
23 $siteOptions = [
24 'linting' => true,
25 'linterOverrides' => $linterOverrides,
26 ];
27 $siteConfig = new MockSiteConfig( $siteOptions );
28
29 $dataAccess = new MockDataAccess( $siteConfig, [] );
30 $parsoid = new Parsoid( $siteConfig, $dataAccess );
31
32 $content = new MockPageContent( [ 'main' => $wt ] );
33 $pageConfig = new MockPageConfig(
34 $siteConfig, [ 'title' => $title ], $content
35 );
36
37 return $parsoid->wikitext2lint( $pageConfig, [] );
38 }
39
40 private function filterLints( array $result, string $cat ): array {
41 $matches = [];
42 foreach ( $result as $r ) {
43 $type = $r['type'] ?? null;
44 if ( $type === $cat ) {
45 $matches[] = $r;
46 }
47 }
48 return $matches;
49 }
50
51 private function expectEmptyResults( string $description, string $wt, array $opts = [] ): void {
52 $result = $this->wtToLint( $wt, $opts );
53 $this->assertSame( [], $result, $description );
54 }
55
56 private function expectLinterCategoryToBeAbsent( string $description, string $wt, string $cat,
57 array $opts = []
58 ): void {
59 $result = $this->wtToLint( $wt, $opts );
60 foreach ( $result as $r ) {
61 if ( isset( $r['type'] ) ) {
62 $this->assertNotEquals( $cat, $r['type'], $description );
63 }
64 }
65 }
66
67 private function noLintsOfThisType( string $description, string $wt, string $type ): void {
68 $this->expectLinterCategoryToBeAbsent( $description, $wt, $type );
69 }
70
71 /**
72 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter
73 */
74 public function testNoIssues(): void {
75 $desc = 'should not have lint any issues';
76 $this->expectEmptyResults( $desc, 'foo' );
77 }
78
79 /**
80 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
81 */
82 public function testMissingEndTags(): void {
83 $desc = 'should lint missing end tags correctly';
84 $result = $this->wtToLint( '<div>foo' );
85 $this->assertCount( 1, $result, $desc );
86 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
87 $this->assertEquals( [ 0, 8, 5, 0 ], $result[0]['dsr'], $desc );
88 $this->assertTrue( isset( $result[0]['params'] ), $desc );
89 $this->assertEquals( 'div', $result[0]['params']['name'], $desc );
90
91 $desc = 'should lint missing end tags for quotes correctly';
92 $result = $this->wtToLint( "'''foo" );
93 $this->assertCount( 1, $result, $desc );
94 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
95 $this->assertEquals( [ 0, 6, 3, 0 ], $result[0]['dsr'], $desc );
96 $this->assertTrue( isset( $result[0]['params'] ), $desc );
97 $this->assertEquals( 'b', $result[0]['params']['name'], $desc );
98
99 $desc = 'should lint missing end tags found in transclusions correctly';
100 $result = $this->wtToLint( '{{1x|<div>foo<p>bar</div>}}' );
101 $this->assertCount( 1, $result, $desc );
102 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
103 $this->assertEquals( [ 0, 27, null, null ], $result[0]['dsr'], $desc );
104 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
105 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
106 $this->assertTrue( isset( $result[0]['params'] ), $desc );
107 $this->assertEquals( 'p', $result[0]['params']['name'], $desc );
108
109 $desc = 'should not flag tags where end tags are optional in the spec';
110 $wt = '<ul><li>x<li>y</ul><table><tr><th>heading 1<tr><td>col 1<td>col 2</table>';
111 $this->expectEmptyResults( $desc, $wt );
112
113 $desc = 'should lint missing end tag for wikitext table syntax';
114 $result = $this->wtToLint( '{|\n|hiho' );
115 $this->assertCount( 1, $result, $desc );
116 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
117 $this->assertEquals( [ 0, 9, 9, 0 ], $result[0]['dsr'], $desc );
118 }
119
120 /**
121 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
122 */
123 public function testStrippedTags(): void {
124 $desc = 'should lint stripped tags correctly';
125 $result = $this->wtToLint( 'foo</div>' );
126 $this->assertCount( 1, $result, $desc );
127 $this->assertEquals( 'stripped-tag', $result[0]['type'], $desc );
128 $this->assertTrue( isset( $result[0]['params'] ), $desc );
129 $this->assertEquals( 'div', $result[0]['params']['name'], $desc );
130 $this->assertEquals( [ 3, 9, null, null ], $result[0]['dsr'], $desc );
131
132 $desc = 'should lint stripped tags found in transclusions correctly';
133 $result = $this->wtToLint( '{{1x|<div>foo</div></div>}}' );
134 $this->assertCount( 1, $result, $desc );
135 $this->assertEquals( 'stripped-tag', $result[0]['type'], $desc );
136 $this->assertTrue( isset( $result[0]['params'] ), $desc );
137 $this->assertEquals( 'div', $result[0]['params']['name'], $desc );
138 $this->assertEquals( [ 0, 27, null, null ], $result[0]['dsr'], $desc );
139 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
140 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
141
142 $desc = 'should lint stripped tags correctly in misnested tag situations (</i> is stripped)';
143 $result = $this->wtToLint( '<b><i>X</b></i>' );
144 $this->assertCount( 1, $result, $desc );
145 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
146 $this->assertEquals( [ 3, 7, 3, 0 ], $result[0]['dsr'], $desc );
147 $this->assertTrue( isset( $result[0]['params'] ), $desc );
148 $this->assertEquals( 'i', $result[0]['params']['name'], $desc );
149
150 $desc = 'should lint stripped tags correctly in misnested tag situations ' .
151 'from template (</i> is stripped)';
152 $result = $this->wtToLint( '{{1x|<b><i>X</b></i>}}' );
153 $this->assertCount( 1, $result, $desc );
154 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
155 $this->assertEquals( [ 0, 22, null, null ], $result[0]['dsr'], $desc );
156 $this->assertTrue( isset( $result[0]['params'] ), $desc );
157 $this->assertEquals( 'i', $result[0]['params']['name'], $desc );
158 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
159 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
160
161 $desc = 'should lint stripped tags correctly in misnested tag situations (<i> is auto-inserted)';
162 $result = $this->wtToLint( '<b><i>X</b>Y</i>' );
163 $this->assertCount( 1, $result, $desc );
164 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
165 $this->assertEquals( [ 3, 7, 3, 0 ], $result[0]['dsr'], $desc );
166 $this->assertTrue( isset( $result[0]['params'] ), $desc );
167 $this->assertEquals( 'i', $result[0]['params']['name'], $desc );
168
169 $desc = 'should lint stripped tags correctly in misnested tag situations ' .
170 '(skip over empty autoinserted <small></small>)';
171 $result = $this->wtToLint( "*a<small>b\n*c</small>d" );
172 $this->assertCount( 1, $result, $desc );
173 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
174 $this->assertEquals( [ 2, 10, 7, 0 ], $result[0]['dsr'], $desc );
175 $this->assertTrue( isset( $result[0]['params'] ), $desc );
176 $this->assertEquals( 'small', $result[0]['params']['name'], $desc );
177
178 $desc = 'should lint stripped tags correctly in misnested tag situations ' .
179 '(formatting tags around lists, but ok for div)';
180 $result = $this->wtToLint( "<small>a\n*b\n*c\nd</small>\n<div>a\n*b\n*c\nd</div>" );
181 $this->assertCount( 1, $result, $desc );
182 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
183 $this->assertEquals( [ 0, 8, 7, 0 ], $result[0]['dsr'], $desc );
184 $this->assertTrue( isset( $result[0]['params'] ), $desc );
185 $this->assertEquals( 'small', $result[0]['params']['name'], $desc );
186
187 $desc = 'should lint stripped tags correctly in misnested tag situations ' .
188 '(T221989 regression test case)';
189 $result = $this->wtToLint( "<div>\n* <span>foo\n\n</div>\n</span>y" );
190 $this->assertCount( 1, $result, $desc );
191 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
192 $this->assertEquals( [ 8, 17, 6, 0 ], $result[0]['dsr'], $desc );
193 $this->assertTrue( isset( $result[0]['params'] ), $desc );
194 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
195
196 $desc = 'should lint stripped tags in nested pipelines (T338325)';
197 $result = $this->wtToLint(
198 "[[Special:Contributions/Yellow Evan|Hurricane</font>]]\n\n" .
199 "[[File:Foobar.jpg|thumb|[[Normande Cattle|Normande cow]]</center>]]\n\n" .
200 "<gallery>\n" .
201 "File:Foobar.jpg|... a [[black-collared barbet]], waiting to be fed</center>\n" .
202 "</gallery>"
203 );
204 $result = $this->filterLints( $result, 'stripped-tag' );
205 $this->assertCount( 3, $result, $desc );
206 $this->assertEquals( 'stripped-tag', $result[0]['type'], $desc );
207 $this->assertEquals( 'stripped-tag', $result[1]['type'], $desc );
208 $this->assertEquals( 'stripped-tag', $result[2]['type'], $desc );
209 }
210
211 /**
212 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintObsoleteTag
213 */
214 public function testObsoleteTags(): void {
215 $desc = 'should lint obsolete tags correctly';
216 $result = $this->wtToLint( '<tt>foo</tt>bar' );
217 $this->assertCount( 1, $result, $desc );
218 $this->assertEquals( 'obsolete-tag', $result[0]['type'], $desc );
219 $this->assertEquals( [ 0, 12, 4, 5 ], $result[0]['dsr'], $desc );
220 $this->assertTrue( isset( $result[0]['params'] ), $desc );
221 $this->assertEquals( 'tt', $result[0]['params']['name'], $desc );
222
223 $desc = 'should not lint big as an obsolete tag';
224 $this->expectEmptyResults( $desc, '<big>foo</big>bar' );
225
226 $desc = 'should lint obsolete tags found in transclusions correctly';
227 $result = $this->wtToLint( '{{1x|<div><tt>foo</tt></div>}}foo' );
228 $this->assertCount( 1, $result, $desc );
229 $this->assertEquals( 'obsolete-tag', $result[0]['type'], $desc );
230 $this->assertEquals( [ 0, 30, null, null ], $result[0]['dsr'], $desc );
231 $this->assertTrue( isset( $result[0]['params'] ), $desc );
232 $this->assertEquals( 'tt', $result[0]['params']['name'], $desc );
233 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
234 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
235
236 $desc = 'should not lint auto-inserted obsolete tags';
237 $result = $this->wtToLint( "<tt>foo\n\n\nbar" );
238 // obsolete-tag and missing-end-tag
239 $this->assertCount( 2, $result, $desc );
240 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
241 $this->assertEquals( 'obsolete-tag', $result[1]['type'], $desc );
242 $this->assertEquals( [ 0, 7, 4, 0 ], $result[1]['dsr'], $desc );
243 $this->assertTrue( isset( $result[1]['params'] ), $desc );
244 $this->assertEquals( 'tt', $result[1]['params']['name'], $desc );
245
246 $desc = 'should not have template info for extension tags';
247 $result = $this->wtToLint( "<gallery>\nFile:Test.jpg|<tt>foo</tt>\n</gallery>" );
248 $this->assertCount( 1, $result, $desc );
249 $this->assertEquals( 'obsolete-tag', $result[0]['type'], $desc );
250 $this->assertFalse( isset( $result[0]['templateInfo'] ), $desc );
251 $this->assertEquals( [ 24, 36, 4, 5 ], $result[0]['dsr'], $desc );
252 }
253
254 /**
255 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintFostered
256 */
257 public function testFosteredContent(): void {
258 $desc = 'should lint fostered content correctly';
259 $result = $this->wtToLint( "{|\nfoo\n|-\n| bar\n|}" );
260 $this->assertCount( 1, $result, $desc );
261 $this->assertEquals( 'fostered', $result[0]['type'], $desc );
262 $this->assertEquals( [ 0, 18, 2, 2 ], $result[0]['dsr'], $desc );
263
264 $desc = 'should lint fostered categories';
265 $result = $this->wtToLint( "{|\n[[Category:Fostered]]\n|-\n| bar\n|}" );
266 $this->assertCount( 1, $result, $desc );
267 $this->assertEquals( 'fostered-transparent', $result[0]['type'], $desc );
268
269 $desc = 'should lint fostered before categories';
270 $result = $this->wtToLint( "{|\nfoo[[Category:Fostered]]\n|-\n| bar\n|}" );
271 $this->assertCount( 1, $result, $desc );
272 $this->assertEquals( 'fostered', $result[0]['type'], $desc );
273
274 $desc = 'should lint fostered after categories';
275 $result = $this->wtToLint( "{|\n[[Category:Fostered]]foo\n|-\n| bar\n|}" );
276 $this->assertCount( 1, $result, $desc );
277 $this->assertEquals( 'fostered', $result[0]['type'], $desc );
278
279 $desc = 'should lint fostered categories from templates';
280 $this->expectEmptyResults( $desc, "{|\n{{1x|[[Category:Fostered]]}}\n|-\n| bar\n|}" );
281
282 $desc = 'should lint fostered behavior switches';
283 $result = $this->wtToLint( "{|\n__NOTOC__\n|-\n| bar\n|}" );
284 $this->assertCount( 1, $result, $desc );
285 $this->assertEquals( 'fostered-transparent', $result[0]['type'], $desc );
286
287 $desc = 'should lint fostered behavior switches from templates';
288 $this->expectEmptyResults( $desc, "{|\n{{1x|__NOTOC__}}\n|-\n| bar\n|}" );
289
290 $desc = 'should lint fostered include directives without fostered content';
291 $result = $this->wtToLint( "{|\n<includeonly>boo</includeonly>\n|-\n| bar\n|}" );
292 $this->assertCount( 1, $result, $desc );
293 $this->assertEquals( 'fostered-transparent', $result[0]['type'], $desc );
294
295 $desc = 'should lint fostered include directives without fostered content on template pages';
296 $result = $this->wtToLint( "{|\n<includeonly>boo</includeonly>\n|-\n| bar\n|}", [], 'Template:Fostered' );
297 $this->assertCount( 1, $result, $desc );
298 $this->assertEquals( 'fostered-transparent', $result[0]['type'], $desc );
299
300 $desc = 'should lint fostered include directives that has fostered content';
301 $result = $this->wtToLint( "{|\n<noinclude>boo</noinclude>\n|-\n| bar\n|}" );
302 $this->assertCount( 1, $result, $desc );
303 $this->assertEquals( 'fostered', $result[0]['type'], $desc );
304
305 $desc = 'should lint fostered section tags';
306 $result = $this->wtToLint( "{|\n<section name='123'/>\n|-\n| bar\n|}" );
307 $this->assertCount( 1, $result, $desc );
308 $this->assertEquals( 'fostered-transparent', $result[0]['type'], $desc );
309 }
310
311 /**
312 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintBogusImageOptions
313 */
314 public function testBogusImageOptions(): void {
315 $desc = 'should lint Bogus image options correctly';
316 $result = $this->wtToLint( '[[file:a.jpg|foo|bar]]' );
317 $this->assertCount( 1, $result, $desc );
318 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
319 $this->assertEquals( [ 0, 22, null, null ], $result[0]['dsr'], $desc );
320 $this->assertTrue( isset( $result[0]['params'] ), $desc );
321 $this->assertTrue( isset( $result[0]['params']['items'] ), $desc );
322 $this->assertEquals( 'foo', $result[0]['params']['items'][0], $desc );
323
324 $desc = 'should lint Bogus image options found in transclusions correctly';
325 $result = $this->wtToLint( '{{1x|[[file:a.jpg|foo|bar]]}}' );
326 $this->assertCount( 1, $result, $desc );
327 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
328 $this->assertEquals( [ 0, 29, null, null ], $result[0]['dsr'], $desc );
329 $this->assertTrue( isset( $result[0]['params'] ), $desc );
330 $this->assertEquals( 'foo', $result[0]['params']['items'][0], $desc );
331 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
332 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
333
334 $desc = 'should batch lint Bogus image options correctly';
335 $result = $this->wtToLint( '[[file:a.jpg|foo|bar|baz]]' );
336 $this->assertCount( 1, $result, $desc );
337 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
338 $this->assertEquals( [ 0, 26, null, null ], $result[0]['dsr'], $desc );
339 $this->assertTrue( isset( $result[0]['params'] ), $desc );
340 $this->assertEquals( 'foo', $result[0]['params']['items'][0], $desc );
341 $this->assertEquals( 'bar', $result[0]['params']['items'][1], $desc );
342
343 $desc = 'should not send any Bogus image options if there are none';
344 $this->expectEmptyResults( $desc, '[[file:a.jpg|foo]]' );
345
346 $desc = 'should flag disablecontrols as bogus options';
347 $result = $this->wtToLint(
348 '[[File:Video.ogv|disablecontrols=ok|These are bogus.]]' );
349 $this->assertCount( 1, $result, $desc );
350 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
351 $this->assertEquals( [ 0, 54, null, null ], $result[0]['dsr'], $desc );
352 $this->assertTrue( isset( $result[0]['params'] ), $desc );
353 $this->assertEquals( 'disablecontrols=ok', $result[0]['params']['items'][0], $desc );
354
355 $desc = 'should not crash on gallery images';
356 $this->expectEmptyResults( $desc, "<gallery>\nfile:a.jpg\n</gallery>" );
357
358 $desc = 'should lint Bogus image width options correctly';
359 $result = $this->wtToLint( '[[File:Foobar.jpg|thumb|left150px|Caption]]' );
360 $result = $this->filterLints( $result, 'bogus-image-options' );
361 $this->assertCount( 1, $result, $desc );
362 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
363 $this->assertEquals( [ 0, 43, 2, 2 ], $result[0]['dsr'], $desc );
364
365 $desc = "should lint Bogus image with bogus width definition correctly";
366 $result = $this->wtToLint(
367 "[[File:Foobar.jpg|thumb|300px300px|Caption]]" );
368 $result = $this->filterLints( $result, 'bogus-image-options' );
369 $this->assertCount( 1, $result, $desc );
370 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
371 $this->assertEquals( [ 0, 44, 2, 2 ], $result[0]['dsr'], $desc );
372
373 $desc = "should lint Bogus image with duplicate width options correctly";
374 $result = $this->wtToLint(
375 "[[File:Foobar.jpg|thumb|300px|250px|Caption]]" );
376 $result = $this->filterLints( $result, 'bogus-image-options' );
377 $this->assertCount( 1, $result, $desc );
378 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
379 $this->assertEquals( [ 0, 45, 2, 2 ], $result[0]['dsr'], $desc );
380 $this->assertTrue( isset( $result[0]['params'] ), $desc );
381 $this->assertEquals( '300px', $result[0]['params']['items'][0], $desc );
382
383 $desc = "should lint Bogus image with separated duplicate widths options correctly";
384 $result = $this->wtToLint(
385 "[[File:Foobar.jpg|thumb|250px|right|thumb|x216px|Caption]]" );
386 $result = $this->filterLints( $result, 'bogus-image-options' );
387 $this->assertCount( 1, $result, $desc );
388 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
389 $this->assertEquals( [ 0, 58, 2, 2 ], $result[0]['dsr'], $desc );
390 $this->assertTrue( isset( $result[0]['params'] ), $desc );
391 $this->assertEquals( '250px', $result[0]['params']['items'][0], $desc );
392 // The second '|thumb' should be linted away
393 $this->assertEquals( 'thumb', $result[0]['params']['items'][1], $desc );
394
395 $desc = 'should not lint image with caption masquerading as width option';
396 $result = $this->wtToLint( '[[File:Foobar.jpg|thumb|Foo px]]' );
397 $result = $this->filterLints( $result, 'bogus-image-options' );
398 $this->assertCount( 0, $result, $desc );
399
400 $desc = "should lint image with width option with redundant units";
401 $result = $this->wtToLint(
402 "[[File:Foobar.jpg|thumb|250pxpx|right|Caption]]" );
403 $result = $this->filterLints( $result, 'bogus-image-options' );
404 $this->assertCount( 1, $result, $desc );
405 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
406 $this->assertEquals( [ 0, 47, 2, 2 ], $result[0]['dsr'], $desc );
407 $this->assertTrue( isset( $result[0]['params'] ), $desc );
408 $this->assertEquals( '250pxpx', $result[0]['params']['items'][0], $desc );
409
410 $desc = "should lint Bogus image with invalid upright value";
411 $result = $this->wtToLint(
412 '[[File:Foobar.jpg|thumb|upright=0.7px|Caption]]' .
413 '[[File:Foobar.jpg|thumb|upright=1.5"|Caption]]' .
414 '[[File:Foobar.jpg|thumb|upright=0|Caption]]' .
415 '[[File:Foobar.jpg|thumb|upright=-1|Caption]]' .
416 '[[File:Foobar.jpg|thumb|upright=1.5|Caption]]'
417 );
418 $result = $this->filterLints( $result, 'bogus-image-options' );
419 $this->assertCount( 4, $result, $desc );
420 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
421 $this->assertEquals( [ 0, 47, 2, 2 ], $result[0]['dsr'], $desc );
422 $this->assertTrue( isset( $result[0]['params'] ), $desc );
423 $this->assertEquals( 'upright=0.7px', $result[0]['params']['items'][0], $desc );
424 $this->assertEquals( 'bogus-image-options', $result[1]['type'], $desc );
425 $this->assertEquals( [ 47, 93, 2, 2 ], $result[1]['dsr'], $desc );
426 $this->assertTrue( isset( $result[1]['params'] ), $desc );
427 $this->assertEquals( 'upright=1.5"', $result[1]['params']['items'][0], $desc );
428 $this->assertEquals( 'bogus-image-options', $result[2]['type'], $desc );
429 $this->assertEquals( [ 93, 136, 2, 2 ], $result[2]['dsr'], $desc );
430 $this->assertTrue( isset( $result[2]['params'] ), $desc );
431 $this->assertEquals( 'upright=0', $result[2]['params']['items'][0], $desc );
432 $this->assertEquals( 'bogus-image-options', $result[3]['type'], $desc );
433 $this->assertEquals( [ 136, 180, 2, 2 ], $result[3]['dsr'], $desc );
434 $this->assertTrue( isset( $result[3]['params'] ), $desc );
435 $this->assertEquals( 'upright=-1', $result[3]['params']['items'][0], $desc );
436
437 $desc = "should lint multiple media formats, first one wins";
438 $result = $this->wtToLint(
439 '[[File:Foobar.jpg|frame|frameless]]' .
440 '[[File:Foobar.jpg|frameless|frame]]' .
441 '[[File:Foobar.jpg|thumbnail=Thumb.png|thumb]]'
442 );
443 $result = $this->filterLints( $result, 'bogus-image-options' );
444 $this->assertCount( 3, $result, $desc );
445 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
446 $this->assertEquals( [ 0, 35, 2, 2 ], $result[0]['dsr'], $desc );
447 $this->assertTrue( isset( $result[0]['params'] ), $desc );
448 $this->assertEquals( 'frameless', $result[0]['params']['items'][0], $desc );
449 $this->assertEquals( 'bogus-image-options', $result[1]['type'], $desc );
450 $this->assertEquals( [ 35, 70, null, null ], $result[1]['dsr'], $desc );
451 $this->assertTrue( isset( $result[1]['params'] ), $desc );
452 $this->assertEquals( 'frame', $result[1]['params']['items'][0], $desc );
453 $this->assertEquals( 'bogus-image-options', $result[2]['type'], $desc );
454 $this->assertEquals( [ 70, 115, 2, 2 ], $result[2]['dsr'], $desc );
455 $this->assertTrue( isset( $result[2]['params'] ), $desc );
456 $this->assertEquals( 'thumb', $result[2]['params']['items'][0], $desc );
457
458 $desc = "should lint defined with for framed or manualthumb formats";
459 $result = $this->wtToLint(
460 '[[File:Foobar.jpg|frame|200px]]' .
461 '[[File:Foobar.jpg|thumbnail=Thumb.png|400px]]'
462 );
463 $result = $this->filterLints( $result, 'bogus-image-options' );
464 $this->assertCount( 2, $result, $desc );
465 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
466 $this->assertEquals( [ 0, 31, 2, 2 ], $result[0]['dsr'], $desc );
467 $this->assertTrue( isset( $result[0]['params'] ), $desc );
468 $this->assertEquals( '200px', $result[0]['params']['items'][0], $desc );
469 $this->assertEquals( 'bogus-image-options', $result[1]['type'], $desc );
470 $this->assertEquals( [ 31, 76, 2, 2 ], $result[1]['dsr'], $desc );
471 $this->assertTrue( isset( $result[1]['params'] ), $desc );
472 $this->assertEquals( '400px', $result[1]['params']['items'][0], $desc );
473
474 $desc = "should lint format option used in gallery";
475 $result = $this->wtToLint(
476 '<gallery>\n' .
477 'File:Foobar.jpg|thumb|lalala\n' .
478 '</gallery>\n' .
479 '{{1x|<gallery>\n' .
480 'File:Foobar.jpg|frame|hihi\n' .
481 '</gallery>}}\n'
482 );
483 $this->assertCount( 2, $result, $desc );
484
485 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
486 $this->assertEquals( [ 0, 51, 9, 10 ], $result[0]['dsr'], $desc );
487 $this->assertTrue( isset( $result[0]['params'] ), $desc );
488 $this->assertEquals( 'thumb', $result[0]['params']['items'][0], $desc );
489
490 $this->assertEquals( 'bogus-image-options', $result[1]['type'], $desc );
491 $this->assertEquals( [ 53, 109, null, null ], $result[1]['dsr'], $desc );
492 $this->assertTrue( isset( $result[1]['params'] ), $desc );
493 $this->assertEquals( 'frame', $result[1]['params']['items'][0], $desc );
494 $this->assertTrue( isset( $result[1]['templateInfo'] ), $desc );
495 $this->assertEquals( 'Template:1x', $result[1]['templateInfo']['name'], $desc );
496
497 $desc = "should lint size option used in gallery";
498 $result = $this->wtToLint(
499 '<gallery>\n' .
500 'File:Foobar.jpg|500px|lalala\n' .
501 '</gallery>'
502 );
503 $this->assertCount( 1, $result, $desc );
504 $this->assertEquals( 'bogus-image-options', $result[0]['type'], $desc );
505 $this->assertEquals( [ 0, 51, 9, 10 ], $result[0]['dsr'], $desc );
506 $this->assertTrue( isset( $result[0]['params'] ), $desc );
507 $this->assertEquals( '500px', $result[0]['params']['items'][0], $desc );
508 }
509
510 /**
511 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
512 */
513 public function testSelfClosingTags(): void {
514 $desc = 'should lint self-closing tags corrrectly';
515 $result = $this->wtToLint( "foo<b />bar<span />baz<hr />boo<br /> <ref name='boo' />" );
516 $this->assertCount( 2, $result, $desc );
517 $this->assertEquals( 'self-closed-tag', $result[0]['type'], $desc );
518 $this->assertEquals( [ 3, 8, 5, 0 ], $result[0]['dsr'], $desc );
519 $this->assertTrue( isset( $result[0]['params'] ), $desc );
520 $this->assertEquals( 'b', $result[0]['params']['name'], $desc );
521 $this->assertEquals( 'self-closed-tag', $result[1]['type'], $desc );
522 $this->assertEquals( [ 11, 19, 8, 0 ], $result[1]['dsr'], $desc );
523 $this->assertTrue( isset( $result[1]['params'] ), $desc );
524 $this->assertEquals( 'span', $result[1]['params']['name'], $desc );
525
526 $desc = 'should lint self-closing tags in a template correctly';
527 $result = $this->wtToLint( "{{1x|<b /> <ref name='boo' />}}" );
528 $this->assertCount( 1, $result, $desc );
529 $this->assertEquals( 'self-closed-tag', $result[0]['type'], $desc );
530 $this->assertEquals( [ 0, 31, null, null ], $result[0]['dsr'], $desc );
531 $this->assertTrue( isset( $result[0]['params'] ), $desc );
532 $this->assertEquals( 'b', $result[0]['params']['name'][0], $desc );
533 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
534 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
535 }
536
537 /**
538 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintDeletableTableTag
539 */
540 public function testDeletableTableTag(): void {
541 $desc = 'should identify deletable table tag for T161341 (1)';
542 $wt = implode( "\n", [
543 "{| style='border:1px solid red;'",
544 "|a",
545 "|-",
546 "{| style='border:1px solid blue;'",
547 "|b",
548 "|c",
549 "|}",
550 "|}"
551 ] );
552 $result = $this->wtToLint( $wt );
553 $this->assertCount( 2, $result, $desc );
554 $this->assertEquals( 'deletable-table-tag', $result[0]['type'], $desc );
555 $this->assertTrue( isset( $result[0]['params'] ), $desc );
556 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
557 $this->assertEquals( [ 39, 72, 0, 0 ], $result[0]['dsr'], $desc );
558
559 $desc = 'should identify deletable table tag for T161341 (2)';
560 $wt = implode( "\n", [
561 "{| style='border:1px solid red;'",
562 "|a",
563 "|- ",
564 " <!--boo--> ",
565 "{| style='border:1px solid blue;'",
566 "|b",
567 "|c",
568 "|}"
569 ] );
570 $result = $this->wtToLint( $wt );
571 $this->assertCount( 1, $result, $desc );
572 $this->assertEquals( 'deletable-table-tag', $result[0]['type'], $desc );
573 $this->assertTrue( isset( $result[0]['params'] ), $desc );
574 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
575 $this->assertEquals( [ 58, 91, 0, 0 ], $result[0]['dsr'], $desc );
576
577 $desc = 'should identify deletable table tag for T161341 (3)';
578 $wt = implode( "\n", [
579 "{{1x|{{{!}}",
580 "{{!}}a",
581 "{{!}}-",
582 "{{{!}}",
583 "{{!}}b",
584 "{{!}}c",
585 "{{!}}}",
586 "}}"
587 ] );
588 $result = $this->wtToLint( $wt );
589 $this->assertCount( 1, $result, $desc );
590 $this->assertEquals( 'deletable-table-tag', $result[0]['type'], $desc );
591 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
592 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
593 $this->assertEquals( [ 0, 56, null, null ], $result[0]['dsr'], $desc );
594
595 $desc = 'should identify deletable table tag for T161341 (4)';
596 $wt = implode( "\n", [
597 "{{1x|{{{!}}",
598 "{{!}}a",
599 "{{!}}-",
600 "}}",
601 "{|",
602 "|b",
603 "|c",
604 "|}"
605 ] );
606 $result = $this->wtToLint( $wt );
607 $this->assertCount( 1, $result, $desc );
608 $this->assertEquals( 'deletable-table-tag', $result[0]['type'], $desc );
609 $this->assertFalse( isset( $result[0]['templateInfo'] ), $desc );
610 $this->assertTrue( isset( $result[0]['params'] ), $desc );
611 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
612 $this->assertEquals( [ 29, 31, 0, 0 ], $result[0]['dsr'], $desc );
613 }
614
615 /**
616 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintPWrapBugWorkaround
617 */
618 public function testPwrapBugWorkaround(): void {
619 $desc = 'should identify rendering workarounds needed for doBlockLevels bug';
620 $wt = implode( "\n", [
621 "<div><span style='white-space:nowrap'>",
622 "a",
623 "</span>",
624 "</div>"
625 ] );
626 $result = $this->wtToLint( $wt );
627 $this->assertCount( 3, $result, $desc );
628 $this->assertEquals( 'pwrap-bug-workaround', $result[1]['type'], $desc );
629 $this->assertFalse( isset( $result[1]['templateInfo'] ), $desc );
630 $this->assertTrue( isset( $result[1]['params'] ), $desc );
631 $this->assertEquals( 'div', $result[1]['params']['root'], $desc );
632 $this->assertEquals( 'span', $result[1]['params']['child'], $desc );
633 $this->assertEquals( [ 5, 48, 33, 0 ], $result[1]['dsr'], $desc );
634
635 $desc = 'should not lint doBlockLevels bug rendering workarounds if newline break is present';
636 $wt = implode( "\n", [
637 "<div>",
638 "<span style='white-space:nowrap'>",
639 "a",
640 "</span>",
641 "</div>"
642 ] );
643 $this->expectLinterCategoryToBeAbsent( $desc, $wt, 'pwrap-bug-workaround' );
644
645 $desc = 'should not lint doBlockLevels bug rendering workarounds if nowrap CSS is not present';
646 $wt = implode( "\n", [
647 "<div><span>",
648 "a",
649 "</span>",
650 "</div>"
651 ] );
652 $this->expectLinterCategoryToBeAbsent( $desc, $wt, 'pwrap-bug-workaround' );
653
654 $desc = 'should not lint doBlockLevels bug rendering workarounds where not required';
655 $wt = implode( "\n", [
656 "<div><small style='white-space:nowrap'>",
657 "a",
658 "</small>",
659 "</div>"
660 ] );
661 $this->expectLinterCategoryToBeAbsent( $desc, $wt, 'pwrap-bug-workaround' );
662 }
663
664 /**
665 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTidyWhitespaceBug
666 */
667 public function testTidyWhitespaceBug(): void {
668 $wt1 = implode( "", [
669 // Basic with inline CSS + text sibling
670 "<span style='white-space:nowrap'>a </span>",
671 "x",
672 // Basic with inline CSS + span sibling
673 "<span style='white-space:nowrap'>a </span>",
674 "<span>x</span>",
675 // Basic with class CSS + text sibling
676 "<span class='nowrap'>a </span>",
677 "x",
678 // Basic with class CSS + span sibling
679 "<span class='nowrap'>a </span>",
680 "<span>x</span>",
681 // Comments shouldn't trip it up
682 "<span style='white-space:nowrap'>a<!--boo--> <!--boo--></span>",
683 "<!--boo-->",
684 "<span>x</span>"
685 ] );
686
687 $desc = 'should detect problematic whitespace hoisting';
688 $result = $this->wtToLint( $wt1, [ 'tidyWhitespaceBugMaxLength' => 0 ] );
689 $this->assertCount( 5, $result, $desc );
690 foreach ( $result as $r ) {
691 $this->assertEquals( 'tidy-whitespace-bug', $r['type'], $desc );
692 $this->assertEquals( 'span', $r['params']['node'], $desc );
693 }
694 $this->assertEquals( '#text', $result[0]['params']['sibling'], $desc );
695 $this->assertEquals( 'span', $result[1]['params']['sibling'], $desc );
696 $this->assertEquals( '#text', $result[2]['params']['sibling'], $desc );
697 $this->assertEquals( 'span', $result[3]['params']['sibling'], $desc );
698 $this->assertEquals( '#comment', $result[4]['params']['sibling'], $desc );
699 // skipping dsr tests
700
701 $desc = 'should not detect problematic whitespace hoisting for short text runs';
702 // Nothing to trigger here
703 $this->expectEmptyResults( $desc, $wt1, [ 'tidyWhitespaceBugMaxLength' => 100 ] );
704
705 $wt2 = implode( "", [
706 "some unaffected text here ",
707 "<span style='white-space:nowrap'>a </span>",
708 "<span style='white-space:nowrap'>bb</span>",
709 "<span class='nowrap'>cc</span>",
710 "<span class='nowrap'>d </span>",
711 "<span style='white-space:nowrap'>e </span>",
712 "<span class='nowrap'>x</span>"
713 ] );
714
715 $desc = 'should flag tidy whitespace bug on a run of affected content';
716 // The run length is 11 chars in the example above
717 $result = $this->wtToLint( $wt2, [ 'tidyWhitespaceBugMaxLength' => 5 ] );
718 $this->assertCount( 3, $result, $desc );
719 foreach ( $result as $r ) {
720 $this->assertEquals( 'tidy-whitespace-bug', $r['type'], $desc );
721 $this->assertEquals( 'span', $r['params']['node'], $desc );
722 }
723 $this->assertEquals( 'span', $result[0]['params']['sibling'], $desc );
724 $this->assertEquals( [ 26, 68, 33, 7 ], $result[0]['dsr'], $desc );
725 $this->assertEquals( 'span', $result[1]['params']['sibling'], $desc );
726 $this->assertEquals( [ 140, 170, 21, 7 ], $result[1]['dsr'], $desc );
727 $this->assertEquals( 'span', $result[2]['params']['sibling'], $desc );
728 $this->assertEquals( [ 170, 212, 33, 7 ], $result[2]['dsr'], $desc );
729
730 $desc = 'should not flag tidy whitespace bug on a run of short affected content';
731 $this->expectEmptyResults( $desc, $wt2, [ 'tidyWhitespaceBugMaxLength' => 12 ] );
732
733 $desc = 'should account for preceding text content';
734 // Run length changes to 16 chars because of preceding text
735 $wt2 = str_replace( 'some unaffected text here ', 'some unaffected text HERE-', $wt2 );
736 $result = $this->wtToLint( $wt2, [ 'tidyWhitespaceBugMaxLength' => 12 ] );
737 $this->assertCount( 3, $result, $desc );
738 foreach ( $result as $r ) {
739 $this->assertEquals( 'tidy-whitespace-bug', $r['type'], $desc );
740 $this->assertEquals( 'span', $r['params']['node'], $desc );
741 }
742 $this->assertEquals( 'span', $result[0]['params']['sibling'], $desc );
743 $this->assertEquals( [ 26, 68, 33, 7 ], $result[0]['dsr'], $desc );
744 $this->assertEquals( 'span', $result[1]['params']['sibling'], $desc );
745 $this->assertEquals( [ 140, 170, 21, 7 ], $result[1]['dsr'], $desc );
746 $this->assertEquals( 'span', $result[2]['params']['sibling'], $desc );
747 $this->assertEquals( [ 170, 212, 33, 7 ], $result[2]['dsr'], $desc );
748
749 $desc = 'should not flag tidy whitespace bug where it does not matter';
750 $wt = implode( "", [
751 // No CSS
752 "<span>a </span>",
753 "<span>x</span>",
754 // No trailing white-space
755 "<span class='nowrap'>a</span>",
756 "x",
757 // White-space follows
758 "<span class='nowrap'>a </span>",
759 " ",
760 "<span>x</span>",
761 // White-space follows
762 "<span style='white-space:nowrap'>a </span>",
763 "<!--boo--> boo",
764 "<span>x</span>",
765 // Block tag
766 "<div class='nowrap'>a </div>",
767 "<span>x</span>",
768 // Block tag sibling
769 "<span class='nowrap'>a </span>",
770 "<div>x</div>",
771 // br sibling
772 "<span class='nowrap'>a </span>",
773 "<br/>",
774 // No next sibling
775 "<span class='nowrap'>a </span>"
776 ] );
777 $this->expectEmptyResults( $desc, $wt, [ 'tidyWhitespaceBugMaxLength' => 0 ] );
778 }
779
780 /**
781 * @covers \Wikimedia\Parsoid\Wt2Html\TT\WikiLinkHandler::getWikiLinkTargetInfo
782 */
783 public function testMultipleColonEscape(): void {
784 $desc = 'should lint links prefixed with multiple colons';
785 $result = $this->wtToLint( "[[None]]\n[[:One]]\n[[::Two]]\n[[:::Three]]" );
786 $this->assertCount( 2, $result, $desc );
787 $this->assertEquals( 'multi-colon-escape', $result[0]['type'], $desc );
788 $this->assertEquals( [ 18, 27, null, null ], $result[0]['dsr'], $desc );
789 $this->assertTrue( isset( $result[0]['params'] ), $desc );
790 $this->assertEquals( '::Two', $result[0]['params']['href'], $desc );
791 $this->assertEquals( 'multi-colon-escape', $result[1]['type'], $desc );
792 $this->assertEquals( [ 28, 40, null, null ], $result[1]['dsr'], $desc );
793 $this->assertTrue( isset( $result[1]['params'] ), $desc );
794 $this->assertEquals( ':::Three', $result[1]['params']['href'], $desc );
795
796 $desc = 'should lint links prefixed with multiple colons from templates';
797 $result = $this->wtToLint( "{{1x|[[:One]]}}\n{{1x|[[::Two]]}}" );
798 $this->assertCount( 1, $result, $desc );
799 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
800 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
801 // TODO(arlolra): Frame doesn't have tsr info yet
802 $this->assertEquals( [ 0, 0, null, null ], $result[0]['dsr'], $desc );
803 $this->assertTrue( isset( $result[0]['params'] ), $desc );
804 $this->assertEquals( '::Two', $result[0]['params']['href'], $desc );
805 }
806
807 /**
808 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
809 */
810 public function testHtml5MisnestedTags(): void {
811 $desc = "should not trigger html5 misnesting if there is no following content";
812 $result = $this->wtToLint( "<del>foo\nbar" );
813 $this->assertCount( 1, $result, $desc );
814 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
815 $this->assertTrue( isset( $result[0]['params'] ), $desc );
816 $this->assertEquals( 'del', $result[0]['params']['name'], $desc );
817
818 $desc = "should trigger html5 misnesting correctly";
819 $result = $this->wtToLint( "<del>foo\n\nbar" );
820 $this->assertCount( 1, $result, $desc );
821 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
822 $this->assertEquals( [ 0, 8, 5, 0 ], $result[0]['dsr'], $desc );
823 $this->assertTrue( isset( $result[0]['params'] ), $desc );
824 $this->assertEquals( 'del', $result[0]['params']['name'], $desc );
825
826 $desc = "should trigger html5 misnesting for span (1)";
827 $result = $this->wtToLint( "<span>foo\n\nbar" );
828 $this->assertCount( 1, $result, $desc );
829 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
830 $this->assertEquals( [ 0, 9, 6, 0 ], $result[0]['dsr'], $desc );
831 $this->assertTrue( isset( $result[0]['params'] ), $desc );
832 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
833
834 $desc = "should trigger html5 misnesting for span (2)";
835 $result = $this->wtToLint( "<span>foo\n\n<div>bar</div>" );
836 $this->assertCount( 1, $result, $desc );
837 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
838 $this->assertEquals( [ 0, 9, 6, 0 ], $result[0]['dsr'], $desc );
839 $this->assertTrue( isset( $result[0]['params'] ), $desc );
840 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
841
842 $desc = "should trigger html5 misnesting for span (3)";
843 $result = $this->wtToLint( "<span>foo\n\n{|\n|x\n|}\nboo" );
844 $this->assertCount( 1, $result, $desc );
845 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
846 $this->assertEquals( [ 0, 9, 6, 0 ], $result[0]['dsr'], $desc );
847 $this->assertTrue( isset( $result[0]['params'] ), $desc );
848 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
849
850 $desc = "should not trigger html5 misnesting when there is no misnested content";
851 $result = $this->wtToLint( "<span>foo\n\n</span>y" );
852 $this->assertCount( 1, $result, $desc );
853 $this->assertEquals( 'misnested-tag', $result[0]['type'], $desc );
854 $this->assertTrue( isset( $result[0]['params'] ), $desc );
855 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
856
857 $desc = "should not trigger html5 misnesting when unclosed tag is inside a td/th/heading tags";
858 $result = $this->wtToLint( "=<span id=\"1\">x=\n{|\n!<span id=\"2\">z\n|-\n|<span>id=\"3\"\n|}" );
859 $this->assertCount( 3, $result, $desc );
860 $this->assertEquals( 'missing-end-tag-in-heading', $result[0]['type'], $desc );
861 $this->assertEquals( 'missing-end-tag', $result[1]['type'], $desc );
862 $this->assertEquals( 'missing-end-tag', $result[2]['type'], $desc );
863
864 $desc = "should not trigger html5 misnesting when misnested content is " .
865 "outside an a-tag (without link-trails)";
866 $result = $this->wtToLint( "[[Foo|<span>foo]]Bar</span>" );
867 $this->assertCount( 2, $result, $desc );
868 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
869 $this->assertTrue( isset( $result[0]['params'] ), $desc );
870 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
871 $this->assertEquals( 'stripped-tag', $result[1]['type'], $desc );
872
873 // Note that this is a false positive because of T177086 and fixing that will fix this.
874 // We expect this to be an edge case.
875 $desc = "should trigger html5 misnesting when linktrails brings content inside an a-tag";
876 $result = $this->wtToLint( "[[Foo|<span>foo]]bar</span>" );
877 $this->assertCount( 2, $result, $desc );
878 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
879 $this->assertTrue( isset( $result[0]['params'] ), $desc );
880 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
881 $this->assertEquals( 'stripped-tag', $result[1]['type'], $desc );
882
883 $desc = "should not trigger html5 misnesting for formatting tags";
884 $result = $this->wtToLint( "<small>foo\n\nbar" );
885 $this->assertCount( 1, $result, $desc );
886 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
887 $this->assertTrue( isset( $result[0]['params'] ), $desc );
888 $this->assertEquals( 'small', $result[0]['params']['name'], $desc );
889
890 $desc = "should not trigger html5 misnesting for span if there is a nested span tag";
891 $result = $this->wtToLint( "<span>foo<span>boo</span>\n\nbar</span>" );
892 $this->assertCount( 2, $result, $desc );
893 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
894 $this->assertEquals( 'stripped-tag', $result[1]['type'], $desc );
895
896 $desc = "should trigger html5 misnesting for span if there is a nested non-span tag";
897 $result = $this->wtToLint( "<span>foo<del>boo</del>\n\nbar</span>" );
898 $this->assertCount( 2, $result, $desc );
899 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
900 $this->assertTrue( isset( $result[0]['params'] ), $desc );
901 $this->assertEquals( 'span', $result[0]['params']['name'], $desc );
902 $this->assertEquals( 'stripped-tag', $result[1]['type'], $desc );
903
904 $desc = "should trigger html5 misnesting for span if there is a nested unclosed span tag";
905 $result = $this->wtToLint( "<span>foo<span>boo\n\nbar</span>" );
906 $this->assertCount( 3, $result, $desc );
907 $this->assertEquals( 'html5-misnesting', $result[0]['type'], $desc );
908 $this->assertEquals( 'html5-misnesting', $result[1]['type'], $desc );
909 $this->assertEquals( 'stripped-tag', $result[2]['type'], $desc );
910 }
911
912 /**
913 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintObsoleteTag
914 */
915 public function testTidyFontBug(): void {
916 $desc = "should flag Tidy font fixups accurately when color attribute is present";
917 $wtLines = [
918 "<font color='green'>[[Foo]]</font>",
919 "<font color='green'>[[Category:Boo]][[Foo]]</font>",
920 "<font color='green'>__NOTOC__[[Foo]]</font>",
921 "<font color='green'><!--boo-->[[Foo]]</font>",
922 "<font color='green'>[[Foo|bar]]</font>",
923 "<font color='green'>[[Foo|''bar'']]</font>",
924 "<font color='green'>[[Foo|''bar'' and boo]]</font>",
925 "<font color='green'>[[Foo]]l</font>",
926 "<font color='green'>{{1x|[[Foo]]}}</font>"
927 ];
928 $n = count( $wtLines );
929 $result = $this->wtToLint( implode( "\n", $wtLines ) );
930 $this->assertCount( 2 * $n, $result, $desc );
931 for ( $i = 0; $i < 2 * $n; $i += 2 ) {
932 $this->assertEquals( 'obsolete-tag', $result[$i]['type'], $desc );
933 $this->assertEquals( 'tidy-font-bug', $result[$i + 1]['type'], $desc );
934 }
935
936 $desc = "should not flag Tidy font fixups when color attribute is absent";
937 $wtLinesReplaced = str_replace( " color='green'", '', $wtLines );
938 $n = count( $wtLinesReplaced );
939 $result = $this->wtToLint( implode( "\n", $wtLinesReplaced ) );
940 $this->assertCount( $n, $wtLinesReplaced, $desc );
941 foreach ( $result as $r ) {
942 $this->assertEquals( 'obsolete-tag', $r['type'], $desc );
943 }
944
945 $desc = "should not flag Tidy font fixups when Tidy does not do the fixups";
946 $wtLines2 = [
947 "<font color='green'></font>", // Regression test for T179757
948 "<font color='green'>[[Foo]][[Bar]]</font>",
949 "<font color='green'> [[Foo]]</font>",
950 "<font color='green'>[[Foo]] </font>",
951 "<font color='green'>[[Foo]]D</font>",
952 "<font color='green'>''[[Foo|bar]]''</font>",
953 "<font color='green'><span>[[Foo|bar]]</span></font>",
954 "<font color='green'><div>[[Foo|bar]]</div></font>"
955 ];
956 $n = count( $wtLines2 );
957 $result = $this->wtToLint( implode( "\n", $wtLines2 ) );
958 $this->assertCount( $n, $result, $desc );
959 foreach ( $result as $r ) {
960 $this->assertEquals( 'obsolete-tag', $r['type'], $desc );
961 }
962 }
963
964 /**
965 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintMultipleUnclosedFormattingTags
966 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
967 */
968 public function testMultipleUnclosedFormatTags(): void {
969 $desc = 'should detect multiple unclosed small tags';
970 $result = $this->wtToLint( '<div><small>x</div><div><small>y</div>' );
971 $this->assertCount( 3, $result, $desc );
972 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[2]['type'], $desc );
973 $this->assertEquals( 'small', $result[2]['params']['name'], $desc );
974
975 $desc = 'should detect multiple unclosed big tags';
976 $result = $this->wtToLint( '<div><big>x</div><div><big>y</div>' );
977 $this->assertCount( 3, $result, $desc );
978 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[2]['type'], $desc );
979 $this->assertEquals( 'big', $result[2]['params']['name'], $desc );
980
981 $desc = 'should detect multiple unclosed big tags';
982 $result = $this->wtToLint( '<div><small><big><small><big>y</div>' );
983 $this->assertCount( 5, $result, $desc );
984 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[4]['type'], $desc );
985 $this->assertEquals( 'small', $result[4]['params']['name'], $desc );
986
987 $desc = 'should ignore unclosed small tags in tables';
988 $this->noLintsOfThisType( $desc, "{|\n|<small>a\n|<small>b\n|}",
989 'multiple-unclosed-formatting-tags' );
990
991 $desc = 'should ignore unclosed small tags in tables but detect those outside it';
992 $result = $this->wtToLint( "<small>x\n{|\n|<small>a\n|<small>b\n|}\n<small>y" );
993 $this->assertCount( 5, $result, $desc );
994 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[4]['type'], $desc );
995 $this->assertEquals( 'small', $result[4]['params']['name'], $desc );
996
997 $desc = 'should not flag undetected misnesting of formatting tags as " .
998 "multiple unclosed formatting tags';
999 $this->noLintsOfThisType( $desc, "<br><small>{{1x|<div>\n*item 1\n</div>}}</small>",
1000 'multiple-unclosed-formatting-tags' );
1001
1002 $desc = "should detect Tidy's smart auto-fixup of paired unclosed formatting tags";
1003 $result = $this->wtToLint( '<b>foo<b>\n<code>foo <span>x</span> bar<code>' );
1004 $this->assertCount( 6, $result, $desc );
1005 $this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc );
1006 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[1]['type'], $desc );
1007 $this->assertEquals( 'b', $result[1]['params']['name'], $desc );
1008 $this->assertEquals( 'missing-end-tag', $result[3]['type'], $desc );
1009 $this->assertEquals( 'multiple-unclosed-formatting-tags', $result[4]['type'], $desc );
1010 $this->assertEquals( 'code', $result[4]['params']['name'], $desc );
1011
1012 $desc = "should not flag Tidy's smart auto-fixup of paired unclosed " .
1013 "formatting tags where Tidy won't do it";
1014 $this->noLintsOfThisType( $desc, "<b>foo <b>\n<code>foo <span>x</span> <!--comment--><code>",
1015 'multiple-unclosed-formatting-tags' );
1016
1017 $desc = "should not flag Tidy's smart auto-fixup of paired unclosed tags for non-formatting tags";
1018 $this->noLintsOfThisType( $desc, "<span>foo<span>\n<div>foo <span>x</span> bar<div>",
1019 'multiple-unclosed-formatting-tags' );
1020 }
1021
1022 /**
1023 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintTreeBuilderFixup
1024 */
1025 public function testUnclosedIBTagsInHeadings(): void {
1026 $desc = "should detect unclosed wikitext i tags in headings";
1027 $result = $this->wtToLint( "==foo<span>''a</span>==\nx" );
1028 $this->assertCount( 1, $result, $desc );
1029 $this->assertEquals( 'unclosed-quotes-in-heading', $result[0]['type'], $desc );
1030 $this->assertEquals( 'i', $result[0]['params']['name'], $desc );
1031 $this->assertEquals( 'h2', $result[0]['params']['ancestorName'], $desc );
1032
1033 $desc = "should detect unclosed wikitext b tags in headings";
1034 $result = $this->wtToLint( "==foo<span>'''a</span>==\nx" );
1035 $this->assertCount( 1, $result, $desc );
1036 $this->assertEquals( 'unclosed-quotes-in-heading', $result[0]['type'], $desc );
1037 $this->assertEquals( 'b', $result[0]['params']['name'], $desc );
1038 $this->assertEquals( 'h2', $result[0]['params']['ancestorName'], $desc );
1039
1040 $desc = "should detect unclosed HTML i/b tags in headings as missing-end-tag-in-heading";
1041 $result = $this->wtToLint( "==foo<span><i>a</span>==\nx\n==foo<span><b>a</span>==\ny" );
1042 $this->assertCount( 2, $result, $desc );
1043 $this->assertEquals( 'missing-end-tag-in-heading', $result[0]['type'], $desc );
1044 $this->assertEquals( 'missing-end-tag-in-heading', $result[1]['type'], $desc );
1045 }
1046
1047 /**
1048 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintMultilineHtmlTableInList
1049 */
1050 public function testMultilineHtmlTablesInLists(): void {
1051 $desc = "should detect multiline HTML tables in lists (li)";
1052 $result = $this->wtToLint( "* <table><tr><td>x</td></tr>\n</table>" );
1053 $this->assertCount( 1, $result, $desc );
1054 $this->assertEquals( 'multiline-html-table-in-list', $result[0]['type'], $desc );
1055 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
1056 $this->assertEquals( 'li', $result[0]['params']['ancestorName'], $desc );
1057
1058 $desc = "should detect multiline HTML tables in lists (table in div)";
1059 $result = $this->wtToLint( "* <div><table><tr><td>x</td></tr>\n</table></div>" );
1060 $this->assertCount( 1, $result, $desc );
1061 $this->assertEquals( 'multiline-html-table-in-list', $result[0]['type'], $desc );
1062 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
1063 $this->assertEquals( 'li', $result[0]['params']['ancestorName'], $desc );
1064
1065 $desc = "should detect multiline HTML tables in lists (dt)";
1066 $result = $this->wtToLint( "; <table><tr><td>x</td></tr>\n</table>" );
1067 $this->assertCount( 1, $result, $desc );
1068 $this->assertEquals( 'multiline-html-table-in-list', $result[0]['type'], $desc );
1069 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
1070 $this->assertEquals( 'dt', $result[0]['params']['ancestorName'], $desc );
1071
1072 $desc = "should detect multiline HTML tables in lists (dd)";
1073 $result = $this->wtToLint( ": <table><tr><td>x</td></tr>\n</table>" );
1074 $this->assertCount( 1, $result, $desc );
1075 $this->assertEquals( 'multiline-html-table-in-list', $result[0]['type'], $desc );
1076 $this->assertEquals( 'table', $result[0]['params']['name'], $desc );
1077 $this->assertEquals( 'dd', $result[0]['params']['ancestorName'], $desc );
1078
1079 $desc = "should not detect multiline HTML tables in HTML lists";
1080 $this->expectEmptyResults( $desc, "<ul><li><table>\n<tr><td>x</td></tr>\n</table>\n</li></ul>" );
1081
1082 $desc = "should not detect single-line HTML tables in lists";
1083 $this->expectEmptyResults( $desc, "* <div><table><tr><td>x</td></tr></table></div>" );
1084
1085 $desc = "should not detect multiline HTML tables in ref tags";
1086 $this->expectEmptyResults( $desc,
1087 "a <ref><table>\n<tr><td>b</td></tr>\n</table></ref> <references />" );
1088 }
1089
1090 /**
1091 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintMiscTidyReplacementIssues
1092 */
1093 public function testDivSpanFlipTidyBug(): void {
1094 $desc = "should not trigger this lint when there are no style or class attributes";
1095 $this->expectEmptyResults( $desc, "<span><div>x</div></span>" );
1096
1097 $desc = "should trigger this lint when there is a style or class attribute (1)";
1098 $result = $this->wtToLint( "<span class='x'><div>x</div></span>" );
1099 $this->assertCount( 1, $result, $desc );
1100 $this->assertEquals( 'misc-tidy-replacement-issues', $result[0]['type'], $desc );
1101 $this->assertEquals( 'div-span-flip', $result[0]['params']['subtype'], $desc );
1102
1103 $desc = "should trigger this lint when there is a style or class attribute (2)";
1104 $result = $this->wtToLint( "<span style='x'><div>x</div></span>" );
1105 $this->assertCount( 1, $result, $desc );
1106 $this->assertEquals( 'misc-tidy-replacement-issues', $result[0]['type'], $desc );
1107 $this->assertEquals( 'div-span-flip', $result[0]['params']['subtype'], $desc );
1108
1109 $desc = "should trigger this lint when there is a style or class attribute (3)";
1110 $result = $this->wtToLint( "<span><div class='x'>x</div></span>" );
1111 $this->assertCount( 1, $result, $desc );
1112 $this->assertEquals( 'misc-tidy-replacement-issues', $result[0]['type'], $desc );
1113 $this->assertEquals( 'div-span-flip', $result[0]['params']['subtype'], $desc );
1114
1115 $desc = "should trigger this lint when there is a style or class attribute (4)";
1116 $result = $this->wtToLint( "<span><div style='x'>x</div></span>" );
1117 $this->assertCount( 1, $result, $desc );
1118 $this->assertEquals( 'misc-tidy-replacement-issues', $result[0]['type'], $desc );
1119 $this->assertEquals( 'div-span-flip', $result[0]['params']['subtype'], $desc );
1120 }
1121
1122 /**
1123 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintWikilinksInExtlink
1124 */
1125 public function testWikilinkInExternalLink(): void {
1126 $desc = "should lint wikilink in external link correctly";
1127 $result = $this->wtToLint( "[http://google.com This is [[Google]]'s search page]" );
1128 $this->assertCount( 1, $result, $desc );
1129 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1130 $this->assertEquals( [ 0, 52, 19, 1 ], $result[0]['dsr'], $desc );
1131
1132 $desc = "should lint wikilink in external link correctly";
1133 $result = $this->wtToLint(
1134 "[http://stackexchange.com is the official website for [[Stack Exchange]]]" );
1135 $this->assertCount( 1, $result, $desc );
1136 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1137 $this->assertEquals( [ 0, 73, 26, 1 ], $result[0]['dsr'], $desc );
1138
1139 $desc = "should lint wikilink in external link correctly";
1140 $result = $this->wtToLint(
1141 "{{1x|foo <div> and [http://google.com [[Google]] bar] baz </div>}}" );
1142 $this->assertCount( 1, $result, $desc );
1143 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1144 $this->assertEquals( [ 0, 66, null, null ], $result[0]['dsr'], $desc );
1145 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
1146 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
1147
1148 $desc = "should lint wikilink set in italics in external link correctly";
1149 $result = $this->wtToLint(
1150 "[http://stackexchange.com is the official website for ''[[Stack Exchange]]'']" );
1151 $this->assertCount( 1, $result, $desc );
1152 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1153 $this->assertEquals( [ 0, 77, 26, 1 ], $result[0]['dsr'], $desc );
1154
1155 $desc = "should lint wikilink set in bold in external link correctly";
1156 $result = $this->wtToLint(
1157 "[http://stackexchange.com is the official website for '''[[Stack Exchange]]''']" );
1158 $this->assertCount( 1, $result, $desc );
1159 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1160 $this->assertEquals( [ 0, 79, 26, 1 ], $result[0]['dsr'], $desc );
1161
1162 $desc = "should lint wikilink set in italics and bold in external link correctly";
1163 $result = $this->wtToLint(
1164 "[http://stackexchange.com is the official website for '''''[[Stack Exchange]]''''']" );
1165 $this->assertCount( 1, $result, $desc );
1166 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1167 $this->assertEquals( [ 0, 83, 26, 1 ], $result[0]['dsr'], $desc );
1168
1169 $desc = "should lint figure wikilink in external link correctly";
1170 $result = $this->wtToLint(
1171 "[http://foo.bar/some.link [[File:Foobar.jpg|scale=0.5]] image]" );
1172 $this->assertCount( 1, $result, $desc );
1173 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1174 $this->assertEquals( [ 0, 62, 26, 1 ], $result[0]['dsr'], $desc );
1175
1176 $desc = "should lint image wikilink in external link correctly";
1177 $result = $this->wtToLint(
1178 "[http://foo.bar/other.link [[File:Foobar.jpg|thumb]] image]" );
1179 $result = $this->filterLints( $result, 'wikilink-in-extlink' );
1180 $this->assertCount( 1, $result, $desc );
1181 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1182 $this->assertEquals( [ 0, 59, 27, 1 ], $result[0]['dsr'], $desc );
1183
1184 $desc = "should lint image wikilink in external link with |link= correctly";
1185 $result = $this->wtToLint(
1186 "[http://foo.bar/other.link [[File:Foobar.jpg|link=]] image]" );
1187 $result = $this->filterLints( $result, 'wikilink-in-extlink' );
1188 $this->assertCount( 0, $result, $desc );
1189
1190 $desc = "should not generate lint error for image wikilink following external link";
1191 $result = $this->wtToLint(
1192 "[http://foo.bar/other.link testing123][[File:Foobar.jpg]]" );
1193 $result = $this->filterLints( $result, 'wikilink-in-extlink' );
1194 $this->assertCount( 0, $result, $desc );
1195
1196 $desc = "should lint audio wikilink in external link correctly";
1197 $result = $this->wtToLint(
1198 "[http://foo.bar/other.link [[File:Audio.oga]] audio]" );
1199 $this->assertCount( 1, $result, $desc );
1200 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1201 $this->assertEquals( [ 0, 52, 27, 1 ], $result[0]['dsr'], $desc );
1202
1203 $desc = "should lint video wikilink in external link correctly";
1204 $result = $this->wtToLint(
1205 "[http://foo.bar/other.link [[File:Video.ogv]] video]" );
1206 $this->assertCount( 1, $result, $desc );
1207 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1208 $this->assertEquals( [ 0, 52, 27, 1 ], $result[0]['dsr'], $desc );
1209
1210 $desc = "should lint audio wikilink with preceding text in external link correctly";
1211 $result = $this->wtToLint(
1212 "[http://foo.bar/other.link first_child [[File:Audio.oga]] audio]" );
1213 $this->assertCount( 1, $result, $desc );
1214 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1215 $this->assertEquals( [ 0, 64, 27, 1 ], $result[0]['dsr'], $desc );
1216
1217 $desc = "should lint video wikilink with prededing bold text in external link correctly";
1218 $result = $this->wtToLint(
1219 "[http://foo.bar/other.link '''first_child''' [[File:Video.ogv]] video]" );
1220 $this->assertCount( 1, $result, $desc );
1221 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1222 $this->assertEquals( [ 0, 70, 27, 1 ], $result[0]['dsr'], $desc );
1223
1224 $desc = "should lint audio wikilink in extlink followed by span correctly";
1225 $result = $this->wtToLint(
1226 "[http://foo.bar/other.link [[File:Audio.oga]] audio]<span>this is an Element</span>" );
1227 $this->assertCount( 1, $result, $desc );
1228 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1229 $this->assertEquals( [ 0, 52, 27, 1 ], $result[0]['dsr'], $desc );
1230
1231 $desc = "should lint audio wikilink in extlink with preceding text followed by span correctly";
1232 $result = $this->wtToLint(
1233 "[http://foo.bar/other.link text content [[File:Audio.oga]] audio]<span>this is an Element</span>" );
1234 $this->assertCount( 1, $result, $desc );
1235 $this->assertEquals( 'wikilink-in-extlink', $result[0]['type'], $desc );
1236 $this->assertEquals( [ 0, 65, 27, 1 ], $result[0]['dsr'], $desc );
1237 }
1238
1239 /**
1240 * Provide test cases for large tables
1241 * @return array[]
1242 */
1243 public function provideLargeTablesTests(): array {
1244 $noLongRowsTable = [
1245 "{|",
1246 "|-",
1247 "! Header 1 !! Header 2 !! Header 3",
1248 ];
1249 for ( $i = 0; $i < 30; $i++ ) {
1250 $noLongRowsTable[] = "|-";
1251 $noLongRowsTable[] = "| Cell 1 || Cell 2 || Cell 3";
1252 }
1253 // Make the last row "large" by adding 3 more columns
1254 $nonUniformLongRowTable = $noLongRowsTable;
1255 $nonUniformLongRowTable[] = "|| Cell 4 || Cell 5 || Cell 6";
1256
1257 $noLongRowsTable[] = "|}";
1258 $nonUniformLongRowTable[] = "|}";
1259 return [
1260 'empty table should not cause crashers' => [
1261 'wikiTextLines' => [
1262 "{|",
1263 "|}"
1264 ],
1265 'columnCount' => 4
1266 // No dsr => no lints found
1267 ],
1268 '6 header columns' => [
1269 'wikiTextLines' => [
1270 "{|",
1271 "|-",
1272 "! Header 1 !! Header 2 !! Header 3 !! Header 4 !! Header 5 !! Header 6",
1273 "|-",
1274 "| Cell 1 || Cell 2 || Cell 3",
1275 "|}"
1276 ],
1277 'columnCount' => 6,
1278 'dsr' => [ 0, 111, 2, 2 ]
1279 ],
1280 '3 header columns and 6 row columns' => [
1281 'wikiTextLines' => [
1282 "{|",
1283 "|-",
1284 "! Header 1 !! Header 2 !! Header 3",
1285 "|-",
1286 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6",
1287 "|}"
1288 ],
1289 'columnCount' => 6,
1290 'dsr' => [ 0, 105, 2, 2 ]
1291 ],
1292 '3 header columns and 3 row columns and 6 row columns' => [
1293 'wikiTextLines' => [
1294 "{|",
1295 "|-",
1296 "! Header 1 !! Header 2 !! Header 3",
1297 "|-",
1298 "| Cell 1 || Cell 2 || Cell 3",
1299 "|-",
1300 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6",
1301 "|}"
1302 ],
1303 'columnCount' => 6,
1304 'dsr' => [ 0, 137, 2, 2 ]
1305 ],
1306 '3 row columns and 6 row columns' => [
1307 'wikiTextLines' => [
1308 "{|",
1309 "|-",
1310 "| Cell 1 || Cell 2 || Cell 3",
1311 "|-",
1312 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6",
1313 "|}"
1314 ],
1315 'columnCount' => 6,
1316 'dsr' => [ 0, 99, 2, 2 ]
1317 ],
1318 'detect and exit on the first lint result' => [
1319 'wikiTextLines' => [
1320 "{|",
1321 "|-",
1322 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6",
1323 "|-",
1324 "| Cell 1 ",
1325 "|-",
1326 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6 || Cell 7 || Cell 8",
1327 "|}"
1328 ],
1329 'columnCount' => 6,
1330 'dsr' => [ 0, 162, 2, 2 ]
1331 ],
1332 '3 and 4 headers intermixed with table rows and but 8 row columns ' => [
1333 'wikiTextLines' => [
1334 "{|",
1335 "|-",
1336 "| Cell 1 || Cell 2",
1337 "|-",
1338 "! Header 1 !! Header 2 !! Header 3",
1339 "|-",
1340 "| Cell 1 || Cell 2 || Cell 3",
1341 "|-",
1342 "! Header 1 !! Header 2 !! Header 3 !! Header 4",
1343 "|-",
1344 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5 || Cell 6 || Cell 7 || Cell 8",
1345 "|}"
1346 ],
1347 'columnCount' => 8,
1348 'dsr' => [ 0, 229, 2, 2 ]
1349 ],
1350 'acceptable table size' => [
1351 'wikiTextLines' => [
1352 "{|",
1353 "|-",
1354 "| Cell 1 || Cell 2",
1355 "|-",
1356 "! Header 1 !! Header 2 !! Header 3",
1357 "|-",
1358 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5",
1359 "|-",
1360 "! Header 1 !! Header 2",
1361 "|-",
1362 "| Cell 1 || Cell 2 || Cell 3 || Cell 4 || Cell 5",
1363 "|}"
1364 ],
1365 'columnCount' => 5
1366 ],
1367 'acceptable table size, rows on multiple wikitext lines' => [
1368 'wikiTextLines' => [
1369 "{|",
1370 "|-",
1371 "! Header 1 !! Header 2 ",
1372 "!! Header 3 !! Header 4",
1373 "|-",
1374 "| Cell 1 || Cell 2",
1375 "|| Cell 3 || Cell 4",
1376 "|}"
1377 ],
1378 'columnCount' => 4
1379 // No dsr => no lints found
1380 ],
1381 '6 header columns and 6 row columns, rows on multiple wikitext lines' => [
1382 'wikiTextLines' => [
1383 "{|",
1384 "|-",
1385 "! Header 1 !! Header 2 ",
1386 "!! Header 3 !! Header 4",
1387 "!! Header 5 !! Header 6",
1388 "|-",
1389 "| Cell 1 || Cell 2",
1390 "|| Cell 3 || Cell 4",
1391 "|| Cell 5 || Cell 6",
1392 "|}"
1393 ],
1394 'columnCount' => 6,
1395 'dsr' => [ 0, 142, 2, 2 ]
1396 ],
1397 'Edge Case' => [
1398 'wikiTextLines' => [
1399 "{|",
1400 "|-",
1401 "|a",
1402 "|-",
1403 "|b",
1404 "|-",
1405 "|c",
1406 "|-",
1407 "|d",
1408 "|-",
1409 "|e",
1410 "|-",
1411 "|f",
1412 "|-",
1413 "| a || b || c || d || e ||f",
1414 "|}"
1415 ],
1416 'columnCount' => 6,
1417 'dsr' => [ 0, 72, 2, 2 ]
1418 ],
1419 'transclusions 5 header columns and 6 row columns' => [
1420 'wikiTextLines' => [
1421 "{{1x|",
1422 "<table>",
1423 "<tr>",
1424 "<th>Header 1</th>",
1425 "<th>Header 2</th>",
1426 "<th>Header 3</th>",
1427 "<th>Header 4</th>",
1428 "<th>Header 5</th>",
1429 "</tr>",
1430 "<tr>",
1431 "<td>Cell 1</td>",
1432 "<td>Cell 2</td>",
1433 "<td>Cell 3</td>",
1434 "<td>Cell 4</td>",
1435 "<td>Cell 5</td>",
1436 "<td>Cell 6</td>",
1437 "</tr>",
1438 "</table>",
1439 "}}"
1440 ],
1441 'columnCount' => 6,
1442 'dsr' => [ 0, 233, null, null ],
1443 'templateName' => 'Template:1x'
1444 ],
1445 'transclusions 6 header columns and 5 row columns' => [
1446 'wikiTextLines' => [
1447 "{{1x|",
1448 "<table>",
1449 "<tr>",
1450 "<th>Header 1</th>",
1451 "<th>Header 2</th>",
1452 "<th>Header 3</th>",
1453 "<th>Header 4</th>",
1454 "<th>Header 5</th>",
1455 "<th>Header 6</th>",
1456 "</tr>",
1457 "<tr>",
1458 "<td>Cell 1</td>",
1459 "<td>Cell 2</td>",
1460 "<td>Cell 3</td>",
1461 "<td>Cell 4</td>",
1462 "<td>Cell 5</td>",
1463 "</tr>",
1464 "</table>",
1465 "}}"
1466 ],
1467 'columnCount' => 6,
1468 'dsr' => [ 0, 235, null, null ],
1469 'templateName' => 'Template:1x'
1470 ],
1471 'transclusions 5 header columns and 5 row columns' => [
1472 'wikiTextLines' => [
1473 "{{1x|",
1474 "<table>",
1475 "<tr>",
1476 "<th>Header 1</th>",
1477 "<th>Header 2</th>",
1478 "<th>Header 3</th>",
1479 "<th>Header 4</th>",
1480 "<th>Header 5</th>",
1481 "</tr>",
1482 "<tr>",
1483 "<td>Cell 1</td>",
1484 "<td>Cell 2</td>",
1485 "<td>Cell 3</td>",
1486 "<td>Cell 4</td>",
1487 "<td>Cell 5</td>",
1488 "</tr>",
1489 "</table>",
1490 "}}"
1491 ],
1492 'columnCount' => 5,
1493 'dsr' => [], // empty dsr => no lints found
1494 'templateName' => 'Template:1x'
1495 ],
1496 'long rows 30' => [
1497 'wikiTextLines' => $noLongRowsTable,
1498 'columnCount' => 3,
1499 'dsr' => [] // empty dsr => no lints found
1500 ],
1501 // An exhaustive table search heuristic will flag a lint here.
1502 // But, a more performant search that only looks at the first
1503 // N (=10 right now) rows will not find a lint here
1504 'non-uniform table with a long row beyond the first 10' => [
1505 'wikiTextLines' => $nonUniformLongRowTable,
1506 'columnCount' => 3,
1507 'dsr' => [] // empty dsr => no lints found
1508 ],
1509 'Nested Table 1' => [
1510 'wikiTextLines' => [
1511 "{|",
1512 "|+",
1513 "!Header 1",
1514 "!Header 2",
1515 "!Header 3",
1516 "|-",
1517 "|Cell 1",
1518 "|Cell 2",
1519 "|Cell 3",
1520 "|-",
1521 "|",
1522 "{|",
1523 "|+",
1524 "!Header 4",
1525 "!Header 5",
1526 "!Header 6",
1527 "|-",
1528 "|Cell 4",
1529 "|Cell 5",
1530 "|Cell 6",
1531 "|}",
1532 "|",
1533 "|",
1534 "|",
1535 "{|",
1536 "|+",
1537 "!Header 7",
1538 "!Header 8",
1539 "!Header 9",
1540 "|-",
1541 "|Cell 7",
1542 "|Cell 8",
1543 "|Cell 9",
1544 "|}",
1545 "|}",
1546 ],
1547 'columnCount' => 1,
1548 'dsr' => [] // empty dsr => no lints found
1549 ],
1550 'Nested Table 2' => [
1551 'wikiTextLines' => [
1552 "{|",
1553 "|+",
1554 "!Header 1",
1555 "!Header 2",
1556 "!Header 3",
1557 "|-",
1558 "|Cell 1",
1559 "|Cell 2",
1560 "|Cell 3",
1561 "|-",
1562 "|",
1563 "{|",
1564 "|+",
1565 "!Header 4",
1566 "!Header 5",
1567 "!Header 6",
1568 "|-",
1569 "|Cell 4",
1570 "|Cell 5",
1571 "|Cell 6",
1572 "|}",
1573 "|",
1574 "|",
1575 "|",
1576 "{|",
1577 "|+",
1578 "!Header 7",
1579 "!Header 8",
1580 "!Header 9",
1581 "|-",
1582 "|Cell 7",
1583 "|Cell 8",
1584 "|Cell 9",
1585 "|Cell 10",
1586 "|Cell 11",
1587 "|Cell 12",
1588 "|}",
1589 "|}",
1590 ],
1591 'columnCount' => 6,
1592 'dsr' => [ 140, 232, 2, 2 ]
1593 ],
1594 ];
1595 }
1596
1597 /**
1598 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintLargeTables
1599 *
1600 * @param string[] $wikiTextLines
1601 * @param int $columnCount
1602 * @param array $dsr
1603 * @param string|null $templateName
1604 *
1605 * @dataProvider provideLargeTablesTests
1606 */
1607 public function testLargeTables( $wikiTextLines, $columnCount, $dsr = [], $templateName = null ): void {
1608 $opts = [];
1609 $siteConfig = new MockSiteConfig( $opts );
1610 $lintConfig = $siteConfig->getLinterSiteConfig();
1611 $columnsMax = $lintConfig['maxTableColumnHeuristic'] ?? 0;
1612
1613 $desc = 'should identify large width table for T334528';
1614 $result = $this->wtToLint( implode( "\n", $wikiTextLines ) );
1615 $expectedCount = count( $dsr ) < 1 ? 0 : 1;
1616 $this->assertCount( $expectedCount, $result, $desc );
1617 if ( $expectedCount < 1 ) {
1618 return;
1619 }
1620 $this->assertEquals( 'large-tables', $result[0]['type'], $desc );
1621 $this->assertTrue( isset( $result[0]['params'] ), $desc );
1622 $this->assertEquals( $columnCount, $result[0]['params']['columns'], $desc );
1623 $this->assertEquals( $columnsMax, $result[0]['params']['columnsMax'], $desc );
1624 $this->assertEquals( $dsr, $result[0]['dsr'], $desc );
1625 if ( $templateName ) {
1626 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
1627 $this->assertEquals( $templateName, $result[0]['templateInfo']['name'], $desc );
1628 }
1629 }
1630
1631 /**
1632 * @see testLogInlineBackgroundWithoutColor
1633 * @see https://phabricator.wikimedia.org/T358238
1634 *
1635 * Provides test cases for the 'night-mode-unaware-background-color' lint
1636 * We are skipping DSR checks for this lint since:
1637 * - DSR functionality is shared with all lints
1638 * - that DSR functionality is adequately tested in other lints
1639 * - there is nothing unusual DSR-wise about this lint that
1640 * needs special verification.
1641 */
1642 public function provideLogInlineBackgroundWithoutColor(): array {
1643 return [
1644 [
1645 'should not lint when style attribute is missing',
1646 '<div></div>',
1647 false,
1648 ],
1649 [
1650 'should lint when background color is present but font color is missing',
1651 '<div style="background-color: red;"></div>',
1652 true,
1653 ],
1654 [
1655 'should lint when background-color is present but font color is missing in a template',
1656 '{{1x|<div style="background-color: red;"></div>}}',
1657 true,
1658 'Template:1x'
1659 ],
1660 [
1661 'should lint when background is present but font color is missing',
1662 '<div style="background: green;"></div>',
1663 true,
1664 ],
1665 [
1666 'should not have lint any issues when both background color and font color are present',
1667 '<div style="background-color: red; color: black;"></div>',
1668 false,
1669 ],
1670 [
1671 'should not lint when font color is present but background color is missing',
1672 '<div style="color: red;"></div>',
1673 false,
1674 ],
1675 [
1676 'should not lint when both background color and font color are present,' .
1677 ' even though they are missing spaces',
1678 '<div style="background-color:black;color:blue;"></div>' .
1679 '<div style="color:blue;background-color:black;"></div>',
1680 false,
1681 ],
1682 ];
1683 }
1684
1685 /**
1686 * Provide test cases for image alt text
1687 * @return array[]
1688 */
1689 public function provideMissingImageAltTextTests() {
1690 return [
1691 [
1692 'wikiText' => '[[File:Foobar.jpg]]',
1693 'count' => 1,
1694 'desc' => 'Inline image, no alt or caption',
1695 ],
1696 [
1697 'wikiText' => '[[File:Foobar.jpg|alt=Painting depicting a gazebo]]',
1698 'count' => 0,
1699 'desc' => 'Inline image, explicit alt',
1700 ],
1701 [
1702 'wikiText' => '[[File:Foobar.jpg|Painting depicting a gazebo]]',
1703 'count' => 0,
1704 'desc' => 'Inline image, implicit alt as caption',
1705 ],
1706 [
1707 'wikiText' => '[[File:Foobar.jpg|thumb|On display]]',
1708 'count' => 1,
1709 'desc' => 'Thumbnail image, no alt, has caption',
1710 ],
1711 [
1712 'wikiText' => '[[File:Foobar.jpg|thumb|alt=Painting depicting a gazebo]]',
1713 'count' => 0,
1714 'desc' => 'Thumbnail image, explicit alt, no caption',
1715 ],
1716 [
1717 'wikiText' => '[[File:Foobar.jpg|thumb|alt=Painting depicting a gazebo|On display]]',
1718 'count' => 0,
1719 'desc' => 'Thumbnail image, explicit alt, has caption',
1720 ],
1721
1722 // Currently an explicitly empty alt does not trigger the lint.
1723 [
1724 'wikiText' => '[[File:Foobar.jpg|alt=]]',
1725 'count' => 0,
1726 'desc' => 'Inline image, explicit empty alt',
1727 ],
1728 [
1729 'wikiText' => '[[File:Foobar.jpg|thumb|alt=]]',
1730 'count' => 0,
1731 'desc' => 'Thumbnail image, explicit empty alt, no caption',
1732 ],
1733 [
1734 'wikiText' => '[[File:Foobar.jpg|thumb|alt=|On display]]',
1735 'count' => 0,
1736 'desc' => 'Thumbnail image, explicit empty alt, has caption',
1737 ],
1738
1739 // Use of aria-hidden=true or role=presentation/role=none suppresses
1740 // the lint on the whole subtree.
1741 [
1742 'wikiText' =>
1743 "<div aria-hidden=true>\n" .
1744 "<div>\n" .
1745 "[[File:Foobar.jpg]]\n" .
1746 "</div>\n" .
1747 "</div>",
1748 'count' => 0,
1749 'desc' => 'Image in an aria-hidden=true div',
1750 ],
1751 // It's common to use aria-hidden=true and role=presentation together
1752 // for backwards compatibility reasons; treat them equivalently here.
1753 [
1754 'wikiText' =>
1755 "<div aria-hidden=true role=presentation>\n" .
1756 "<div>\n" .
1757 "[[File:Foobar.jpg]]\n" .
1758 "</div>\n" .
1759 "</div>",
1760 'count' => 0,
1761 'desc' => 'Image in an aria-hidden=true role=presentation div',
1762 ],
1763 [
1764 'wikiText' =>
1765 "<div role=presentation>\n" .
1766 "<div>\n" .
1767 "[[File:Foobar.jpg]]\n" .
1768 "</div>\n" .
1769 "</div>",
1770 'count' => 0,
1771 'desc' => 'Image inside a bare role=presentation div',
1772 ],
1773 [
1774 'wikiText' =>
1775 "<div role=none>\n" .
1776 "<div>\n" .
1777 "[[File:Foobar.jpg]]\n" .
1778 "</div>\n" .
1779 "</div>",
1780 'count' => 0,
1781 'desc' => 'Image inside a bare role=none div',
1782 ],
1783 ];
1784 }
1785
1786 /**
1787 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintNightModeUnawareBackgroundColor
1788 * @see https://phabricator.wikimedia.org/T358238
1789 * @dataProvider provideLogInlineBackgroundWithoutColor
1790 */
1791 public function testLogInlineBackgroundWithoutColor(
1792 string $desc, string $wikitext, bool $haveLint, ?string $templateName = null
1793 ): void {
1794 if ( !$haveLint ) {
1795 $this->expectEmptyResults( $desc, $wikitext );
1796 } else {
1797 $result = $this->wtToLint( $wikitext );
1798 $this->assertCount( 1, $result, $desc );
1799 $this->assertEquals( 'night-mode-unaware-background-color', $result[0]['type'], $desc );
1800 if ( $templateName ) {
1801 $this->assertTrue( isset( $result[0]['templateInfo'] ), $desc );
1802 $this->assertEquals( $templateName, $result[0]['templateInfo']['name'], $desc );
1803 }
1804 }
1805 }
1806
1807 /**
1808 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\Linter::lintMissingAltText
1809 *
1810 * @param string[] $wikiText input text
1811 * @param int $count of expected lint results
1812 * @param string $desc description of test case
1813 *
1814 * @dataProvider provideMissingImageAltTextTests
1815 */
1816 public function testMissingImageAltText( $wikiText, $count, $desc ): void {
1817 $result = $this->wtToLint( $wikiText );
1818 $result = $this->filterLints( $result, 'missing-image-alt-text' );
1819 $this->assertCount( $count, $result, $desc );
1820 }
1821
1822 /**
1823 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\Headings::dedupeHeadingIds
1824 */
1825 public function testDuplicateIds(): void {
1826 $desc = 'should not lint unique ids';
1827 $result = $this->wtToLint( "<div id='one'>Hi</div><div id='two'>Ho</div>" );
1828 $this->assertCount( 0, $result, $desc );
1829
1830 $desc = 'should lint duplicate ids';
1831 $result = $this->wtToLint( "<div id='one'>Hi</div><div id='one'>Ho</div>" );
1832 $this->assertCount( 1, $result, $desc );
1833 $this->assertEquals( 'duplicate-ids', $result[0]['type'], $desc );
1834 $this->assertEquals( [ 22, 44, 14, 6 ], $result[0]['dsr'], $desc );
1835 $this->assertTrue( isset( $result[0]['params']['id'] ), $desc );
1836 $this->assertEquals( 'one', $result[0]['params']['id'], $desc );
1837
1838 $desc = 'should lint duplicate ids from templates';
1839 $result = $this->wtToLint( "{{1x|1=<div id='one'>Hi</div>}}{{1x|1=<div id='one'>Ho</div>}}" );
1840 $this->assertCount( 1, $result, $desc );
1841 $this->assertEquals( 'duplicate-ids', $result[0]['type'], $desc );
1842 $this->assertEquals( [ 31, 62, null, null ], $result[0]['dsr'], $desc );
1843 $this->assertTrue( isset( $result[0]['params']['id'] ), $desc );
1844 $this->assertEquals( 'one', $result[0]['params']['id'], $desc );
1845 $this->assertEquals( 'Template:1x', $result[0]['templateInfo']['name'], $desc );
1846
1847 $desc = 'should not lint duplicate ids from headings';
1848 $result = $this->wtToLint( "== Hi ho ==\n== Hi ho ==" );
1849 $this->assertCount( 0, $result, $desc );
1850
1851 $desc = 'should maybe lint ids that would conflict with headings';
1852 $result = $this->wtToLint( "==hi==\n<div id='hi'>ho</div>" );
1853 $this->assertCount( 0, $result, $desc );
1854 }
1855
1856 }