Debian Codegraph @

tests/phpunit/unit/includes/tidy/RemexDriverTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\Config\ServiceOptions;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Parser\Sanitizer;
7
8 class RemexDriverTest extends MediaWikiUnitTestCase {
9 private const REMEX_TIDY_TEST_DATA = [
10 [
11 'Empty string',
12 "",
13 ""
14 ],
15 [
16 'Simple p-wrap',
17 "x",
18 "<p>x</p>"
19 ],
20 [
21 'No p-wrap of blank node',
22 " ",
23 " "
24 ],
25 [
26 'p-wrap terminated by div',
27 "x<div></div>",
28 "<p>x</p><div></div>"
29 ],
30 [
31 'p-wrap not terminated by span',
32 "x<span></span>",
33 "<p>x<span></span></p>"
34 ],
35 [
36 'An element is non-blank and so gets p-wrapped',
37 "<span></span>",
38 "<p><span></span></p>"
39 ],
40 [
41 'The blank flag is set after a block-level element',
42 "<div></div> ",
43 "<div></div> "
44 ],
45 [
46 'Blank detection between two block-level elements',
47 "<div></div> <div></div>",
48 "<div></div> <div></div>"
49 ],
50 [
51 'But p-wrapping of non-blank content works after an element',
52 "<div></div>x",
53 "<div></div><p>x</p>"
54 ],
55 [
56 'p-wrapping between two block-level elements',
57 "<div></div>x<div></div>",
58 "<div></div><p>x</p><div></div>"
59 ],
60 [
61 'p-wrap inside blockquote',
62 "<blockquote>x</blockquote>",
63 "<blockquote><p>x</p></blockquote>"
64 ],
65 [
66 'A comment is blank for p-wrapping purposes',
67 "<!-- x -->",
68 "<!-- x -->"
69 ],
70 [
71 'A comment is blank even when a p-wrap was opened by a text node',
72 " <!-- x -->",
73 " <!-- x -->"
74 ],
75 [
76 'A comment does not open a p-wrap',
77 "<!-- x -->x",
78 "<!-- x --><p>x</p>"
79 ],
80 [
81 'A comment does not close a p-wrap',
82 "x<!-- x -->",
83 "<p>x<!-- x --></p>"
84 ],
85 [
86 'Empty li',
87 "<ul><li></li></ul>",
88 "<ul><li class=\"mw-empty-elt\"></li></ul>"
89 ],
90 [
91 'li with element',
92 "<ul><li><span></span></li></ul>",
93 "<ul><li><span></span></li></ul>"
94 ],
95 [
96 'li with text',
97 "<ul><li>x</li></ul>",
98 "<ul><li>x</li></ul>"
99 ],
100 [
101 'Empty tr',
102 "<table><tbody><tr></tr></tbody></table>",
103 "<table><tbody><tr class=\"mw-empty-elt\"></tr></tbody></table>"
104 ],
105 [
106 'Empty p',
107 "<p>\n</p>",
108 "<p class=\"mw-empty-elt\">\n</p>"
109 ],
110 [
111 'No p-wrapping of an inline element which contains a block element (T150317)',
112 "<small><div>x</div></small>",
113 "<small><div>x</div></small>"
114 ],
115 [
116 'p-wrapping of an inline element which contains an inline element',
117 "<small><b>x</b></small>",
118 "<p><small><b>x</b></small></p>"
119 ],
120 [
121 'p-wrapping is enabled in a blockquote in an inline element',
122 "<small><blockquote>x</blockquote></small>",
123 "<small><blockquote><p>x</p></blockquote></small>"
124 ],
125 [
126 'All bare text should be p-wrapped even when surrounded by block tags',
127 "<small><blockquote>x</blockquote></small>y<div></div>z",
128 "<small><blockquote><p>x</p></blockquote></small><p>y</p><div></div><p>z</p>"
129 ],
130 [
131 'Split tag stack 1',
132 "<small>x<div>y</div>z</small>",
133 "<p><small>x</small></p><small><div>y</div></small><p><small>z</small></p>"
134 ],
135 [
136 'Split tag stack 2',
137 "<small><div>y</div>z</small>",
138 "<small><div>y</div></small><p><small>z</small></p>"
139 ],
140 [
141 'Split tag stack 3',
142 "<small>x<div>y</div></small>",
143 "<p><small>x</small></p><small><div>y</div></small>"
144 ],
145 [
146 'Split tag stack 4 (modified to use splittable tag)',
147 "a<code>b<i>c<div>d</div></i>e</code>",
148 "<p>a<code>b<i>c</i></code></p><code><i><div>d</div></i></code><p><code>e</code></p>"
149 ],
150 [
151 "Split tag stack regression check 1",
152 "x<span><div>y</div></span>",
153 "<p>x</p><span><div>y</div></span>"
154 ],
155 [
156 "Split tag stack regression check 2 (modified to use splittable tag)",
157 "a<code><i><div>d</div></i>e</code>",
158 "<p>a</p><code><i><div>d</div></i></code><p><code>e</code></p>"
159 ],
160 // Simple tests from pwrap.js
161 [
162 'Simple pwrap test 1',
163 'a',
164 '<p>a</p>'
165 ],
166 [
167 '<span> is not a splittable tag',
168 '<span>x<div>a</div>y</span> <span>x<div></div>y</span>',
169 '<span>x<div>a</div>y</span> <span>x<div></div>y</span>',
170 ],
171 [
172 '<span> is not a splittable tag, but gets p-wrapped in simple wrapping scenarios',
173 '<span>a</span>',
174 '<p><span>a</span></p>'
175 ],
176 [
177 'Simple pwrap test 3',
178 'x <div>a</div> <div>b</div> y',
179 '<p>x </p><div>a</div> <div>b</div><p> y</p>'
180 ],
181 [
182 'Simple pwrap test 4',
183 'x<!--c--> <div>a</div> <div>b</div> <!--c-->y',
184 '<p>x<!--c--> </p><div>a</div> <div>b</div> <!--c--><p>y</p>'
185 ],
186 // Complex tests from pwrap.js
187 [
188 'Complex pwrap test 1',
189 '<i>x<div>a</div>y</i>',
190 '<p><i>x</i></p><i><div>a</div></i><p><i>y</i></p>'
191 ],
192 [
193 'Complex pwrap test 2',
194 'a<small>b</small><i>c<div>d</div>e</i>f',
195 '<p>a<small>b</small><i>c</i></p><i><div>d</div></i><p><i>e</i>f</p>'
196 ],
197 [
198 'Complex pwrap test 3',
199 'a<small>b<i>c<div>d</div></i>e</small>',
200 '<p>a<small>b<i>c</i></small></p><small><i><div>d</div></i></small><p><small>e</small></p>'
201 ],
202 [
203 'Complex pwrap test 4',
204 'x<small><div>y</div></small>',
205 '<p>x</p><small><div>y</div></small>'
206 ],
207 [
208 'Complex pwrap test 5',
209 'a<small><i><div>d</div></i>e</small>',
210 '<p>a</p><small><i><div>d</div></i></small><p><small>e</small></p>'
211 ],
212 [
213 'Complex pwrap test 6',
214 '<i>a<div>b</div>c<b>d<div>e</div>f</b>g</i>',
215 '<p><i>a</i></p><i><div>b</div></i><p><i>c<b>d</b></i></p><i><b><div>e</div></b></i><p><i><b>f</b>g</i></p>'
216 ],
217 /* FIXME the second <b> causes a stack split which clones the <i> even
218 * though no <p> is actually generated
219 [
220 'Complex pwrap test 7',
221 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>',
222 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>'
223 ],
224 */
225 // New local tests
226 [
227 'Blank text node after block end',
228 '<small>x<div>y</div> <b>z</b></small>',
229 '<p><small>x</small></p><small><div>y</div></small><p><small> <b>z</b></small></p>'
230 ],
231 [
232 'Text node fostering (FIXME: wrap missing)',
233 '<table>x</table>',
234 'x<table></table>'
235 ],
236 [
237 'Blockquote fostering',
238 '<table><blockquote>x</blockquote></table>',
239 '<blockquote><p>x</p></blockquote><table></table>'
240 ],
241 [
242 'Block element fostering',
243 '<table><div>x',
244 '<div>x</div><table></table>'
245 ],
246 [
247 'Formatting element fostering (FIXME: wrap missing)',
248 '<table><b>x',
249 '<b>x</b><table></table>'
250 ],
251 [
252 'AAA clone of p-wrapped element (FIXME: empty b)',
253 '<b>x<p>y</b>z</p>',
254 '<p><b>x</b></p><b></b><p><b>y</b>z</p>',
255 ],
256 [
257 'AAA with fostering (FIXME: wrap missing)',
258 '<table><b>1<p>2</b>3</p>',
259 '<b>1</b><p><b>2</b>3</p><table></table>'
260 ],
261 [
262 'AAA causes reparent of p-wrapped text node (T178632)',
263 '<i><blockquote>x</i></blockquote>',
264 '<i></i><blockquote><p><i>x</i></p></blockquote>',
265 ],
266 [
267 'p-wrap ended by reparenting (T200827)',
268 '<i><blockquote><p></i>',
269 '<i></i><blockquote><p><i></i></p><p><i></i></p></blockquote>',
270 ],
271 [
272 'style tag isn\'t p-wrapped (T186965)',
273 '<style>/* ... */</style>',
274 '<style>/* ... */</style>',
275 ],
276 [
277 'link tag isn\'t p-wrapped (T186965)',
278 '<link rel="foo" href="bar" />',
279 '<link rel="foo" href="bar" />',
280 ],
281 [
282 'style tag doesn\'t split p-wrapping (T208901)',
283 'foo <style>/* ... */</style> bar',
284 '<p>foo <style>/* ... */</style> bar</p>',
285 ],
286 [
287 'link tag doesn\'t split p-wrapping (T208901)',
288 'foo <link rel="foo" href="bar" /> bar',
289 '<p>foo <link rel="foo" href="bar" /> bar</p>',
290 ],
291 // From the old TidyTest class
292 [
293 '<mw:editsection> should survive tidy',
294 '<mw:editsection page="foo" section="bar">foo</mw:editsection>',
295 '<mw:editsection page="foo" section="bar">foo</mw:editsection>',
296 ],
297 [
298 'TOC_PLACEHOLDER should survive tidy',
299 '<meta property="mw:PageProp/toc" />',
300 '<meta property="mw:PageProp/toc" />',
301 ],
302 [
303 '<link> should survive tidy',
304 '<link foo="bar"/>foo',
305 "<link foo=\"bar\" /><p>foo</p>",
306 ],
307 [
308 '<meta> should survive tidy',
309 '<meta foo="bar"/>foo',
310 "<meta foo=\"bar\" /><p>foo</p>",
311 ],
312 [
313 'Unicode combining characters (T387130)',
314 "<p>\u{0338} <!--comment-->\u{0338}</p>",
315 '<p>&#x338; <!--comment-->&#x338;</p>',
316 ],
317 ];
318
319 public static function provider() {
320 $testMathML = <<<'MathML'
321 <math xmlns="http://www.w3.org/1998/Math/MathML">
322 <mrow>
323 <mi>a</mi>
324 <mo>&InvisibleTimes;</mo>
325 <msup>
326 <mi>x</mi>
327 <mn>2</mn>
328 </msup>
329 <mo>+</mo>
330 <mi>b</mi>
331 <mo>&InvisibleTimes; </mo>
332 <mi>x</mi>
333 <mo>+</mo>
334 <mi>c</mi>
335 </mrow>
336 </math>
337 MathML;
338 $testMathML = Sanitizer::normalizeCharReferences( $testMathML );
339 return array_merge( self::REMEX_TIDY_TEST_DATA, [ [
340 '<math> should survive tidy',
341 $testMathML,
342 $testMathML,
343 ] ] );
344 }
345
346 /**
347 * @dataProvider provider
348 * @covers \MediaWiki\Tidy\RemexCompatFormatter
349 * @covers \MediaWiki\Tidy\RemexCompatMunger
350 * @covers \MediaWiki\Tidy\RemexDriver
351 * @covers \MediaWiki\Tidy\RemexMungerData
352 */
353 public function testTidy( $desc, $input, $expected ) {
354 $r = new MediaWiki\Tidy\RemexDriver(
355 new ServiceOptions(
356 MediaWiki\Tidy\RemexDriver::CONSTRUCTOR_OPTIONS,
357 new HashConfig( [
358 MainConfigNames::TidyConfig => [],
359 MainConfigNames::ParserEnableLegacyMediaDOM => false,
360 ] )
361 )
362 );
363 $result = $r->tidy( $input );
364 $this->assertEquals( $expected, $result, $desc );
365 }
366
367 public static function html5libProvider() {
368 $files = json_decode( file_get_contents( __DIR__ . '/html5lib-tests.json' ), true );
369 $tests = [];
370 foreach ( $files as $file => $fileTests ) {
371 foreach ( $fileTests as $i => $test ) {
372 $tests[] = [ "$file:$i", $test['data'] ];
373 }
374 }
375 return $tests;
376 }
377
378 /**
379 * This is a quick and dirty test to make sure none of the html5lib tests
380 * generate exceptions. We don't really know what the expected output is.
381 *
382 * @dataProvider html5libProvider
383 * @coversNothing
384 */
385 public function testHtml5Lib( $desc, $input ) {
386 $r = new MediaWiki\Tidy\RemexDriver(
387 new ServiceOptions(
388 MediaWiki\Tidy\RemexDriver::CONSTRUCTOR_OPTIONS,
389 new HashConfig( [
390 MainConfigNames::TidyConfig => [],
391 MainConfigNames::ParserEnableLegacyMediaDOM => false,
392 ] )
393 )
394 );
395 $result = $r->tidy( $input );
396 $this->assertTrue( true, $desc );
397 }
398 }