diff options
author | Yazhong Liu <yorkiefixer@gmail.com> | 2014-11-20 23:46:38 +0800 |
---|---|---|
committer | Chris Dickinson <christopher.s.dickinson@gmail.com> | 2014-12-02 11:32:08 -0800 |
commit | ba687f6eb431ce7fd5e324ef495d2e133a3f254b (patch) | |
tree | df7825a7e63e59aeeccf811877a1509f06910027 /lib/url.js | |
parent | d24b7b85006dc3dd8e07dc6d4a0c630329833456 (diff) | |
download | android-node-v8-ba687f6eb431ce7fd5e324ef495d2e133a3f254b.tar.gz android-node-v8-ba687f6eb431ce7fd5e324ef495d2e133a3f254b.tar.bz2 android-node-v8-ba687f6eb431ce7fd5e324ef495d2e133a3f254b.zip |
url: support `path` for url.format
this adds support for a "path" field that overrides
"query", "search", and "pathname" if given.
Fixes: https://github.com/joyent/node/issues/8722
PR-URL: https://github.com/joyent/node/pull/8755
Reviewed-by: Chris Dickinson <christopher.s.dickinson@gmail.com>
Diffstat (limited to 'lib/url.js')
-rw-r--r-- | lib/url.js | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/url.js b/lib/url.js index 6a6a19d81e..56b1be9328 100644 --- a/lib/url.js +++ b/lib/url.js @@ -362,7 +362,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { } // finally, reconstruct the href based on what has been validated. - this.href = this.format(); + this.href = this.format(parseQueryString); return this; }; @@ -377,7 +377,7 @@ function urlFormat(obj) { return obj.format(); } -Url.prototype.format = function() { +Url.prototype.format = function(parseQueryString) { var auth = this.auth || ''; if (auth) { auth = encodeURIComponent(auth); @@ -389,7 +389,26 @@ Url.prototype.format = function() { pathname = this.pathname || '', hash = this.hash || '', host = false, - query = ''; + query = '', + search = ''; + + if (this.path) { + var qm = this.path.indexOf('?'); + if (qm !== -1) { + query = this.path.slice(qm + 1); + search = '?' + query; + pathname = this.path.slice(0, qm); + } else { + if (parseQueryString) { + this.query = {}; + this.search = ''; + } else { + this.query = null; + this.search = null; + } + pathname = this.path; + } + } if (this.host) { host = auth + this.host; @@ -402,13 +421,15 @@ Url.prototype.format = function() { } } - if (this.query && + if (!query && + this.query && util.isObject(this.query) && Object.keys(this.query).length) { query = querystring.stringify(this.query); } - var search = this.search || (query && ('?' + query)) || ''; + if (!search) + search = this.search || (query && ('?' + query)) || ''; if (protocol && protocol.substr(-1) !== ':') protocol += ':'; |