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 #39944

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
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) {
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
11 changes: 10 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
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,15 @@ 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 {
this[searchParams].push(typedKey, typedValue);
visited[typedKey] = this[searchParams].length - 1;
}
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/wpt/status/url.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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`.
Copy link
Member

Choose a reason for hiding this comment

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

If we decided to add FormData later someone might miss removing this. It would be worthwhile making this replacement dependent on whether FormData is undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I've add typeof FormData === 'undefined'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/ping @jasnell

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/ping @jasnell

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