| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Html2Wt; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\DOM\Node; |
| 7 |
use Wikimedia\Parsoid\Html2Wt\DiffUtils; |
| 8 |
use Wikimedia\Parsoid\Html2Wt\DOMNormalizer; |
| 9 |
use Wikimedia\Parsoid\Html2Wt\SerializerState; |
| 10 |
use Wikimedia\Parsoid\Html2Wt\WikitextSerializer; |
| 11 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 12 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 13 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 14 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 15 |
use Wikimedia\TestingAccessWrapper; |
| 16 |
|
| 17 |
/** |
| 18 |
* Test DOM normalisation, the tests used for Porting DOMNormalizer class from JS |
| 19 |
* and based on similar tests in tests/mocha/dom_normalizer.js |
| 20 |
* @coversDefaultClass \Wikimedia\Parsoid\Html2Wt\DOMNormalizer |
| 21 |
*/ |
| 22 |
class DOMNormalizerTest extends TestCase { |
| 23 |
|
| 24 |
/** |
| 25 |
* @covers ::normalize |
| 26 |
* @dataProvider provideNormalize |
| 27 |
* @param string $html |
| 28 |
* @param string $expected |
| 29 |
* @param string|null $message |
| 30 |
* @param array $opts |
| 31 |
* @param bool $stripDiffMarkers |
| 32 |
*/ |
| 33 |
public function testNormalize( |
| 34 |
string $html, string $expected, ?string $message = null, array $opts = [], bool $stripDiffMarkers = true |
| 35 |
) { |
| 36 |
$mockEnv = new MockEnv( $opts ); |
| 37 |
$mockSerializer = new WikitextSerializer( $mockEnv, [] ); |
| 38 |
$mockState = new SerializerState( $mockSerializer, [ 'selserMode' => false ] ); |
| 39 |
/** @var DOMNormalizer $DOMNormalizer */ |
| 40 |
$DOMNormalizer = TestingAccessWrapper::newFromObject( new DOMNormalizer( $mockState ) ); |
| 41 |
$doc = ContentUtils::createAndLoadDocument( $html, [ 'markNew' => true ] ); |
| 42 |
$body = DOMCompat::getBody( $doc ); |
| 43 |
$DOMNormalizer->normalize( $body ); |
| 44 |
|
| 45 |
if ( $stripDiffMarkers ) { |
| 46 |
DOMUtils::visitDOM( $body, static function ( Node $node ) { |
| 47 |
if ( DiffUtils::isDiffMarker( $node ) ) { |
| 48 |
$node->parentNode->removeChild( $node ); |
| 49 |
} |
| 50 |
} ); |
| 51 |
} |
| 52 |
|
| 53 |
$actual = ContentUtils::ppToXML( $body, [ 'discardDataParsoid' => true, 'innerXML' => true ] ); |
| 54 |
$this->assertEquals( $expected, $actual, $message ); |
| 55 |
} |
| 56 |
|
| 57 |
public function provideNormalize(): array { |
| 58 |
return [ |
| 59 |
// Tag Minimization |
| 60 |
[ '<i>X</i><i>Y</i>', '<i>XY</i>', 'Tag Minimization #1', ], |
| 61 |
[ |
| 62 |
'<i>X</i><b><i>Y</i></b>', |
| 63 |
'<i>X<b>Y</b></i>', |
| 64 |
'Tag Minimization #2', |
| 65 |
], |
| 66 |
[ |
| 67 |
'<i>A</i><b><i>X</i></b><b><i>Y</i></b><i>Z</i>', |
| 68 |
'<i>A<b>XY</b>Z</i>', |
| 69 |
'Tag Minimization #3', |
| 70 |
], |
| 71 |
[ |
| 72 |
// Second node is a newly inserted node |
| 73 |
'<a data-parsoid="{}" href="FootBall">Foot</a><a href="FootBall">Ball</a>', |
| 74 |
'<a href="FootBall">FootBall</a>', |
| 75 |
'Tag Minimization #4 Second node is a newly inserted node', |
| 76 |
], |
| 77 |
[ |
| 78 |
// Both nodes are old unedited nodes |
| 79 |
'<a data-parsoid="{}" href="FootBall">Foot</a><a data-parsoid="{}" href="FootBall">Ball</a>', |
| 80 |
'<a href="FootBall">Foot</a><a href="FootBall">Ball</a>', |
| 81 |
'Tag Minimization #5 Both nodes are old unedited nodes', |
| 82 |
|
| 83 |
], |
| 84 |
// Headings |
| 85 |
[ |
| 86 |
'<h2>H2<link href="Category:A1" rel="mw:PageProp/Category"/></h2>', |
| 87 |
'<h2>H2</h2><link href="Category:A1" rel="mw:PageProp/Category"/>', |
| 88 |
'Headings #1' |
| 89 |
], |
| 90 |
[ |
| 91 |
'<h2><meta property="mw:PageProp/toc"/> ok</h2>', |
| 92 |
'<meta property="mw:PageProp/toc"/><h2>ok</h2>', |
| 93 |
'Headings #2' |
| 94 |
], |
| 95 |
// Empty tag normalization |
| 96 |
// These are stripped |
| 97 |
[ '<b></b>', '', 'Empty tag normalization #1' ], |
| 98 |
[ '<i></i>', '', 'Empty tag normalization #2' ], |
| 99 |
[ '<h2></h2>', '', 'Empty tag normalization #3' ], |
| 100 |
[ '<a rel="mw:WikiLink" href="http://foo.org"></a>', '', 'Empty tag normalization #4' ], |
| 101 |
// These should not be stripped |
| 102 |
[ '<p></p>', '<p></p>', 'Empty tag normalization #5' ], |
| 103 |
[ '<div></div>', '<div></div>', 'Empty tag normalization #6' ], |
| 104 |
[ |
| 105 |
'<a href="http://foo.org"></a>', |
| 106 |
'<a href="http://foo.org"></a>', |
| 107 |
'Empty tag normalization #7', |
| 108 |
], |
| 109 |
[ |
| 110 |
'<p><meta typeof="mw:Annotation/translate"/></p>', |
| 111 |
'<meta typeof="mw:Annotation/translate"/>', |
| 112 |
'Normalization of annotations in paragraph', |
| 113 |
], |
| 114 |
// Trailing spaces in links |
| 115 |
[ |
| 116 |
'<a rel="mw:WikiLink" href="./Foo">Foo </a>', |
| 117 |
'<a rel="mw:WikiLink" href="./Foo">Foo</a>', |
| 118 |
'Trailing spaces in links #1', |
| 119 |
], |
| 120 |
[ |
| 121 |
'<a rel="mw:WikiLink" href="./Foo">Foo </a>bar', |
| 122 |
'<a rel="mw:WikiLink" href="./Foo">Foo</a> bar', |
| 123 |
'Trailing spaces in links #2', |
| 124 |
], |
| 125 |
[ |
| 126 |
'<a rel="mw:WikiLink" href="./Foo">Foo </a> bar', |
| 127 |
'<a rel="mw:WikiLink" href="./Foo">Foo</a> bar', |
| 128 |
'Trailing spaces in links #3', |
| 129 |
], |
| 130 |
// Formatting tags in links |
| 131 |
[ |
| 132 |
'<a rel="mw:WikiLink" href="./Football"><u><i><b>Football</b></i></u></a>', |
| 133 |
'<u><i><b><a rel="mw:WikiLink" href="./Football">Football</a></b></i></u>', |
| 134 |
'Formatting tags in links #1 Reordered HTML serializable to simplified form', |
| 135 |
], |
| 136 |
[ |
| 137 |
'<a rel="mw:WikiLink" href="./Football"><i color="brown">Football</i></a>', |
| 138 |
'<a rel="mw:WikiLink" href="./Football"><i color="brown">Football</i></a>', |
| 139 |
'Formatting tags in links #2 Reordered HTML changes semantics', |
| 140 |
], |
| 141 |
[ |
| 142 |
'<a rel="mw:WikiLink" href="./Football"><u><i><b>Soccer</b></i></u></a>', |
| 143 |
'<a rel="mw:WikiLink" href="./Football"><u><i><b>Soccer</b></i></u></a>', |
| 144 |
'Formatting tags in links #3 Reordered HTML NOT serializable to simplified form', |
| 145 |
], |
| 146 |
[ |
| 147 |
'<table><tbody><tr><td>+</td><td>-</td></tr></tbody></table>', |
| 148 |
'<table><tbody><tr><td> +</td><td> -</td></tr></tbody></table>', |
| 149 |
'Escapable prefixes in table cells' |
| 150 |
], |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
} |