6 class HtmlFormatterTest
extends MediaWikiTestCase
{
8 * @dataProvider getHtmlData
10 * @param string $input
11 * @param string $expectedText
12 * @param array $expectedRemoved
13 * @param callable|bool $callback
15 public function testTransform( $input, $expectedText,
16 $expectedRemoved = array(), $callback = false
18 $input = self
::normalize( $input );
19 $formatter = new HtmlFormatter( HtmlFormatter
::wrapHTML( $input ) );
21 $callback( $formatter );
23 $removedElements = $formatter->filterContent();
24 $html = $formatter->getText();
26 foreach ( $removedElements as $removedElement ) {
27 $removed[] = self
::normalize( $formatter->getText( $removedElement ) );
29 $expectedRemoved = array_map( 'self::normalize', $expectedRemoved );
31 $this->assertValidHtmlSnippet( $html );
32 $this->assertEquals( self
::normalize( $expectedText ), self
::normalize( $html ) );
33 $this->assertEquals( asort( $expectedRemoved ), asort( $removed ) );
36 private static function normalize( $s ) {
37 return str_replace( "\n", '',
38 str_replace( "\r", '', $s ) // "yay" to Windows!
42 public function getHtmlData() {
43 $removeImages = function ( HtmlFormatter
$f ) {
46 $removeTags = function ( HtmlFormatter
$f ) {
47 $f->remove( array( 'table', '.foo', '#bar', 'div.baz' ) );
49 $flattenSomeStuff = function ( HtmlFormatter
$f ) {
50 $f->flatten( array( 's', 'div' ) );
52 $flattenEverything = function ( HtmlFormatter
$f ) {
56 // remove images if asked
58 '<img src="/foo/bar.jpg" alt="Blah"/>',
60 array( '<img src="/foo/bar.jpg" alt="Blah">' ),
65 // @codingStandardsIgnoreStart Ignore long line warnings.
66 '<table><tr><td>foo</td></tr></table><div class="foo">foo</div><div class="foo quux">foo</div><span id="bar">bar</span>
67 <strong class="foo" id="bar">foobar</strong><div class="notfoo">test</div><div class="baz"/>
68 <span class="baz">baz</span>',
69 // @codingStandardsIgnoreEnd
70 '<div class="notfoo">test</div>
71 <span class="baz">baz</span>',
73 '<table><tr><td>foo</td></tr></table>',
74 '<div class="foo">foo</div>',
75 '<div class="foo quux">foo</div>',
76 '<span id="bar">bar</span>',
77 '<strong class="foo" id="bar">foobar</strong>',
82 // don't flatten tags that start like chosen ones
84 '<div><s>foo</s> <span>bar</span></div>',
85 'foo <span>bar</span>',
91 '<div style="foo">bar<sup>2</sup></div>',
96 // UTF-8 preservation and security
98 '<span title="" \' &"><Тест!></span> &<&&&&',
99 '<span title="" \' &"><Тест!></span> &<&&&&',
101 $removeTags, // Have some rules to trigger a DOM parse
103 // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
105 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup>'
106 . ' <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
107 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup>'
108 . ' <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
113 public function testQuickProcessing() {
114 $f = new MockHtmlFormatter( 'foo' );
116 $this->assertFalse( $f->hasDoc
, 'HtmlFormatter should not needlessly parse HTML' );
120 class MockHtmlFormatter
extends HtmlFormatter
{
121 public $hasDoc = false;
123 public function getDoc() {
124 $this->hasDoc
= true;
125 return parent
::getDoc();