Constructor
new mw.Uri(uriopt, optionsopt)
#
Create and manipulate MediaWiki URIs.
Intended to be minimal, but featureful; do not expect full RFC 3986 compliance. The use cases we have in mind are constructing 'next page' or 'previous page' URLs, detecting whether we need to use cross-domain proxies for an API, constructing simple URL-based API calls, etc. Parsing here is regex-based, so may not work on all URIs, but is good enough for most.
You can modify the properties directly, then use the toString
method to extract the full URI
string again. Example:
var uri = new mw.Uri( 'http://example.com/mysite/mypage.php?quux=2' );
if ( uri.host == 'example.com' ) {
uri.host = 'foo.example.com';
uri.extend( { bar: 1 } );
$( 'a#id1' ).attr( 'href', uri );
// anchor with id 'id1' now links to http://foo.example.com/mysite/mypage.php?bar=1&quux=2
$( 'a#id2' ).attr( 'href', uri.clone().extend( { bar: 3, pif: 'paf' } ) );
// anchor with id 'id2' now links to http://foo.example.com/mysite/mypage.php?bar=3&quux=2&pif=paf
}
Given a URI like
http://usr:pwd@www.example.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=&test3=value+%28escaped%29&r=1&r=2#top
the returned object will have the following properties:
protocol 'http'
user 'usr'
password 'pwd'
host 'www.example.com'
port '81'
path '/dir/dir.2/index.htm'
query {
q1: '0',
test1: null,
test2: '',
test3: 'value (escaped)'
r: ['1', '2']
}
fragment 'top'
(N.b., 'password' is technically not allowed for HTTP URIs, but it is possible with other kinds of URIs.)
Parsing based on parseUri 1.2.2 (c) Steven Levithan http://stevenlevithan.com, MIT License. http://stevenlevithan.com/demo/parseuri/js/
Construct a new URI object. Throws error if arguments are illegal/impossible, or otherwise don't parse.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
uri |
Object | string |
<optional> |
URI string, or an Object with appropriate properties (especially
another URI object to clone). Object must have non-blank |
options |
mw.Uri.UriOptions | boolean |
<optional> |
Object with options, or (backwards compatibility) a boolean for strictMode |
- Source:
Throws:
-
when the query string or fragment contains an unknown % sequence
- Type
- Error
Properties
fragment :string|undefined
#
undefined
#
top
.
host :string
#
www.example.com
(always present).
password :string|undefined
#
undefined
#
pwd
.
path :string
#
For example /dir/dir.2/index.htm
(always present).
Type:
- string
- Source:
/dir/dir.2/index.htm
(always present).
port :string|undefined
#
undefined
#
81
.
protocol :string
#
http
(always present).
query :Object
#
For example { a: '0', b: '', c: 'value' }
(always present).
Type:
- Object
- Source:
{ a: '0', b: '', c: 'value' }
(always present).
user :string|undefined
#
undefined
#
usr
.
Methods
clone() → {Object}
#
Clone this URI.
- Source:
Returns:
New URI object with same properties
- Type
- Object
extend(parameters) → {Object}
#
Extend the query section of the URI with new parameters.
Parameters:
Name | Type | Description |
---|---|---|
parameters |
Object | Query parameters to add to ours (or to override ours with) as an object |
- Source:
Returns:
This URI object
- Type
- Object
getAuthority() → {string}
#
Get the userInfo, host and port section of the URI.
In most real-world URLs this is simply the hostname, but the definition of 'authority' section is more general.
- Source:
Returns:
- Type
- string
getHostPort() → {string}
#
getQueryString() → {string}
#
Get the query arguments of the URL, encoded into a string.
Does not preserve the original order of arguments passed in the URI. Does handle escaping.
- Source:
Returns:
- Type
- string
getRelativePath() → {string}
#
Get everything after the authority section of the URI.
- Source:
Returns:
- Type
- string
getUserInfo() → {string}
#
toString() → {string}
#
Get the entire URI string.
Note that the output may not be precisely the same as the constructor input, due to order of query arguments. Note also that the fragment is not always roundtripped as-is; some characters will become encoded, including the slash character, which can cause problems with e.g. mediawiki.router. It is recommended to use the native URL class (via web2017-polyfills, which loads a polyfill if needed) in contexts where the fragment is important.
- Source:
Returns:
The URI string
- Type
- string
decode(s) → {string}static
#
Decode a url encoded value.
Reversed encode
. Standard decodeURIComponent, with addition of replacing
+
with a space.
Parameters:
Name | Type | Description |
---|---|---|
s |
string | String to decode |
- Source:
Throws:
-
when the string contains an unknown % sequence
- Type
- Error
Returns:
Decoded string
- Type
- string
encode(s) → {string}static
#
Encode a value for inclusion in a url.
Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more
compliant with RFC 3986. Similar to rawurlencode from PHP and our JS library
mw.util.rawurlencode
, except this also replaces spaces with +
.
Parameters:
Name | Type | Description |
---|---|---|
s |
string | String to encode |
- Source:
Returns:
Encoded string for URI
- Type
- string
Type Definitions
UriOptions
#
Options for mw.Uri object.
Type:
- Object
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
strictMode |
boolean |
<optional> |
false | Trigger strict mode parsing of the url. |
overrideKeys |
boolean |
<optional> |
false | Whether to let duplicate query parameters
override each other ( |
arrayParams |
boolean |
<optional> |
false | Whether to parse array query parameters (e.g.
|
- Source: