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,lib: pass urlsearchparams-constructor.any.js #41197

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions lib/internal/per_context/domexception.js
Expand Up @@ -10,12 +10,27 @@ const {
TypeError,
} = primordials;

class ERR_INVALID_THIS extends TypeError {
constructor(type) {
super('Value of "this" must be of ' + type);
}

get code() { return 'ERR_INVALID_THIS'; }
function throwInvalidThisError(Base, type) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the change to domexception.js related to the urlsearchparams change? If it is, it's not clear how. Also, it would likely be better to separate this out into a separate commit in the PR if it is related.

Copy link
Member Author

Choose a reason for hiding this comment

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

This modification is to pass urlsearchparams-contructor.any.js too.

Copy link
Member Author

Choose a reason for hiding this comment

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

URLSearchParams constructor should throw exactly TypeError if any Error occurrs but not inheritance.

Copy link
Member Author

Choose a reason for hiding this comment

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

const err = new Base();
const key = 'ERR_INVALID_THIS';
ObjectDefineProperties(err, {
message: {
value: `Value of "this" must be of ${type}`,
enumerable: false,
writable: true,
configurable: true,
},
toString: {
value() {
return `${this.name} [${key}]: ${this.message}`;
},
enumerable: false,
writable: true,
configurable: true,
},
});
err.code = key;
throw err;
}

let internalsMap;
Expand Down Expand Up @@ -51,7 +66,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
return internals.name;
}
Expand All @@ -60,7 +75,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
return internals.message;
}
Expand All @@ -69,7 +84,7 @@ class DOMException extends Error {
ensureInitialized();
const internals = internalsMap.get(this);
if (internals === undefined) {
throw new ERR_INVALID_THIS('DOMException');
throwInvalidThisError(TypeError, 'DOMException');
}
const code = nameToCodeMap.get(internals.name);
return code === undefined ? 0 : code;
Expand Down
12 changes: 11 additions & 1 deletion lib/internal/url.js
Expand Up @@ -219,6 +219,7 @@ class URLSearchParams {
} else {
// Record<USVString, USVString>
// Need to use reflection APIs for full spec compliance.
const visited = {};
this[searchParams] = [];
const keys = ReflectOwnKeys(init);
for (let i = 0; i < keys.length; i++) {
Expand All @@ -227,7 +228,16 @@ class URLSearchParams {
if (desc !== undefined && desc.enumerable) {
const typedKey = toUSVString(key);
const typedValue = toUSVString(init[key]);
this[searchParams].push(typedKey, typedValue);

// Two different key may result same after `toUSVString()`, we only
// leave the later one. Refers to WPT.
if (visited[typedKey] !== undefined) {
this[searchParams][visited[typedKey]] = typedValue;
} else {
visited[typedKey] = ArrayPrototypePush(this[searchParams],
typedKey,
typedValue) - 1;
}
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/wpt/status/url.json
Expand Up @@ -12,9 +12,6 @@
"urlencoded-parser.any.js": {
"fail": "missing Request and Response"
},
"urlsearchparams-constructor.any.js": {
"fail": "FormData is not defined"
},
"url-constructor.any.js": {
"requires": ["small-icu"]
},
Expand Down
9 changes: 9 additions & 0 deletions test/wpt/test-url.js
Expand Up @@ -11,6 +11,15 @@ runner.setScriptModifier((obj) => {
// created via `document.createElement`. So we need to ignore them and just
// test `URL`.
obj.code = obj.code.replace(/\["url", "a", "area"\]/, '[ "url" ]');
} else if (typeof FormData === 'undefined' && // eslint-disable-line
obj.filename.includes('urlsearchparams-constructor.any.js')) {
// TODO(XadillaX): Remove this `else if` after `FormData` is supported.

// Ignore test named `URLSearchParams constructor, FormData.` because we do
// not have `FormData`.
obj.code = obj.code.replace(
/('URLSearchParams constructor, object\.'\);[\w\W]+)test\(function\(\) {[\w\W]*?}, 'URLSearchParams constructor, FormData\.'\);/,
'$1');
}
});
runner.pretendGlobalThisAs('Window');
Expand Down