MediaWiki  1.23.0
HtmlFormatterTest.php
Go to the documentation of this file.
1 <?php
2 
11  public function testTransform( $input, $expected, $callback = false ) {
12  $input = self::normalize( $input );
13  $formatter = new HtmlFormatter( HtmlFormatter::wrapHTML( $input ) );
14  if ( $callback ) {
15  $callback( $formatter );
16  }
17  $formatter->filterContent();
18  $html = $formatter->getText();
19 
20  $this->assertValidHtmlSnippet( $html );
21  $this->assertEquals( self::normalize( $expected ), self::normalize( $html ) );
22  }
23 
24  private static function normalize( $s ) {
25  return str_replace( "\n", '',
26  str_replace( "\r", '', $s ) // "yay" to Windows!
27  );
28  }
29 
30  public function getHtmlData() {
31  $removeImages = function( HtmlFormatter $f ) {
32  $f->setRemoveMedia();
33  };
34  $removeTags = function( HtmlFormatter $f ) {
35  $f->remove( array( 'table', '.foo', '#bar', 'div.baz' ) );
36  };
37  $flattenSomeStuff = function( HtmlFormatter $f ) {
38  $f->flatten( array( 's', 'div' ) );
39  };
40  $flattenEverything = function( HtmlFormatter $f ) {
41  $f->flattenAllTags();
42  };
43  return array(
44  // remove images if asked
45  array(
46  '<img src="/foo/bar.jpg" alt="Blah"/>',
47  '',
48  $removeImages,
49  ),
50  // basic tag removal
51  array(
52  '<table><tr><td>foo</td></tr></table><div class="foo">foo</div><div class="foo quux">foo</div><span id="bar">bar</span>
53 <strong class="foo" id="bar">foobar</strong><div class="notfoo">test</div><div class="baz"/>
54 <span class="baz">baz</span>',
55 
56  '<div class="notfoo">test</div>
57 <span class="baz">baz</span>',
58  $removeTags,
59  ),
60  // don't flatten tags that start like chosen ones
61  array(
62  '<div><s>foo</s> <span>bar</span></div>',
63  'foo <span>bar</span>',
64  $flattenSomeStuff,
65  ),
66  // total flattening
67  array(
68  '<div style="foo">bar<sup>2</sup></div>',
69  'bar2',
70  $flattenEverything,
71  ),
72  // UTF-8 preservation and security
73  array(
74  '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&#38;&#0038;&#x26;&#x026;',
75  '<span title="&quot; \' &amp;">&lt;Тест!&gt;</span> &amp;&lt;&amp;&amp;&amp;&amp;',
76  ),
77  // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
78  array(
79  '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>',
80  '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>',
81  ),
82  );
83  }
84 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$html
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1530
$f
$f
Definition: UtfNormalTest2.php:38
$s
$s
Definition: mergeMessageFileList.php:156
HtmlFormatterTest\getHtmlData
getHtmlData()
Definition: HtmlFormatterTest.php:30
MediaWikiTestCase\assertValidHtmlSnippet
assertValidHtmlSnippet( $html)
Asserts that the given string is a valid HTML snippet.
Definition: MediaWikiTestCase.php:1055
HtmlFormatterTest
@group HtmlFormatter
Definition: HtmlFormatterTest.php:6
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
HtmlFormatter\wrapHTML
static wrapHTML( $html)
Turns a chunk of HTML into a proper document.
Definition: HtmlFormatter.php:47
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
HtmlFormatter
Definition: HtmlFormatter.php:23
HtmlFormatterTest\normalize
static normalize( $s)
Definition: HtmlFormatterTest.php:24
HtmlFormatterTest\testTransform
testTransform( $input, $expected, $callback=false)
@dataProvider getHtmlData @covers HtmlFormatter::getText
Definition: HtmlFormatterTest.php:11