setMwGlobals( 'wgTidyInternal', $tidySupport->isInternal() );
}
/**
* @dataProvider getHtmlData
*
* @param string $input
* @param string $expectedText
* @param array $expectedRemoved
* @param callable|bool $callback
*/
public function testTransform( $input, $expectedText,
$expectedRemoved = [], $callback = false
) {
$input = self::normalize( $input );
$formatter = new HtmlFormatter( HtmlFormatter::wrapHTML( $input ) );
if ( $callback ) {
$callback( $formatter );
}
$removedElements = $formatter->filterContent();
$html = $formatter->getText();
$removed = [];
foreach ( $removedElements as $removedElement ) {
$removed[] = self::normalize( $formatter->getText( $removedElement ) );
}
$expectedRemoved = array_map( 'self::normalize', $expectedRemoved );
$this->assertValidHtmlSnippet( $html );
$this->assertEquals( self::normalize( $expectedText ), self::normalize( $html ) );
$this->assertEquals( asort( $expectedRemoved ), asort( $removed ) );
}
private static function normalize( $s ) {
return str_replace( "\n", '',
str_replace( "\r", '', $s ) // "yay" to Windows!
);
}
public function getHtmlData() {
$removeImages = function ( HtmlFormatter $f ) {
$f->setRemoveMedia();
};
$removeTags = function ( HtmlFormatter $f ) {
$f->remove( [ 'table', '.foo', '#bar', 'div.baz' ] );
};
$flattenSomeStuff = function ( HtmlFormatter $f ) {
$f->flatten( [ 's', 'div' ] );
};
$flattenEverything = function ( HtmlFormatter $f ) {
$f->flattenAllTags();
};
return [
// remove images if asked
[
'',
'',
[ '' ],
$removeImages,
],
// basic tag removal
[
// @codingStandardsIgnoreStart Ignore long line warnings.
'
foo
foo
bar
foobartest
baz',
// @codingStandardsIgnoreEnd
'test
baz',
[
'',
'foo
',
'foo
',
'bar',
'foobar',
'',
],
$removeTags,
],
// don't flatten tags that start like chosen ones
[
'foo bar
',
'foo bar',
[],
$flattenSomeStuff,
],
// total flattening
[
'bar2
',
'bar2',
[],
$flattenEverything,
],
// UTF-8 preservation and security
[
'<Тест!> &<&&&&',
'<Тест!> &<&&&&',
[],
$removeTags, // Have some rules to trigger a DOM parse
],
// https://phabricator.wikimedia.org/T55086
[
'Foo[1]'
. ' Bar',
'Foo[1]'
. ' Bar',
],
];
}
public function testQuickProcessing() {
$f = new MockHtmlFormatter( 'foo' );
$f->filterContent();
$this->assertFalse( $f->hasDoc, 'HtmlFormatter should not needlessly parse HTML' );
}
}
class MockHtmlFormatter extends HtmlFormatter {
public $hasDoc = false;
public function getDoc() {
$this->hasDoc = true;
return parent::getDoc();
}
}