Path: blob/master/webroot/rsrc/externals/javelin/lib/URI.js
12242 views
/**1* @provides javelin-uri2* @requires javelin-install3* javelin-util4* javelin-stratcom5*6* @javelin-installs JX.$U7*8* @javelin9*/1011/**12* Handy convenience function that returns a @{class:JX.URI} instance. This13* allows you to write things like:14*15* JX.$U('http://zombo.com/').getDomain();16*17* @param string Unparsed URI.18* @return @{class:JX.URI} JX.URI instance.19*/20JX.$U = function(uri) {21return new JX.URI(uri);22};2324/**25* Convert a string URI into a maleable object.26*27* var uri = new JX.URI('http://www.example.com/asdf.php?a=b&c=d#anchor123');28* uri.getProtocol(); // http29* uri.getDomain(); // www.example.com30* uri.getPath(); // /asdf.php31* uri.getQueryParams(); // {a: 'b', c: 'd'}32* uri.getFragment(); // anchor12333*34* ...and back into a string:35*36* uri.setFragment('clowntown');37* uri.toString() // http://www.example.com/asdf.php?a=b&c=d#clowntown38*/39JX.install('URI', {40statics : {41_uriPattern : /(?:([^:\/?#]+):)?(?:\/\/([^:\/?#]*)(?::(\d*))?)?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,4243/**44* Convert a Javascript object into an HTTP query string.45*46* @param Object Map of query keys to values.47* @return String HTTP query string, like 'cow=quack&duck=moo'.48*/49_defaultQuerySerializer : function(obj) {50var kv_pairs = [];51for (var key in obj) {52if (obj[key] !== null) {53var value = encodeURIComponent(obj[key]);54kv_pairs.push(encodeURIComponent(key) + (value ? '=' + value : ''));55}56}5758return kv_pairs.join('&');59},6061_decode : function(str) {62return decodeURIComponent(str.replace(/\+/g, ' '));63}64},6566/**67* Construct a URI68*69* Accepts either absolute or relative URIs. Relative URIs may have protocol70* and domain properties set to undefined71*72* @param string absolute or relative URI73*/74construct : function(uri) {75// need to set the default value here rather than in the properties map,76// or else we get some crazy global state breakage77this.setQueryParams({});7879if (uri) {80// parse the url81var result = JX.URI._uriPattern.exec(uri);8283// fallback to undefined because IE has weird behavior otherwise84this.setProtocol(result[1] || undefined);85this.setDomain(result[2] || undefined);86this.setPort(result[3] || undefined);87var path = result[4];88var query = result[5];89this.setFragment(result[6] || undefined);9091// parse the path92this.setPath(path.charAt(0) == '/' ? path : '/' + path);9394// parse the query data95if (query && query.length) {96var dict = {};97var parts = query.split('&');98for (var ii = 0; ii < parts.length; ii++) {99var part = parts[ii];100if (!part.length) {101continue;102}103var pieces = part.split('=');104var name = pieces[0];105if (!name.length) {106continue;107}108var value = pieces.slice(1).join('=') || '';109dict[JX.URI._decode(name)] = JX.URI._decode(value);110}111this.setQueryParams(dict);112}113}114},115116properties : {117protocol: undefined,118port: undefined,119path: undefined,120queryParams: undefined,121fragment: undefined,122querySerializer: undefined123},124125members : {126_domain: undefined,127128/**129* Append and override query data values130* Remove a query key by setting it undefined131*132* @param map133* @return @{JX.URI} self134*/135addQueryParams : function(map) {136JX.copy(this.getQueryParams(), map);137return this;138},139140/**141* Set a specific query parameter142* Remove a query key by setting it undefined143*144* @param string145* @param wild146* @return @{JX.URI} self147*/148setQueryParam : function(key, value) {149var map = {};150map[key] = value;151return this.addQueryParams(map);152},153154/**155* Set the domain156*157* This function checks the domain name to ensure that it is safe for158* browser consumption.159*/160setDomain : function(domain) {161var re = new RegExp(162// For the bottom 128 code points, we use a strict whitelist of163// characters that are allowed by all browsers: -.0-9:A-Z[]_a-z164'[\\x00-\\x2c\\x2f\\x3b-\\x40\\x5c\\x5e\\x60\\x7b-\\x7f' +165// In IE, these chararacters cause problems when entity-encoded.166'\\uFDD0-\\uFDEF\\uFFF0-\\uFFFF' +167// In Safari, these characters terminate the hostname.168'\\u2047\\u2048\\uFE56\\uFE5F\\uFF03\\uFF0F\\uFF1F]');169if (re.test(domain)) {170JX.$E('JX.URI.setDomain(...): invalid domain specified.');171}172this._domain = domain;173return this;174},175176getDomain : function() {177return this._domain;178},179180getRelativeURI: function() {181return JX.$U(this.toString())182.setProtocol(null)183.setPort(null)184.setDomain(null)185.toString();186},187188toString : function() {189if (__DEV__) {190if (this.getPath() && this.getPath().charAt(0) != '/') {191JX.$E(192'JX.URI.toString(): ' +193'Path does not begin with a "/" which means this URI will likely' +194'be malformed. Ensure any string passed to .setPath() leads "/"');195}196}197var str = '';198if (this.getProtocol()) {199str += this.getProtocol() + '://';200}201str += this.getDomain() || '';202203if (this.getPort()) {204str += ':' + this.getPort();205}206207// If there is a domain or a protocol, we need to provide '/' for the208// path. If we don't have either and also don't have a path, we can omit209// it to produce a partial URI without path information which begins210// with "?", "#", or is empty.211str += this.getPath() || (str ? '/' : '');212213str += this._getQueryString();214if (this.getFragment()) {215str += '#' + this.getFragment();216}217return str;218},219220_getQueryString : function() {221var str = (222this.getQuerySerializer() || JX.URI._defaultQuerySerializer223)(this.getQueryParams());224return str ? '?' + str : '';225},226227/**228* Redirect the browser to another page by changing the window location. If229* the URI is empty, reloads the current page.230*231* You can install a Stratcom listener for the 'go' event if you need to log232* or prevent redirects.233*234* @return void235*/236go : function() {237var uri = this.toString();238if (JX.Stratcom.invoke('go', null, {uri: uri}).getPrevented()) {239return;240}241if (!uri) {242// window.location.reload clears cache in Firefox.243uri = window.location.pathname + (window.location.query || '');244}245window.location = uri;246}247248}249});250251252