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

url: improve resolveObject with ObjectAssign #54092

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
26 changes: 10 additions & 16 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
Boolean,
Int8Array,
ObjectAssign,
ObjectKeys,
StringPrototypeCharCodeAt,
decodeURIComponent,
Expand Down Expand Up @@ -735,11 +736,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
}

const result = new Url();
const tkeys = ObjectKeys(this);
for (let tk = 0; tk < tkeys.length; tk++) {
const tkey = tkeys[tk];
result[tkey] = this[tkey];
}
ObjectAssign(result, this);

// Hash is always overridden, no matter what.
// even href="" will remove it.
Expand All @@ -754,12 +751,13 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// Hrefs like //foo/bar always cut to the protocol.
if (relative.slashes && !relative.protocol) {
// Take everything except the protocol from relative
const rkeys = ObjectKeys(relative);
for (let rk = 0; rk < rkeys.length; rk++) {
const rkey = rkeys[rk];
if (rkey !== 'protocol')
result[rkey] = relative[rkey];
}
const relativeWithoutProtocol = ObjectKeys(relative).reduce((acc, key) => {
if (key !== 'protocol') {
acc[key] = relative[key];
}
return acc;
}, {});
ObjectAssign(result, relativeWithoutProtocol);

// urlParse appends trailing / to urls like http://www.example.com
if (slashedProtocol.has(result.protocol) &&
Expand All @@ -781,11 +779,7 @@ Url.prototype.resolveObject = function resolveObject(relative) {
// because that's known to be hostless.
// anything else is assumed to be absolute.
if (!slashedProtocol.has(relative.protocol)) {
const keys = ObjectKeys(relative);
for (let v = 0; v < keys.length; v++) {
const k = keys[v];
result[k] = relative[k];
}
ObjectAssign(result, relative);
result.href = result.format();
return result;
}
Expand Down
Loading