Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix formatting inconsistencies in lib/url.js #1587

Closed
Closed
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
105 changes: 55 additions & 50 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ const noSerializePattern = new RegExp('^(?:' +
')$');

function urlParse(url, parseQueryString, slashesDenoteHost) {
if (url instanceof Url) return url;
if (url instanceof Url)
return url;

var u = new Url();
u.parse(url, parseQueryString, slashesDenoteHost);
return u;
}

Url.prototype.parse = function(str, parseQueryString, hostDenotesSlash) {
Url.prototype.parse = function(str, parseQueryString, slashesDenoteHost) {
if (typeof str !== 'string') {
throw new TypeError(`Parameter 'url' must be a string, not ` +
typeof str);
Expand All @@ -134,7 +135,7 @@ Url.prototype.parse = function(str, parseQueryString, hostDenotesSlash) {

// Javascript doesn't have host.
if (this._protocol !== 'javascript') {
start = this._parseHost(str, trimmedStart, start, end, hostDenotesSlash);
start = this._parseHost(str, trimmedStart, start, end, slashesDenoteHost);
var proto = this._protocol;
if (!this._hostname &&
(this.slashes || (proto && !_slashProtocols[proto])))
Expand Down Expand Up @@ -216,32 +217,30 @@ Url.prototype.format = function() {

// Cache the result of the getter function.
var q = this.query;
if (q !== null && typeof q === 'object') {
if (q !== null && typeof q === 'object')
query = querystring.stringify(q);
}

if (!search) {
if (!search)
search = query ? '?' + query : '';
}

if (protocol && protocol.charCodeAt(protocol.length - 1) !== 0x3A /*':'*/)
protocol += ':';

if (this.host) {
if (this.host)
host = auth + this.host;
} else if (hostname) {
else if (hostname)
host = auth + hostname + (port ? ':' + port : '');
}

var slashes = this.slashes ||
((!protocol || _slashProtocols[protocol]) && host !== false);

if (protocol) scheme = protocol + (slashes ? '//' : '');
else if (slashes) scheme = '//';
if (protocol)
scheme = protocol + (slashes ? '//' : '');
else if (slashes)
scheme = '//';

if (slashes && pathname && pathname.charCodeAt(0) !== 0x2F /*'/'*/) {
if (slashes && pathname && pathname.charCodeAt(0) !== 0x2F /*'/'*/)
pathname = '/' + pathname;
}
if (search && search.charCodeAt(0) !== 0x3F /*'?'*/)
search = '?' + search;
if (hash && hash.charCodeAt(0) !== 0x23 /*'#'*/)
Expand All @@ -254,7 +253,8 @@ Url.prototype.format = function() {
};

function urlResolveObject(source, relative) {
if (!source) return relative;
if (!source)
return relative;
return urlParse(source, false, true).resolveObject(relative);
}

Expand Down Expand Up @@ -307,10 +307,14 @@ Url.prototype.resolveObject = function(relative) {
relative._protocol !== 'javascript') {
var relPath = (relative._pathname || '').split('/');
while (relPath.length && !(relative._host = relPath.shift()));
if (!relative._host) relative._host = '';
if (!relative._hostname) relative._hostname = '';
if (relPath[0] !== '') relPath.unshift('');
if (relPath.length < 2) relPath.unshift('');
if (!relative._host)
relative._host = '';
if (!relative._hostname)
relative._hostname = '';
if (relPath[0] !== '')
relPath.unshift('');
if (relPath.length < 2)
relPath.unshift('');
result._pathname = relPath.join('/');
} else {
result._pathname = relative._pathname;
Expand Down Expand Up @@ -351,16 +355,20 @@ Url.prototype.resolveObject = function(relative) {
result._hostname = '';
result._port = -1;
if (result._host) {
if (srcPath[0] === '') srcPath[0] = result._host;
else srcPath.unshift(result._host);
if (srcPath[0] === '')
srcPath[0] = result._host;
else
srcPath.unshift(result._host);
}
result._host = '';
if (relative._protocol) {
relative._hostname = '';
relative._port = -1;
if (relative._host) {
if (relPath[0] === '') relPath[0] = relative._host;
else relPath.unshift(relative._host);
if (relPath[0] === '')
relPath[0] = relative._host;
else
relPath.unshift(relative._host);
}
relative._host = '';
}
Expand All @@ -378,7 +386,8 @@ Url.prototype.resolveObject = function(relative) {
} else if (relPath.length) {
// It's relative
// throw away the existing file, and take the new path instead.
if (!srcPath) srcPath = [];
if (!srcPath)
srcPath = [];
srcPath.pop();
srcPath = srcPath.concat(relPath);
result._search = relative._search;
Expand Down Expand Up @@ -437,19 +446,17 @@ Url.prototype.resolveObject = function(relative) {

// If the path is allowed to go above the root, restore leading ..s.
if (!mustEndAbs && !removeAllDots) {
for (; up--; up) {
for (; up--; up)
srcPath.unshift('..');
}
}

if (mustEndAbs && srcPath[0] !== '' &&
(!srcPath[0] || srcPath[0].charCodeAt(0) !== 0x2F /*'/'*/)) {
srcPath.unshift('');
}

if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/'))
srcPath.push('');
}

var isAbsolute = srcPath[0] === '' ||
(srcPath[0] && srcPath[0].charCodeAt(0) === 0x2F /*'/'*/);
Expand All @@ -471,9 +478,8 @@ Url.prototype.resolveObject = function(relative) {

mustEndAbs = mustEndAbs || (result._host && srcPath.length);

if (mustEndAbs && !isAbsolute) {
if (mustEndAbs && !isAbsolute)
srcPath.unshift('');
}

result._pathname = srcPath.length === 0 ? null : srcPath.join('/');
result._auth = relative._auth || result._auth;
Expand All @@ -482,7 +488,6 @@ Url.prototype.resolveObject = function(relative) {
return result;
};


Url.prototype._parseProtocol = function(str, start, end) {
var needsLowerCasing = false;
var protocolCharacters = _protocolCharacters;
Expand All @@ -494,7 +499,8 @@ Url.prototype._parseProtocol = function(str, start, end) {
if (i - start === 0)
return start;
var protocol = str.slice(start, i);
if (needsLowerCasing) protocol = protocol.toLowerCase();
if (needsLowerCasing)
protocol = protocol.toLowerCase();
this._protocol = protocol;
return i + 1;
} else if (protocolCharacters[ch] === 1) {
Expand All @@ -510,9 +516,8 @@ Url.prototype._parseProtocol = function(str, start, end) {

Url.prototype._parseAuth = function(str, start, end, decode) {
var auth = str.slice(start, end + 1);
if (decode) {
if (decode)
auth = decodeURIComponent(auth);
}
this._auth = auth;
};

Expand Down Expand Up @@ -585,7 +590,8 @@ Url.prototype._parseHost = function(str,
// The string starts with // or \\.
if (start === trimmedStart) {
// The string is just '//' or '\\'.
if (end - start === 1) return start;
if (end - start === 1)
return start;
// If slashes do not denote host and there is no auth,
// there is no host when the string starts with // or \\.
var hasAuth =
Expand Down Expand Up @@ -699,9 +705,8 @@ Url.prototype._parseHost = function(str,
}
} else if (ch >= 0x7B /*'{'*/) {
if (ch <= 0x7E /*'~'*/) {
if (hostEndingCharactersNoPrependSlash[ch] === 0) {
if (hostEndingCharactersNoPrependSlash[ch] === 0)
this._prependSlash = true;
}
hostNameEnd = i - 1;
break;
}
Expand Down Expand Up @@ -734,9 +739,8 @@ Url.prototype._parseHost = function(str,
};

Url.prototype._copyPropsTo = function(target, noProtocol) {
if (!noProtocol) {
if (!noProtocol)
target._protocol = this._protocol;
}
// Forces getter recalculation.
target._href = null;
target._host = this._host;
Expand Down Expand Up @@ -824,11 +828,10 @@ Url.prototype._parsePath = function(str, start, end) {
}

var path;
if (escape) {
if (escape)
path = getComponentEscaped(str, pathStart, pathEnd, false);
} else {
else
path = str.slice(pathStart, pathEnd + 1);
}

this._pathname = prePath === '' ?
(this._prependSlash ? '/' + path : path) : prePath + path;
Expand All @@ -847,8 +850,9 @@ Url.prototype._parseQuery = function(str, start, end) {
this._parseHash(str, i, end);
queryEnd = i - 1;
break;
} else if (!escape && autoEscapeCharacters[ch] === 1)
} else if (!escape && autoEscapeCharacters[ch] === 1) {
escape = true;
}
}

if (queryStart > queryEnd) {
Expand Down Expand Up @@ -917,7 +921,8 @@ Object.defineProperty(Url.prototype, 'query', {
},
set: function(v) {
if (typeof v === 'string') {
if (v !== '') this.search = '?' + v;
if (v !== '')
this.search = '?' + v;
this._query = v;
} else if (v !== null && typeof v === 'object') {
var string = querystring.stringify(v);
Expand Down Expand Up @@ -1085,9 +1090,8 @@ Object.defineProperty(Url.prototype, 'hash', {
this._hash = null;
} else {
var hash = '' + v;
if (hash.charCodeAt(0) !== 0x23 /*'#'*/) {
if (hash.charCodeAt(0) !== 0x23 /*'#'*/)
hash = '#' + hash;
}
this._hash = hash;
this._href = '';
}
Expand All @@ -1111,9 +1115,8 @@ Object.defineProperty(Url.prototype, 'search', {

this._search = search;

if (this._parsesQueryStrings) {
if (this._parsesQueryStrings)
this.query = querystring.parse(search.slice(1));
}
this._href = '';
}
},
Expand Down Expand Up @@ -1229,12 +1232,14 @@ function getComponentEscaped(str, start, end, isAfterQuery) {
var escaped = autoEscapeMap[ch];

if (escaped !== '' && escaped !== undefined) {
if (cur < i) ret += str.slice(cur, i);
if (cur < i)
ret += str.slice(cur, i);
ret += escaped;
cur = i + 1;
}
}
if (cur < i + 1) ret += str.slice(cur, i);
if (cur < i + 1)
ret += str.slice(cur, i);
return ret;
}

Expand Down