(bug 36495) Sanitizer: Convert align to margin/float outside tables.
authorMax Semenik <maxsem.wiki@gmail.com>
Fri, 29 Jun 2012 16:24:20 +0000 (20:24 +0400)
committerTimo Tijhof <ttijhof@wikimedia.org>
Tue, 3 Jul 2012 06:54:46 +0000 (08:54 +0200)
Change-Id: I108cbd100cff6bade011b14d74b5bca82f2a1e5f

RELEASE-NOTES-1.20
includes/Sanitizer.php
tests/phpunit/includes/SanitizerTest.php

index 798e8de..03728e4 100644 (file)
@@ -139,6 +139,8 @@ upgrade PHP if you have not done so prior to upgrading MediaWiki.
 * (bug 36812) Special:ActiveUsers "Hide bots" should hide users from any group
   having the "bot" user right, instead of just the default "bot" user group.
 * (bug 35082) mw.util.addPortletLink incorrectly adds link to mutiple <ul> tags.
+* (bug 36495) Sanitizer::fixDeprecatedAttributes should convert "align"
+  attribute to margin or float instead of text-align (for non-table-cells).
 
 === API changes in 1.20 ===
 * (bug 34316) Add ability to retrieve maximum upload size from MediaWiki API.
index 8cd5a37..fed5762 100644 (file)
@@ -692,6 +692,16 @@ class Sanitizer {
                                }
                        }
 
+                       if ( $attribute === 'align' && !in_array( $element, $cells ) ) {
+                               if ( $value === 'center' ) {
+                                       $style .= ' margin-left: auto;';
+                                       $property = 'margin-right';
+                                       $value = 'auto';
+                               } else {
+                                       $property = 'float';
+                               }
+                       }
+
                        $style .= " $property: $value;";
 
                        unset( $attribs[$attribute] );
index b76aa5c..c929989 100644 (file)
@@ -110,21 +110,37 @@ class SanitizerTest extends MediaWikiTestCase {
                $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=&foobar;' ), array( 'foo' => '&foobar;' ), 'Entity-like items are accepted' );
        }
 
-       function testDeprecatedAttributes() {
-               $GLOBALS['wgCleanupPresentationalAttributes'] = true;
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), ' style="clear: left;"', 'Deprecated attributes are converted to styles when enabled.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="all"', 'br' ), ' style="clear: both;"', 'clear=all is converted to clear: both; not clear: all;' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'CLEAR="ALL"', 'br' ), ' style="clear: both;"', 'clear=ALL is not treated differently from clear=all' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'width="100"', 'td' ), ' style="width: 100px;"', 'Numeric sizes use pixels instead of numbers.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'width="100%"', 'td' ), ' style="width: 100%;"', 'Units are allowed in sizes.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'WIDTH="100%"', 'td' ), ' style="width: 100%;"', 'Uppercase WIDTH is treated as lowercase width.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'WiDTh="100%"', 'td' ), ' style="width: 100%;"', 'Mixed case does not break WiDTh.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'nowrap="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute is output as white-space: nowrap; not something else.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'nowrap=""', 'td' ), ' style="white-space: nowrap;"', 'nowrap="" is considered true, not false' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'NOWRAP="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute works when uppercase.' );
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'NoWrAp="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute works when mixed-case.' );
+       function testDeprecatedAttributesDisabled() {
                $GLOBALS['wgCleanupPresentationalAttributes'] = false;
-               $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), ' clear="left"', 'Deprecated attributes are not converted to styles when enabled.' );
+               $this->assertEquals( ' clear="left"', Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), 'Deprecated attributes are not converted to styles when enabled.' );
+       }
+
+       /**
+        * @dataProvider provideDeprecatedAttributes
+        */
+       function testDeprecatedAttributes( $input, $tag, $expected, $message = null ) {
+               $GLOBALS['wgCleanupPresentationalAttributes'] = true;
+               $this->assertEquals( $expected, Sanitizer::fixTagAttributes( $input, $tag ), $message );
+       }
+
+       function provideDeprecatedAttributes() {
+               return array(
+                       array( 'clear="left"', 'br', ' style="clear: left;"', 'Deprecated attributes are converted to styles when enabled.' ),
+                       array( 'clear="all"', 'br', ' style="clear: both;"', 'clear=all is converted to clear: both; not clear: all;' ),
+                       array( 'CLEAR="ALL"', 'br', ' style="clear: both;"', 'clear=ALL is not treated differently from clear=all' ),
+                       array( 'width="100"', 'td', ' style="width: 100px;"', 'Numeric sizes use pixels instead of numbers.' ),
+                       array( 'width="100%"', 'td', ' style="width: 100%;"', 'Units are allowed in sizes.' ),
+                       array( 'WIDTH="100%"', 'td', ' style="width: 100%;"', 'Uppercase WIDTH is treated as lowercase width.' ),
+                       array( 'WiDTh="100%"', 'td', ' style="width: 100%;"', 'Mixed case does not break WiDTh.' ),
+                       array( 'nowrap="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute is output as white-space: nowrap; not something else.' ),
+                       array( 'nowrap=""', 'td', ' style="white-space: nowrap;"', 'nowrap="" is considered true, not false' ),
+                       array( 'NOWRAP="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when uppercase.' ),
+                       array( 'NoWrAp="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when mixed-case.' ),
+                       array( 'align="right"', 'td', ' style="text-align: right;"', 'align on table cells gets converted to text-align' ),
+                       array( 'align="center"', 'td', ' style="text-align: center;"', 'align on table cells gets converted to text-align' ),
+                       array( 'align="left"', 'div', ' style="float: left;"', 'align=(left|right) on non-cells gets converted to float' ),
+                       array( 'align="center"', 'div', ' style="margin-left: auto; margin-right: auto;"', 'align="center" on non-cells' ),
+               );
        }
 
        /**