MediaWiki  1.28.1
BalancerTest.php
Go to the documentation of this file.
1 <?php
2 
4  private $balancer;
5 
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).
13  parent::setUp();
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 */
19  ] );
20  }
21 
26  public function testBalancer( $description, $input, $expected ) {
27  $output = $this->balancer->balance( $input );
28 
29  // Ignore self-closing tags
30  $output = preg_replace( '/\s*\/>/', '>', $output );
31 
32  $this->assertEquals( $expected, $output, $description );
33  }
34 
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'
39  ), true );
40  // Munge this slightly into the format phpunit expects
41  // for providers, and filter out HTML constructs which
42  // the balancer doesn't support.
43  $tests = [];
44  $okre = "~ \A
45  (?i:<!DOCTYPE\ html>)?
46  <html><head></head><body>
47  .*
48  </body></html>
49  \z ~xs";
50  foreach ( $json as $filename => $cases ) {
51  foreach ( $cases as $case ) {
52  $html = $case['document']['html'];
53  if ( !preg_match( $okre, $html ) ) {
54  // Skip tests which involve stuff in the <head> or
55  // weird doctypes.
56  continue;
57  }
58  // We used to do this:
59  // $html = substr( $html, strlen( $start ), -strlen( $end ) );
60  // But now we use a different field in the test case,
61  // which reports how domino would parse this case in a
62  // no-quirks <body> context. (The original test case may
63  // have had a different context, or relied on quirks mode.)
64  $html = $case['document']['noQuirksBodyHtml'];
65  // Normalize case of SVG attributes.
66  $html = str_replace( 'foreignObject', 'foreignobject', $html );
67  // Normalize case of MathML attributes.
68  $html = str_replace( 'definitionURL', 'definitionurl', $html );
69 
70  if (
71  isset( $case['document']['props']['comment'] ) &&
72  preg_match( ',<!--[^>]*<,', $html )
73  ) {
74  // Skip tests which include HTML comments containing
75  // the < character, which we don't support.
76  continue;
77  }
78  if ( strpos( $case['data'], '<![CDATA[' ) !== false ) {
79  // Skip tests involving <![CDATA[ ]]> quoting.
80  continue;
81  }
82  if (
83  stripos( $case['data'], '<!DOCTYPE' ) !== false &&
84  stripos( $case['data'], '<!DOCTYPE html>' ) === false
85  ) {
86  // Skip tests involving unusual doctypes.
87  continue;
88  }
89  $literalre = "~ <rdar: | <isindex | < /? (
90  html | head | body | frame | frameset | plaintext
91  ) > ~xi";
92  if ( preg_match( $literalre, $case['data'] ) ) {
93  // Skip tests involving some literal tags, which are
94  // unsupported but don't show up in the expected output.
95  continue;
96  }
97  if (
98  isset( $case['document']['props']['tags']['iframe'] ) ||
99  isset( $case['document']['props']['tags']['noembed'] ) ||
100  isset( $case['document']['props']['tags']['noscript'] ) ||
101  isset( $case['document']['props']['tags']['script'] ) ||
102  isset( $case['document']['props']['tags']['svg script'] ) ||
103  isset( $case['document']['props']['tags']['svg title'] ) ||
104  isset( $case['document']['props']['tags']['title'] ) ||
105  isset( $case['document']['props']['tags']['xmp'] )
106  ) {
107  // Skip tests with unsupported tags which *do* show
108  // up in the expected output.
109  continue;
110  }
111  if (
112  $filename === 'entities01.dat' ||
113  $filename === 'entities02.dat' ||
114  preg_match( '/&([a-z]+|#x[0-9A-F]+);/i', $case['data'] ) ||
115  preg_match( '/^(&|&#|&#X|&#x|&#45|&x-test|&AMP)$/', $case['data'] )
116  ) {
117  // Skip tests involving entity encoding.
118  continue;
119  }
120  if (
121  isset( $case['document']['props']['tagWithLt'] ) ||
122  isset( $case['document']['props']['attrWithFunnyChar'] ) ||
123  preg_match( ':^(</b test|<di|<foo bar=qux/>)$:', $case['data'] ) ||
124  preg_match( ':</p<p>:', $case['data'] ) ||
125  preg_match( ':<b &=&amp>|<p/x/y/z>:', $case['data'] )
126  ) {
127  // Skip tests with funny tag or attribute names,
128  // which are really tests of the HTML tokenizer, not
129  // the tree builder.
130  continue;
131  }
132  if (
133  preg_match( ':encoding=" text/html "|type=" hidden":', $case['data'] )
134  ) {
135  // The Sanitizer normalizes whitespace in attribute
136  // values, which makes this test case invalid.
137  continue;
138  }
139  if ( $filename === 'plain-text-unsafe.dat' ) {
140  // Skip tests with ASCII null, etc.
141  continue;
142  }
143  $data = preg_replace(
144  '~<!DOCTYPE html>~i', '', $case['data']
145  );
146  $tests[] = [
147  $filename, # use better description?
148  $data,
149  $html
150  ];
151  }
152  }
153  return $tests;
154  }
155 }
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:1936
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testBalancer($description, $input, $expected)
MediaWiki\Tidy\Balancer::balance provideBalancerTests.
static provideBalancerTests()
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition: hooks.txt:1046
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
An implementation of the tree building portion of the HTML5 parsing spec.
Definition: Balancer.php:1792
setUp()
Anything that needs to happen before your tests should go here.
Definition: BalancerTest.php:9