From fb8101f1482838143b347fe9280dbf4929f5c476 Mon Sep 17 00:00:00 2001 From: Mark Holmquist Date: Thu, 19 Jul 2012 13:55:21 -0700 Subject: [PATCH] mediawiki.Uri: Optional url use default location. 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 | 7 +++- .../resources/mediawiki/mediawiki.Uri.test.js | 42 +++++++++---------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/resources/mediawiki/mediawiki.Uri.js b/resources/mediawiki/mediawiki.Uri.js index b871ac71e4..95e5e80079 100644 --- a/resources/mediawiki/mediawiki.Uri.js +++ b/resources/mediawiki/mediawiki.Uri.js @@ -105,6 +105,8 @@ * @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 @@ -117,7 +119,7 @@ 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' ) { @@ -137,6 +139,9 @@ this.query = {}; } } + } else { + // If we didn't get a URI in the constructor, use the default one. + return defaultUri.clone(); } // protocol-relative URLs diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js index 9913fb9b25..9ab5c78758 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js @@ -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' ); +} ); -- 2.20.1