mediawiki.Uri: Optional url use default location.
authorMark Holmquist <mtraceur@member.fsf.org>
Thu, 19 Jul 2012 20:55:21 +0000 (13:55 -0700)
committerTimo Tijhof <ttijhof@wikimedia.org>
Thu, 26 Jul 2012 09:17:56 +0000 (02:17 -0700)
This patch does three things:

1. If mw.Uri doesn't get any arguments, it will return the default
   URI. This is a good default, because it's something that will be
   used often, and the URI object that refers to the default location
   already exists locally, we're just using it to return a clone of
   that.

2. Fixes a strange logic error that prevents #1 from taking effect
   properly. The last clause in the 'if' statement on line 120
   meant that if uri is undefined, the if block would execute. I
   doubt that was intended, but it happened, and now it's fixed.
   There may be some error handling in mw.Uri.parse that could be
   removed since the URI passed in will almost never be undefined.

3. Adds tests for the new constructor values.

Change-Id: I9254b89bba41572c6dce72d2100d7085c37fb2c3

resources/mediawiki/mediawiki.Uri.js
tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js

index b871ac7..95e5e80 100644 (file)
                 * @constructor
                 * @param {Object|String} URI string, or an Object with appropriate properties (especially another URI object to clone).
                 * Object must have non-blank 'protocol', 'host', and 'path' properties.
+                * This parameter is optional. If omitted (or set to undefined, null or empty string), then an object will be created
+                * for the default uri of this constructor (e.g. document.location for mw.Uri in MediaWiki core).
                 * @param {Object|Boolean} Object with options, or (backwards compatibility) a boolean for strictMode
                 * - strictMode {Boolean} Trigger strict mode parsing of the url. Default: false
                 * - overrideKeys {Boolean} Wether to let duplicate query parameters override eachother (true) or automagically
                                overrideKeys: false
                        }, options );
 
-                       if ( uri !== undefined && uri !== null || uri !== '' ) {
+                       if ( uri !== undefined && uri !== null && uri !== '' ) {
                                if ( typeof uri === 'string' ) {
                                        this.parse( uri, options );
                                } else if ( typeof uri === 'object' ) {
                                                this.query = {};
                                        }
                                }
+                       } else {
+                               // If we didn't get a URI in the constructor, use the default one.
+                               return defaultUri.clone();
                        }
 
                        // protocol-relative URLs
index 9913fb9..9ab5c78 100644 (file)
@@ -344,27 +344,7 @@ test( 'Handle protocol-relative URLs', function () {
 
 test( 'Bad calls', function () {
        var uri;
-       expect( 5 );
-
-       raises(
-               function () {
-                       new mw.Uri();
-               },
-               function ( e ) {
-                       return e.message === 'Bad constructor arguments';
-               },
-               'throw error on no arguments to constructor'
-       );
-
-       raises(
-               function () {
-                       new mw.Uri( '' );
-               },
-               function ( e ) {
-                       return e.message === 'Bad constructor arguments';
-               },
-               'throw error on empty string as argument to constructor'
-       );
+       expect( 3 );
 
        raises(
                function () {
@@ -415,3 +395,23 @@ test( 'bug 35658', function () {
        equal( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' );
 
 } );
+
+QUnit.test( 'Constructor falls back to default location', function (assert) {
+       var testuri, MyUri, uri;
+       QUnit.expect( 4 );
+
+       testuri = 'http://example.org/w/index.php';
+       MyUri = mw.UriRelative( testuri );
+
+       uri = new MyUri();
+       assert.equal( uri.toString(), testuri, 'no arguments' );
+
+       uri = new MyUri( undefined );
+       assert.equal( uri.toString(), testuri, 'undefined' );
+
+       uri = new MyUri( null );
+       assert.equal( uri.toString(), testuri, 'null' );
+
+       uri = new MyUri( '' );
+       assert.equal( uri.toString(), testuri, 'empty string' );
+} );