| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Wt2Html\DOM\Processors; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\Core\DomSourceRange; |
| 7 |
use Wikimedia\Parsoid\DOM\Element; |
| 8 |
use Wikimedia\Parsoid\Mocks\MockDataAccess; |
| 9 |
use Wikimedia\Parsoid\Mocks\MockPageConfig; |
| 10 |
use Wikimedia\Parsoid\Mocks\MockPageContent; |
| 11 |
use Wikimedia\Parsoid\Mocks\MockSiteConfig; |
| 12 |
use Wikimedia\Parsoid\Parsoid; |
| 13 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 15 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 16 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* based on tests/mocha/dsr.js |
| 20 |
* @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\ComputeDSR |
| 21 |
*/ |
| 22 |
class ComputeDSRTest extends TestCase { |
| 23 |
|
| 24 |
public function validateSpec( string $wt, Element $doc, array $spec ): void { |
| 25 |
$elts = DOMCompat::querySelectorAll( $doc, $spec['selector'] ); |
| 26 |
$this->assertCount( 1, $elts, |
| 27 |
"Cannot match selector \"{$spec['selector']}\" in doc " . |
| 28 |
ContentUtils::toXML( $doc ) ); |
| 29 |
|
| 30 |
$dp = DOMDataUtils::getDataParsoid( $elts[0] ); |
| 31 |
if ( $spec['dsrContent'] === null ) { |
| 32 |
$this->assertObjectNotHasProperty( 'dsr', $dp ); |
| 33 |
return; |
| 34 |
} |
| 35 |
$this->assertObjectHasProperty( 'dsr', $dp ); |
| 36 |
$dsr = $dp->dsr; |
| 37 |
$this->assertInstanceOf( DomSourceRange::class, $dsr ); |
| 38 |
|
| 39 |
$this->assertSubstring( $spec['dsrContent'][0], $wt, $dsr->start, $dsr->length(), |
| 40 |
"all" ); |
| 41 |
if ( $spec['dsrContent'][1] === null ) { |
| 42 |
$this->assertNull( $dsr->openWidth ); |
| 43 |
} else { |
| 44 |
$this->assertSubstring( $spec['dsrContent'][1], $wt, $dsr->start, $dsr->openWidth, |
| 45 |
"open" ); |
| 46 |
} |
| 47 |
if ( $spec['dsrContent'][2] === null ) { |
| 48 |
$this->assertNull( $dsr->closeWidth ); |
| 49 |
} else { |
| 50 |
$this->assertSubstring( $spec['dsrContent'][2], $wt, $dsr->innerEnd(), $dsr->closeWidth, |
| 51 |
"close" ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
private function assertSubstring( $expected, $wt, $start, $length, $type ) { |
| 56 |
$this->assertIsInt( $start ); |
| 57 |
$this->assertIsInt( $length ); |
| 58 |
$this->assertGreaterThanOrEqual( 0, $start, |
| 59 |
"DSR $type start was less than zero" ); |
| 60 |
if ( $length > 0 ) { |
| 61 |
$this->assertLessThan( strlen( $wt ), $start, |
| 62 |
"DSR $type start was greater than or equal to the input length" ); |
| 63 |
} |
| 64 |
$this->assertGreaterThanOrEqual( 0, $length, |
| 65 |
"DSR $type length was less than zero" ); |
| 66 |
$this->assertLessThanOrEqual( strlen( $wt ), $length, |
| 67 |
"DSR $type length was greater than the input length" ); |
| 68 |
$this->assertSame( $expected, PHPUtils::safeSubstr( $wt, $start, $length ), |
| 69 |
"DSR $type did not match the expected string" ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* For every test with a 'wt' property, provide the 'spec' property that is |
| 74 |
* an array of DSR specs that need to be verified on the parsed output of 'wt'. |
| 75 |
* Every spec should have: |
| 76 |
* selector: a CSS selector for picking a DOM node in the parsed output. |
| 77 |
* dsrContent: a 3-element array. The first element is the wikitext corresponding |
| 78 |
* to the wikitext substring between dsr[0]..dsr[1]. The second and |
| 79 |
* third elements are the opening/closing wikitext tag for that node. |
| 80 |
* @dataProvider provideComputeDSR |
| 81 |
* @param string $wt |
| 82 |
* @param array $specs |
| 83 |
*/ |
| 84 |
public function testComputeDSR( string $wt, array $specs ): void { |
| 85 |
$siteConfig = new MockSiteConfig( [] ); |
| 86 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 87 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 88 |
|
| 89 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 90 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 91 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] ); |
| 92 |
|
| 93 |
$doc = ContentUtils::createAndLoadDocument( $html ); |
| 94 |
|
| 95 |
foreach ( $specs as $spec ) { |
| 96 |
$this->validateSpec( $wt, DOMCompat::getBody( $doc ), $spec ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function provideComputeDSR(): array { |
| 101 |
return [ |
| 102 |
// Paragraph wrapping |
| 103 |
'para 1' => [ |
| 104 |
'wt' => 'a', // disable first test so we can get to the juicy failure immediately. |
| 105 |
'specs' => [ |
| 106 |
[ 'selector' => 'body > p', 'dsrContent' => [ 'a', '', '' ] ] |
| 107 |
] |
| 108 |
], |
| 109 |
'para 2' => [ |
| 110 |
'wt' => "a\n\nb", |
| 111 |
'specs' => [ |
| 112 |
[ 'selector' => 'body > p:nth-child(1)', 'dsrContent' => [ 'a', '', '' ] ], |
| 113 |
[ 'selector' => 'body > p:nth-child(2)', 'dsrContent' => [ 'b', '', '' ] ] |
| 114 |
] |
| 115 |
], |
| 116 |
|
| 117 |
// Lists |
| 118 |
'list 1' => [ |
| 119 |
'wt' => "*a\n*b", |
| 120 |
'specs' => [ |
| 121 |
[ 'selector' => 'ul', 'dsrContent' => [ "*a\n*b", '', '' ] ], |
| 122 |
[ 'selector' => 'ul > li:nth-child(1)', 'dsrContent' => [ '*a', '*', '' ] ], |
| 123 |
[ 'selector' => 'ul > li:nth-child(2)', 'dsrContent' => [ '*b', '*', '' ] ] |
| 124 |
] |
| 125 |
], |
| 126 |
'list 2' => [ |
| 127 |
'wt' => "*a\n**b\n***c\n*d", |
| 128 |
'specs' => [ |
| 129 |
[ 'selector' => 'body > ul', 'dsrContent' => [ "*a\n**b\n***c\n*d", '', '' ] ], |
| 130 |
[ 'selector' => 'body > ul > li:first-child', 'dsrContent' => [ "*a\n**b\n***c", '*', '' ] ], |
| 131 |
[ 'selector' => 'body > ul > li:first-child > ul', 'dsrContent' => [ "**b\n***c", '', '' ] ], |
| 132 |
[ |
| 133 |
'selector' => 'body > ul > li:first-child > ul > li:first-child', |
| 134 |
'dsrContent' => [ "**b\n***c", '**', '' ] |
| 135 |
], |
| 136 |
[ |
| 137 |
'selector' => 'body > ul > li:first-child > ul > li:first-child > ul > li', |
| 138 |
'dsrContent' => [ '***c', '***', '' ] |
| 139 |
], |
| 140 |
[ 'selector' => 'body > ul > li:nth-child(2)', 'dsrContent' => [ '*d', '*', '' ] ] |
| 141 |
] |
| 142 |
], |
| 143 |
'list 3' => [ |
| 144 |
// TODO: this test covers computeListEltWidth() but the return value does |
| 145 |
// not find its way into the DOM, so we're only really testing that it |
| 146 |
// doesn't throw. |
| 147 |
'wt' => '{{1x|** foo}}', |
| 148 |
'specs' => [ |
| 149 |
[ |
| 150 |
'selector' => 'body > ul > li', |
| 151 |
'dsrContent' => null |
| 152 |
], |
| 153 |
] |
| 154 |
], |
| 155 |
|
| 156 |
// Headings |
| 157 |
'heading 1' => [ |
| 158 |
'wt' => "=A=\n==B==\n===C===\n====D====", |
| 159 |
'specs' => [ |
| 160 |
[ 'selector' => 'body > h1', 'dsrContent' => [ '=A=', '=', '=' ] ], |
| 161 |
[ 'selector' => 'body > h2', 'dsrContent' => [ '==B==', '==', '==' ] ], |
| 162 |
[ 'selector' => 'body > h3', 'dsrContent' => [ '===C===', '===', '===' ] ], |
| 163 |
[ 'selector' => 'body > h4', 'dsrContent' => [ '====D====', '====', '====' ] ] |
| 164 |
] |
| 165 |
], |
| 166 |
'heading 2' => [ |
| 167 |
'wt' => "=A New Use for the = Sign=\n==The == Operator==", |
| 168 |
'specs' => [ |
| 169 |
[ |
| 170 |
'selector' => 'body > h1', |
| 171 |
'dsrContent' => [ '=A New Use for the = Sign=', '=', '=' ] |
| 172 |
], |
| 173 |
[ 'selector' => 'body > h2', 'dsrContent' => [ '==The == Operator==', '==', '==' ] ] |
| 174 |
] |
| 175 |
], |
| 176 |
|
| 177 |
// Quotes |
| 178 |
'quotes 1' => [ |
| 179 |
'wt' => "''a''\n'''b'''", |
| 180 |
'specs' => [ |
| 181 |
[ 'selector' => 'p > i', 'dsrContent' => [ "''a''", "''", "''" ] ], |
| 182 |
[ 'selector' => 'p > b', 'dsrContent' => [ "'''b'''", "'''", "'''" ] ] |
| 183 |
] |
| 184 |
], |
| 185 |
|
| 186 |
// Tables |
| 187 |
'tables 1' => [ |
| 188 |
'wt' => "{|\n|-\n|A\n|}", |
| 189 |
'specs' => [ |
| 190 |
[ 'selector' => 'body > table', 'dsrContent' => [ "{|\n|-\n|A\n|}", '{|', '|}' ] ], |
| 191 |
[ 'selector' => 'body > table > tbody > tr', 'dsrContent' => [ "|-\n|A", '|-', '' ] ], |
| 192 |
[ 'selector' => 'body > table > tbody > tr > td', 'dsrContent' => [ '|A', '|', '' ] ] |
| 193 |
] |
| 194 |
], |
| 195 |
|
| 196 |
'tables 2' => [ |
| 197 |
'wt' => "{|\n<span>A</span>\n|}", |
| 198 |
'specs' => [ |
| 199 |
[ |
| 200 |
'selector' => 'body > table', |
| 201 |
'dsrContent' => [ "{|\n<span>A</span>\n|}", '{|', '|}' ] |
| 202 |
], |
| 203 |
// FIXME: throws due to negative length |
| 204 |
/* |
| 205 |
[ |
| 206 |
'selector' => 'body > p > span', |
| 207 |
'dsrContent' => [ "<span>A</span>", '<span>', '</span>' ] |
| 208 |
], |
| 209 |
*/ |
| 210 |
] |
| 211 |
], |
| 212 |
|
| 213 |
'tables 3 mixed stx' => [ |
| 214 |
'wt' => "{|\n|foo\n</table>", |
| 215 |
'specs' => [ |
| 216 |
[ |
| 217 |
'selector' => 'body > table', |
| 218 |
'dsrContent' => [ "{|\n|foo\n</table>", '{|', '</table>' ] |
| 219 |
], |
| 220 |
[ |
| 221 |
'selector' => 'body > table > tbody', |
| 222 |
'dsrContent' => [ "|foo\n", '', '' ] |
| 223 |
], |
| 224 |
[ |
| 225 |
'selector' => 'tr > td', |
| 226 |
'dsrContent' => [ "|foo", '|', '' ] |
| 227 |
] |
| 228 |
] |
| 229 |
], |
| 230 |
|
| 231 |
// Pre |
| 232 |
'pre 1' => [ |
| 233 |
'wt' => " Preformatted text ", |
| 234 |
'specs' => [ |
| 235 |
[ 'selector' => 'body > pre', 'dsrContent' => [ " Preformatted text ", ' ', '' ] ] |
| 236 |
] |
| 237 |
], |
| 238 |
// Regression test for robust DSR computation when PreHandler has a bug. |
| 239 |
// PreHandler swallows the category link into <pre> which creates an 1-char |
| 240 |
// difference in DSR offset by the time the <i> tag is encountered. |
| 241 |
// The endTSR value on the <i> tag should override the buggy computed value. |
| 242 |
'pre 2' => [ |
| 243 |
'wt' => " ''a'' b\n[[Category:Foo]]", |
| 244 |
'specs' => [ |
| 245 |
[ 'selector' => 'body > pre > i', 'dsrContent' => [ "''a''", "''", "''" ] ] |
| 246 |
] |
| 247 |
], |
| 248 |
|
| 249 |
// Elements |
| 250 |
'elt 1' => [ |
| 251 |
'wt' => "<small>'''bold'''</small>", |
| 252 |
'specs' => [ |
| 253 |
[ |
| 254 |
'selector' => 'body > p', |
| 255 |
'dsrContent' => [ "<small>'''bold'''</small>", '', '' ] |
| 256 |
], |
| 257 |
[ |
| 258 |
'selector' => 'body > p > small', |
| 259 |
'dsrContent' => [ "<small>'''bold'''</small>", '<small>', '</small>' ] |
| 260 |
] |
| 261 |
] |
| 262 |
], |
| 263 |
'elt 2 misnested' => [ |
| 264 |
'wt' => '<b><i>A</b></i>', |
| 265 |
'specs' => [ |
| 266 |
[ |
| 267 |
'selector' => 'body > p > b', |
| 268 |
'dsrContent' => [ '<b><i>A</b></i>', '<b>', '</i>' ] |
| 269 |
], |
| 270 |
[ |
| 271 |
'selector' => 'body > p > b > i', |
| 272 |
'dsrContent' => [ '<i>A', '<i>', '' ] |
| 273 |
], |
| 274 |
] |
| 275 |
], |
| 276 |
'elt 3 p-wrap node splitting' => [ |
| 277 |
'wt' => '<b>a<div>b</div>c</b>', |
| 278 |
'specs' => [ |
| 279 |
[ |
| 280 |
'selector' => 'body > p:nth-of-type(1) > b', |
| 281 |
'dsrContent' => [ '<b>a', '<b>', '' ] |
| 282 |
], |
| 283 |
[ |
| 284 |
'selector' => 'body > b', |
| 285 |
'dsrContent' => [ '<div>b</div>', '', '' ] |
| 286 |
], |
| 287 |
[ |
| 288 |
'selector' => 'body > p:nth-of-type(2) > b', |
| 289 |
'dsrContent' => [ 'c</b>', '', '</b>' ] |
| 290 |
] |
| 291 |
] |
| 292 |
], |
| 293 |
'elt 4 AFE reconstruction' => [ |
| 294 |
'wt' => '<div><p><b>a<p>b', |
| 295 |
'specs' => [ |
| 296 |
[ |
| 297 |
'selector' => 'body > div > p:nth-of-type(1) > b', |
| 298 |
'dsrContent' => [ '<b>a', '<b>', '' ] |
| 299 |
], |
| 300 |
[ |
| 301 |
'selector' => 'body > div > p:nth-of-type(2) > b', |
| 302 |
'dsrContent' => [ 'b', '', '' ] |
| 303 |
], |
| 304 |
] |
| 305 |
], |
| 306 |
'elt 5 AAA' => [ |
| 307 |
'wt' => '<div><b>a<div>b</b>c', |
| 308 |
'specs' => [ |
| 309 |
[ |
| 310 |
'selector' => 'body > div > b', |
| 311 |
'dsrContent' => [ '<b>a', '<b>', '' ] |
| 312 |
], |
| 313 |
[ |
| 314 |
'selector' => 'body > div > div', |
| 315 |
'dsrContent' => [ '<div>b</b>c', '<div>', '' ] |
| 316 |
], |
| 317 |
] |
| 318 |
], |
| 319 |
'elt 6 misnested' => [ |
| 320 |
'wt' => "<span>''''A</span><span>B</span>", |
| 321 |
'specs' => [ |
| 322 |
[ |
| 323 |
'selector' => 'body > p > span > b', |
| 324 |
'dsrContent' => [ "'''A", "'''", '' ] |
| 325 |
], |
| 326 |
[ |
| 327 |
'selector' => 'body > p > b', |
| 328 |
'dsrContent' => [ '<span>B</span>', '', '' ] |
| 329 |
], |
| 330 |
] |
| 331 |
], |
| 332 |
|
| 333 |
// Mixed HTML and native-wikitext tags |
| 334 |
'html quote end tag' => [ |
| 335 |
'wt' => "'''x</b>", |
| 336 |
'specs' => [ |
| 337 |
[ |
| 338 |
'selector' => 'body > p > b', |
| 339 |
'dsrContent' => [ "'''x</b>", "'''", '</b>' ] |
| 340 |
] |
| 341 |
], |
| 342 |
], |
| 343 |
|
| 344 |
// Links |
| 345 |
'link 1' => [ |
| 346 |
'wt' => 'Foo https://en.wikipedia.org/ bar', |
| 347 |
'specs' => [ |
| 348 |
[ |
| 349 |
'selector' => 'a', |
| 350 |
'dsrContent' => [ 'https://en.wikipedia.org/', '', '' ] |
| 351 |
], |
| 352 |
] |
| 353 |
], |
| 354 |
'link 2' => [ |
| 355 |
'wt' => 'http://example.com/?foo=bar', |
| 356 |
'specs' => [ |
| 357 |
[ |
| 358 |
'selector' => 'a', |
| 359 |
'dsrContent' => [ 'http://example.com/?foo=bar', '', '' ] |
| 360 |
] |
| 361 |
] |
| 362 |
], |
| 363 |
'link 3' => [ |
| 364 |
'wt' => 'RFC 2616', |
| 365 |
'specs' => [ |
| 366 |
[ |
| 367 |
'selector' => 'a', |
| 368 |
'dsrContent' => [ 'RFC 2616', '', '' ] |
| 369 |
] |
| 370 |
] |
| 371 |
], |
| 372 |
'link 4' => [ |
| 373 |
'wt' => '[[Foo]]', |
| 374 |
'specs' => [ |
| 375 |
[ |
| 376 |
'selector' => 'a', |
| 377 |
'dsrContent' => [ '[[Foo]]', '[[', ']]' ] |
| 378 |
] |
| 379 |
] |
| 380 |
], |
| 381 |
'link 5' => [ |
| 382 |
'wt' => '[[Foo|bar]]', |
| 383 |
'specs' => [ |
| 384 |
[ |
| 385 |
'selector' => 'a', |
| 386 |
'dsrContent' => [ '[[Foo|bar]]', '[[Foo|', ']]' ] |
| 387 |
] |
| 388 |
] |
| 389 |
], |
| 390 |
'link 6' => [ |
| 391 |
'wt' => '[https://en.wikipedia.org/ Wikipedia]', |
| 392 |
'specs' => [ |
| 393 |
[ |
| 394 |
'selector' => 'a', |
| 395 |
'dsrContent' => [ '[https://en.wikipedia.org/ Wikipedia]', '[https://en.wikipedia.org/ ', ']' ], |
| 396 |
] |
| 397 |
] |
| 398 |
], |
| 399 |
|
| 400 |
// xmlish extension tags |
| 401 |
'xmlish 1' => [ |
| 402 |
'wt' => '<pre>Like rain gone wooden</pre>', |
| 403 |
'specs' => [ |
| 404 |
[ |
| 405 |
'selector' => 'pre[typeof="mw:Extension/pre"]', |
| 406 |
'dsrContent' => [ '<pre>Like rain gone wooden</pre>', '<pre>', '</pre>' ] |
| 407 |
] |
| 408 |
] |
| 409 |
], |
| 410 |
|
| 411 |
// Comments |
| 412 |
'comment 1' => [ |
| 413 |
'wt' => '* <!--a--> b', |
| 414 |
'specs' => [ |
| 415 |
[ |
| 416 |
'selector' => 'li', |
| 417 |
'dsrContent' => [ '* <!--a--> b', '*', '' ] |
| 418 |
] |
| 419 |
] |
| 420 |
], |
| 421 |
'comment 2' => [ |
| 422 |
// Misnesting is a way to get the comment length to appear in the output |
| 423 |
'wt' => '<div><b><i>AAA</b><!--BBB--></i></div>', |
| 424 |
'specs' => [ |
| 425 |
[ |
| 426 |
'selector' => 'body > div', |
| 427 |
'dsrContent' => [ '<div><b><i>AAA</b><!--BBB--></i></div>', '<div>', '</div>' ] |
| 428 |
] |
| 429 |
] |
| 430 |
], |
| 431 |
'unclosed comment' => [ |
| 432 |
'wt' => '{{1x|<div>a}}\n\nb\n<!--\nx', |
| 433 |
'specs' => [ |
| 434 |
[ |
| 435 |
'selector' => 'body > div', |
| 436 |
'dsrContent' => [ '{{1x|<div>a}}\n\nb\n<!--\nx', null, '' ] |
| 437 |
] |
| 438 |
] |
| 439 |
], |
| 440 |
|
| 441 |
// Entities |
| 442 |
'entity 1' => [ |
| 443 |
'wt' => '—', |
| 444 |
'specs' => [ |
| 445 |
[ |
| 446 |
'selector' => 'body > p > span', |
| 447 |
'dsrContent' => [ '—', null, null ] |
| 448 |
], |
| 449 |
] |
| 450 |
] |
| 451 |
]; |
| 452 |
} |
| 453 |
} |