namespace === HTMLData::NS_HTML && $node->name === 'a' && isset( $node->attrs['href'] ); }; $modifyCallbackInPlace = static function ( SerializerNode $node ) { $node->attrs['href'] = 'https://tracker.org/' . $node->attrs['href']; return $node; }; $input = '
Foo bar baz
'; $expectedOutput = 'Foo bar baz
'; $output = HtmlHelper::modifyElements( $input, $shouldModifyCallback, $modifyCallbackInPlace ); $this->assertSame( $expectedOutput, $output ); $modifyCallbackNew = static function ( SerializerNode $node ) { $href = 'https://tracker.org/' . $node->attrs['href']; $newNode = new SerializerNode( $node->id, $node->parentId, $node->namespace, $node->name, new PlainAttributes( [ 'href' => $href ] ), $node->void ); $node->attrs['href'] = 'https://tracker.org/' . $node->attrs['href']; return $newNode; }; $output = HtmlHelper::modifyElements( $input, $shouldModifyCallback, $modifyCallbackNew ); $this->assertSame( $expectedOutput, $output ); // Check the "legacy compatibility" mode, for void elements like $input = ""; $shouldModifyCallback = static function ( SerializerNode $node ) { return false; }; // HTML5 output $expectedOutput = ''; $output = HtmlHelper::modifyElements( $input, $shouldModifyCallback, $modifyCallbackInPlace, true ); $this->assertSame( $expectedOutput, $output ); // "Legacy" output $expectedOutput = ''; $output = HtmlHelper::modifyElements( $input, $shouldModifyCallback, $modifyCallbackInPlace, false ); $this->assertSame( $expectedOutput, $output ); } }