aboutsummaryrefslogtreecommitdiff
path: root/lib/url.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/url.js')
-rw-r--r--lib/url.js31
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 += ':';