| 1 |
<?php |
| 2 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 3 |
|
| 4 |
namespace Test\Parsoid\Wt2Html; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 7 |
use Wikimedia\Parsoid\Parsoid; |
| 8 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 9 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 10 |
use Wikimedia\Parsoid\Wt2Html\DOM\Handlers\CleanUp; |
| 11 |
use Wikimedia\Parsoid\Wt2Html\DOM\Processors\Normalize; |
| 12 |
use Wikimedia\Parsoid\Wt2Html\DOMPostProcessor; |
| 13 |
use Wikimedia\Parsoid\Wt2Html\ParserPipelineFactory; |
| 14 |
|
| 15 |
class DOMPostProcessorTest extends \PHPUnit\Framework\TestCase { |
| 16 |
|
| 17 |
private static $defaultContentVersion = Parsoid::AVAILABLE_VERSIONS[0]; |
| 18 |
|
| 19 |
/** |
| 20 |
* @covers \Wikimedia\Parsoid\Wt2Html\DOMPostProcessor |
| 21 |
* @dataProvider provideDOMPostProcessor |
| 22 |
*/ |
| 23 |
public function testDOMPostProcessor( bool $atTopLevel, array $processors, string $html, string $expected ) { |
| 24 |
// Use 'Test Page' to verify that dc:isVersioOf link in header uses underscores |
| 25 |
// but the user rendered version in <title> in header uses spaces. |
| 26 |
$mockEnv = new MockEnv( [ 'title' => 'Test Page' ] ); |
| 27 |
$dpp = new DOMPostProcessor( $mockEnv, [ 'inTemplate' => false ] ); |
| 28 |
$dpp->registerProcessors( $processors ); |
| 29 |
$opts = [ |
| 30 |
'toplevel' => $atTopLevel |
| 31 |
]; |
| 32 |
$dpp->resetState( $opts ); |
| 33 |
$dpp->setFrame( $mockEnv->topFrame ); |
| 34 |
$document = ContentUtils::createAndLoadDocument( $html ); |
| 35 |
$dpp->doPostProcess( DOMCompat::getBody( $document ) ); |
| 36 |
$this->assertEquals( $expected, DOMCompat::getOuterHTML( $document->documentElement ) ); |
| 37 |
} |
| 38 |
|
| 39 |
public function provideDOMPostProcessor(): array { |
| 40 |
return [ |
| 41 |
[ |
| 42 |
false, |
| 43 |
ParserPipelineFactory::procNamesToProcs( ParserPipelineFactory::NESTED_PIPELINE_DOM_TRANSFORMS ), |
| 44 |
"<div>123</div>", |
| 45 |
'<html><head></head><body data-object-id="0"><div data-object-id="1">123</div></body></html>' |
| 46 |
], |
| 47 |
[ |
| 48 |
true, |
| 49 |
ParserPipelineFactory::procNamesToProcs( array_merge( |
| 50 |
ParserPipelineFactory::NESTED_PIPELINE_DOM_TRANSFORMS, |
| 51 |
ParserPipelineFactory::FULL_PARSE_GLOBAL_DOM_TRANSFORMS |
| 52 |
) ), |
| 53 |
"<div>123</div>", |
| 54 |
'<html prefix="dc: http://purl.org/dc/terms/ mw: http://mediawiki.org/rdf/" about="https://my.wiki.example/wikix/Special:Redirect/revision/1"><head prefix="mwr: https://my.wiki.example/wikix/Special:Redirect/"><meta charset="utf-8"/><meta property="mw:pageId" content="-1"/><meta property="mw:pageNamespace" content="0"/><meta property="mw:htmlVersion" content="' . self::$defaultContentVersion . '"/><meta property="mw:html:version" content="' . self::$defaultContentVersion . '"/><link rel="dc:isVersionOf" href="//my.wiki.example/wikix/Test_Page"/><base href="//my.wiki.example/wikix/"/><title>Test Page</title><link rel="stylesheet" href="//my.wiki.example/wx/load.php?lang=en&modules=mediawiki.skinning.content.parsoid%7Cmediawiki.skinning.interface%7Csite.styles&only=styles&skin=vector"/><meta http-equiv="content-language" content="en"/><meta http-equiv="vary" content="Accept"/></head><body data-parsoid=\'{"dsr":[0,39,0,0]}\' lang="en" class="mw-content-rtl sitedir-rtl rtl mw-body-content parsoid-body mediawiki mw-parser-output" dir="rtl" data-mw-parsoid-version="' . Parsoid::version() . '" data-mw-html-version="' . self::$defaultContentVersion . '"><section data-mw-section-id="0" data-parsoid="{}"><div data-parsoid=\'{"dsr":[null,39,null,null]}\'>123</div></section></body></html>' |
| 55 |
], |
| 56 |
[ |
| 57 |
false, |
| 58 |
[ |
| 59 |
[ 'Processor' => Normalize::class ] |
| 60 |
], |
| 61 |
"<p>hi</p><p></p><p>ho</p>", |
| 62 |
'<html><head></head><body data-object-id="0"><p data-object-id="1">hi</p><p data-object-id="2"></p><p data-object-id="3">ho</p></body></html>' |
| 63 |
], |
| 64 |
[ |
| 65 |
false, |
| 66 |
[ |
| 67 |
[ |
| 68 |
'name' => 'CleanUp-handleEmptyElts', |
| 69 |
'shortcut' => 'cleanup', |
| 70 |
'tplInfo' => true, |
| 71 |
'handlers' => [ |
| 72 |
[ |
| 73 |
'nodeName' => null, |
| 74 |
'action' => [ CleanUp::class, 'handleEmptyElements' ] |
| 75 |
] |
| 76 |
] |
| 77 |
] |
| 78 |
], |
| 79 |
"<p>hi</p><p></p><p>ho</p><p typeof='mw:Transclusion' about='#mwt1' data-mw='{}' data-parsoid='{}'></p>", |
| 80 |
'<html><head></head><body data-object-id="0"><p data-object-id="1">hi</p><p data-object-id="2" class="mw-empty-elt"></p><p data-object-id="3">ho</p><p typeof="mw:Transclusion" about="#mwt1" data-object-id="4" class="mw-empty-elt"></p></body></html>' |
| 81 |
] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
} |