mediawiki.Uri: Don't double-escape fragments
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.Uri.js
index 0c47dbe..7f12835 100644 (file)
                 *  override each other (`true`) or automagically convert them to an array (`false`).
                 */
                function Uri( uri, options ) {
-                       var prop,
+                       var prop, hrefCur,
+                               hasOptions = ( options !== undefined ),
                                defaultUri = getDefaultUri();
 
                        options = typeof options === 'object' ? options : { strictMode: !!options };
                                                // Only copy direct properties, not inherited ones
                                                if ( uri.hasOwnProperty( prop ) ) {
                                                        // Deep copy object properties
-                                                       if ( $.isArray( uri[ prop ] ) || $.isPlainObject( uri[ prop ] ) ) {
+                                                       if ( Array.isArray( uri[ prop ] ) || $.isPlainObject( uri[ prop ] ) ) {
                                                                this[ prop ] = $.extend( true, {}, uri[ prop ] );
                                                        } else {
                                                                this[ prop ] = uri[ prop ];
                                                this.query = {};
                                        }
                                }
+                       } else if ( hasOptions ) {
+                               // We didn't get a URI in the constructor, but we got options.
+                               hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
+                               this.parse( hrefCur, options );
                        } else {
-                               // If we didn't get a URI in the constructor, use the default one.
+                               // We didn't get a URI or options in the constructor, use the default instance.
                                return defaultUri.clone();
                        }
 
 
                                // Apply parser regex and set all properties based on the result
                                matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
-                               $.each( properties, function ( i, property ) {
+                               properties.forEach( function ( property, i ) {
                                        uri[ property ] = matches[ i + 1 ];
                                } );
 
                                                                        q[ k ] = [ q[ k ] ];
                                                                }
                                                                // Add to the array
-                                                               if ( $.isArray( q[ k ] ) ) {
+                                                               if ( Array.isArray( q[ k ] ) ) {
                                                                        q[ k ].push( v );
                                                                }
                                                        }
                                        } );
                                }
                                uri.query = q;
+
+                               // Decode uri.fragment, otherwise it gets double-encoded when serializing
+                               if ( uri.fragment !== undefined ) {
+                                       uri.fragment = Uri.decode( uri.fragment );
+                               }
                        },
 
                        /**
                                var args = [];
                                $.each( this.query, function ( key, val ) {
                                        var k = Uri.encode( key ),
-                                               vals = $.isArray( val ) ? val : [ val ];
-                                       $.each( vals, function ( i, v ) {
+                                               vals = Array.isArray( val ) ? val : [ val ];
+                                       vals.forEach( function ( v ) {
                                                if ( v === null ) {
                                                        args.push( k );
                                                } else if ( k === 'title' ) {