| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Wt2Html\DOM\Handlers; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\DOM\Element; |
| 7 |
use Wikimedia\Parsoid\Mocks\MockDataAccess; |
| 8 |
use Wikimedia\Parsoid\Mocks\MockPageConfig; |
| 9 |
use Wikimedia\Parsoid\Mocks\MockPageContent; |
| 10 |
use Wikimedia\Parsoid\Mocks\MockSiteConfig; |
| 11 |
use Wikimedia\Parsoid\Parsoid; |
| 12 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 13 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 15 |
|
| 16 |
class UnpackDOMFragmentsTest extends TestCase { |
| 17 |
/** @var array */ |
| 18 |
private $liveDocs; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
$this->liveDocs = []; |
| 23 |
} |
| 24 |
|
| 25 |
private function getOutput( string $wt ): Element { |
| 26 |
$siteConfig = new MockSiteConfig( [] ); |
| 27 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 28 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 29 |
|
| 30 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 31 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 32 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] ); |
| 33 |
|
| 34 |
$doc = ContentUtils::createAndLoadDocument( $html ); |
| 35 |
|
| 36 |
// Prevent GC from reclaiming $doc once we exit this function. |
| 37 |
// Necessary hack because we use PHPDOM which wraps libxml. |
| 38 |
$this->liveDocs[] = $doc; |
| 39 |
|
| 40 |
return DOMCompat::getBody( $doc ); |
| 41 |
} |
| 42 |
|
| 43 |
private function validateFixedupDSR( Element $body, ?Element $markerNode ): void { |
| 44 |
$links = DOMCompat::querySelectorAll( $body, 'a[rel~=mw:ExtLink]' ); |
| 45 |
$count = 0; |
| 46 |
$extLink = null; |
| 47 |
foreach ( $links as $link ) { |
| 48 |
$dp = DOMDataUtils::getDataParsoid( $link ); |
| 49 |
if ( !( $dp->misnested ?? false ) ) { |
| 50 |
$extLink = $link; |
| 51 |
$count++; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Assert we have exactly 1 extlink (that should be marked misnested); |
| 56 |
$this->assertSame( 1, $count ); |
| 57 |
|
| 58 |
// Assert all nodes from extLink's next sibling till markerNode's previous sibling |
| 59 |
// have 'misnested' flags set and have non-null zero-range DSR. |
| 60 |
$n = $extLink->nextSibling; |
| 61 |
while ( $n !== $markerNode ) { |
| 62 |
if ( $n instanceof Element ) { |
| 63 |
$dp = DOMDataUtils::getDataParsoid( $n ); |
| 64 |
$dsr = $dp->dsr ?? null; |
| 65 |
|
| 66 |
$this->assertTrue( $dp->misnested ); |
| 67 |
$this->assertNotNull( $dsr ); |
| 68 |
$this->assertSame( $dsr->end, $dsr->start ); |
| 69 |
} |
| 70 |
|
| 71 |
$n = $n->nextSibling; |
| 72 |
} |
| 73 |
|
| 74 |
// Assert that nothing beyond the marker node has 'misnested' flags set. |
| 75 |
while ( $n ) { |
| 76 |
if ( $n instanceof Element ) { |
| 77 |
$dp = DOMDataUtils::getDataParsoid( $n ); |
| 78 |
$this->assertFalse( $dp->misnested ?? false ); |
| 79 |
} |
| 80 |
$n = $n->nextSibling; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @covers \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\UnpackDOMFragments |
| 86 |
* @dataProvider provideFixMisnestedTagDSRCases |
| 87 |
* @param string $wt |
| 88 |
* @param string|null $marker |
| 89 |
*/ |
| 90 |
public function testFixMisnestedTagDSRCases( string $wt, ?string $marker ): void { |
| 91 |
// Strictly speaking, *NOT* a unit test, but we are |
| 92 |
// abusing this notion for verification of properties |
| 93 |
// not easily verifiable via parser tests. |
| 94 |
$body = $this->getOutput( $wt ); |
| 95 |
$markerNode = $marker ? DOMCompat::getElementById( $body->ownerDocument, $marker ) : null; |
| 96 |
$this->validateFixedupDSR( $body, $markerNode ); |
| 97 |
} |
| 98 |
|
| 99 |
public function provideFixMisnestedTagDSRCases(): array { |
| 100 |
return [ |
| 101 |
[ "[http://example.org Link with [[wikilink]] link in the label]", null ], |
| 102 |
[ "[http://example.org <span>[[wikilink]]</span> link in the label]", null ], |
| 103 |
[ "[http://example.org <div>[[wikilink]]</div> link in the label]", null ], |
| 104 |
[ "[http://example.org <b>''[[wikilink]]''</b> link in the label]", null ], |
| 105 |
// phpcs:ignore Generic.Files.LineLength.TooLong |
| 106 |
[ "[http://example.org <b>''[[wikilink]]''</b> link in the label] <span id='marker'></span> and ''stuff''", 'marker' ], |
| 107 |
[ "* [http://example.org [[wikilink]]] <span id='marker'></span>foo ''bar''", 'marker' ] |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
} |