mediawiki.util: Decode parentheses and bang in wikiUrlencode
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 14 May 2014 15:08:23 +0000 (17:08 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 14 May 2014 15:08:23 +0000 (17:08 +0200)
Matches behaviour of wfUrlencode() and creates prettier urls.

Change-Id: I9b3ecda19f743cd87e790d99d50dd6b730e5ee21

resources/src/mediawiki/mediawiki.util.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index 298415c..e2fa123 100644 (file)
 
                /**
                 * Encode page titles for use in a URL
+                *
                 * We want / and : to be included as literal characters in our title URLs
-                * as they otherwise fatally break the title
+                * as they otherwise fatally break the title.
+                *
+                * The others are decoded because we can, it's prettier and matches behaviour
+                * of `wfUrlencode` in PHP.
                 *
                 * @param {string} str String to be encoded.
                 */
                wikiUrlencode: function ( str ) {
                        return util.rawurlencode( str )
-                               .replace( /%20/g, '_' ).replace( /%3A/g, ':' ).replace( /%2F/g, '/' );
+                               .replace( /%20/g, '_' )
+                               // wfUrlencode replacements
+                               .replace( /%3B/g, ';' )
+                               .replace( /%40/g, '@' )
+                               .replace( /%24/g, '$' )
+                               .replace( /%21/g, '!' )
+                               .replace( /%2A/g, '*' )
+                               .replace( /%28/g, '(' )
+                               .replace( /%29/g, ')' )
+                               .replace( /%2C/g, ',' )
+                               .replace( /%2F/g, '/' )
+                               .replace( /%3A/g, ':' );
                },
 
                /**
index b2587be..1526679 100644 (file)
                assert.equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
        } );
 
-       QUnit.test( 'wikiUrlencode', 1, function ( assert ) {
+       QUnit.test( 'wikiUrlencode', 10, function ( assert ) {
                assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
+               // See also wfUrlencodeTest.php#provideURLS
+               $.each( {
+                       '+': '%2B',
+                       '&': '%26',
+                       '=': '%3D',
+                       ':': ':',
+                       ';@$-_.!*': ';@$-_.!*',
+                       '/': '/',
+                       '[]': '%5B%5D',
+                       '<>': '%3C%3E',
+                       '\'': '%27'
+               }, function ( input, output ) {
+                       assert.equal( mw.util.wikiUrlencode( input ), output );
+               } );
        } );
 
        QUnit.test( 'getUrl', 4, function ( assert ) {
@@ -30,7 +44,7 @@
                assert.equal( href, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
 
                href = mw.util.getUrl( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
-               assert.equal( href, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
+               assert.equal( href, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_!_(test)/subpage',
                        'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
 
                href = mw.util.getUrl();