mediawiki-extensions-Discus.../tests/phpunit/CommentUtilsTest.php
Bartosz Dziewoński 8f42c74985 Fix skipping to the end of paragraph, now it considers nested tags
Add yet another tree walking utility: CommentUtils::linearWalk().
Unlike TreeWalker, it allows handling the beginnings and ends of nodes
separately – kind of like parsing a XML token stream, or kind of like
VisualEditor's linear model.

(Add unit tests for this utility. The simple.html test case is copied
from [VisualEditor/VisualEditor]/demos/ve/pages/simple.html.)

Use this utility to stop skipping when we reach either a closing or
opening block node tag. Previously we'd skip over such tags inside
nested "transparent" nodes (like <a>, <del>, or apparently <font>).

Bug: T271385
Change-Id: I201a942eb3a56335e84d94e150ec2c33f8b4f4e0
2021-01-18 18:20:20 +00:00

44 lines
1.2 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools\Tests;
use DOMDocument;
use MediaWiki\Extension\DiscussionTools\CommentUtils;
/**
* @coversDefaultClass \MediaWiki\Extension\DiscussionTools\CommentUtils
*
* @group DiscussionTools
*/
class CommentUtilsTest extends CommentTestCase {
/**
* @dataProvider provideLinearWalk
* @covers ::linearWalk
*/
public function testLinearWalk( string $name, string $htmlPath, string $expectedPath ) {
$html = self::getHtml( $htmlPath );
// Slightly awkward to get the same output as in the JS version
$fragment = ( new DOMDocument() )->createDocumentFragment();
$fragment->appendXML( trim( $html ) );
$expected = self::getJson( $expectedPath );
$actual = [];
CommentUtils::linearWalk( $fragment, function ( $event, $node ) use ( &$actual ) {
$actual[] = "$event {$node->nodeName}({$node->nodeType})";
} );
// Optionally write updated content to the JSON files
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) {
self::overwriteJsonFile( $expectedPath, $actual );
}
self::assertEquals( $expected, $actual, $name );
}
public function provideLinearWalk() : array {
return self::getJson( '../cases/linearWalk.json' );
}
}