From 7ad5282938ae66c0bcf1af7279385657296d6fc4 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Thu, 31 Aug 2017 17:48:42 -0700 Subject: [PATCH] Don't percent-encode HTML5 IDs During the TechCom meeting, it was decided this is the better way. Bug: T152540 Change-Id: I6c3ec1c407225b4e925b7373bf52208e2f6b6c4a --- includes/DefaultSettings.php | 4 +-- includes/Linker.php | 14 +++++++--- includes/Sanitizer.php | 18 ------------ resources/src/mediawiki/mediawiki.util.js | 9 ++---- tests/parser/parserTests.txt | 28 +++++++++---------- tests/phpunit/includes/SanitizerTest.php | 11 ++++---- .../mediawiki/mediawiki.util.test.js | 8 +++--- 7 files changed, 37 insertions(+), 55 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 86b1bdca55..5b77d16c4e 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -3394,8 +3394,8 @@ $wgExperimentalHtmlIds = false; /** * How should section IDs be encoded? * This array can contain 1 or 2 elements, each of them can be one of: - * - 'html5' is modern HTML5 style encoding with minimal escaping. Allows to - * display Unicode characters in many browsers' address bars. + * - 'html5' is modern HTML5 style encoding with minimal escaping. Displays Unicode + * characters in most browsers' address bars. * - 'legacy' is old MediaWiki-style encoding, e.g. 啤酒 turns into .E5.95.A4.E9.85.92 * - 'html5-legacy' corresponds to DEPRECATED $wgExperimentalHtmlIds mode. DO NOT use * it for anything but migration off that mode (see below). diff --git a/includes/Linker.php b/includes/Linker.php index aedb704a8f..dccd99c73e 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1539,10 +1539,16 @@ class Linker { if ( $sectionIndex !== false ) { $classes .= " tocsection-$sectionIndex"; } - return "\n
  • ' . - $tocnumber . ' ' . - $tocline . ''; + + // \n
  • + // $tocnumber $tocline + return "\n" . Html::openElement( 'li', [ 'class' => $classes ] ) + . Html::rawElement( 'a', + [ 'href' => "#$anchor" ], + Html::element( 'span', [ 'class' => 'tocnumber' ], $tocnumber ) + . ' ' + . Html::rawElement( 'span', [ 'class' => 'toctext' ], $tocline ) + ); } /** diff --git a/includes/Sanitizer.php b/includes/Sanitizer.php index ed09701d4b..7d17cd1065 100644 --- a/includes/Sanitizer.php +++ b/includes/Sanitizer.php @@ -1284,7 +1284,6 @@ class Sanitizer { $mode = $wgFragmentMode[self::ID_PRIMARY]; $id = self::escapeIdInternal( $id, $mode ); - $id = self::urlEscapeId( $id, $mode ); return $id; } @@ -1302,23 +1301,6 @@ class Sanitizer { global $wgExternalInterwikiFragmentMode; $id = self::escapeIdInternal( $id, $wgExternalInterwikiFragmentMode ); - $id = self::urlEscapeId( $id, $wgExternalInterwikiFragmentMode ); - - return $id; - } - - /** - * Helper for escapeIdFor*() functions. URL-escapes the ID if needed. - * - * @param string $id String to escape - * @param string $mode One of modes from $wgFragmentMode - * @return string - */ - private static function urlEscapeId( $id, $mode ) { - if ( $mode === 'html5' ) { - $id = urlencode( $id ); - $id = str_replace( '%3A', ':', $id ); - } return $id; } diff --git a/resources/src/mediawiki/mediawiki.util.js b/resources/src/mediawiki/mediawiki.util.js index 34f7eba473..fb34a89063 100644 --- a/resources/src/mediawiki/mediawiki.util.js +++ b/resources/src/mediawiki/mediawiki.util.js @@ -90,14 +90,9 @@ * @return {string} Encoded string */ escapeIdForLink: function ( str ) { - var mode = mw.config.get( 'wgFragmentMode' )[ 0 ], - id = escapeIdInternal( str, mode ); - - if ( mode === 'html5' ) { - id = encodeURIComponent( id ).replace( /%3A/g, ':' ); - } + var mode = mw.config.get( 'wgFragmentMode' )[ 0 ]; - return id; + return escapeIdInternal( str, mode ); }, /** diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index 3f93793263..bf2679f770 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -29307,10 +29307,10 @@ wgFragmentMode=[ 'html5', 'legacy' ] @@ -29320,9 +29320,9 @@ wgFragmentMode=[ 'html5', 'legacy' ]

    Тест[edit]

    тест[edit]

    Hey < # " > % : '[edit]

    -

    #Foo bar #foo Bar #Тест #тест #Hey < # " > % : ' -

    %F0%9F%92%A9 -

    #啤酒 #啤酒 +

    #Foo bar #foo Bar #Тест #тест #Hey < # " > % : ' +

    💩 +

    #啤酒 #啤酒

    !! end @@ -29401,10 +29401,10 @@ wgFragmentMode=[ 'html5' ] @@ -29414,8 +29414,8 @@ wgFragmentMode=[ 'html5' ]

    Тест[edit]

    тест[edit]

    Hey < # " > % : '[edit]

    -

    #Foo bar #foo Bar #Тест #тест #Hey < # " > % : ' -

    %F0%9F%92%A9 -

    #啤酒 #啤酒 +

    #Foo bar #foo Bar #Тест #тест #Hey < # " > % : ' +

    💩 +

    #啤酒 #啤酒

    !! end diff --git a/tests/phpunit/includes/SanitizerTest.php b/tests/phpunit/includes/SanitizerTest.php index d5066235ec..7472fb933d 100644 --- a/tests/phpunit/includes/SanitizerTest.php +++ b/tests/phpunit/includes/SanitizerTest.php @@ -456,7 +456,6 @@ class SanitizerTest extends MediaWikiTestCase { $text = 'foo тест_#%!\'()[]:<>'; $legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E'; $html5Encoded = 'foo_тест_#%!\'()[]:<>'; - $html5Escaped = 'foo_%D1%82%D0%B5%D1%81%D1%82_%23%25%21%27%28%29%5B%5D:%3C%3E'; $html5Experimental = 'foo_тест_!_()[]:<>'; // Settings: last element is $wgExternalInterwikiFragmentMode, the rest is $wgFragmentMode @@ -484,20 +483,20 @@ class SanitizerTest extends MediaWikiTestCase { // New world: HTML5 links, legacy fallbacks [ 'Attribute', $newLegacy, $text, $html5Encoded, Sanitizer::ID_PRIMARY ], [ 'Attribute', $newLegacy, $text, $legacyEncoded, Sanitizer::ID_FALLBACK ], - [ 'Link', $newLegacy, $text, $html5Escaped ], + [ 'Link', $newLegacy, $text, $html5Encoded ], [ 'ExternalInterwiki', $newLegacy, $text, $legacyEncoded ], // Distant future: no legacy fallbacks, but still linking to leagacy wikis [ 'Attribute', $new, $text, $html5Encoded, Sanitizer::ID_PRIMARY ], [ 'Attribute', $new, $text, false, Sanitizer::ID_FALLBACK ], - [ 'Link', $new, $text, $html5Escaped ], + [ 'Link', $new, $text, $html5Encoded ], [ 'ExternalInterwiki', $new, $text, $legacyEncoded ], // Just before the heat death of universe: external interwikis are also HTML5 \m/ [ 'Attribute', $allNew, $text, $html5Encoded, Sanitizer::ID_PRIMARY ], [ 'Attribute', $allNew, $text, false, Sanitizer::ID_FALLBACK ], - [ 'Link', $allNew, $text, $html5Escaped ], - [ 'ExternalInterwiki', $allNew, $text, $html5Escaped ], + [ 'Link', $allNew, $text, $html5Encoded ], + [ 'ExternalInterwiki', $allNew, $text, $html5Encoded ], // Someone flipped $wgExperimentalHtmlIds on [ 'Attribute', $experimentalLegacy, $text, $html5Experimental, Sanitizer::ID_PRIMARY ], @@ -508,7 +507,7 @@ class SanitizerTest extends MediaWikiTestCase { // Migration from $wgExperimentalHtmlIds to modern HTML5 [ 'Attribute', $newExperimental, $text, $html5Encoded, Sanitizer::ID_PRIMARY ], [ 'Attribute', $newExperimental, $text, $html5Experimental, Sanitizer::ID_FALLBACK ], - [ 'Link', $newExperimental, $text, $html5Escaped ], + [ 'Link', $newExperimental, $text, $html5Encoded ], [ 'ExternalInterwiki', $newExperimental, $text, $legacyEncoded ], ]; } diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 2efe9cd7f4..bb27626575 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -156,7 +156,7 @@ // Test cases are kept in sync with SanitizerTest.php var text = 'foo тест_#%!\'()[]:<>', legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E', - html5Escaped = 'foo_%D1%82%D0%B5%D1%81%D1%82_%23%25!\'()%5B%5D:%3C%3E', + html5Encoded = 'foo_тест_#%!\'()[]:<>', html5Experimental = 'foo_тест_!_()[]:<>', // Settings: this is wgFragmentMode legacy = [ 'legacy' ], @@ -172,13 +172,13 @@ // Transition to a new world: legacy links with HTML5 fallback [ legacyNew, text, legacyEncoded ], // New world: HTML5 links, legacy fallbacks - [ newLegacy, text, html5Escaped ], + [ newLegacy, text, html5Encoded ], // Distant future: no legacy fallbacks - [ allNew, text, html5Escaped ], + [ allNew, text, html5Encoded ], // Someone flipped wgExperimentalHtmlIds on [ experimentalLegacy, text, html5Experimental ], // Migration from wgExperimentalHtmlIds to modern HTML5 - [ newExperimental, text, html5Escaped ] + [ newExperimental, text, html5Encoded ] ], function ( index, testCase ) { mw.config.set( 'wgFragmentMode', testCase[ 0 ] ); -- 2.20.1