Skip to content

Commit 2cc9ec1

Browse files
Implement URLPattern.hasRegExpGroups.
This allows authors to tell whether a pattern contains one or more regexp groups, which may limit the uses they're able to make of it. Bug: 1504650 Change-Id: I07b684788e921448b60c4ffee9cfe87585a794c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5054393 Reviewed-by: Shunya Shishido <[email protected]> Commit-Queue: Jeremy Roman <[email protected]> Cr-Commit-Position: refs/heads/main@{#1228894}
1 parent 23dcf5b commit 2cc9ec1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test(() => {
2+
assert_implements('hasRegExpGroups' in URLPattern.prototype, "hasRegExpGroups is not implemented");
3+
assert_false(new URLPattern({}).hasRegExpGroups, "match-everything pattern");
4+
for (let component of ['protocol', 'username', 'password', 'hostname', 'port', 'pathname', 'search', 'hash']) {
5+
assert_false(new URLPattern({[component]: '*'}).hasRegExpGroups, `wildcard in ${component}`);
6+
assert_false(new URLPattern({[component]: ':foo'}).hasRegExpGroups, `segment wildcard in ${component}`);
7+
assert_false(new URLPattern({[component]: ':foo?'}).hasRegExpGroups, `optional segment wildcard in ${component}`);
8+
assert_true(new URLPattern({[component]: ':foo(hi)'}).hasRegExpGroups, `named regexp group in ${component}`);
9+
assert_true(new URLPattern({[component]: '(hi)'}).hasRegExpGroups, `anonymous regexp group in ${component}`);
10+
if (component !== 'protocol' && component !== 'port') {
11+
// These components are more narrow in what they accept in any case.
12+
assert_false(new URLPattern({[component]: 'a-{:hello}-z-*-a'}).hasRegExpGroups, `wildcards mixed in with fixed text and wildcards in ${component}`);
13+
assert_true(new URLPattern({[component]: 'a-(hi)-z-(lo)-a'}).hasRegExpGroups, `regexp groups mixed in with fixed text and wildcards in ${component}`);
14+
}
15+
}
16+
assert_false(new URLPattern({pathname: '/a/:foo/:baz?/b/*'}).hasRegExpGroups, "complex pathname with no regexp");
17+
assert_true(new URLPattern({pathname: '/a/:foo/:baz([a-z]+)?/b/*'}).hasRegExpGroups, "complex pathname with regexp");
18+
}, '');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// META: global=window,worker
2+
// META: script=resources/urlpattern-hasregexpgroups-tests.js

0 commit comments

Comments
 (0)