Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Utils/DOMCompatTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use Wikimedia\Parsoid\DOM\Document;
6 use Wikimedia\Parsoid\DOM\Element;
7 use Wikimedia\Parsoid\DOM\Node;
8 use Wikimedia\Parsoid\Utils\DOMCompat;
9 use Wikimedia\Parsoid\Utils\DOMCompat\TokenList;
10 use Wikimedia\Parsoid\Utils\DOMUtils;
11 use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @coversDefaultClass \Wikimedia\Parsoid\Utils\DOMCompat
16 */
17 class DOMCompatTest extends \PHPUnit\Framework\TestCase {
18
19 /**
20 * @covers ::getBody()
21 */
22 public function testGetBody() {
23 $html = '<html><head><title>Foo</title></head><body id="x"><div>y</div></body></html>';
24 $doc = DOMCompat::newDocument( true );
25 $doc->loadHTML( $html );
26 $body = DOMCompat::getBody( $doc );
27 $this->assertSameNode( $body, $doc->getElementById( 'x' ) );
28
29 $html = '<html><head><title>Foo</title></head></html>';
30 $doc = DOMCompat::newDocument( true );
31 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
32 $body = DOMCompat::getBody( $doc );
33 $this->assertNull( $body );
34 }
35
36 /**
37 * @covers ::getHead()
38 */
39 public function testGetHead() {
40 $html = '<html><head id="x"><title>Foo</title></head><body><div>y</div></body></html>';
41 $doc = DOMCompat::newDocument( true );
42 $doc->loadHTML( $html );
43 $head = DOMCompat::getHead( $doc );
44 $this->assertSameNode( $head, $doc->getElementById( 'x' ) );
45
46 $html = '<html><body><div>y</div></body></html>';
47 $doc = DOMCompat::newDocument( true );
48 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
49 $head = DOMCompat::getHead( $doc );
50 $this->assertNull( $head );
51 }
52
53 /**
54 * @covers ::getTitle()
55 */
56 public function testGetTitle() {
57 $html = '<html><head><title>Foo</title></head><body><div>y</div></body></html>';
58 $doc = DOMCompat::newDocument( true );
59 $doc->loadHTML( $html );
60 $title = DOMCompat::getTitle( $doc );
61 $this->assertSame( 'Foo', $title );
62
63 $html = '<html><head><title> Foo&#9;Bar </title></head><body><div>y</div></body></html>';
64 $doc = DOMCompat::newDocument( true );
65 $doc->loadHTML( $html );
66 $title = DOMCompat::getTitle( $doc );
67 $this->assertSame( 'Foo Bar', $title );
68
69 $html = '<html><body><div>y</div></body></html>';
70 $doc = DOMCompat::newDocument( true );
71 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
72 $title = DOMCompat::getTitle( $doc );
73 $this->assertSame( '', $title );
74 }
75
76 /**
77 * @covers ::setTitle()
78 */
79 public function testSetTitle() {
80 // modify <title> if it is exist
81 $html = '<html><head><title>Foo</title></head><body><div>y</div></body></html>';
82 $doc = DOMCompat::newDocument( true );
83 $doc->loadHTML( $html );
84 DOMCompat::setTitle( $doc, 'Bar' );
85 $title = $doc->getElementsByTagName( 'title' )->item( 0 );
86 $this->assertInstanceOf( Element::class, $title );
87 $this->assertSame( 'Bar', $title->textContent );
88
89 // ...even if it is not in <head>
90 $html = '<html><body><title>Foo</title><div>y</div></body></html>';
91 $doc = DOMCompat::newDocument( true );
92 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
93 DOMCompat::setTitle( $doc, 'Bar' );
94 $title = $doc->getElementsByTagName( 'title' )->item( 0 );
95 $this->assertInstanceOf( Element::class, $title );
96 $this->assertSame( 'Bar', $title->textContent );
97
98 // append it to <head> if it does not exist
99 $html = '<html><head><style></style></head><body><div>y</div></body></html>';
100 $doc = DOMCompat::newDocument( true );
101 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
102 DOMCompat::setTitle( $doc, 'Bar' );
103 $title = $doc->getElementsByTagName( 'title' )->item( 0 );
104 $this->assertInstanceOf( Element::class, $title );
105 $this->assertSame( 'Bar', $title->textContent );
106 $this->assertNotSame( $title, $title->parentNode->firstChild );
107 $this->assertSame( $title, $title->parentNode->lastChild );
108
109 // bail out if there's no <head>
110 $html = '<html><body><div>y</div></body></html>';
111 $doc = DOMCompat::newDocument( true );
112 $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED );
113 DOMCompat::setTitle( $doc, 'Bar' );
114 $head = $doc->getElementsByTagName( 'head' )->item( 0 );
115 $this->assertNull( $head );
116 $title = $doc->getElementsByTagName( 'title' )->item( 0 );
117 $this->assertNull( $title );
118 }
119
120 /**
121 * @covers ::getParentElement()
122 */
123 public function testGetParentElement() {
124 // has parent element
125 $html = '<html><body id="x"><div>1</div><div id="y">2</div></body></html>';
126 $doc = DOMCompat::newDocument( true );
127 $doc->loadHTML( $html );
128 $element = $doc->getElementById( 'y' );
129 $this->assertSame( $doc->getElementById( 'x' ), DOMCompat::getParentElement( $element ) );
130
131 // no parent element
132 $html = '<html></html>';
133 $doc = DOMCompat::newDocument( true );
134 $doc->loadHTML( $html );
135 $this->assertNull( DOMCompat::getParentElement( $doc->documentElement ) );
136
137 // TODO is it possible to have a node with non-Element parent element?
138 }
139
140 /**
141 * @covers ::getElementById()
142 */
143 public function testGetElementById() {
144 $html = '<html><body><div id="x"></div></body></html>';
145 $doc = DOMCompat::newDocument( true );
146 $doc->loadHTML( $html );
147
148 $x = $doc->getElementById( 'x' );
149 $this->assertSame( $x, DOMCompat::getElementById( $doc, 'x' ) );
150
151 $x->parentNode->removeChild( $x );
152
153 // https://bugs.php.net/bug.php?id=77686
154 if ( version_compare( PHP_VERSION, '8.1.20', '>' ) ) {
155 $this->assertNull( $doc->getElementById( 'x' ) );
156 } else {
157 $this->assertSame( $x, $doc->getElementById( 'x' ) );
158 $this->assertNull( DOMCompat::getElementById( $doc, 'x' ) );
159 }
160 }
161
162 /**
163 * @covers ::getLastElementChild()
164 */
165 public function testGetLastElementChild() {
166 $html = '<html><body><div id="a"></div>1<div id="b"></div><div id="c"></div>3</body></html>';
167 $doc = DOMCompat::newDocument( true );
168 $doc->loadHTML( $html );
169 $html = $doc->getElementsByTagName( 'html' )->item( 0 );
170 '@phan-var Element $html'; /** @var Element $html */
171 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
172 '@phan-var Element $body'; /** @var Element $body */
173 $this->assertSameNode( $doc->getElementById( 'c' ), DOMCompat::getLastElementChild( $body ) );
174 $this->assertSameNode( $html, DOMCompat::getLastElementChild( $doc ) );
175 $this->assertSameNode( $body, DOMCompat::getLastElementChild( $html ) );
176
177 $html = '<html><body></body></html>';
178 $doc = DOMCompat::newDocument( true );
179 $doc->loadHTML( $html );
180 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
181 '@phan-var Element $body'; /** @var Element $body */
182 $this->assertNull( DOMCompat::getLastElementChild( $body ) );
183 }
184
185 /**
186 * @dataProvider provideQuerySelector
187 * @covers ::querySelector()
188 * @param string $html
189 * @param string $selector
190 * @param callback|null $contextCallback
191 * @param string|null $expectedDataIds
192 */
193 public function testQuerySelector( $html, $selector, $contextCallback, $expectedDataIds ) {
194 $doc = DOMCompat::newDocument( true );
195 $doc->loadHTML( $html );
196 $context = $contextCallback ? $contextCallback( $doc ) : $doc->documentElement;
197 $result = DOMCompat::querySelector( $context, $selector );
198
199 $expectedDataId = $expectedDataIds[0] ?? null;
200 $actualDataId = $result ? DOMCompat::getAttribute( $result, 'data-id' ) : null;
201 $this->assertSame( $expectedDataId, $actualDataId );
202 }
203
204 /**
205 * @dataProvider provideQuerySelector
206 * @covers ::querySelectorAll()
207 * @param string $html
208 * @param string $selector
209 * @param callback|null $contextCallback
210 * @param string|null $expectedDataIds
211 */
212 public function testQuerySelectorAll( $html, $selector, $contextCallback, $expectedDataIds ) {
213 $doc = DOMCompat::newDocument( true );
214 $doc->loadHTML( $html );
215 $context = $contextCallback ? $contextCallback( $doc ) : $doc->documentElement;
216 $results = DOMCompat::querySelectorAll( $context, $selector );
217
218 $actualDataIds = [];
219 foreach ( $results as $result ) {
220 $actualDataIds[] = DOMCompat::getAttribute( $result, 'data-id' );
221 }
222 $this->assertSame( $expectedDataIds, $actualDataIds );
223 }
224
225 public function provideQuerySelector() {
226 // TODO would be nicer to have separate HTML per test (maybe something like parser tests)
227 $html1 = <<<'HTML'
228 <html><body>
229 <div id="a" class="a" data-id="a">
230 <div class="b1 b2 b3" data-id="b">
231 <div rel="c" data-id="c" rel2="x">1</div>
232 <div data-id="empty-div"></div>
233 </div>
234 </div>'
235 <span rel2="x" data-id="span"></span>
236 <div class="e">
237 <div class="xxx" data-id="x1"></div>
238 </div>
239 <div class="e" id="ctx1">
240 <div>
241 <div class="xxx" data-id="x2"></div>
242 </div>
243 </div>
244 <div class="f" id="ctx2" data-id="f">
245 <div class="f xxx" data-id="x3"></div>
246 </div>
247 </body></html>
248 HTML;
249
250 return [
251 // simple selectors
252 'id' => [
253 'html' => $html1,
254 'selector' => '#a',
255 'context' => null,
256 'expectedDataId' => [ 'a' ],
257 ],
258 'simple class' => [
259 'html' => $html1,
260 'selector' => '.a',
261 'context' => null,
262 'expectedDataId' => [ 'a' ],
263 ],
264 'compound class string, start' => [
265 'html' => $html1,
266 'selector' => '.b1',
267 'context' => null,
268 'expectedTagHtmls' => [ 'b' ],
269 ],
270 'compound class string, mid' => [
271 'html' => $html1,
272 'selector' => '.b2',
273 'context' => null,
274 'expectedTagHtmls' => [ 'b' ],
275 ],
276 'compound class string, end' => [
277 'html' => $html1,
278 'selector' => '.b3',
279 'context' => null,
280 'expectedTagHtmls' => [ 'b' ],
281 ],
282 'attribute' => [
283 'html' => $html1,
284 'selector' => '[rel=c]',
285 'context' => null,
286 'expectedTagHtmls' => [ 'c' ],
287 ],
288 'attribute #2' => [
289 'html' => $html1,
290 'selector' => '[rel2=x]',
291 'context' => null,
292 'expectedTagHtmls' => [ 'c', 'span' ],
293 ],
294 'attribute, case sensitive' => [
295 'html' => $html1,
296 'selector' => '[rel=C]',
297 'context' => null,
298 'expectedTagHtmls' => [],
299 ],
300 /* not yet supported in css-parser
301 'attribute, case insensitive' => [
302 'html' => $html1,
303 'selector' => '[rel="C"i]',
304 'context' => null,
305 'expectedTagHtmls' => [],
306 ],
307 */
308 'attribute word' => [
309 'html' => $html1,
310 'selector' => '[class~=b2]',
311 'context' => null,
312 'expectedTagHtmls' => [ 'b' ],
313 ],
314 'attribute word, case sensitive' => [
315 'html' => $html1,
316 'selector' => '[class~=B2]',
317 'context' => null,
318 'expectedTagHtmls' => [],
319 ],
320 /* not yet supported in css-parser
321 'attribute word, case insensitive' => [
322 'html' => $html1,
323 'selector' => '[class~="B2"i]',
324 'context' => null,
325 'expectedTagHtmls' => [ 'b' ],
326 ],
327 */
328 'tag' => [
329 'html' => $html1,
330 'selector' => 'span',
331 'context' => null,
332 'expectedTagHtmls' => [ 'span' ],
333 ],
334 ':empty' => [
335 'html' => $html1,
336 'selector' => ':empty',
337 'context' => null,
338 'expectedTagHtmls' => [ 'empty-div', 'span', 'x1', 'x2', 'x3' ],
339 ],
340 'recursion' => [
341 'html' => $html1,
342 'selector' => '.f',
343 'context' => null,
344 'expectedTagHtmls' => [ 'f', 'x3' ],
345 ],
346
347 // simple selector sequence
348 'id + class' => [
349 'html' => $html1,
350 'selector' => '#a.a',
351 'context' => null,
352 'expectedTagHtmls' => [ 'a' ],
353 ],
354 'id + class #2' => [
355 'html' => $html1,
356 'selector' => '.a#a',
357 'context' => null,
358 'expectedTagHtmls' => [ 'a' ],
359 ],
360 'multiple classes' => [
361 'html' => $html1,
362 'selector' => '.b2.b3',
363 'context' => null,
364 'expectedTagHtmls' => [ 'b' ],
365 ],
366 'element + attribute' => [
367 'html' => $html1,
368 'selector' => 'div[rel2=x]',
369 'context' => null,
370 'expectedTagHtmls' => [ 'c' ],
371 ],
372 'star + attribute' => [
373 'html' => $html1,
374 'selector' => '*[rel=c]',
375 'context' => null,
376 'expectedTagHtmls' => [ 'c' ],
377 ],
378
379 // selector
380 'child' => [
381 'html' => $html1,
382 'selector' => '.e > .xxx',
383 'context' => null,
384 'expectedTagHtmls' => [ 'x1' ],
385 ],
386 'descendant' => [
387 'html' => $html1,
388 'selector' => '.e .xxx',
389 'context' => null,
390 'expectedTagHtmls' => [ 'x1', 'x2' ],
391 ],
392
393 'selector group' => [
394 'html' => $html1,
395 'selector' => '#a, .e .xxx',
396 'context' => null,
397 'expectedTagHtmls' => [ 'a', 'x1', 'x2' ],
398 ],
399
400 // context
401 'context check' => [
402 'html' => $html1,
403 'selector' => '.xxx',
404 'context' => null,
405 'expectedTagHtmls' => [ 'x1', 'x2', 'x3' ],
406 ],
407 'does not select outside context' => [
408 'html' => $html1,
409 'selector' => '.xxx',
410 'context' => static function ( Document $doc ) {
411 return $doc->getElementById( 'ctx1' );
412 },
413 'expectedTagHtmls' => [ 'x2' ],
414 ],
415 'does not select context' => [
416 'html' => $html1,
417 'selector' => '.f',
418 'context' => static function ( Document $doc ) {
419 return $doc->getElementById( 'ctx2' );
420 },
421 'expectedTagHtmls' => [ 'x3' ],
422 ],
423 ];
424 }
425
426 /**
427 * @covers ::getPreviousElementSibling()
428 */
429 public function testGetPreviousElementSibling() {
430 $html = '<html><body>0<div id="a"></div>1<div id="b"></div><div id="c"></div>3</body></html>';
431 $doc = DOMCompat::newDocument( true );
432 $doc->loadHTML( $html );
433 $a = $doc->getElementById( 'a' );
434 $b = $doc->getElementById( 'b' );
435 $c = $doc->getElementById( 'c' );
436 $this->assertNull( DOMCompat::getPreviousElementSibling( $a ) );
437 $this->assertSameNode( $a, DOMCompat::getPreviousElementSibling( $b ) );
438 $this->assertSameNode( $b, DOMCompat::getPreviousElementSibling( $c ) );
439 }
440
441 /**
442 * @covers ::getNextElementSibling()
443 */
444 public function testGetNextElementSibling() {
445 $html = '<html><body>0<div id="a"></div>1<div id="b"></div><div id="c"></div>3</body></html>';
446 $doc = DOMCompat::newDocument( true );
447 $doc->loadHTML( $html );
448 $a = $doc->getElementById( 'a' );
449 $b = $doc->getElementById( 'b' );
450 $c = $doc->getElementById( 'c' );
451 $this->assertSameNode( $b, DOMCompat::getNextElementSibling( $a ) );
452 $this->assertSameNode( $c, DOMCompat::getNextElementSibling( $b ) );
453 $this->assertNull( DOMCompat::getNextElementSibling( $c ) );
454 }
455
456 /**
457 * @covers ::remove()
458 */
459 public function testRemove() {
460 $html = '<html><body><div id="a"></div><div id="b"><span id="c">1</span></div>2</body></html>';
461 $doc = DOMCompat::newDocument( true );
462 $doc->loadHTML( $html );
463
464 // do not error when element has no parent
465 $elmt = $doc->createElement( 'div' );
466 DOMCompat::remove( $elmt );
467
468 $b = $doc->getElementById( 'b' );
469 $this->assertNotNull( $doc->getElementById( 'a' ) );
470 $this->assertNotNull( $doc->getElementById( 'b' ) );
471 $this->assertNotNull( $doc->getElementById( 'c' ) );
472 DOMCompat::remove( $b );
473 $this->assertNotNull( $doc->getElementById( 'a' ) );
474
475 $this->assertSame( '<html><body><div id="a"></div>2</body></html>',
476 DOMCompat::getOuterHTML( $doc->documentElement ) );
477
478 // https://bugs.php.net/bug.php?id=77686
479 if ( version_compare( PHP_VERSION, '8.1.20', '>' ) ) {
480 $this->assertNull( $doc->getElementById( 'b' ) );
481 $this->assertNull( $doc->getElementById( 'c' ) );
482 }
483 }
484
485 /**
486 * @covers ::getInnerHTML()
487 */
488 public function testGetInnerHTML() {
489 $html = '<html><body>0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3</body></html>';
490 $doc = DOMCompat::newDocument( true );
491 $doc->loadHTML( $html );
492 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
493 '@phan-var Element $body'; /** @var Element $body */
494 $b = $doc->getElementById( 'b' );
495 $this->assertSame( '<body>0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3</body>',
496 DOMCompat::getInnerHTML( $doc->documentElement ) );
497 $this->assertSame( '0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3',
498 DOMCompat::getInnerHTML( $body ) );
499 $this->assertSame( '2', DOMCompat::getInnerHTML( $b ) );
500 }
501
502 /**
503 * @covers ::setInnerHTML()
504 */
505 public function testSetInnerHTML() {
506 $html = '<html><body><div>1</div><div>2</div>3</body></html>';
507 $innerHtml = '<div>4</div><!-- 5 -->6';
508 $doc = DOMCompat::newDocument( true );
509 $doc->loadHTML( $html );
510 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
511 '@phan-var Element $body'; /** @var Element $body */
512 DOMCompat::setInnerHTML( $body, $innerHtml );
513 $this->assertSame( "<body>$innerHtml</body>", $doc->saveXML( $body, LIBXML_NSCLEAN ) );
514 $this->assertSame( '4',
515 $doc->getElementsByTagName( 'body' )->item( 0 )->firstChild->textContent );
516 }
517
518 /**
519 * @covers ::getOuterHTML()
520 */
521 public function testGetOuterHTML() {
522 $html = '<html><body>0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3</body></html>';
523 $doc = DOMCompat::newDocument( true );
524 $doc->loadHTML( $html );
525 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
526 '@phan-var Element $body'; /** @var Element $body */
527 $b = $doc->getElementById( 'b' );
528 $this->assertSame(
529 '<html><body>0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3</body></html>',
530 DOMCompat::getOuterHTML( $doc->documentElement ) );
531 $this->assertSame( '<body>0<div id="a"></div>1<div id="b">2</div><div id="c"></div>3</body>',
532 DOMCompat::getOuterHTML( $body ) );
533 $this->assertSame( '<div id="b">2</div>', DOMCompat::getOuterHTML( $b ) );
534 }
535
536 /**
537 * @covers ::getClassList()
538 * @covers \Wikimedia\Parsoid\Utils\DOMCompat\TokenList
539 */
540 public function testGetClassList() {
541 $html = '<html><body><div id="x" class=" a b&#9;c "></div></body></html>';
542 $doc = DOMCompat::newDocument( true );
543 $doc->loadHTML( $html );
544 $x = $doc->getElementById( 'x' );
545 $classList = DOMCompat::getClassList( $x );
546 $this->assertInstanceOf( TokenList::class, $classList );
547 $this->assertCount( 3, $classList );
548 $this->assertTrue( $classList->contains( 'a' ) );
549 $this->assertTrue( $classList->contains( 'b' ) );
550 $this->assertTrue( $classList->contains( 'c' ) );
551 $this->assertFalse( $classList->contains( 'd' ) );
552 $this->assertSame( [ 'a', 'b', 'c' ], iterator_to_array( $classList ) );
553 // make sure rewinding works
554 $this->assertSame( [ 'a', 'b', 'c' ], iterator_to_array( $classList ) );
555 $classList->add( 'd' );
556 $classList->remove( 'b' );
557 $this->assertFalse( $classList->contains( 'b' ) );
558 $this->assertTrue( $classList->contains( 'd' ) );
559 $this->assertSame( [ 'a', 'c', 'd' ], iterator_to_array( $classList ) );
560 $this->assertSame( '<div id="x" class="a c d"></div>', DOMCompat::getOuterHTML( $x ) );
561 $x->setAttribute( 'class', 'e f g' );
562 $this->assertSame( [ 'e', 'f', 'g' ], iterator_to_array( $classList ) );
563 $classList->add( 'a' );
564 $classList->remove( 'g' );
565 $this->assertSame( 'e f a', DOMCompat::getAttribute( $x, 'class' ) );
566 }
567
568 /**
569 * @covers ::stripAndCollapseASCIIWhitespace()
570 * @dataProvider provideStripAndCollapseASCIIWhitespace
571 */
572 public function testStripAndCollapseASCIIWhitespace( $input, $expectedOutput ) {
573 $domCompat = TestingAccessWrapper::newFromClass( DOMCompat::class );
574 $actualOutput = $domCompat->stripAndCollapseASCIIWhitespace( $input );
575 $this->assertSame( $expectedOutput, $actualOutput );
576 }
577
578 public function provideStripAndCollapseASCIIWhitespace() {
579 return [
580 [ ' foo ', 'foo' ],
581 [ " \n foo \t \n bar ", 'foo bar' ],
582 [ "foo\r\fbar", 'foo bar' ],
583 [ " \n ", '' ],
584 ];
585 }
586
587 private function assertSameNode( Node $expected, Node $actual, $message = '' ) {
588 if ( !$expected->isSameNode( $actual ) ) {
589 // try to give a somewhat informative error
590 $actualHtml = XMLSerializer::serialize( $actual )['html'];
591 $expectedHtml = XMLSerializer::serialize( $expected )['html'];
592 $this->assertSame( $expectedHtml, $actualHtml, $message );
593 $this->assertSame( $expected, $actual, $message );
594 } else {
595 $this->assertTrue( true );
596 }
597 }
598
599 /**
600 * See https://bugs.php.net/bug.php?id=78221 for the upstream bug
601 * we're working around here.
602 * @covers ::normalize()
603 * @dataProvider provideNormalize
604 */
605 public function testNormalize( $textNodeCount, $interleaveSpan, $expectedNodeCount ) {
606 $doc = DOMCompat::newDocument( true );
607 $doc->loadXML( "<html><body></body></html>" );
608 $body = $doc->getElementsByTagName( 'body' )->item( 0 );
609 $div = $doc->createElement( 'div' );
610 $body->appendChild( $div );
611 while ( $textNodeCount > 0 ) {
612 $div->appendChild( $doc->createTextNode( '' ) );
613 if ( $interleaveSpan ) {
614 $div->appendChild( $doc->createElement( 'span' ) );
615 }
616 $textNodeCount--;
617 }
618
619 DOMCompat::normalize( $body );
620 $this->assertSame( $expectedNodeCount, $div->childNodes->length );
621 }
622
623 public function provideNormalize() {
624 return [
625 [
626 "textNodeCount" => 1,
627 "interleaveSpan" => false,
628 "expected" => 0
629 ],
630 [
631 "textNodeCount" => 5,
632 "interleaveSpan" => false,
633 "expected" => 0
634 ],
635 [
636 "textNodeCount" => 1,
637 "interleaveSpan" => true,
638 "expected" => 1
639 ],
640 [
641 "textNodeCount" => 5,
642 "interleaveSpan" => true,
643 "expected" => 5
644 ],
645 ];
646 }
647
648 /**
649 * @covers ::setIdAttribute
650 */
651 public function testSetIdAttribute() {
652 $doc = DOMUtils::parseHTML( "<p class=xyz>Hello, world</p>" );
653 $head = DOMCompat::getHead( $doc );
654 $elmt = $doc->createElement( 'div' );
655 $head->appendChild( $elmt );
656 DOMCompat::setIdAttribute( $elmt, "this-is-an-id" );
657
658 // Note we're testing the "fast path" native implementation here,
659 // not the workaround version in DOMCompat::getElementById()
660 $q = $doc->getElementById( 'this-is-an-id' );
661 $this->assertNotEquals( null, $q );
662 $this->assertEquals( 'this-is-an-id', DOMCompat::getAttribute( $q, 'id' ) );
663 }
664 }