* @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
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 () {
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' );
+} );