6 class HtmlFormatterTest
extends MediaWikiTestCase
{
8 * @dataProvider getHtmlData
9 * @covers HtmlFormatter::getText
11 public function testTransform( $input, $expectedText,
12 $expectedRemoved = array(), $callback = false
14 $input = self
::normalize( $input );
15 $formatter = new HtmlFormatter( HtmlFormatter
::wrapHTML( $input ) );
17 $callback( $formatter );
19 $removedElements = $formatter->filterContent();
20 $html = $formatter->getText();
22 foreach ( $removedElements as $removedElement ) {
23 $removed[] = self
::normalize( $formatter->getText( $removedElement ) );
25 $expectedRemoved = array_map( 'self::normalize', $expectedRemoved );
27 $this->assertValidHtmlSnippet( $html );
28 $this->assertEquals( self
::normalize( $expectedText ), self
::normalize( $html ) );
29 $this->assertEquals( asort( $expectedRemoved ), asort( $removed ) );
32 private static function normalize( $s ) {
33 return str_replace( "\n", '',
34 str_replace( "\r", '', $s ) // "yay" to Windows!
38 public function getHtmlData() {
39 $removeImages = function( HtmlFormatter
$f ) {
42 $removeTags = function( HtmlFormatter
$f ) {
43 $f->remove( array( 'table', '.foo', '#bar', 'div.baz' ) );
45 $flattenSomeStuff = function( HtmlFormatter
$f ) {
46 $f->flatten( array( 's', 'div' ) );
48 $flattenEverything = function( HtmlFormatter
$f ) {
52 // remove images if asked
54 '<img src="/foo/bar.jpg" alt="Blah"/>',
56 array( '<img src="/foo/bar.jpg" alt="Blah">' ),
61 // @codingStandardsIgnoreStart Ignore long line warnings.
62 '<table><tr><td>foo</td></tr></table><div class="foo">foo</div><div class="foo quux">foo</div><span id="bar">bar</span>
63 <strong class="foo" id="bar">foobar</strong><div class="notfoo">test</div><div class="baz"/>
64 <span class="baz">baz</span>',
65 // @codingStandardsIgnoreEnd
66 '<div class="notfoo">test</div>
67 <span class="baz">baz</span>',
69 '<table><tr><td>foo</td></tr></table>',
70 '<div class="foo">foo</div>',
71 '<div class="foo quux">foo</div>',
72 '<span id="bar">bar</span>',
73 '<strong class="foo" id="bar">foobar</strong>',
78 // don't flatten tags that start like chosen ones
80 '<div><s>foo</s> <span>bar</span></div>',
81 'foo <span>bar</span>',
87 '<div style="foo">bar<sup>2</sup></div>',
92 // UTF-8 preservation and security
94 '<span title="" \' &"><Тест!></span> &<&&&&',
95 '<span title="" \' &"><Тест!></span> &<&&&&',
97 // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
99 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup>'
100 . ' <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
101 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup>'
102 . ' <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',