Skip to content
Open
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
18 changes: 11 additions & 7 deletions src/node_url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
// - new URLPattern(input, baseURL)
// - new URLPattern(input, options)
// - new URLPattern(input, baseURL, options)
if (args[0]->IsString()) {
// Per WebIDL, null/undefined for a union type including a dictionary
// uses the default value (empty init).
if (args[0]->IsNullOrUndefined()) {
init = ada::url_pattern_init{};
} else if (args[0]->IsString()) {
BufferValue input_buffer(env->isolate(), args[0]);
CHECK_NOT_NULL(*input_buffer);
input = input_buffer.ToString();
Expand All @@ -218,7 +222,7 @@ void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
}

// The next argument can be baseURL or options.
if (args.Length() > 1) {
if (args.Length() > 1 && !args[1]->IsNullOrUndefined()) {
if (args[1]->IsString()) {
BufferValue base_url_buffer(env->isolate(), args[1]);
CHECK_NOT_NULL(*base_url_buffer);
Expand All @@ -239,7 +243,7 @@ void URLPattern::New(const FunctionCallbackInfo<Value>& args) {
}

// Only remaining argument can be options.
if (args.Length() > 2) {
if (args.Length() > 2 && !args[2]->IsNullOrUndefined()) {
if (!args[2]->IsObject()) {
THROW_ERR_INVALID_ARG_TYPE(env, "options must be an object");
return;
Expand Down Expand Up @@ -564,7 +568,7 @@ void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) {
ada::url_pattern_input input;
std::optional<std::string> baseURL{};
std::string input_base;
if (args.Length() == 0) {
if (args.Length() == 0 || args[0]->IsNullOrUndefined()) {
input = ada::url_pattern_init{};
} else if (args[0]->IsString()) {
Utf8Value input_value(env->isolate(), args[0].As<String>());
Expand All @@ -580,7 +584,7 @@ void URLPattern::Exec(const FunctionCallbackInfo<Value>& args) {
return;
}

if (args.Length() > 1) {
if (args.Length() > 1 && !args[1]->IsNullOrUndefined()) {
if (!args[1]->IsString()) {
THROW_ERR_INVALID_ARG_TYPE(env, "baseURL must be a string");
return;
Expand All @@ -607,7 +611,7 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
ada::url_pattern_input input;
std::optional<std::string> baseURL{};
std::string input_base;
if (args.Length() == 0) {
if (args.Length() == 0 || args[0]->IsNullOrUndefined()) {
input = ada::url_pattern_init{};
} else if (args[0]->IsString()) {
Utf8Value input_value(env->isolate(), args[0].As<String>());
Expand All @@ -623,7 +627,7 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
return;
}

if (args.Length() > 1) {
if (args.Length() > 1 && !args[1]->IsNullOrUndefined()) {
if (!args[1]->IsString()) {
THROW_ERR_INVALID_ARG_TYPE(env, "baseURL must be a string");
return;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Last update:
- resources: https://github.com/web-platform-tests/wpt/tree/6a2f322376/resources
- streams: https://github.com/web-platform-tests/wpt/tree/bc9dcbbf1a/streams
- url: https://github.com/web-platform-tests/wpt/tree/7a3645b79a/url
- urlpattern: https://github.com/web-platform-tests/wpt/tree/a2e15ad405/urlpattern
- urlpattern: https://github.com/web-platform-tests/wpt/tree/f07c03cbed/urlpattern
- user-timing: https://github.com/web-platform-tests/wpt/tree/5ae85bf826/user-timing
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/65a2134d50/wasm/jsapi
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
Expand Down
36 changes: 36 additions & 0 deletions test/fixtures/wpt/urlpattern/resources/urlpatterntestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3118,5 +3118,41 @@
"expected_match": {
"hostname": { "input": "localhost", "groups": { "domain" : "localhost"} }
}
},
{
"pattern": ["((?R)):"],
"expected_obj": "error"
},
{
"pattern": ["(\\H):"],
"expected_obj": "error"
},
{
"pattern": [
{"pathname": "/:foo((?<x>a))"}
],
"inputs": [
{"pathname": "/a"}
],
"expected_match": {
"pathname": {
"input": "/a",
"groups": {"foo": "a"}
}
}
},
{
"pattern": [
{"pathname": "/foo/(bar(?<x>baz))"}
],
"inputs": [
{"pathname": "/foo/barbaz"}
],
"expected_match": {
"pathname": {
"input": "/foo/barbaz",
"groups": {"0": "barbaz"}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
// META: global=window,worker
test(() => {
assert_throws_js(TypeError, () => { new URLPattern(new URL('https://example.org/%(')); } );
assert_throws_js(TypeError, () => { new URLPattern(new URL('https://example.org/%((')); } );
Expand All @@ -11,4 +8,3 @@
test(() => {
new URLPattern(undefined, undefined);
}, `Test constructor with undefined`);
</script>
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"path": "url"
},
"urlpattern": {
"commit": "a2e15ad40518c30c4e7f649584dbda699a40d531",
"commit": "f07c03cbede41ba677c3d26fd17ff3e02ba26783",
"path": "urlpattern"
},
"user-timing": {
Expand Down
91 changes: 91 additions & 0 deletions test/parallel/test-urlpattern-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,94 @@ assert.throws(() => pattern.test(1), {
assert.throws(() => pattern.test('', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});

// Per WebIDL, undefined/null for optional arguments with defaults should use
// the default value (empty URLPatternInit {}) rather than throwing.
// For a union type (USVString or URLPatternInit), null resolves to the
// dictionary branch, yielding an empty URLPatternInit.

// Constructor: undefined input should be treated as empty init.
{
const p = new URLPattern(undefined);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.hostname, '*');
}

// Constructor: null input should be treated as empty init.
{
const p = new URLPattern(null);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.hostname, '*');
}

// Constructor: undefined input with undefined baseURL.
{
const p = new URLPattern(undefined, undefined);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.pathname, '*');
}

// Constructor: null input with null baseURL.
{
const p = new URLPattern(null, null);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.pathname, '*');
}

// Constructor: valid input with undefined options.
{
const p = new URLPattern({ pathname: '/foo' }, undefined);
assert.strictEqual(p.pathname, '/foo');
}

// Constructor: valid input with null options.
{
const p = new URLPattern({ pathname: '/foo' }, null);
assert.strictEqual(p.pathname, '/foo');
}

// Constructor: string input, undefined baseURL, undefined options.
{
const p = new URLPattern('https://example.com/foo', undefined, undefined);
assert.strictEqual(p.hostname, 'example.com');
assert.strictEqual(p.pathname, '/foo');
}

// Constructor: string input, null baseURL, null options.
{
const p = new URLPattern('https://example.com/foo', null, null);
assert.strictEqual(p.hostname, 'example.com');
assert.strictEqual(p.pathname, '/foo');
}

// exec() and test(): undefined input should be treated as empty init.
{
const p = new URLPattern();
assert.strictEqual(p.test(undefined), true);
assert.strictEqual(p.test(undefined, undefined), true);
assert.notStrictEqual(p.exec(undefined), null);
assert.notStrictEqual(p.exec(undefined, undefined), null);
}

// exec() and test(): null input should be treated as empty init.
{
const p = new URLPattern();
assert.strictEqual(p.test(null), true);
assert.strictEqual(p.test(null, null), true);
assert.notStrictEqual(p.exec(null), null);
assert.notStrictEqual(p.exec(null, null), null);
}

// exec() and test(): valid input with undefined baseURL.
{
const p = new URLPattern({ protocol: 'https' });
assert.strictEqual(p.test('https://example.com', undefined), true);
assert.notStrictEqual(p.exec('https://example.com', undefined), null);
}

// exec() and test(): valid input with null baseURL.
{
const p = new URLPattern({ protocol: 'https' });
assert.strictEqual(p.test('https://example.com', null), true);
assert.notStrictEqual(p.exec('https://example.com', null), null);
}
Loading