Debian Codegraph @

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

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use Wikimedia\Parsoid\Config\Env;
6 use Wikimedia\Parsoid\DOM\Element;
7 use Wikimedia\Parsoid\DOM\Node;
8 use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
9 use Wikimedia\Parsoid\Mocks\MockEnv;
10 use Wikimedia\Parsoid\Utils\ContentUtils;
11 use Wikimedia\Parsoid\Utils\DOMCompat;
12 use Wikimedia\Parsoid\Utils\DOMTraverser;
13 use Wikimedia\Parsoid\Utils\DTState;
14
15 class DOMTraverserTest extends \PHPUnit\Framework\TestCase {
16
17 /**
18 * @phpcs:disable Generic.Files.LineLength.TooLong
19 * @dataProvider provideTraverse
20 * @covers \Wikimedia\Parsoid\Utils\DOMTraverser::addHandler
21 * @covers \Wikimedia\Parsoid\Utils\DOMTraverser::traverse
22 */
23 public function testTraverse(
24 callable $callback, ?string $nodeName, Env $env, array $expectedTrace,
25 bool $withTplInfo = false, bool $processAttrEmbeddedHTML = false
26 ) {
27 $html = <<<'HTML'
28 <html><body>
29 <div id="x1">
30 <div id="x1_1" typeof="mw:Transclusion" about="#mwt1">
31 </div><blockquote id="x1_2" about="#mwt1">
32 <div id="x1_2_1"></div>
33 <div id="x1_2_2"></div>
34 </blockquote>
35 <div id="x1_3"></div>
36 </div>
37 <div id="x2">
38 <div id="x2_1"></div>
39 </div>
40 <!--dummy expanded attrs representation to exercise embedded html code-->
41 <!--no id on this div so we don't have to update all tests, but add id on the embedded span-->
42 <div typeof='mw:ExpandedAttrs' data-mw='{"attribs": [[{"txt": "foo"},{"html": "<span id=\"e_span\">x</span>"}]]}'>x</div>
43 </body></html>
44 HTML;
45 if ( $withTplInfo || $processAttrEmbeddedHTML ) {
46 $doc = ContentUtils::createAndLoadDocument( $html );
47 } else {
48 $doc = DOMCompat::newDocument( true );
49 $doc->loadHTML( $html );
50 }
51
52 $state = new DTState( $env, [], true );
53 $trace = [];
54
55 $traverser = new DOMTraverser( $withTplInfo, $processAttrEmbeddedHTML );
56 $traverser->addHandler( $nodeName, $callback );
57 $traverser->addHandler( null, static function ( Node $node, DTState $state ) use ( &$trace ) {
58 if ( $node instanceof Element && $node->hasAttribute( 'id' ) ) {
59 $trace[] = DOMCompat::getAttribute( $node, 'id' );
60 }
61 return true;
62 } );
63 $traverser->traverse( new ParsoidExtensionAPI( $env ), $doc->documentElement, $state );
64 $this->assertSame( $expectedTrace, $trace );
65 }
66
67 public function provideTraverse() {
68 $basicEnv = new MockEnv( [] );
69
70 $expectError = $this->getMockBuilder( MockEnv::class )
71 ->setConstructorArgs( [ [] ] )
72 ->onlyMethods( [ 'log' ] )
73 ->getMock();
74 $expectError->expects( $this->atLeastOnce() )
75 ->method( 'log' )
76 ->willReturnCallback( function ( $prefix ) {
77 $this->assertSame( 'error', $prefix );
78 } );
79 $dontExpectError = $this->getMockBuilder( MockEnv::class )
80 ->setConstructorArgs( [ [] ] )
81 ->onlyMethods( [ 'log' ] )
82 ->getMock();
83 $dontExpectError->expects( $this->never() )
84 ->method( 'log' );
85
86 return [
87 'basic' => [
88 'callback' => function ( Node $node, ?DTState $state ) use ( $basicEnv ) {
89 $this->assertTrue( $state->atTopLevel );
90 return true;
91 },
92 'nodeName' => null,
93 'env' => $basicEnv,
94 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ],
95 ],
96 'return true' => [
97 'callback' => static function ( Node $node ) {
98 return true;
99 },
100 'nodeName' => null,
101 'env' => $basicEnv,
102 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ],
103 ],
104 'return first child' => [
105 'callback' => static function ( Node $node ) {
106 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
107 return $node->firstChild;
108 }
109 return true;
110 },
111 'nodeName' => null,
112 'env' => $basicEnv,
113 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2_1', 'x1_2_2', 'x2', 'x2_1' ],
114 ],
115 'return next sibling' => [
116 'callback' => static function ( Node $node ) {
117 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
118 return $node->nextSibling;
119 }
120 return true;
121 },
122 'nodeName' => null,
123 'env' => $basicEnv,
124 'expectedTrace' => [ 'x1', 'x1_1', 'x1_3', 'x2', 'x2_1' ],
125 ],
126 'return null' => [
127 'callback' => static function ( Node $node ) {
128 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
129 return null;
130 }
131 return true;
132 },
133 'nodeName' => null,
134 'env' => $basicEnv,
135 'expectedTrace' => [ 'x1', 'x1_1', 'x2', 'x2_1' ],
136 ],
137 'return another node' => [
138 'callback' => static function ( Node $node ) {
139 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
140 $newNode = $node->ownerDocument->createElement( 'div' );
141 $newNode->setAttribute( 'id', 'new' );
142 return $newNode;
143 }
144 return true;
145 },
146 'nodeName' => null,
147 'env' => $basicEnv,
148 'expectedTrace' => [ 'x1', 'x1_1', 'new', 'x2', 'x2_1' ],
149 ],
150 'name filter' => [
151 'callback' => static function ( Node $node ) {
152 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
153 return null;
154 }
155 return true;
156 },
157 'nodeName' => 'div',
158 'env' => $basicEnv,
159 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ],
160 ],
161 'not traversing with tplinfo' => [
162 'callback' => function ( Node $node, DTState $state ) {
163 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) {
164 $this->assertTrue( $state->tplInfo === null );
165 }
166 return true;
167 },
168 'nodeName' => null,
169 'env' => $basicEnv,
170 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ],
171 ],
172 'traversing with tplinfo' => [
173 'callback' => function ( Node $node, DTState $state ) {
174 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) {
175 $this->assertTrue( $state->tplInfo->first === $node );
176 }
177 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) {
178 $this->assertTrue( $state->tplInfo->last === $node );
179 }
180 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2_1' ) {
181 $this->assertTrue( $state->tplInfo !== null );
182 }
183 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_3' ) {
184 $this->assertTrue( $state->tplInfo === null );
185 }
186 return true;
187 },
188 'nodeName' => null,
189 'env' => $basicEnv,
190 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ],
191 'withTplInfo' => true,
192 ],
193 'not traversing with tplinfo, with embedded html' => [
194 'callback' => function ( Node $node, DTState $state ) {
195 if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) {
196 $this->assertTrue( $state->tplInfo === null );
197 }
198 return true;
199 },
200 'nodeName' => null,
201 'env' => $basicEnv,
202 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1', 'e_span' ],
203 false,
204 true /* process attribute embedded html */
205 ],
206 ];
207 }
208 }