*
*/
-( function( $, mw ) {
+( function ( mw, $ ) {
/**
* Function that's useful when constructing the URI string -- we frequently encounter the pattern of
function cat( pre, val, post, raw ) {
if ( val === undefined || val === null || val === '' ) {
return '';
- } else {
- return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
}
+ return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
}
// Regular expressions to parse many common URIs.
'host', // www.test.com
'port', // 81
'path', // /dir/dir.2/index.htm
- 'query', // q1=0&&test1&test2=value (will become { q1: 0, test1: '', test2: 'value' } )
+ 'query', // q1=0&&test1&test2=value (will become { q1: '0', test1: '', test2: 'value' } )
'fragment' // top
];
* We use a factory to inject a document location, for relative URLs, including protocol-relative URLs.
* so the library is still testable & purely functional.
*/
- mw.UriRelative = function( documentLocation ) {
+ mw.UriRelative = function ( documentLocation ) {
+ var defaultUri;
/**
* Constructs URI object. Throws error if arguments are illegal/impossible, or otherwise don't parse.
* @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.
- * @param {Boolean} strict mode (when parsing a string)
+ * @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.
+ * @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
+ * convert to an array (false, default).
*/
- function Uri( uri, strictMode ) {
- strictMode = !!strictMode;
+ function Uri( uri, options ) {
+ options = typeof options === 'object' ? options : { strictMode: !!options };
+ options = $.extend( {
+ strictMode: false,
+ overrideKeys: false
+ }, options );
+
if ( uri !== undefined && uri !== null || uri !== '' ) {
if ( typeof uri === 'string' ) {
- this._parse( uri, strictMode );
+ this.parse( uri, options );
} else if ( typeof uri === 'object' ) {
- var _this = this;
- $.each( properties, function( i, property ) {
- _this[property] = uri[property];
+ var uriObj = this;
+ $.each( properties, function ( i, property ) {
+ uriObj[property] = uri[property];
} );
if ( this.query === undefined ) {
this.query = {};
// protocol-relative URLs
if ( !this.protocol ) {
- this.protocol = defaultProtocol;
+ this.protocol = defaultUri.protocol;
+ }
+ // No host given:
+ if ( !this.host ) {
+ this.host = defaultUri.host;
+ // port ?
+ if ( !this.port ) {
+ this.port = defaultUri.port;
+ }
+ }
+ if ( this.path && this.path.charAt( 0 ) !== '/' ) {
+ // A real relative URL, relative to defaultUri.path. We can't really handle that since we cannot
+ // figure out whether the last path compoennt of defaultUri.path is a directory or a file.
+ throw new Error( 'Bad constructor arguments' );
}
-
if ( !( this.protocol && this.host && this.path ) ) {
throw new Error( 'Bad constructor arguments' );
}
* @param {String} string
* @return {String} encoded for URI
*/
- Uri.encode = function( s ) {
+ Uri.encode = function ( s ) {
return encodeURIComponent( s )
.replace( /!/g, '%21').replace( /'/g, '%27').replace( /\(/g, '%28')
.replace( /\)/g, '%29').replace( /\*/g, '%2A')
* @param {String} string encoded for URI
* @return {String} decoded string
*/
- Uri.decode = function( s ) {
- return decodeURIComponent( s ).replace( /\+/g, ' ' );
+ Uri.decode = function ( s ) {
+ return decodeURIComponent( s.replace( /\+/g, '%20' ) );
};
Uri.prototype = {
/**
* Parse a string and set our properties accordingly.
* @param {String} URI
- * @param {Boolean} strictness
+ * @param {Object} options
* @return {Boolean} success
*/
- _parse: function( str, strictMode ) {
- var matches = parser[ strictMode ? 'strict' : 'loose' ].exec( str );
- var uri = this;
- $.each( properties, function( i, property ) {
+ parse: function ( str, options ) {
+ var q,
+ uri = this,
+ matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
+ $.each( properties, function ( i, property ) {
uri[ property ] = matches[ i+1 ];
} );
// uri.query starts out as the query string; we will parse it into key-val pairs then make
// that object the "query" property.
// we overwrite query in uri way to make cloning easier, it can use the same list of properties.
- var q = {};
+ q = {};
// using replace to iterate over a string
if ( uri.query ) {
uri.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, function ($0, $1, $2, $3) {
+ var k, v;
if ( $1 ) {
- var k = Uri.decode( $1 );
- var v = ( $2 === '' || $2 === undefined ) ? null : Uri.decode( $3 );
- if ( typeof q[ k ] === 'string' ) {
- q[ k ] = [ q[ k ] ];
- }
- if ( typeof q[ k ] === 'object' ) {
- q[ k ].push( v );
- } else {
+ k = Uri.decode( $1 );
+ v = ( $2 === '' || $2 === undefined ) ? null : Uri.decode( $3 );
+
+ // If overrideKeys, always (re)set top level value.
+ // If not overrideKeys but this key wasn't set before, then we set it as well.
+ if ( options.overrideKeys || q[ k ] === undefined ) {
q[ k ] = v;
+
+ // Use arrays if overrideKeys is false and key was already seen before
+ } else {
+ // Once before, still a string, turn into an array
+ if ( typeof q[ k ] === 'string' ) {
+ q[ k ] = [ q[ k ] ];
+ }
+ // Add to the array
+ if ( $.isArray( q[ k ] ) ) {
+ q[ k ].push( v );
+ }
}
}
} );
* Returns user and password portion of a URI.
* @return {String}
*/
- getUserInfo: function() {
+ getUserInfo: function () {
return cat( '', this.user, cat( ':', this.password, '' ) );
},
* Gets host and port portion of a URI.
* @return {String}
*/
- getHostPort: function() {
+ getHostPort: function () {
return this.host + cat( ':', this.port, '' );
},
* In most real-world URLs, this is simply the hostname, but it is more general.
* @return {String}
*/
- getAuthority: function() {
+ getAuthority: function () {
return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
},
* Does not preserve the order of arguments passed into the URI. Does handle escaping.
* @return {String}
*/
- getQueryString: function() {
+ getQueryString: function () {
var args = [];
- $.each( this.query, function( key, val ) {
- var k = Uri.encode( key );
- var vals = val === null ? [ null ] : $.makeArray( val );
- $.each( vals, function( i, v ) {
+ $.each( this.query, function ( key, val ) {
+ var k = Uri.encode( key ),
+ vals = val === null ? [ null ] : $.makeArray( val );
+ $.each( vals, function ( i, v ) {
args.push( k + ( v === null ? '' : '=' + Uri.encode( v ) ) );
} );
} );
* Returns everything after the authority section of the URI
* @return {String}
*/
- getRelativePath: function() {
+ getRelativePath: function () {
return this.path + cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment, '' );
},
* Gets the entire URI string. May not be precisely the same as input due to order of query arguments.
* @return {String} the URI string
*/
- toString: function() {
+ toString: function () {
return this.protocol + '://' + this.getAuthority() + this.getRelativePath();
},
* Clone this URI
* @return {Object} new URI object with same properties
*/
- clone: function() {
+ clone: function () {
return new Uri( this );
},
* @param {Object} query parameters in key-val form to override or add
* @return {Object} this URI object
*/
- extend: function( parameters ) {
+ extend: function ( parameters ) {
$.extend( this.query, parameters );
return this;
}
};
- var defaultProtocol = ( new Uri( documentLocation ) ).protocol;
+ defaultUri = new Uri( documentLocation );
return Uri;
};
- // inject the current document location, for relative URLs
- mw.Uri = mw.UriRelative( document.location.href );
-
-
+ // if we are running in a browser, inject the current document location, for relative URLs
+ if ( document && document.location && document.location.href ) {
+ mw.Uri = mw.UriRelative( document.location.href );
+ }
-} )( jQuery, mediaWiki );
+}( mediaWiki, jQuery ) );