-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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: make URLSearchParams properties spec-compliant #11057
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -647,6 +647,35 @@ function getObjectFromParams(array) { | |
return obj; | ||
} | ||
|
||
// Mainly to mitigate func-name-matching ESLint rule | ||
function defineIDLClass(proto, classStr, obj) { | ||
// https://heycam.github.io/webidl/#dfn-class-string | ||
Object.defineProperty(proto, Symbol.toStringTag, { | ||
writable: false, | ||
enumerable: false, | ||
configurable: true, | ||
value: classStr | ||
}); | ||
|
||
// https://heycam.github.io/webidl/#es-operations | ||
for (const key of Object.keys(obj)) { | ||
Object.defineProperty(proto, key, { | ||
writable: true, | ||
enumerable: true, | ||
configurable: true, | ||
value: obj[key] | ||
}); | ||
} | ||
for (const key of Object.getOwnPropertySymbols(obj)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be easier to read if s/key/symbol/, or just separate the two mixins(take |
||
Object.defineProperty(proto, key, { | ||
writable: true, | ||
enumerable: false, | ||
configurable: true, | ||
value: obj[key] | ||
}); | ||
} | ||
} | ||
|
||
class URLSearchParams { | ||
constructor(init = '') { | ||
if (init instanceof URLSearchParams) { | ||
|
@@ -662,11 +691,39 @@ class URLSearchParams { | |
this[context] = null; | ||
} | ||
|
||
get [Symbol.toStringTag]() { | ||
return this instanceof URLSearchParams ? | ||
'URLSearchParams' : 'URLSearchParamsPrototype'; | ||
[util.inspect.custom](recurseTimes, ctx) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
throw new TypeError('Value of `this` is not a URLSearchParams'); | ||
} | ||
|
||
const separator = ', '; | ||
const innerOpts = Object.assign({}, ctx); | ||
if (recurseTimes !== null) { | ||
innerOpts.depth = recurseTimes - 1; | ||
} | ||
const innerInspect = (v) => util.inspect(v, innerOpts); | ||
|
||
const list = this[searchParams]; | ||
const output = []; | ||
for (var i = 0; i < list.length; i += 2) | ||
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`); | ||
|
||
const colorRe = /\u001b\[\d\d?m/g; | ||
const length = output.reduce( | ||
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length, | ||
-separator.length | ||
); | ||
if (length > ctx.breakLength) { | ||
return `${this.constructor.name} {\n ${output.join(',\n ')} }`; | ||
} else if (output.length) { | ||
return `${this.constructor.name} { ${output.join(separator)} }`; | ||
} else { | ||
return `${this.constructor.name} {}`; | ||
} | ||
} | ||
} | ||
|
||
defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { | ||
append(name, value) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
throw new TypeError('Value of `this` is not a URLSearchParams'); | ||
|
@@ -679,7 +736,7 @@ class URLSearchParams { | |
value = String(value); | ||
this[searchParams].push(name, value); | ||
update(this[context], this); | ||
} | ||
}, | ||
|
||
delete(name) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -700,47 +757,7 @@ class URLSearchParams { | |
} | ||
} | ||
update(this[context], this); | ||
} | ||
|
||
set(name, value) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
throw new TypeError('Value of `this` is not a URLSearchParams'); | ||
} | ||
if (arguments.length < 2) { | ||
throw new TypeError('"name" and "value" arguments must be specified'); | ||
} | ||
|
||
const list = this[searchParams]; | ||
name = String(name); | ||
value = String(value); | ||
|
||
// If there are any name-value pairs whose name is `name`, in `list`, set | ||
// the value of the first such name-value pair to `value` and remove the | ||
// others. | ||
var found = false; | ||
for (var i = 0; i < list.length;) { | ||
const cur = list[i]; | ||
if (cur === name) { | ||
if (!found) { | ||
list[i + 1] = value; | ||
found = true; | ||
i += 2; | ||
} else { | ||
list.splice(i, 2); | ||
} | ||
} else { | ||
i += 2; | ||
} | ||
} | ||
|
||
// Otherwise, append a new name-value pair whose name is `name` and value | ||
// is `value`, to `list`. | ||
if (!found) { | ||
list.push(name, value); | ||
} | ||
|
||
update(this[context], this); | ||
} | ||
}, | ||
|
||
get(name) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -758,7 +775,7 @@ class URLSearchParams { | |
} | ||
} | ||
return null; | ||
} | ||
}, | ||
|
||
getAll(name) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -777,7 +794,7 @@ class URLSearchParams { | |
} | ||
} | ||
return values; | ||
} | ||
}, | ||
|
||
has(name) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -795,7 +812,47 @@ class URLSearchParams { | |
} | ||
} | ||
return false; | ||
} | ||
}, | ||
|
||
set(name, value) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
throw new TypeError('Value of `this` is not a URLSearchParams'); | ||
} | ||
if (arguments.length < 2) { | ||
throw new TypeError('"name" and "value" arguments must be specified'); | ||
} | ||
|
||
const list = this[searchParams]; | ||
name = String(name); | ||
value = String(value); | ||
|
||
// If there are any name-value pairs whose name is `name`, in `list`, set | ||
// the value of the first such name-value pair to `value` and remove the | ||
// others. | ||
var found = false; | ||
for (var i = 0; i < list.length;) { | ||
const cur = list[i]; | ||
if (cur === name) { | ||
if (!found) { | ||
list[i + 1] = value; | ||
found = true; | ||
i += 2; | ||
} else { | ||
list.splice(i, 2); | ||
} | ||
} else { | ||
i += 2; | ||
} | ||
} | ||
|
||
// Otherwise, append a new name-value pair whose name is `name` and value | ||
// is `value`, to `list`. | ||
if (!found) { | ||
list.push(name, value); | ||
} | ||
|
||
update(this[context], this); | ||
}, | ||
|
||
// https://heycam.github.io/webidl/#es-iterators | ||
// Define entries here rather than [Symbol.iterator] as the function name | ||
|
@@ -806,7 +863,7 @@ class URLSearchParams { | |
} | ||
|
||
return createSearchParamsIterator(this, 'key+value'); | ||
} | ||
}, | ||
|
||
forEach(callback, thisArg = undefined) { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -827,7 +884,7 @@ class URLSearchParams { | |
list = this[searchParams]; | ||
i += 2; | ||
} | ||
} | ||
}, | ||
|
||
// https://heycam.github.io/webidl/#es-iterable | ||
keys() { | ||
|
@@ -836,16 +893,17 @@ class URLSearchParams { | |
} | ||
|
||
return createSearchParamsIterator(this, 'key'); | ||
} | ||
}, | ||
|
||
values() { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
throw new TypeError('Value of `this` is not a URLSearchParams'); | ||
} | ||
|
||
return createSearchParamsIterator(this, 'value'); | ||
} | ||
}, | ||
|
||
// https://heycam.github.io/webidl/#es-stringifier | ||
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior | ||
toString() { | ||
if (!this || !(this instanceof URLSearchParams)) { | ||
|
@@ -854,37 +912,14 @@ class URLSearchParams { | |
|
||
return querystring.stringify(getObjectFromParams(this[searchParams])); | ||
} | ||
} | ||
// https://heycam.github.io/webidl/#es-iterable-entries | ||
URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries; | ||
|
||
URLSearchParams.prototype[util.inspect.custom] = | ||
function inspect(recurseTimes, ctx) { | ||
const separator = ', '; | ||
const innerOpts = Object.assign({}, ctx); | ||
if (recurseTimes !== null) { | ||
innerOpts.depth = recurseTimes - 1; | ||
} | ||
const innerInspect = (v) => util.inspect(v, innerOpts); | ||
|
||
const list = this[searchParams]; | ||
const output = []; | ||
for (var i = 0; i < list.length; i += 2) | ||
output.push(`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`); | ||
}); | ||
|
||
const colorRe = /\u001b\[\d\d?m/g; | ||
const length = output.reduce( | ||
(prev, cur) => prev + cur.replace(colorRe, '').length + separator.length, | ||
-separator.length | ||
); | ||
if (length > ctx.breakLength) { | ||
return `${this.constructor.name} {\n ${output.join(',\n ')} }`; | ||
} else if (output.length) { | ||
return `${this.constructor.name} { ${output.join(separator)} }`; | ||
} else { | ||
return `${this.constructor.name} {}`; | ||
} | ||
}; | ||
// https://heycam.github.io/webidl/#es-iterable-entries | ||
Object.defineProperty(URLSearchParams.prototype, Symbol.iterator, { | ||
writable: true, | ||
configurable: true, | ||
value: URLSearchParams.prototype.entries | ||
}); | ||
|
||
// https://heycam.github.io/webidl/#dfn-default-iterator-object | ||
function createSearchParamsIterator(target, kind) { | ||
|
@@ -898,7 +933,9 @@ function createSearchParamsIterator(target, kind) { | |
} | ||
|
||
// https://heycam.github.io/webidl/#dfn-iterator-prototype-object | ||
const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({ | ||
const URLSearchParamsIteratorPrototype = Object.create(IteratorPrototype); | ||
|
||
defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', { | ||
next() { | ||
if (!this || | ||
Object.getPrototypeOf(this) !== URLSearchParamsIteratorPrototype) { | ||
|
@@ -937,7 +974,7 @@ const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({ | |
done: false | ||
}; | ||
}, | ||
[util.inspect.custom]: function inspect(recurseTimes, ctx) { | ||
[util.inspect.custom](recurseTimes, ctx) { | ||
const innerOpts = Object.assign({}, ctx); | ||
if (recurseTimes !== null) { | ||
innerOpts.depth = recurseTimes - 1; | ||
|
@@ -968,15 +1005,6 @@ const URLSearchParamsIteratorPrototype = Object.setPrototypeOf({ | |
} | ||
return `${this[Symbol.toStringTag]} {${outputStr} }`; | ||
} | ||
}, IteratorPrototype); | ||
|
||
// Unlike interface and its prototype object, both default iterator object and | ||
// iterator prototype object of an interface have the same class string. | ||
Object.defineProperty(URLSearchParamsIteratorPrototype, Symbol.toStringTag, { | ||
value: 'URLSearchParamsIterator', | ||
writable: false, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
|
||
function originFor(url, base) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if there is a possibility/necessity to generate these stuff from an IDL file, like what browsers do, if there will be more convergence with Web APIs in Node core (console, crypto, streams, etc.) Not that this is what this PR should deal with though(and this is just to mitigate func-name-matching anyway, not full-on generation), just a thought.
Also the name is a little bit too general, could be something like
defineIDLData
ordefineIDLValues
, since it's about properties that need to be defined withvalue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would likely be a bit overkill at the moment. This is the only IDL defined API we have currently. Eventually if we go down that road more then we can consider this.