| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Utils; |
| 4 |
|
| 5 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 6 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 7 |
|
| 8 |
/** |
| 9 |
* @coversDefaultClass \Wikimedia\Parsoid\Utils\DOMUtils |
| 10 |
*/ |
| 11 |
class DOMUtilsTest extends \PHPUnit\Framework\TestCase { |
| 12 |
|
| 13 |
/** |
| 14 |
* Test node properties methods. |
| 15 |
* |
| 16 |
* @covers ::isRemexBlockNode |
| 17 |
* @covers ::isWikitextBlockNode |
| 18 |
* @covers ::isFormattingElt |
| 19 |
* @covers ::isQuoteElt |
| 20 |
* @covers ::isBody |
| 21 |
* @covers ::isRemoved |
| 22 |
* @covers ::isHeading |
| 23 |
* @covers ::isList |
| 24 |
* @covers ::isListItem |
| 25 |
* @covers ::isListOrListItem |
| 26 |
* @covers ::isNestedInListItem |
| 27 |
* @covers ::hasElementChild |
| 28 |
* @covers ::hasBlockElementDescendant |
| 29 |
* @covers ::isIEW |
| 30 |
* @covers ::allChildrenAreTextOrComments |
| 31 |
* @covers ::isTableTag |
| 32 |
* @covers ::isRawTextElement |
| 33 |
* @covers ::hasBlockTag |
| 34 |
* @dataProvider provideNodeProperties |
| 35 |
*/ |
| 36 |
public function testNodeProperties( string $html, array $props ) { |
| 37 |
$doc = DOMUtils::parseHTML( $html ); |
| 38 |
$sel = $props['selector'] ?? null; |
| 39 |
if ( $sel ) { |
| 40 |
$node = DOMCompat::querySelector( $doc, $sel ); |
| 41 |
} else { |
| 42 |
$node = DOMCompat::getBody( $doc )->firstChild; |
| 43 |
} |
| 44 |
|
| 45 |
$methods = [ |
| 46 |
// This method doesn't match the naming pattern: |
| 47 |
'allChildrenAreTextOrComments', |
| 48 |
'hasBlockElementDescendant', |
| 49 |
'hasBlockTag', |
| 50 |
'hasElementChild', |
| 51 |
'isBody', |
| 52 |
'isFormattingElt', |
| 53 |
'isHeading', |
| 54 |
'isIEW', |
| 55 |
'isList', |
| 56 |
'isListItem', |
| 57 |
'isListOrListItem', |
| 58 |
'isNestedInListItem', |
| 59 |
'isQuoteElt', |
| 60 |
'isRawTextElement', |
| 61 |
'isRemexBlockNode', |
| 62 |
'isRemoved', |
| 63 |
'isTableTag', |
| 64 |
'isWikitextBlockNode', |
| 65 |
]; |
| 66 |
$expected = []; |
| 67 |
$actual = []; |
| 68 |
foreach ( $methods as $m ) { |
| 69 |
if ( $props[$m] ?? false ) { |
| 70 |
$expected[$m] = true; |
| 71 |
} |
| 72 |
if ( DOMUtils::$m( $node ) ) { |
| 73 |
$actual[$m] = true; |
| 74 |
} |
| 75 |
} |
| 76 |
$this->assertSame( $expected, $actual ); |
| 77 |
} |
| 78 |
|
| 79 |
public function provideNodeProperties() { |
| 80 |
return [ |
| 81 |
[ '<a href="xyz">foo<!--bar--></a>', [ |
| 82 |
'allChildrenAreTextOrComments' => true, |
| 83 |
'isFormattingElt' => true, |
| 84 |
] ], |
| 85 |
[ '<abbr>FOO</abbr>', [ |
| 86 |
'allChildrenAreTextOrComments' => true, |
| 87 |
] ], |
| 88 |
[ '<b>bold</b>', [ |
| 89 |
'allChildrenAreTextOrComments' => true, |
| 90 |
'isFormattingElt' => true, |
| 91 |
'isQuoteElt' => true, |
| 92 |
] ], |
| 93 |
[ '<h1>heading1</h1>', [ |
| 94 |
'allChildrenAreTextOrComments' => true, |
| 95 |
'hasBlockTag' => true, |
| 96 |
'isHeading' => true, |
| 97 |
'isRemexBlockNode' => true, |
| 98 |
'isWikitextBlockNode' => true, |
| 99 |
] ], |
| 100 |
[ '<h2>heading<b>bold</b></h2>', [ |
| 101 |
'hasBlockTag' => true, |
| 102 |
'hasElementChild' => true, |
| 103 |
'isHeading' => true, |
| 104 |
'isRemexBlockNode' => true, |
| 105 |
'isWikitextBlockNode' => true, |
| 106 |
] ], |
| 107 |
[ '<body>foo<!--bar-->bat</body>', [ |
| 108 |
'selector' => 'body', |
| 109 |
'allChildrenAreTextOrComments' => true, |
| 110 |
'hasBlockTag' => true, |
| 111 |
'isBody' => true, |
| 112 |
'isRemexBlockNode' => true, |
| 113 |
] ], |
| 114 |
[ " \t ", [ |
| 115 |
'allChildrenAreTextOrComments' => true, |
| 116 |
'isIEW' => true, |
| 117 |
] ], |
| 118 |
[ "<!--comment-->", [ |
| 119 |
'allChildrenAreTextOrComments' => true, |
| 120 |
] ], |
| 121 |
[ "<ol><li>foo<b>bar", [ |
| 122 |
'hasBlockElementDescendant' => true, |
| 123 |
'hasBlockTag' => true, |
| 124 |
'hasElementChild' => true, |
| 125 |
'isList' => true, |
| 126 |
'isListOrListItem' => true, |
| 127 |
'isRemexBlockNode' => true, |
| 128 |
'isWikitextBlockNode' => true, |
| 129 |
] ], |
| 130 |
[ "<ol><li>foo<b>bar", [ |
| 131 |
'selector' => 'li', |
| 132 |
'hasBlockTag' => true, |
| 133 |
'hasElementChild' => true, |
| 134 |
'isListItem' => true, |
| 135 |
'isListOrListItem' => true, |
| 136 |
'isRemexBlockNode' => true, |
| 137 |
'isWikitextBlockNode' => true, |
| 138 |
] ], |
| 139 |
[ "<ol><li>foo<b>bar", [ |
| 140 |
'selector' => 'b', |
| 141 |
'allChildrenAreTextOrComments' => true, |
| 142 |
'isFormattingElt' => true, |
| 143 |
'isNestedInListItem' => true, |
| 144 |
'isQuoteElt' => true, |
| 145 |
] ], |
| 146 |
[ "<pre>preformatted<b>text</b></pre>", [ |
| 147 |
'hasBlockTag' => true, |
| 148 |
'hasElementChild' => true, |
| 149 |
'isRemexBlockNode' => true, |
| 150 |
'isWikitextBlockNode' => true, |
| 151 |
] ], |
| 152 |
[ "<style>ignore <b> tag</style>", [ |
| 153 |
'allChildrenAreTextOrComments' => true, |
| 154 |
'isRawTextElement' => true, |
| 155 |
] ], |
| 156 |
[ "<table><tr><td>foo</td></tr></table>", [ |
| 157 |
'hasBlockElementDescendant' => true, |
| 158 |
'hasBlockTag' => true, |
| 159 |
'hasElementChild' => true, |
| 160 |
'isRemexBlockNode' => true, |
| 161 |
'isTableTag' => true, |
| 162 |
'isWikitextBlockNode' => true, |
| 163 |
] ], |
| 164 |
[ "<table><tr><td>foo</td></tr></table>", [ |
| 165 |
'selector' => 'tr', |
| 166 |
'hasBlockElementDescendant' => true, |
| 167 |
'hasBlockTag' => true, |
| 168 |
'hasElementChild' => true, |
| 169 |
'isRemexBlockNode' => true, |
| 170 |
'isTableTag' => true, |
| 171 |
'isWikitextBlockNode' => true, |
| 172 |
] ], |
| 173 |
[ "<table><tr><td>foo</td></tr></table>", [ |
| 174 |
'selector' => 'td', |
| 175 |
'allChildrenAreTextOrComments' => true, |
| 176 |
'hasBlockTag' => true, |
| 177 |
'isRemexBlockNode' => true, |
| 178 |
'isTableTag' => true, |
| 179 |
'isWikitextBlockNode' => true, |
| 180 |
] ], |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Test element properties methods. |
| 186 |
* |
| 187 |
* @covers ::assertElt |
| 188 |
* @covers ::attributes |
| 189 |
* @covers ::isMetaDataTag |
| 190 |
* @dataProvider provideElementProperties |
| 191 |
*/ |
| 192 |
public function testElementProperties( string $html, array $props ) { |
| 193 |
$doc = DOMUtils::parseHTML( $html ); |
| 194 |
$sel = $props['selector'] ?? 'body > *'; |
| 195 |
$node = DOMCompat::querySelector( $doc, $sel ); |
| 196 |
DOMUtils::assertElt( $node ); |
| 197 |
|
| 198 |
$methods = [ |
| 199 |
'attributes', |
| 200 |
'isMetaDataTag', |
| 201 |
]; |
| 202 |
$expected = []; |
| 203 |
$actual = []; |
| 204 |
foreach ( $methods as $m ) { |
| 205 |
$e = $props[$m] ?? false; |
| 206 |
if ( $e ) { |
| 207 |
$expected[$m] = $e; |
| 208 |
} |
| 209 |
$a = DOMUtils::$m( $node ); |
| 210 |
if ( $a ) { |
| 211 |
$actual[$m] = $a; |
| 212 |
} |
| 213 |
} |
| 214 |
$this->assertSame( $expected, $actual ); |
| 215 |
} |
| 216 |
|
| 217 |
public function provideElementProperties() { |
| 218 |
return [ |
| 219 |
[ '<a href="xyz">foo<!--bar--></a>', [ |
| 220 |
'attributes' => [ 'href' => 'xyz' ], |
| 221 |
] ], |
| 222 |
[ '<link rel="foo" />', [ |
| 223 |
'attributes' => [ 'rel' => 'foo' ], |
| 224 |
'isMetaDataTag' => true, |
| 225 |
] ], |
| 226 |
[ '<base href="//foo/" />', [ |
| 227 |
'attributes' => [ 'href' => '//foo/' ], |
| 228 |
'isMetaDataTag' => true, |
| 229 |
] ], |
| 230 |
[ '<meta name="foo" />', [ |
| 231 |
'attributes' => [ 'name' => 'foo' ], |
| 232 |
'isMetaDataTag' => true, |
| 233 |
] ], |
| 234 |
[ '<span xmlns="test" class="foo">bar</span>', [ |
| 235 |
'attributes' => [ 'xmlns' => 'test', 'class' => 'foo' ], |
| 236 |
] ], |
| 237 |
]; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @covers ::addRel |
| 242 |
* @covers ::addTypeOf |
| 243 |
* @covers ::hasRel |
| 244 |
* @covers ::hasTypeOf |
| 245 |
* @covers ::matchRel |
| 246 |
* @covers ::matchTypeOf |
| 247 |
* @covers ::removeRel |
| 248 |
* @covers ::removeTypeOf |
| 249 |
* @dataProvider provideMultivalAttr |
| 250 |
*/ |
| 251 |
public function testMultivalAttr( string $which ) { |
| 252 |
$addMethod = "add$which"; |
| 253 |
$hasMethod = "has$which"; |
| 254 |
$matchMethod = "match$which"; |
| 255 |
$removeMethod = "remove$which"; |
| 256 |
|
| 257 |
$doc = DOMUtils::parseHtml( '<span>' ); |
| 258 |
$el = DOMCompat::querySelector( $doc, 'span' ); |
| 259 |
|
| 260 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 261 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 262 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, '0' ) ); |
| 263 |
$this->assertSame( null, DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 264 |
DOMUtils::$addMethod( $el, 'foo' ); |
| 265 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 266 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 267 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, '0' ) ); |
| 268 |
$this->assertSame( 'foo', DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 269 |
DOMUtils::$addMethod( $el, 'bar' ); |
| 270 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 271 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 272 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, '0' ) ); |
| 273 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 274 |
DOMUtils::$addMethod( $el, '0' ); # check that a falsy value is ok |
| 275 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 276 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 277 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 278 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 279 |
DOMUtils::$addMethod( $el, '' ); # should be a no-op |
| 280 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 281 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 282 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 283 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 284 |
$this->assertSame( [ 'foo bar 0' ], array_values( DOMUtils::attributes( $el ) ) ); |
| 285 |
DOMUtils::$addMethod( $el, ' ' ); # should be a no-op |
| 286 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 287 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 288 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 289 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 290 |
$this->assertSame( [ 'foo bar 0' ], array_values( DOMUtils::attributes( $el ) ) ); |
| 291 |
DOMUtils::$removeMethod( $el, '' ); # should be a no-op |
| 292 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 293 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 294 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 295 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 296 |
$this->assertSame( [ 'foo bar 0' ], array_values( DOMUtils::attributes( $el ) ) ); |
| 297 |
DOMUtils::$removeMethod( $el, ' ' ); # should be a no-op |
| 298 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 299 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 300 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 301 |
$this->assertNotNull( DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 302 |
DOMUtils::$removeMethod( $el, 'foo' ); |
| 303 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 304 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 305 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 306 |
$this->assertSame( 'bar', DOMUtils::$matchMethod( $el, '/^(fo|ba)/' ) ); |
| 307 |
DOMUtils::$removeMethod( $el, 'bar' ); |
| 308 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 309 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 310 |
$this->assertSame( true, DOMUtils::$hasMethod( $el, '0' ) ); |
| 311 |
$this->assertSame( '0', DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 312 |
DOMUtils::$removeMethod( $el, '0' ); |
| 313 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'foo' ) ); |
| 314 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, 'bar' ) ); |
| 315 |
$this->assertSame( false, DOMUtils::$hasMethod( $el, '0' ) ); |
| 316 |
$this->assertSame( null, DOMUtils::$matchMethod( $el, '/^(fo|ba|0)/' ) ); |
| 317 |
$this->assertSame( [], DOMUtils::attributes( $el ) ); |
| 318 |
} |
| 319 |
|
| 320 |
public function provideMultivalAttr() { |
| 321 |
return [ [ 'TypeOf' ], [ 'Rel' ] ]; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* @covers ::addTypeOf |
| 326 |
*/ |
| 327 |
public function testAddTypeOfPrepend() { |
| 328 |
$doc = DOMUtils::parseHtml( '<span>' ); |
| 329 |
$el = DOMCompat::querySelector( $doc, 'span' ); |
| 330 |
$this->assertSame( [], DOMUtils::attributes( $el ) ); |
| 331 |
DOMUtils::addTypeOf( $el, 'foo' ); |
| 332 |
$this->assertSame( [ 'typeof' => 'foo' ], DOMUtils::attributes( $el ) ); |
| 333 |
DOMUtils::addTypeOf( $el, 'bar' ); |
| 334 |
$this->assertSame( [ 'typeof' => 'foo bar' ], DOMUtils::attributes( $el ) ); |
| 335 |
DOMUtils::addTypeOf( $el, 'bat', true /* prepend */ ); |
| 336 |
$this->assertSame( [ 'typeof' => 'bat foo bar' ], DOMUtils::attributes( $el ) ); |
| 337 |
# these should all be no-ops |
| 338 |
DOMUtils::addTypeOf( $el, '' ); |
| 339 |
$this->assertSame( [ 'typeof' => 'bat foo bar' ], DOMUtils::attributes( $el ) ); |
| 340 |
DOMUtils::addTypeOf( $el, " \t " ); |
| 341 |
$this->assertSame( [ 'typeof' => 'bat foo bar' ], DOMUtils::attributes( $el ) ); |
| 342 |
DOMUtils::addTypeOf( $el, '', true ); |
| 343 |
$this->assertSame( [ 'typeof' => 'bat foo bar' ], DOMUtils::attributes( $el ) ); |
| 344 |
DOMUtils::addTypeOf( $el, " \t ", true ); |
| 345 |
$this->assertSame( [ 'typeof' => 'bat foo bar' ], DOMUtils::attributes( $el ) ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @covers ::hasClass |
| 350 |
*/ |
| 351 |
public function testHasClass() { |
| 352 |
$doc = DOMUtils::parseHtml( '<span class="0">' ); |
| 353 |
$el = DOMCompat::querySelector( $doc, 'span' ); |
| 354 |
$this->assertTrue( DOMUtils::hasClass( $el, '\\d' ) ); |
| 355 |
} |
| 356 |
|
| 357 |
} |