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

Optional wildcard groups inconsistent with path-to-regexp behavior #162

Closed
HansBrende opened this issue Jan 29, 2022 · 12 comments
Closed

Optional wildcard groups inconsistent with path-to-regexp behavior #162

HansBrende opened this issue Jan 29, 2022 · 12 comments

Comments

@HansBrende
Copy link

HansBrende commented Jan 29, 2022

const pattern = '/products/:remaining_path(.*)?'

function test(path) {
   console.log(`Matching ${path} with pattern ${pattern}
   path-to-regexp says remaining_path=${pathToRegexp(pattern).exec(path)[1]}
   URLPattern says remaining_path=${new URLPattern({pathname: pattern}).exec({pathname: path}).pathname.groups.remaining_path}`);
}

test('/products/') prints:

Matching /products/ with pattern /products/:remaining_path(.*)?
   path-to-regexp says remaining_path=
   URLPattern says remaining_path=

So far so good: both agree the result is empty string...

But now... test('/products') prints:

Matching /products with pattern /products/:remaining_path(.*)?
   path-to-regexp says remaining_path=undefined
   URLPattern says remaining_path=

Is this expected behavior? Path-to-regexp's behavior here is preferable IMO... if the path does not end with a slash, one cannot rightly say that remaining_path had an empty string match, unless you assume that a url both without and with a trailing slash are exactly synonymous, which is not necessarily the case.

(The same suboptimal results occur for URLPattern when using the more concise pattern /products/*?)

@wanderview
Copy link
Member

I agree this behavior does not seem correct. Let me look into it.

@wanderview
Copy link
Member

wanderview commented Jan 31, 2022

I think this is coming from default regexp behavior. This:

/(.*)/.exec('')[1]

Evaluates to the empty string. I'll have to see what path-to-regexp is doing to get it to be undefined.

@HansBrende
Copy link
Author

HansBrende commented Jan 31, 2022

@wanderview but that's not the pattern it should evaluate to, right?

It should be:

/(?:\/(.*))?/.exec('')[1] // prints undefined

@wanderview
Copy link
Member

Right, I see the regexp is different and urlpattern is using the same regexp. I'm still trying to figure out where the empty string is coming in.

@wanderview
Copy link
Member

At least part of the problem here is urlpattern uses record<USVString, USVString> for the groups webidl which does not permit an undefined value. This needs to change to record<USVString, (USVString or undefined)>.

@HansBrende
Copy link
Author

@wanderview your comment just made me realize that the scope of this bug is actually way larger than I thought! It doesn't just apply to optional wildcards but also optional variables:

new URLPattern({pathname: '/a/:id?/b'}).exec({pathname: '/a/b'}).pathname.groups

prints:
{id: ''}

@wanderview
Copy link
Member

Yea, this is a good catch. Thanks for filing the issue! This will change a significant number of test results I think, but I think its worth it.

@wanderview
Copy link
Member

The other half of this is that chromium converts the null WTF::String internal type to empty string in its webidl bindings. This means the spec is also not right because it does not do this auto-conversion, but also has webidl that says undefined should not be returned right now.

So I need to fix the implementation and the spec.

@wanderview
Copy link
Member

FYI @lucacasonato. When this lands it will likely be a large number of tests changed.

@lucacasonato
Copy link
Member

lucacasonato commented Jan 31, 2022

@wanderview Instead of making it record<USVString, (USVString or undefined)> just keep it as record<USVString, USVString> and not add the keys you want to be undefined. That way "key" in res.pathname.result also works correctly

But generally in favor 👍

@wanderview
Copy link
Member

wanderview commented Jan 31, 2022

@domenic mentioned that possibility, but we want to add the record with an undefined value to match what RegExp currently does.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 1, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

It turned out, however, that StringUTF8Adaptor was also converting
empty strings to null strings.  It was necessary to fix this in order
to return undefined and empty string in the correct places in the
returned values.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 2, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

It turned out, however, that StringUTF8Adaptor was also converting
empty strings to null strings.  It was necessary to fix this in order
to return undefined and empty string in the correct places in the
returned values.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 2, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

It turned out, however, that StringUTF8Adaptor was also converting
empty strings to null strings.  It was necessary to fix this in order
to return undefined and empty string in the correct places in the
returned values.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 3, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

It turned out, however, that StringUTF8Adaptor was also converting
empty strings to null strings.  It was necessary to fix this in order
to return undefined and empty string in the correct places in the
returned values.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
@wanderview
Copy link
Member

The spec and polyfill have been fixed. The implementation fix and WPT tests are still in review but should land shortly.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 8, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 8, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
aarongable pushed a commit to chromium/chromium that referenced this issue Feb 8, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Feb 8, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
mattwoodrow pushed a commit to mattwoodrow/wpt that referenced this issue Feb 15, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Feb 26, 2022
…s to undefined instead of ''., a=testonly

Automatic update from web-platform-tests
URLPattern: Set unmatched optional groups to undefined instead of ''.

This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}

--

wpt-commits: 111b696bdbb572a89065ce2d3cd524f7d6798301
wpt-pr: 32650
DanielRyanSmith pushed a commit to DanielRyanSmith/wpt that referenced this issue Feb 28, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Mar 1, 2022
…s to undefined instead of ''., a=testonly

Automatic update from web-platform-tests
URLPattern: Set unmatched optional groups to undefined instead of ''.

This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}

--

wpt-commits: 111b696bdbb572a89065ce2d3cd524f7d6798301
wpt-pr: 32650
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Mar 8, 2022
…s to undefined instead of ''., a=testonly

Automatic update from web-platform-tests
URLPattern: Set unmatched optional groups to undefined instead of ''.

This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}

--

wpt-commits: 111b696bdbb572a89065ce2d3cd524f7d6798301
wpt-pr: 32650
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Mar 8, 2022
…s to undefined instead of ''., a=testonly

Automatic update from web-platform-tests
URLPattern: Set unmatched optional groups to undefined instead of ''.

This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}

--

wpt-commits: 111b696bdbb572a89065ce2d3cd524f7d6798301
wpt-pr: 32650
mjfroman pushed a commit to mjfroman/moz-libwebrtc-third-party that referenced this issue Oct 14, 2022
This addresses the issues raised in:

whatwg/urlpattern#162

The main changes in this CL are:

1. The webidl is modified to allow passed back undefined.  I'm told
   real webidl should support `(USVString or undefined)` here, but our
   webidl compiler does not support that.  So we use `any` instead.
2. We must examine if the WTF::String is null or not before populating
   the returned array.  If its null, then we convert to undefined
   instead.

Bug: 1292699
Change-Id: I27a0be2567bb9ce4592ca15a5216fd5a58bdbf17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3428631
Reviewed-by: Jeremy Roman <[email protected]>
Commit-Queue: Ben Kelly <[email protected]>
Cr-Commit-Position: refs/heads/main@{#968611}
NOKEYCHECK=True
GitOrigin-RevId: 27016ffa44b1b63cfef634f009ad881f90ee6e6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants