3 class SanitizerTest
extends MediaWikiTestCase
{
6 AutoLoader
::loadClass( 'Sanitizer' );
9 function testDecodeNamedEntities() {
12 Sanitizer
::decodeCharReferences( 'école' ),
13 'decode named entities'
17 function testDecodeNumericEntities() {
19 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
20 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
21 'decode numeric entities'
25 function testDecodeMixedEntities() {
27 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
28 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
29 'decode mixed numeric/named entities'
33 function testDecodeMixedComplexEntities() {
35 "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas Ĉio dans l'école)",
36 Sanitizer
::decodeCharReferences(
37 "Ĉio bonas dans l'école! (mais pas Ĉio dans l'école)"
39 'decode mixed complex entities'
43 function testInvalidAmpersand() {
46 Sanitizer
::decodeCharReferences( 'a & b' ),
51 function testInvalidEntities() {
54 Sanitizer
::decodeCharReferences( '&foo;' ),
55 'Invalid named entity'
59 function testInvalidNumberedEntities() {
60 $this->assertEquals( UTF8_REPLACEMENT
, Sanitizer
::decodeCharReferences( "�" ), 'Invalid numbered entity' );
63 function testSelfClosingTag() {
64 $GLOBALS['wgUseTidy'] = false;
66 '<div>Hello world</div>',
67 Sanitizer
::removeHTMLtags( '<div>Hello world</div />' ),
68 'Self-closing closing div'
72 function testDecodeTagAttributes() {
73 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=bar' ), array( 'foo' => 'bar' ), 'Unquoted attribute' );
74 $this->assertEquals( Sanitizer
::decodeTagAttributes( ' foo = bar ' ), array( 'foo' => 'bar' ), 'Spaced attribute' );
75 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo="bar"' ), array( 'foo' => 'bar' ), 'Double-quoted attribute' );
76 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Single-quoted attribute' );
77 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
79 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
80 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
82 $this->assertEquals( Sanitizer
::decodeTagAttributes( ':foo=\'bar\'' ), array( ':foo' => 'bar' ), 'Leading :' );
83 $this->assertEquals( Sanitizer
::decodeTagAttributes( '_foo=\'bar\'' ), array( '_foo' => 'bar' ), 'Leading _' );
84 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'Foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Leading capital' );
85 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'FOO=BAR' ), array( 'foo' => 'BAR' ), 'Attribute keys are normalized to lowercase' );
88 $this->assertEquals( Sanitizer
::decodeTagAttributes( '-foo=bar' ), array(), 'Leading - is forbidden' );
89 $this->assertEquals( Sanitizer
::decodeTagAttributes( '.foo=bar' ), array(), 'Leading . is forbidden' );
90 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo-bar=bar' ), array( 'foo-bar' => 'bar' ), 'A - is allowed inside the attribute' );
91 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo-=bar' ), array( 'foo-' => 'bar' ), 'A - is allowed inside the attribute' );
93 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo.bar=baz' ), array( 'foo.bar' => 'baz' ), 'A . is allowed inside the attribute' );
94 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo.=baz' ), array( 'foo.' => 'baz' ), 'A . is allowed as last character' );
96 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo6=baz' ), array( 'foo6' => 'baz' ), 'Numbers are allowed' );
98 # This bit is more relaxed than XML rules, but some extensions use it, like ProofreadPage (see bug 27539)
99 $this->assertEquals( Sanitizer
::decodeTagAttributes( '1foo=baz' ), array( '1foo' => 'baz' ), 'Leading numbers are allowed' );
101 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo$=baz' ), array(), 'Symbols are not allowed' );
102 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo@=baz' ), array(), 'Symbols are not allowed' );
103 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo~=baz' ), array(), 'Symbols are not allowed' );
106 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=1[#^`*%w/(' ), array( 'foo' => '1[#^`*%w/(' ), 'All kind of characters are allowed as values' );
107 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo="1[#^`*%\'w/("' ), array( 'foo' => '1[#^`*%\'w/(' ), 'Double quotes are allowed if quoted by single quotes' );
108 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'1[#^`*%"w/(\'' ), array( 'foo' => '1[#^`*%"w/(' ), 'Single quotes are allowed if quoted by double quotes' );
109 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=&"' ), array( 'foo' => '&"' ), 'Special chars can be provided as entities' );
110 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=&foobar;' ), array( 'foo' => '&foobar;' ), 'Entity-like items are accepted' );
113 function testDeprecatedAttributesDisabled() {
114 $GLOBALS['wgCleanupPresentationalAttributes'] = false;
115 $this->assertEquals( ' clear="left"', Sanitizer
::fixTagAttributes( 'clear="left"', 'br' ), 'Deprecated attributes are not converted to styles when enabled.' );
119 * @dataProvider provideDeprecatedAttributes
121 function testDeprecatedAttributes( $input, $tag, $expected, $message = null ) {
122 $GLOBALS['wgCleanupPresentationalAttributes'] = true;
123 $this->assertEquals( $expected, Sanitizer
::fixTagAttributes( $input, $tag ), $message );
126 function provideDeprecatedAttributes() {
128 array( 'clear="left"', 'br', ' style="clear: left;"', 'Deprecated attributes are converted to styles when enabled.' ),
129 array( 'clear="all"', 'br', ' style="clear: both;"', 'clear=all is converted to clear: both; not clear: all;' ),
130 array( 'CLEAR="ALL"', 'br', ' style="clear: both;"', 'clear=ALL is not treated differently from clear=all' ),
131 array( 'width="100"', 'td', ' style="width: 100px;"', 'Numeric sizes use pixels instead of numbers.' ),
132 array( 'width="100%"', 'td', ' style="width: 100%;"', 'Units are allowed in sizes.' ),
133 array( 'WIDTH="100%"', 'td', ' style="width: 100%;"', 'Uppercase WIDTH is treated as lowercase width.' ),
134 array( 'WiDTh="100%"', 'td', ' style="width: 100%;"', 'Mixed case does not break WiDTh.' ),
135 array( 'nowrap="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute is output as white-space: nowrap; not something else.' ),
136 array( 'nowrap=""', 'td', ' style="white-space: nowrap;"', 'nowrap="" is considered true, not false' ),
137 array( 'NOWRAP="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when uppercase.' ),
138 array( 'NoWrAp="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when mixed-case.' ),
139 array( 'align="right"', 'td', ' style="text-align: right;"' , 'align on table cells gets converted to text-align' ),
140 array( 'align="center"', 'td', ' style="text-align: center;"' , 'align on table cells gets converted to text-align' ),
141 array( 'align="left"' , 'div', ' style="text-align: left;"' , 'align=(left|right) on div elements gets converted to text-align' ),
142 array( 'align="center"', 'div', ' style="text-align: center;"', 'align="center" on div elements gets converted to text-align' ),
143 array( 'align="left"' , 'p', ' style="text-align: left;"' , 'align on p elements gets converted to text-align' ),
144 array( 'align="left"' , 'h1', ' style="text-align: left;"' , 'align on h1 elements gets converted to text-align' ),
145 array( 'align="left"' , 'h1', ' style="text-align: left;"' , 'align on h1 elements gets converted to text-align' ),
146 array( 'align="left"' , 'caption',' style="text-align: left;"','align on caption elements gets converted to text-align' ),
147 array( 'align="left"' , 'tfoot',' style="text-align: left;"' , 'align on tfoot elements gets converted to text-align' ),
148 array( 'align="left"' , 'tbody',' style="text-align: left;"' , 'align on tbody elements gets converted to text-align' ),
151 array( 'align="right"' , 'tr', ' style="text-align: right;"' , 'align on table row get converted to text-align' ),
152 array( 'align="center"', 'tr', ' style="text-align: center;"', 'align on table row get converted to text-align' ),
153 array( 'align="left"' , 'tr', ' style="text-align: left;"' , 'align on table row get converted to text-align' ),
156 array( 'align="left"' , 'table', ' style="float: left;"' , 'align on table converted to float' ),
157 array( 'align="center"', 'table', ' style="margin-left: auto; margin-right: auto;"', 'align center on table converted to margins' ),
158 array( 'align="right"' , 'table', ' style="float: right;"' , 'align on table converted to float' ),
163 * @dataProvider provideCssCommentsFixtures
165 function testCssCommentsChecking( $expected, $css, $message = '' ) {
168 Sanitizer
::checkCss( $css ),
173 function provideCssCommentsFixtures() {
174 /** array( <expected>, <css>, [message] ) */
176 array( ' ', '/**/' ),
177 array( ' ', '/****/' ),
178 array( ' ', '/* comment */' ),
179 array( ' ', "\\2f\\2a foo \\2a\\2f",
180 'Backslash-escaped comments must be stripped (bug 28450)' ),
181 array( '', '/* unfinished comment structure',
182 'Remove anything after a comment-start token' ),
183 array( '', "\\2f\\2a unifinished comment'",
184 'Remove anything after a backslash-escaped comment-start token' ),