diff options
author | Gergő Tisza <tgr.huwiki@gmail.com> | 2022-05-08 15:50:08 +0200 |
---|---|---|
committer | Gergő Tisza <tgr.huwiki@gmail.com> | 2022-05-08 15:55:00 +0200 |
commit | 2557f96cb75e25c4c8e3de7febcbfe4aece42bbf (patch) | |
tree | 11154fe93e8c741c880658b1828de6f6a42e583f /tests/phpunit/unit/includes/HtmlHelperTest.php | |
parent | a8938359a9560759f145ea935ac0b011ca740be2 (diff) | |
download | mediawikicore-2557f96cb75e25c4c8e3de7febcbfe4aece42bbf.tar.gz mediawikicore-2557f96cb75e25c4c8e3de7febcbfe4aece42bbf.zip |
Add HtmlHelper::modifyElements() for small HTML modifications
Adds HtmlHelper, which is intended to be a static utility class
for doing simple modifications to HTML strings in a safe way,
without exposing the caller to the complex Remex interface.
Currently only has one method, which is mainly useful for
modifying element attributes.
The code is largely based on the Wikibase FormatEntities class.
Bug: T217850
Change-Id: I45db9e61e47eb69df32a167d9d1dd146a8719676
Diffstat (limited to 'tests/phpunit/unit/includes/HtmlHelperTest.php')
-rw-r--r-- | tests/phpunit/unit/includes/HtmlHelperTest.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/phpunit/unit/includes/HtmlHelperTest.php b/tests/phpunit/unit/includes/HtmlHelperTest.php new file mode 100644 index 000000000000..bf432a7d6fa2 --- /dev/null +++ b/tests/phpunit/unit/includes/HtmlHelperTest.php @@ -0,0 +1,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 ); + } + +} |