| 1 |
<?php |
| 2 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace Test\Parsoid; |
| 6 |
|
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
use Wikimedia\Parsoid\Core\SelectiveUpdateData; |
| 9 |
use Wikimedia\Parsoid\DOM\Element; |
| 10 |
use Wikimedia\Parsoid\Mocks\MockDataAccess; |
| 11 |
use Wikimedia\Parsoid\Mocks\MockPageConfig; |
| 12 |
use Wikimedia\Parsoid\Mocks\MockPageContent; |
| 13 |
use Wikimedia\Parsoid\Mocks\MockSiteConfig; |
| 14 |
use Wikimedia\Parsoid\Parsoid; |
| 15 |
use Wikimedia\Parsoid\Utils\DiffDOMUtils; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 18 |
|
| 19 |
/** |
| 20 |
* Tests parsing for regressions against specific issues that have been resolved |
| 21 |
* based on tests/mocha/regression.specs.js |
| 22 |
*/ |
| 23 |
class RegressionSpecsTest extends TestCase { |
| 24 |
/** |
| 25 |
* @param string $wt |
| 26 |
* @param array $pageOpts |
| 27 |
* @param bool $wrapSections |
| 28 |
* @return Element |
| 29 |
*/ |
| 30 |
private function parseWT( string $wt, array $pageOpts = [], $wrapSections = false ): Element { |
| 31 |
$siteConfig = new MockSiteConfig( [] ); |
| 32 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 33 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 34 |
|
| 35 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 36 |
$pageConfig = new MockPageConfig( $siteConfig, $pageOpts, $content ); |
| 37 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => $wrapSections ] ); |
| 38 |
|
| 39 |
$doc = DOMUtils::parseHTML( $html ); |
| 40 |
|
| 41 |
$docBody = DOMCompat::getBody( $doc ); |
| 42 |
|
| 43 |
return( $docBody ); |
| 44 |
} |
| 45 |
|
| 46 |
private function sharedTest( |
| 47 |
string $description, string $wt, array $search, array $replace, |
| 48 |
string $withoutSelser, string $withSelser |
| 49 |
): void { |
| 50 |
$siteConfig = new MockSiteConfig( [] ); |
| 51 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 52 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 53 |
|
| 54 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 55 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 56 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] ); |
| 57 |
|
| 58 |
// This is mimicking a copy/paste in an editor |
| 59 |
$editedHTML = str_replace( $search, $replace, $html ); |
| 60 |
|
| 61 |
// Without selser |
| 62 |
$editedWT = $parsoid->html2wikitext( $pageConfig, $editedHTML, [], null ); |
| 63 |
$this->assertEquals( $withoutSelser, $editedWT, $description ); |
| 64 |
|
| 65 |
// With selser |
| 66 |
$selserData = new SelectiveUpdateData( $wt, $html ); |
| 67 |
$editedWT = $parsoid->html2wikitext( $pageConfig, $editedHTML, [], $selserData ); |
| 68 |
$this->assertEquals( $withSelser, $editedWT, $description ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* should prevent regression of T153107 |
| 73 |
* @covers \Wikimedia\Parsoid\Wt2Html\ParserPipeline |
| 74 |
*/ |
| 75 |
public function testPreventRegressionT153107(): void { |
| 76 |
$description = "Regression Specs: should prevent regression of T153107"; |
| 77 |
$wt = "[[Foo|bar]]"; |
| 78 |
$searchItems = [ 'bar' ]; |
| 79 |
$replaceItems = [ 'Foo' ]; |
| 80 |
$withoutSelser = "[[Foo|Foo]]"; |
| 81 |
$withSelser = "[[Foo]]"; |
| 82 |
|
| 83 |
$this->sharedTest( $description, $wt, $searchItems, $replaceItems, |
| 84 |
$withoutSelser, $withSelser ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* should prevent regression of T268737 |
| 89 |
* @covers \Wikimedia\Parsoid\Html2Wt\Separators::recoverTrimmedWhitespace |
| 90 |
*/ |
| 91 |
public function testPreventRegressionT268737(): void { |
| 92 |
$description = "Regression Specs: should prevent regression of T262737"; |
| 93 |
$wt = "* [[Foo]]--[[Bar]]"; |
| 94 |
$searchItems = [ '--' ]; |
| 95 |
$replaceItems = [ '..' ]; |
| 96 |
$withoutSelser = "*[[Foo]]..[[Bar]]"; |
| 97 |
$withSelser = "* [[Foo]]..[[Bar]]"; |
| 98 |
|
| 99 |
$this->sharedTest( $description, $wt, $searchItems, $replaceItems, |
| 100 |
$withoutSelser, $withSelser ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* should ensure edited lists, headings, table cells preserve original |
| 105 |
* whitespace in some scenarios |
| 106 |
* @covers \Wikimedia\Parsoid\Wt2Html\ParserPipeline |
| 107 |
*/ |
| 108 |
public function testPreserveWhitespace(): void { |
| 109 |
$description = "should ensure edited lists, headings, table cells preserve original " . |
| 110 |
"whitespace in some scenarios"; |
| 111 |
|
| 112 |
$wt = implode( "\n", [ |
| 113 |
"* item", |
| 114 |
"* <!--cmt--> item", |
| 115 |
"* <div>item</div>", |
| 116 |
"* [[Link|item]]", |
| 117 |
"* wrap", |
| 118 |
"== heading ==", |
| 119 |
"== <!--cmt--> heading ==", |
| 120 |
"== <div>heading</div> ==", |
| 121 |
"== [[Link|heading]] ==", |
| 122 |
"{|", |
| 123 |
"| cell", |
| 124 |
"| <!--cmt--> cell", |
| 125 |
"| <div>cell</div>", |
| 126 |
"| [[Link|cell]]", |
| 127 |
"| unedited c1 || cell || unedited c3 || cell", |
| 128 |
"| unedited c1 || cell || unedited c3 || unedited c4", |
| 129 |
"|}" |
| 130 |
] ); |
| 131 |
|
| 132 |
$searchItems = [ 'item', 'heading', 'cell', 'wrap' ]; |
| 133 |
$replaceItems = [ 'edited item', 'edited heading', 'edited cell', '<span>wrap</span>' ]; |
| 134 |
|
| 135 |
// Without selser, we should see normalized wikitext |
| 136 |
$withoutSelser = implode( "\n", [ |
| 137 |
"*edited item", |
| 138 |
"*<!--cmt-->edited item", |
| 139 |
"*<div>edited item</div>", |
| 140 |
"*[[Link|edited item]]", |
| 141 |
"*<span>wrap</span>", |
| 142 |
"==edited heading==", |
| 143 |
"==<!--cmt-->edited heading==", |
| 144 |
"==<div>edited heading</div>==", |
| 145 |
"==[[Link|edited heading]]==", |
| 146 |
"{|", |
| 147 |
"|edited cell", |
| 148 |
"|<!--cmt-->edited cell", |
| 149 |
"|<div>edited cell</div>", |
| 150 |
"|[[Link|edited cell]]", |
| 151 |
"|unedited c1||edited cell||unedited c3||edited cell", |
| 152 |
"|unedited c1||edited cell||unedited c3||unedited c4", |
| 153 |
"|}" |
| 154 |
] ); |
| 155 |
|
| 156 |
// With selser, we should have whitespace heuristics applied |
| 157 |
$withSelser = implode( "\n", [ |
| 158 |
"* edited item", |
| 159 |
"* <!--cmt-->edited item", |
| 160 |
"* <div>edited item</div>", |
| 161 |
"* [[Link|edited item]]", |
| 162 |
"* <span>wrap</span>", |
| 163 |
"== edited heading ==", |
| 164 |
"== <!--cmt-->edited heading ==", |
| 165 |
"== <div>edited heading</div> ==", |
| 166 |
"== [[Link|edited heading]] ==", |
| 167 |
"{|", |
| 168 |
"| edited cell", |
| 169 |
"| <!--cmt-->edited cell", |
| 170 |
"| <div>edited cell</div>", |
| 171 |
"| [[Link|edited cell]]", |
| 172 |
"| unedited c1 || edited cell || unedited c3 || edited cell", |
| 173 |
"| unedited c1 || edited cell || unedited c3 || unedited c4", |
| 174 |
"|}" |
| 175 |
] ); |
| 176 |
|
| 177 |
$this->sharedTest( $description, $wt, $searchItems, $replaceItems, |
| 178 |
$withoutSelser, $withSelser ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* should not apply whitespace heuristics for HTML versions older than 1.7.0 |
| 183 |
* @covers \Wikimedia\Parsoid\Wt2Html\ParserPipeline |
| 184 |
*/ |
| 185 |
public function testNoWhitespaceHeuristics(): void { |
| 186 |
$description = "should not apply whitespace heuristics for HTML versions older than 1.7.0"; |
| 187 |
$wt = implode( "\n", [ |
| 188 |
"* item", |
| 189 |
"* <!--cmt--> item", |
| 190 |
"* <div>item</div>", |
| 191 |
"* [[Link|item]]", |
| 192 |
"* wrap", |
| 193 |
"== heading ==", |
| 194 |
"== <!--cmt--> heading ==", |
| 195 |
"== <div>heading</div> ==", |
| 196 |
"== [[Link|heading]] ==", |
| 197 |
"{|", |
| 198 |
"| cell", |
| 199 |
"| <!--cmt--> cell", |
| 200 |
"| <div>cell</div>", |
| 201 |
"| [[Link|cell]]", |
| 202 |
"|}" |
| 203 |
] ); |
| 204 |
|
| 205 |
$siteConfig = new MockSiteConfig( [] ); |
| 206 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 207 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 208 |
|
| 209 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 210 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 211 |
|
| 212 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] ); |
| 213 |
|
| 214 |
$search = [ 'item', 'heading', 'cell', 'wrap' ]; |
| 215 |
$replace = [ 'edited item', 'edited heading', 'edited cell', '<span>wrap</span>' ]; |
| 216 |
$editedBody = str_replace( $search, $replace, $html ); |
| 217 |
|
| 218 |
// Whitespace heuristics are enabled |
| 219 |
$newVersion = implode( "\n", [ |
| 220 |
"* edited item", |
| 221 |
"* <!--cmt-->edited item", |
| 222 |
"* <div>edited item</div>", |
| 223 |
"* [[Link|edited item]]", |
| 224 |
"* <span>wrap</span>", |
| 225 |
"== edited heading ==", |
| 226 |
"== <!--cmt-->edited heading ==", |
| 227 |
"== <div>edited heading</div> ==", |
| 228 |
"== [[Link|edited heading]] ==", |
| 229 |
"{|", |
| 230 |
"| edited cell", |
| 231 |
"| <!--cmt-->edited cell", |
| 232 |
"| <div>edited cell</div>", |
| 233 |
"| [[Link|edited cell]]", |
| 234 |
"|}" |
| 235 |
] ); |
| 236 |
|
| 237 |
$selserData = new SelectiveUpdateData( $wt, $html ); |
| 238 |
$editedWT = $parsoid->html2wikitext( $pageConfig, $editedBody, [], $selserData ); |
| 239 |
$this->assertEquals( $newVersion, $editedWT, $description ); |
| 240 |
|
| 241 |
// Whitespace heuristics are disabled, but selser's buildSep heuristics |
| 242 |
// will do the magic for old non-text and non-comment nodes. |
| 243 |
$oldVersion = implode( "\n", [ |
| 244 |
"*edited item", |
| 245 |
"*<!--cmt-->edited item", |
| 246 |
"* <div>edited item</div>", |
| 247 |
"* [[Link|edited item]]", |
| 248 |
"*<span>wrap</span>", |
| 249 |
"==edited heading==", |
| 250 |
"==<!--cmt-->edited heading==", |
| 251 |
"== <div>edited heading</div> ==", |
| 252 |
"== [[Link|edited heading]] ==", |
| 253 |
"{|", |
| 254 |
"|edited cell", |
| 255 |
"|<!--cmt-->edited cell", |
| 256 |
"| <div>edited cell</div>", |
| 257 |
"| [[Link|edited cell]]", |
| 258 |
"|}" |
| 259 |
] ); |
| 260 |
|
| 261 |
// Pretend we are in 1.6.1 version to disable whitespace heuristics |
| 262 |
$htmlVersion = '1.6.1'; |
| 263 |
|
| 264 |
$selserData = new SelectiveUpdateData( $wt, $html ); |
| 265 |
$editedWT = $parsoid->html2wikitext( $pageConfig, $editedBody, |
| 266 |
[ 'inputContentVersion' => $htmlVersion ], $selserData ); |
| 267 |
$this->assertEquals( $oldVersion, $editedWT, $description ); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* should not wrap templatestyles style tags in p-wrappers |
| 272 |
* @covers \Wikimedia\Parsoid\Wt2Html\TT\TemplateHandler |
| 273 |
*/ |
| 274 |
public function testNoWrapTemplateStyles(): void { |
| 275 |
$description = "Regression Specs: should not wrap templatestyles style tags in p-wrappers"; |
| 276 |
$wt = "<templatestyles src='Template:Quote/styles.css'/><div>foo</div>"; |
| 277 |
$docBody = $this->parseWT( $wt ); |
| 278 |
$node = DOMCompat::nodeName( $docBody->firstChild ); |
| 279 |
|
| 280 |
$this->assertEquals( "style", $node, $description ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* For https://phabricator.wikimedia.org/T208901 |
| 285 |
* should not split p-wrappers around templatestyles |
| 286 |
* @covers \Wikimedia\Parsoid\Wt2Html\TT\TemplateHandler |
| 287 |
*/ |
| 288 |
public function testNoSplitPWrapper(): void { |
| 289 |
$description = "Regression Specs: should not split p-wrappers around templatestyles"; |
| 290 |
$wt = 'abc {{1x|<templatestyles src="Template:Quote/styles.css" /> def}} ghi ' . |
| 291 |
'[[File:Thumb.png|thumb|250px]]'; |
| 292 |
$docBody = $this->parseWT( $wt ); |
| 293 |
|
| 294 |
$node = DOMCompat::nodeName( $docBody->firstChild ); |
| 295 |
$this->assertEquals( "p", $node, $description ); |
| 296 |
|
| 297 |
$node = DOMCompat::nodeName( DiffDOMUtils::nextNonSepSibling( $docBody->firstChild ) ); |
| 298 |
$this->assertEquals( "figure", $node, $description ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Regression Specs: should deduplicate templatestyles style tags |
| 303 |
* @covers \Wikimedia\Parsoid\Wt2Html\TT\TemplateHandler |
| 304 |
*/ |
| 305 |
public function testNoDupTemplateStyleTags(): void { |
| 306 |
$description = "Regression Specs: should deduplicate templatestyles style tags"; |
| 307 |
$wt = [ |
| 308 |
'<templatestyles src="Template:Quote/styles.css" /><span>a</span>', |
| 309 |
'<templatestyles src="Template:Quote/styles.css" /><span>b</span>' |
| 310 |
]; |
| 311 |
$wt = implode( "\n", $wt ); |
| 312 |
$docBody = $this->parseWT( $wt ); |
| 313 |
|
| 314 |
$firstStyle = $docBody->firstChild->firstChild; |
| 315 |
$this->assertEquals( "style", DOMCompat::nodeName( $firstStyle ), $description ); |
| 316 |
|
| 317 |
$secondStyle = $firstStyle->nextSibling->nextSibling->nextSibling; |
| 318 |
$this->assertEquals( "link", DOMCompat::nodeName( $secondStyle ), $description ); |
| 319 |
|
| 320 |
$this->assertEquals( "mw-deduplicated-inline-style", |
| 321 |
DOMCompat::getAttribute( $secondStyle, 'rel' ), $description ); |
| 322 |
|
| 323 |
$this->assertEquals( 'mw-data:' . |
| 324 |
DOMCompat::getAttribute( $firstStyle, 'data-mw-deduplicate' ), |
| 325 |
DOMCompat::getAttribute( $secondStyle, 'href' ), $description ); |
| 326 |
|
| 327 |
$keys = [ 'about', 'typeof', 'data-mw', 'data-parsoid' ]; |
| 328 |
foreach ( $keys as $key ) { |
| 329 |
$this->assertTrue( $secondStyle->hasAttribute( $key ), $description ); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* @covers \Wikimedia\Parsoid\Ext\Gallery\Gallery::domToWikitext |
| 335 |
*/ |
| 336 |
public function testGalleryLineWithTemplatestyleInCaption(): void { |
| 337 |
$description = "Deduplicated templatestyles shouldn't lead to differing alt and caption text."; |
| 338 |
$wt = <<<EOT |
| 339 |
The first instance ensures the one in the caption is deduped <templatestyles src="Template:Quote/styles.css" /> |
| 340 |
<gallery> |
| 341 |
File:Foobar.jpg|caption with <templatestyles src="Template:Quote/styles.css" /> |
| 342 |
</gallery> |
| 343 |
EOT; |
| 344 |
$docBody = $this->parseWT( $wt ); |
| 345 |
|
| 346 |
$siteConfig = new MockSiteConfig( [] ); |
| 347 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 348 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 349 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 350 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 351 |
|
| 352 |
$editedWt = $parsoid->html2wikitext( $pageConfig, DOMCompat::getOuterHTML( $docBody ), [], null ); |
| 353 |
|
| 354 |
$this->assertEquals( $wt, $editedWt ); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* @covers \Wikimedia\Parsoid\Config\SiteConfig::getMagicWordForMediaOption |
| 359 |
*/ |
| 360 |
public function testSub(): void { |
| 361 |
$wt = <<<EOT |
| 362 |
[[File:Foobar.jpg|sub|caption]] |
| 363 |
EOT; |
| 364 |
$docBody = $this->parseWT( $wt ); |
| 365 |
$figure = DOMCompat::querySelector( $docBody, 'span[typeof~="mw:File"]' ); |
| 366 |
$this->assertInstanceOf( Element::class, $figure ); |
| 367 |
$this->assertTrue( DOMUtils::hasClass( $figure, 'mw-valign-sub' ) ); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Tests TOC edge cases in T350625 and T352467 |
| 372 |
* Cannot test some of this via parser tests because that framework strips |
| 373 |
* about attributes and here, we want to assert the presence of 'about'. |
| 374 |
* @covers \Wikimedia\Parsoid\Wt2Html\DOM\Processors\WrapSectionsState |
| 375 |
*/ |
| 376 |
public function testTocEdgeCases(): void { |
| 377 |
// For the test below, |
| 378 |
// - Synthetic meta should get an about id |
| 379 |
// matching the surrounding template |
| 380 |
// - Synthetic section shoult not get an about id |
| 381 |
$wt = <<<EOT |
| 382 |
<div> |
| 383 |
{{1x|1= |
| 384 |
foo |
| 385 |
==h1== |
| 386 |
}} |
| 387 |
a |
| 388 |
==h2== |
| 389 |
b |
| 390 |
==h3== |
| 391 |
c |
| 392 |
==h4== |
| 393 |
d |
| 394 |
</div> |
| 395 |
EOT; |
| 396 |
$docBody = $this->parseWT( $wt, [], true ); |
| 397 |
|
| 398 |
$syntheticMeta = DOMCompat::querySelector( $docBody, 'meta[property=mw:PageProp/toc]' ); |
| 399 |
$about = DOMCompat::getAttribute( $syntheticMeta, 'about' ); |
| 400 |
$this->assertNotNull( $about ); |
| 401 |
|
| 402 |
$syntheticSection = $syntheticMeta->parentNode; |
| 403 |
$this->assertSame( '-2', DOMCompat::getAttribute( $syntheticSection, 'data-mw-section-id' ) ); |
| 404 |
$this->assertSame( $about, DOMCompat::getAttribute( $syntheticSection->previousSibling, 'about' ) ); // <span> for \n after 'foo' |
| 405 |
$this->assertSame( $about, DOMCompat::getAttribute( $syntheticSection->nextSibling->firstChild, 'about' ) ); // <h1> |
| 406 |
$this->assertNull( DOMCompat::getAttribute( $syntheticSection, 'about' ) ); |
| 407 |
|
| 408 |
// For the test below, |
| 409 |
// - Synthetic meta and synthetic section should not get an about id |
| 410 |
// - The synthetic section should be nested in a <div> tag |
| 411 |
$wt = <<<EOT |
| 412 |
{{1x|foo |
| 413 |
<div>}} |
| 414 |
==h1== |
| 415 |
a |
| 416 |
==h2== |
| 417 |
b |
| 418 |
==h3== |
| 419 |
c |
| 420 |
==h4== |
| 421 |
d |
| 422 |
==h5== |
| 423 |
e |
| 424 |
EOT; |
| 425 |
$docBody = $this->parseWT( $wt, [], true ); |
| 426 |
$syntheticMeta = DOMCompat::querySelector( $docBody, 'meta[property=mw:PageProp/toc]' ); |
| 427 |
$this->assertNull( DOMCompat::getAttribute( $syntheticMeta, 'about' ) ); |
| 428 |
|
| 429 |
$syntheticSection = $syntheticMeta->parentNode; |
| 430 |
$this->assertSame( '-2', DOMCompat::getAttribute( $syntheticSection, 'data-mw-section-id' ) ); |
| 431 |
$this->assertSame( 'div', DOMCompat::nodeName( $syntheticSection->parentNode ) ); |
| 432 |
$this->assertNull( DOMCompat::getAttribute( $syntheticSection, 'about' ) ); |
| 433 |
} |
| 434 |
|
| 435 |
} |