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