Skip to content

Commit

Permalink
Implement URL.parse()
Browse files Browse the repository at this point in the history
Closes #277.
  • Loading branch information
domenic committed Dec 4, 2024
1 parent 5115e7e commit 252f08c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/URL-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const URLSearchParams = require("./URLSearchParams");
exports.implementation = class URLImpl {
// Unlike the spec, we duplicate some code between the constructor and canParse, because we want to give useful error
// messages in the constructor that distinguish between the different causes of failure.
constructor(globalObject, constructorArgs) {
const url = constructorArgs[0];
const base = constructorArgs[1];

constructor(globalObject, [url, base]) {
let parsedBase = null;
if (base !== undefined) {
parsedBase = usm.basicURLParse(base);
Expand All @@ -33,6 +30,14 @@ exports.implementation = class URLImpl {
this._query._url = this;
}

static parse(globalObject, input, base) {
try {
return new URLImpl(globalObject, [input, base]);
} catch {
return null;
}
}

static canParse(url, base) {
let parsedBase = null;
if (base !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions lib/URL.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
interface URL {
constructor(USVString url, optional USVString base);

[WebIDL2JSCallWithGlobal] static URL? parse(USVString url, optional USVString base);
static boolean canParse(USVString url, optional USVString base);

stringifier attribute USVString href;
Expand Down
3 changes: 2 additions & 1 deletion scripts/get-latest-platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const path = require("path");
// 1. Go to https://github.com/web-platform-tests/wpt/tree/master/url
// 2. Press "y" on your keyboard to get a permalink
// 3. Copy the commit hash
const commitHash = "72b915d4b3754f081ef5899bf6a777efe71b2fc5";
const commitHash = "48178346fe87222856812842fb7af7b01baa1530";

const urlPrefix = `https://raw.githubusercontent.com/web-platform-tests/wpt/${commitHash}/url/`;
const targetDir = path.resolve(__dirname, "..", "test", "web-platform-tests");
Expand All @@ -32,6 +32,7 @@ exports.directlyRunnableTests = [
"url-searchparams.any.js",
"url-setters-stripping.any.js",
"url-statics-canparse.any.js",
"url-statics-parse.any.js",
"url-tojson.any.js",
"urlencoded-parser.any.js",
"urlsearchparams-append.any.js",
Expand Down
4 changes: 4 additions & 0 deletions test/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ module.exports = {
assert.strictEqual(actual, expected);
},

assert_not_equals(actual, expected) {
assert.notStrictEqual(actual, expected);
},

assert_array_equals(actual, expected) {
assert.deepStrictEqual([...actual], [...expected]);
},
Expand Down

0 comments on commit 252f08c

Please sign in to comment.