MediaWiki  REL1_23
HtmlFormatterTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HtmlFormatterTest extends MediaWikiTestCase {
00011     public function testTransform( $input, $expected, $callback = false ) {
00012         $input = self::normalize( $input );
00013         $formatter = new HtmlFormatter( HtmlFormatter::wrapHTML( $input ) );
00014         if ( $callback ) {
00015             $callback( $formatter );
00016         }
00017         $formatter->filterContent();
00018         $html = $formatter->getText();
00019 
00020         $this->assertValidHtmlSnippet( $html );
00021         $this->assertEquals( self::normalize( $expected ), self::normalize( $html ) );
00022     }
00023 
00024     private static function normalize( $s ) {
00025         return str_replace( "\n", '',
00026             str_replace( "\r", '', $s ) // "yay" to Windows!
00027         );
00028     }
00029 
00030     public function getHtmlData() {
00031         $removeImages = function( HtmlFormatter $f ) {
00032             $f->setRemoveMedia();
00033         };
00034         $removeTags = function( HtmlFormatter $f ) {
00035             $f->remove( array( 'table', '.foo', '#bar', 'div.baz' ) );
00036         };
00037         $flattenSomeStuff = function( HtmlFormatter $f ) {
00038             $f->flatten( array( 's', 'div' ) );
00039         };
00040         $flattenEverything = function( HtmlFormatter $f ) {
00041             $f->flattenAllTags();
00042         };
00043         return array(
00044             // remove images if asked
00045             array(
00046                 '<img src="/foo/bar.jpg" alt="Blah"/>',
00047                 '',
00048                 $removeImages,
00049             ),
00050             // basic tag removal
00051             array(
00052                 '<table><tr><td>foo</td></tr></table><div class="foo">foo</div><div class="foo quux">foo</div><span id="bar">bar</span>
00053 <strong class="foo" id="bar">foobar</strong><div class="notfoo">test</div><div class="baz"/>
00054 <span class="baz">baz</span>',
00055 
00056                 '<div class="notfoo">test</div>
00057 <span class="baz">baz</span>',
00058                 $removeTags,
00059             ),
00060             // don't flatten tags that start like chosen ones
00061             array(
00062                 '<div><s>foo</s> <span>bar</span></div>',
00063                 'foo <span>bar</span>',
00064                 $flattenSomeStuff,
00065             ),
00066             // total flattening
00067             array(
00068                 '<div style="foo">bar<sup>2</sup></div>',
00069                 'bar2',
00070                 $flattenEverything,
00071             ),
00072             // UTF-8 preservation and security
00073             array(
00074                 '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&#38;&#0038;&#x26;&#x026;',
00075                 '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&amp;&amp;&amp;&amp;',
00076             ),
00077             // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
00078             array(
00079                 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup> <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
00080                 'Foo<sup id="cite_ref-1" class="reference"><a href="#cite_note-1">[1]</a></sup> <a href="/wiki/Bar" title="Bar" class="mw-redirect">Bar</a>',
00081             ),
00082         );
00083     }
00084 }