Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

url: fix/8722 #8755

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions doc/api/url.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ Take a parsed URL object, and return a formatted URL string.
* `hostname` will only be used if `host` is absent.
* `port` will only be used if `host` is absent.
* `host` will be used in place of `hostname` and `port`
* `pathname` is treated the same with or without the leading `/` (slash)
* `search` will be used in place of `query`
* `pathname` is treated the same with or without the leading `/` (slash).
* `path` is treated the same with `pathname` but able to contain `query` as well.
* `search` will be used in place of `query`.
* `query` (object; see `querystring`) will only be used if `search` is absent.
* `search` is treated the same with or without the leading `?` (question mark)
* `hash` is treated the same with or without the leading `#` (pound sign, anchor)
* `search` is treated the same with or without the leading `?` (question mark).
* `hash` is treated the same with or without the leading `#` (pound sign, anchor).

## url.resolve(from, to)

Expand Down
31 changes: 26 additions & 5 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,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;
};

Expand All @@ -375,7 +375,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);
Expand All @@ -387,7 +387,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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's one last case to check here -- path is present, but has no ?..., so the output url should have no ?... -- i.e., the query, search, and pathname keys are ignored if path is given.

}
}

if (this.host) {
host = auth + this.host;
Expand All @@ -400,13 +419,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 += ':';

Expand Down
77 changes: 73 additions & 4 deletions test/simple/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ var formatTests = {

// `#`,`?` in path
'/path/to/%%23%3F+=&.txt?foo=theA1#bar' : {
href : '/path/to/%%23%3F+=&.txt?foo=theA1#bar',
href: '/path/to/%%23%3F+=&.txt?foo=theA1#bar',
pathname: '/path/to/%#?+=&.txt',
query: {
foo: 'theA1'
Expand All @@ -1095,7 +1095,7 @@ var formatTests = {

// `#`,`?` in path + `#` in query
'/path/to/%%23%3F+=&.txt?foo=the%231#bar' : {
href : '/path/to/%%23%3F+=&.txt?foo=the%231#bar',
href: '/path/to/%%23%3F+=&.txt?foo=the%231#bar',
pathname: '/path/to/%#?+=&.txt',
query: {
foo: 'the#1'
Expand All @@ -1110,7 +1110,7 @@ var formatTests = {
hostname: 'ex.com',
hash: '#frag',
search: '?abc=the#1?&foo=bar',
pathname: '/foo?100%m#r',
pathname: '/foo?100%m#r'
},

// `?` and `#` in search only
Expand All @@ -1120,8 +1120,77 @@ var formatTests = {
hostname: 'ex.com',
hash: '#frag',
search: '?abc=the#1?&foo=bar',
pathname: '/fooA100%mBr',
pathname: '/fooA100%mBr'
},

// path
'http://github.com/joyent/node#js1': {
href: 'http://github.com/joyent/node#js1',
protocol: 'http:',
hostname: 'github.com',
hash: '#js1',
path: '/joyent/node'
},

// pathname vs. path, path wins
'http://github.com/joyent/node2#js1': {
href: 'http://github.com/joyent/node2#js1',
protocol: 'http:',
hostname: 'github.com',
hash: '#js1',
path: '/joyent/node2',
pathname: '/joyent/node'
},

// pathname with query/search
'http://github.com/joyent/node?foo=bar#js2': {
href: 'http://github.com/joyent/node?foo=bar#js2',
protocol: 'http:',
hostname: 'github.com',
hash: '#js2',
path: '/joyent/node?foo=bar'
},

// path vs. query, path wins
'http://github.com/joyent/node?foo=bar2#js3': {
href: 'http://github.com/joyent/node?foo=bar2#js3',
protocol: 'http:',
hostname: 'github.com',
hash: '#js3',
path: '/joyent/node?foo=bar2',
query: {foo: 'bar'}
},

// path vs. search, path wins
'http://github.com/joyent/node?foo=bar3#js4': {
href: 'http://github.com/joyent/node?foo=bar3#js4',
protocol: 'http:',
hostname: 'github.com',
hash: '#js4',
path: '/joyent/node?foo=bar3',
search: '?foo=bar'
},

// path is present without ? vs. query given
'http://github.com/joyent/node#js5': {
href: 'http://github.com/joyent/node#js5',
protocol: 'http:',
hostname: 'github.com',
hash: '#js5',
path: '/joyent/node',
query: {foo: 'bar'}
},

// path is present without ? vs. search given
'http://github.com/joyent/node#js6': {
href: 'http://github.com/joyent/node#js6',
protocol: 'http:',
hostname: 'github.com',
hash: '#js6',
path: '/joyent/node',
search: '?foo=bar'
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @chrisdickinson, please review the above 5 tests from here, it follows your diagram and description: higher than lower. btw, thanks for your such detailed diagram, that's helpful and super awesome 🍒


};
for (var u in formatTests) {
var expect = formatTests[u].href;
Expand Down