blob: bf432a7d6fa2fddb6542363dc195959f11197aa9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<?php
namespace MediaWiki\Tests\Unit;
use MediaWiki\HtmlHelper;
use MediaWikiUnitTestCase;
use Wikimedia\RemexHtml\HTMLData;
use Wikimedia\RemexHtml\Serializer\SerializerNode;
use Wikimedia\RemexHtml\Tokenizer\PlainAttributes;
class HtmlHelperTest extends MediaWikiUnitTestCase {
/**
* @covers \MediaWiki\HtmlHelper::modifyElements
*/
public function testModifyElements() {
$shouldModifyCallback = static function ( SerializerNode $node ) {
return $node->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 = '<p>Foo <a href="https://example.org/">bar</a> baz</p>';
$expectedOutput = '<p>Foo <a href="https://tracker.org/https://example.org/">bar</a> baz</p>';
$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 );
}
}
|