x
HTML; if ( $withTplInfo || $processAttrEmbeddedHTML ) { $doc = ContentUtils::createAndLoadDocument( $html ); } else { $doc = DOMCompat::newDocument( true ); $doc->loadHTML( $html ); } $state = new DTState( $env, [], true ); $trace = []; $traverser = new DOMTraverser( $withTplInfo, $processAttrEmbeddedHTML ); $traverser->addHandler( $nodeName, $callback ); $traverser->addHandler( null, static function ( Node $node, DTState $state ) use ( &$trace ) { if ( $node instanceof Element && $node->hasAttribute( 'id' ) ) { $trace[] = DOMCompat::getAttribute( $node, 'id' ); } return true; } ); $traverser->traverse( new ParsoidExtensionAPI( $env ), $doc->documentElement, $state ); $this->assertSame( $expectedTrace, $trace ); } public function provideTraverse() { $basicEnv = new MockEnv( [] ); $expectError = $this->getMockBuilder( MockEnv::class ) ->setConstructorArgs( [ [] ] ) ->onlyMethods( [ 'log' ] ) ->getMock(); $expectError->expects( $this->atLeastOnce() ) ->method( 'log' ) ->willReturnCallback( function ( $prefix ) { $this->assertSame( 'error', $prefix ); } ); $dontExpectError = $this->getMockBuilder( MockEnv::class ) ->setConstructorArgs( [ [] ] ) ->onlyMethods( [ 'log' ] ) ->getMock(); $dontExpectError->expects( $this->never() ) ->method( 'log' ); return [ 'basic' => [ 'callback' => function ( Node $node, ?DTState $state ) use ( $basicEnv ) { $this->assertTrue( $state->atTopLevel ); return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ], ], 'return true' => [ 'callback' => static function ( Node $node ) { return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ], ], 'return first child' => [ 'callback' => static function ( Node $node ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { return $node->firstChild; } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2_1', 'x1_2_2', 'x2', 'x2_1' ], ], 'return next sibling' => [ 'callback' => static function ( Node $node ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { return $node->nextSibling; } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_3', 'x2', 'x2_1' ], ], 'return null' => [ 'callback' => static function ( Node $node ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { return null; } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x2', 'x2_1' ], ], 'return another node' => [ 'callback' => static function ( Node $node ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { $newNode = $node->ownerDocument->createElement( 'div' ); $newNode->setAttribute( 'id', 'new' ); return $newNode; } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'new', 'x2', 'x2_1' ], ], 'name filter' => [ 'callback' => static function ( Node $node ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { return null; } return true; }, 'nodeName' => 'div', 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ], ], 'not traversing with tplinfo' => [ 'callback' => function ( Node $node, DTState $state ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) { $this->assertTrue( $state->tplInfo === null ); } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ], ], 'traversing with tplinfo' => [ 'callback' => function ( Node $node, DTState $state ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) { $this->assertTrue( $state->tplInfo->first === $node ); } if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2' ) { $this->assertTrue( $state->tplInfo->last === $node ); } if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_2_1' ) { $this->assertTrue( $state->tplInfo !== null ); } if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_3' ) { $this->assertTrue( $state->tplInfo === null ); } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1' ], 'withTplInfo' => true, ], 'not traversing with tplinfo, with embedded html' => [ 'callback' => function ( Node $node, DTState $state ) { if ( $node instanceof Element && DOMCompat::getAttribute( $node, 'id' ) === 'x1_1' ) { $this->assertTrue( $state->tplInfo === null ); } return true; }, 'nodeName' => null, 'env' => $basicEnv, 'expectedTrace' => [ 'x1', 'x1_1', 'x1_2', 'x1_2_1', 'x1_2_2', 'x1_3', 'x2', 'x2_1', 'e_span' ], false, true /* process attribute embedded html */ ], ]; } }