3 class RemexDriverTest
extends MediaWikiTestCase
{
4 private static $remexTidyTestData = [
16 'No p-wrap of blank node',
21 'p-wrap terminated by div',
26 'p-wrap not terminated by span',
28 "<p>x<span></span></p>"
31 'An element is non-blank and so gets p-wrapped',
33 "<p><span></span></p>"
36 'The blank flag is set after a block-level element',
41 'Blank detection between two block-level elements',
42 "<div></div> <div></div>",
43 "<div></div> <div></div>"
46 'But p-wrapping of non-blank content works after an element',
51 'p-wrapping between two block-level elements',
52 "<div></div>x<div></div>",
53 "<div></div><p>x</p><div></div>"
56 'p-wrap inside blockquote',
57 "<blockquote>x</blockquote>",
58 "<blockquote><p>x</p></blockquote>"
61 'A comment is blank for p-wrapping purposes',
66 'A comment is blank even when a p-wrap was opened by a text node',
71 'A comment does not open a p-wrap',
76 'A comment does not close a p-wrap',
83 "<ul><li class=\"mw-empty-elt\"></li></ul>"
87 "<ul><li><span></span></li></ul>",
88 "<ul><li><span></span></li></ul>"
92 "<ul><li>x</li></ul>",
97 "<table><tbody><tr></tr></tbody></table>",
98 "<table><tbody><tr class=\"mw-empty-elt\"></tr></tbody></table>"
103 "<p class=\"mw-empty-elt\">\n</p>"
106 'No p-wrapping of an inline element which contains a block element (T150317)',
107 "<small><div>x</div></small>",
108 "<small><div>x</div></small>"
111 'p-wrapping of an inline element which contains an inline element',
112 "<small><b>x</b></small>",
113 "<p><small><b>x</b></small></p>"
116 'p-wrapping is enabled in a blockquote in an inline element',
117 "<small><blockquote>x</blockquote></small>",
118 "<small><blockquote><p>x</p></blockquote></small>"
121 'All bare text should be p-wrapped even when surrounded by block tags',
122 "<small><blockquote>x</blockquote></small>y<div></div>z",
123 "<small><blockquote><p>x</p></blockquote></small><p>y</p><div></div><p>z</p>"
127 "<small>x<div>y</div>z</small>",
128 "<p><small>x</small></p><small><div>y</div></small><p><small>z</small></p>"
132 "<small><div>y</div>z</small>",
133 "<small><div>y</div></small><p><small>z</small></p>"
137 "<small>x<div>y</div></small>",
138 "<p><small>x</small></p><small><div>y</div></small>"
141 'Split tag stack 4 (modified to use splittable tag)',
142 "a<code>b<i>c<div>d</div></i>e</code>",
143 "<p>a<code>b<i>c</i></code></p><code><i><div>d</div></i></code><p><code>e</code></p>"
146 "Split tag stack regression check 1",
147 "x<span><div>y</div></span>",
148 "<p>x</p><span><div>y</div></span>"
151 "Split tag stack regression check 2 (modified to use splittable tag)",
152 "a<code><i><div>d</div></i>e</code>",
153 "<p>a</p><code><i><div>d</div></i></code><p><code>e</code></p>"
155 // Simple tests from pwrap.js
157 'Simple pwrap test 1',
162 '<span> is not a splittable tag, but gets p-wrapped in simple wrapping scenarios',
164 '<p><span>a</span></p>'
167 'Simple pwrap test 3',
168 'x <div>a</div> <div>b</div> y',
169 '<p>x </p><div>a</div> <div>b</div><p> y</p>'
172 'Simple pwrap test 4',
173 'x<!--c--> <div>a</div> <div>b</div> <!--c-->y',
174 '<p>x<!--c--> </p><div>a</div> <div>b</div> <!--c--><p>y</p>'
176 // Complex tests from pwrap.js
178 'Complex pwrap test 1',
179 '<i>x<div>a</div>y</i>',
180 '<p><i>x</i></p><i><div>a</div></i><p><i>y</i></p>'
183 'Complex pwrap test 2',
184 'a<small>b</small><i>c<div>d</div>e</i>f',
185 '<p>a<small>b</small><i>c</i></p><i><div>d</div></i><p><i>e</i>f</p>'
188 'Complex pwrap test 3',
189 'a<small>b<i>c<div>d</div></i>e</small>',
190 '<p>a<small>b<i>c</i></small></p><small><i><div>d</div></i></small><p><small>e</small></p>'
193 'Complex pwrap test 4',
194 'x<small><div>y</div></small>',
195 '<p>x</p><small><div>y</div></small>'
198 'Complex pwrap test 5',
199 'a<small><i><div>d</div></i>e</small>',
200 '<p>a</p><small><i><div>d</div></i></small><p><small>e</small></p>'
202 // phpcs:disable Generic.Files.LineLength
204 'Complex pwrap test 6',
205 '<i>a<div>b</div>c<b>d<div>e</div>f</b>g</i>',
206 // PHP 5 does not allow concatenation in initialisation of a class static variable
207 '<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>'
210 /* FIXME the second <b> causes a stack split which clones the <i> even
211 * though no <p> is actually generated
213 'Complex pwrap test 7',
214 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>',
215 '<i><b><font><div>x</div></font></b><div>y</div><b><font><div>z</div></font></b></i>'
220 'Blank text node after block end',
221 '<small>x<div>y</div> <b>z</b></small>',
222 '<p><small>x</small></p><small><div>y</div></small><p><small> <b>z</b></small></p>'
225 'Text node fostering (FIXME: wrap missing)',
230 'Blockquote fostering',
231 '<table><blockquote>x</blockquote></table>',
232 '<blockquote><p>x</p></blockquote><table></table>'
235 'Block element fostering',
237 '<div>x</div><table></table>'
240 'Formatting element fostering (FIXME: wrap missing)',
242 '<b>x</b><table></table>'
245 'AAA clone of p-wrapped element (FIXME: empty b)',
247 '<p><b>x</b></p><b></b><p><b>y</b>z</p>',
250 'AAA with fostering (FIXME: wrap missing)',
251 '<table><b>1<p>2</b>3</p>',
252 '<b>1</b><p><b>2</b>3</p><table></table>'
255 'AAA causes reparent of p-wrapped text node (T178632)',
256 '<i><blockquote>x</i></blockquote>',
257 '<i></i><blockquote><p><i>x</i></p></blockquote>',
260 'p-wrap ended by reparenting (T200827)',
261 '<i><blockquote><p></i>',
262 '<i></i><blockquote><p><i></i></p><p><i></i></p></blockquote>',
265 'style tag isn\'t p-wrapped (T186965)',
266 '<style>/* ... */</style>',
267 '<style>/* ... */</style>',
270 'link tag isn\'t p-wrapped (T186965)',
271 '<link rel="foo" href="bar" />',
272 '<link rel="foo" href="bar" />',
275 'style tag doesn\'t split p-wrapping (T208901)',
276 'foo <style>/* ... */</style> bar',
277 '<p>foo <style>/* ... */</style> bar</p>',
280 'link tag doesn\'t split p-wrapping (T208901)',
281 'foo <link rel="foo" href="bar" /> bar',
282 '<p>foo <link rel="foo" href="bar" /> bar</p>',
286 public function provider() {
287 return self
::$remexTidyTestData;
291 * @dataProvider provider
292 * @covers MediaWiki\Tidy\RemexCompatFormatter
293 * @covers MediaWiki\Tidy\RemexCompatMunger
294 * @covers MediaWiki\Tidy\RemexDriver
295 * @covers MediaWiki\Tidy\RemexMungerData
297 public function testTidy( $desc, $input, $expected ) {
298 $r = new MediaWiki\Tidy\
RemexDriver( [] );
299 $result = $r->tidy( $input );
300 $this->assertEquals( $expected, $result, $desc );
303 public function html5libProvider() {
304 $files = json_decode( file_get_contents( __DIR__
. '/html5lib-tests.json' ), true );
306 foreach ( $files as $file => $fileTests ) {
307 foreach ( $fileTests as $i => $test ) {
308 $tests[] = [ "$file:$i", $test['data'] ];
315 * This is a quick and dirty test to make sure none of the html5lib tests
316 * generate exceptions. We don't really know what the expected output is.
318 * @dataProvider html5libProvider
321 public function testHtml5Lib( $desc, $input ) {
322 $r = new MediaWiki\Tidy\
RemexDriver( [] );
323 $result = $r->tidy( $input );
324 $this->assertTrue( true, $desc );