<?php
class HtmlTest extends MediaWikiTestCase {
+ private $restoreWarnings;
protected function setUp() {
parent::setUp();
] );
$this->setUserLang( $langObj );
$this->setContentLang( $langObj );
+ $this->restoreWarnings = false;
+ }
+
+ protected function tearDown() {
+ if ( $this->restoreWarnings ) {
+ $this->restoreWarnings = false;
+ Wikimedia\restoreWarnings();
+ }
+ parent::tearDown();
}
/**
Html::errorBox( 'err', 'heading' ),
'<div class="errorbox"><h2>heading</h2>err</div>'
);
+ $this->assertEquals(
+ Html::errorBox( 'err', '0' ),
+ '<div class="errorbox"><h2>0</h2>err</div>'
+ );
}
/**
public function testSrcSet( $images, $expected, $message ) {
$this->assertEquals( Html::srcSet( $images ), $expected, $message );
}
+
+ public static function provideInlineScript() {
+ return [
+ 'Empty' => [
+ '',
+ '<script></script>'
+ ],
+ 'Simple' => [
+ 'EXAMPLE.label("foo");',
+ '<script>EXAMPLE.label("foo");</script>'
+ ],
+ 'Ampersand' => [
+ 'EXAMPLE.is(a && b);',
+ '<script>EXAMPLE.is(a && b);</script>'
+ ],
+ 'HTML' => [
+ 'EXAMPLE.label("<a>");',
+ '<script>EXAMPLE.label("<a>");</script>'
+ ],
+ 'Script closing string (lower)' => [
+ 'EXAMPLE.label("</script>");',
+ '<script>/* ERROR: Invalid script */</script>',
+ true,
+ ],
+ 'Script closing with non-standard attributes (mixed)' => [
+ 'EXAMPLE.label("</SCriPT and STyLE>");',
+ '<script>/* ERROR: Invalid script */</script>',
+ true,
+ ],
+ 'HTML-comment-open and script-open' => [
+ // In HTML, <script> contents aren't just plain CDATA until </script>,
+ // there are levels of escaping modes, and the below sequence puts an
+ // HTML parser in a state where </script> would *not* close the script.
+ // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
+ 'var a = "<!--<script>";',
+ '<script>/* ERROR: Invalid script */</script>',
+ true,
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideInlineScript
+ * @covers Html::inlineScript
+ */
+ public function testInlineScript( $code, $expected, $error = false ) {
+ if ( $error ) {
+ Wikimedia\suppressWarnings();
+ $this->restoreWarnings = true;
+ }
+ $this->assertSame( Html::inlineScript( $code ), $expected );
+ }
}
class HtmlTestValue {