3 class BalancerTest
extends MediaWikiTestCase
{
7 * Anything that needs to happen before your tests should go here.
9 protected function setUp() {
10 // Be sure to do call the parent setup and teardown functions.
11 // This makes sure that all the various cleanup and restorations
12 // happen as they should (including the restoration for setMwGlobals).
14 $this->balancer
= new MediaWiki\Tidy\
Balancer( [
15 'strict' => false, /* not strict */
16 'allowedHtmlElements' => null, /* no sanitization */
17 'tidyCompat' => false, /* standard parser */
18 'allowComments' => true, /* comment parsing */
23 * @covers MediaWiki\Tidy\Balancer::balance
24 * @dataProvider provideBalancerTests
26 public function testBalancer( $description, $input, $expected ) {
27 $output = $this->balancer
->balance( $input );
29 // Ignore self-closing tags
30 $output = preg_replace( '/\s*\/>/', '>', $output );
32 $this->assertEquals( $expected, $output, $description );
35 public static function provideBalancerTests() {
36 // Get the tests from html5lib-tests.json
37 $json = json_decode( file_get_contents(
38 __DIR__
. '/html5lib-tests.json'
40 // Munge this slightly into the format phpunit expects
41 // for providers, and filter out HTML constructs which
42 // the balancer doesn't support.
44 $start = '<html><head></head><body>';
45 $end = '</body></html>';
46 foreach ( $json as $filename => $cases ) {
47 foreach ( $cases as $case ) {
48 $html = $case['document']['html'];
50 substr( $html, 0, strlen( $start ) ) !== $start ||
51 substr( $html, -strlen( $end ) ) !== $end
53 // Skip tests which involve stuff in the <head> or
57 // We used to do this:
58 // $html = substr( $html, strlen( $start ), -strlen( $end ) );
59 // But now we use a different field in the test case,
60 // which reports how domino would parse this case in a
61 // no-quirks <body> context. (The original test case may
62 // have had a different context, or relied on quirks mode.)
63 $html = $case['document']['noQuirksBodyHtml'];
64 // Normalize case of SVG attributes.
65 $html = str_replace( 'foreignObject', 'foreignobject', $html );
68 isset( $case['document']['props']['comment'] ) &&
69 preg_match( ',<!--[^>]*<,', $html )
71 // Skip tests which include HTML comments containing
72 // the < character, which we don't support.
75 if ( strpos( $case['data'], '<![CDATA[' ) !== false ) {
76 // Skip tests involving <![CDATA[ ]]> quoting.
79 if ( stripos( $case['data'], '<!DOCTYPE' ) !== false ) {
80 // Skip tests involving doctypes.
83 if ( preg_match( ',</?(html|head|body|frame|plaintext)>|<rdar:|<isindex,i', $case['data'] ) ) {
84 // Skip tests involving some literal tags, which are
85 // unsupported but don't show up in the expected output.
89 isset( $case['document']['props']['tags']['iframe'] ) ||
90 isset( $case['document']['props']['tags']['noembed'] ) ||
91 isset( $case['document']['props']['tags']['noscript'] ) ||
92 isset( $case['document']['props']['tags']['script'] ) ||
93 isset( $case['document']['props']['tags']['svg script'] ) ||
94 isset( $case['document']['props']['tags']['svg title'] ) ||
95 isset( $case['document']['props']['tags']['title'] ) ||
96 isset( $case['document']['props']['tags']['xmp'] )
98 // Skip tests with unsupported tags which *do* show
99 // up in the expected output.
103 $filename === 'entities01.dat' ||
104 $filename === 'entities02.dat' ||
105 preg_match( '/&([a-z]+|#x[0-9A-F]+);/i', $case['data'] ) ||
106 preg_match( '/^(&|&#|&#X|&#x|-|&x-test|&)$/', $case['data'] )
108 // Skip tests involving entity encoding.
112 isset( $case['document']['props']['tagWithLt'] ) ||
113 isset( $case['document']['props']['attrWithFunnyChar'] ) ||
114 preg_match( ':^(</b test|<di|<foo bar=qux/>)$:', $case['data'] ) ||
115 preg_match( ':</p<p>:', $case['data'] )
117 // Skip tests with funny tag or attribute names,
118 // which are really tests of the HTML tokenizer, not
123 stripos( $case['data'], 'encoding=" text/html "' ) !== false
125 // The Sanitizer normalizes whitespace in attribute
126 // values, which makes this test case invalid.
129 if ( $filename === 'plain-text-unsafe.dat' ) {
130 // Skip tests with ASCII null, etc.
134 $filename, # use better description?