From 222cdd646fda82d8bd3ab3e966852a1d2a45a49d Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Tue, 3 May 2022 17:34:41 +0200 Subject: [PATCH 1/3] Add URLPatternList This commit adds support for a URLPatternList class. --- spec.bs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/spec.bs b/spec.bs index 764732e..eb23c5c 100644 --- a/spec.bs +++ b/spec.bs @@ -810,6 +810,83 @@ To compute protocol matches a special scheme flag given a [=construct 1. If the result of running [=protocol component matches a special scheme=] given |protocol component| is true, then set |parser|'s [=constructor string parser/protocol matches a special scheme flag=] to true. +

The {{URLPatternList}} class

+ + +[Exposed=(Window,Worker)] +interface URLPatternList { + constructor(sequence<URLPatternListEntry> entries); + + USVString? test(optional URLPatternInput input = {}, optional USVString baseURL); + + URLPatternListResult? exec(optional URLPatternInput input = {}, optional USVString baseURL); +}; + +dictionary URLPatternListEntry { + required (USVString or URLPattern) pattern; + required USVString key; +}; + +dictionary URLPatternListResult { + URLPatternResult match; + USVString key; +}; + + +Note: The {{URLPatternList}} is a utility that combines many patterns into one +matcher. This functionality can also be achieved in a naive userland +implementation that iterates over a list of patterns and matches each one +individually. This has the unfortunate effect of not being very fast. With the +built-in {{URLPatternList}} implementers can use a radix tree for matching under +the hood which can drastically improve performance over the naive userland +implementation. + +Each {{URLPatternList}} has an associated pattern list, a [=list=] of {{URLPattern}}s. + +Each {{URLPatternList}} has an associated key list, a [=list=] of [=string=]s. + +
+ The new URLPattern(|entries|) constructor steps are: + + 1. [=list/For each=] |entry| in |entries|, run the following steps: + 1. [=list/Append=] the |entry|'s {{URLPatternListEntry/pattern}} to [=this=]'s [=URLPatternList/pattern list=]. + 1. Let |key| be |entry|'s {{URLPatternListEntry/key}}. + 1. If |key| is the empty string, throw a {{TypeError}}. + 1. If [=this=]'s [=URLPatternList/key list=] [=list/contains=] |key|, throw a {{TypeError}}. + 1. [=list/Append=] |key| to [=this=]'s [=URLPatternList/key list=]. + 1. If [=this=]'s [=URLPatternList/pattern list=] is empty, throw a {{TypeError}}. + + Issue: The patterns need to be in a "least -> most significant" sort order so + implementers can efficiently use a radix tree under the hood. Enforcing / + sorting the patterns in this order depends on + https://github.com/WICG/urlpattern/issues/61. +
+ +
+ The test(|input|, |baseURL|) method steps are: + + 1. Let |i| be 0. + 1. [=list/For each=] |pattern| in [=this=]'s [=URLPatternList/pattern list=], run the following steps: + 1. Let |result| be the result of [=match=] given |pattern|, |input|, and |baseURL| if given. + 1. If |result| is null, [=continue=]. + 1. Return [=this=]'s [=URLPatternList/key list=][|i|]. + 1. Return null. +
+ +
+ The match(|input|, |baseURL|) method steps are: + + 1. Let |i| be 0. + 1. [=list/For each=] |pattern| in [=this=]'s [=URLPatternList/pattern list=], run the following steps: + 1. Let |match| be the result of [=match=] given |pattern|, |input|, and |baseURL| if given. + 1. If |match| is null, [=continue=]. + 1. Let |result| be a new {{URLPatternListResult}} dictionary. + 1. Set |result|'s {{URLPatternListResult/match}} to |match|. + 1. Set |result|'s {{URLPatternListResult/key}} to [=this=]'s [=URLPatternList/key list=][|i|]. + 1. Return |result|. + 1. Return null. +
+

Patterns

A pattern string is a string that is written to match a set of target strings. A well formed pattern string conforms to a particular pattern syntax. This pattern syntax is directly based on the syntax used by the popular [path-to-regexp](https://github.com/pillarjs/path-to-regexp) JavaScript library. From 5357b1cd2d3ff9ff0eb7a1dce786425bdddc82d1 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 5 May 2022 19:29:12 +0200 Subject: [PATCH 2/3] simplify --- spec.bs | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/spec.bs b/spec.bs index eb23c5c..933e64a 100644 --- a/spec.bs +++ b/spec.bs @@ -815,21 +815,15 @@ To compute protocol matches a special scheme flag given a [=construct [Exposed=(Window,Worker)] interface URLPatternList { - constructor(sequence<URLPatternListEntry> entries); + constructor(sequence<URLPattern> patterns); - USVString? test(optional URLPatternInput input = {}, optional USVString baseURL); + boolean test(optional URLPatternInput input = {}, optional USVString baseURL); URLPatternListResult? exec(optional URLPatternInput input = {}, optional USVString baseURL); }; -dictionary URLPatternListEntry { - required (USVString or URLPattern) pattern; - required USVString key; -}; - -dictionary URLPatternListResult { - URLPatternResult match; - USVString key; +dictionary URLPatternListResult : URLPatternResult { + URLPattern pattern; }; @@ -843,17 +837,12 @@ implementation. Each {{URLPatternList}} has an associated pattern list, a [=list=] of {{URLPattern}}s. -Each {{URLPatternList}} has an associated key list, a [=list=] of [=string=]s. -
- The new URLPattern(|entries|) constructor steps are: + The new URLPatternList(|patterns|) constructor steps are: - 1. [=list/For each=] |entry| in |entries|, run the following steps: - 1. [=list/Append=] the |entry|'s {{URLPatternListEntry/pattern}} to [=this=]'s [=URLPatternList/pattern list=]. - 1. Let |key| be |entry|'s {{URLPatternListEntry/key}}. - 1. If |key| is the empty string, throw a {{TypeError}}. - 1. If [=this=]'s [=URLPatternList/key list=] [=list/contains=] |key|, throw a {{TypeError}}. - 1. [=list/Append=] |key| to [=this=]'s [=URLPatternList/key list=]. + 1. [=list/For each=] |pattern| in |patterns|, run the following steps: + 1. If [=this=]'s [=URLPatternList/pattern list=] [=list/contains=] |pattern|, throw a {{TypeError}}. + 1. [=list/Append=] the |pattern| to [=this=]'s [=URLPatternList/pattern list=]. 1. If [=this=]'s [=URLPatternList/pattern list=] is empty, throw a {{TypeError}}. Issue: The patterns need to be in a "least -> most significant" sort order so @@ -865,24 +854,20 @@ Each {{URLPatternList}} has an associated key list
The test(|input|, |baseURL|) method steps are: - 1. Let |i| be 0. 1. [=list/For each=] |pattern| in [=this=]'s [=URLPatternList/pattern list=], run the following steps: 1. Let |result| be the result of [=match=] given |pattern|, |input|, and |baseURL| if given. 1. If |result| is null, [=continue=]. - 1. Return [=this=]'s [=URLPatternList/key list=][|i|]. - 1. Return null. + 1. Return true. + 1. Return false.
The match(|input|, |baseURL|) method steps are: - 1. Let |i| be 0. 1. [=list/For each=] |pattern| in [=this=]'s [=URLPatternList/pattern list=], run the following steps: - 1. Let |match| be the result of [=match=] given |pattern|, |input|, and |baseURL| if given. - 1. If |match| is null, [=continue=]. - 1. Let |result| be a new {{URLPatternListResult}} dictionary. - 1. Set |result|'s {{URLPatternListResult/match}} to |match|. - 1. Set |result|'s {{URLPatternListResult/key}} to [=this=]'s [=URLPatternList/key list=][|i|]. + 1. Let |result| be the result of [=match=] given |pattern|, |input|, and |baseURL| if given. + 1. If |result| is null, [=continue=]. + 1. Set |result|'s {{URLPatternListResult/pattern}} to |pattern|. 1. Return |result|. 1. Return null.
From 14086eeaa66fe43cde36595a08ef1fecf2f1d5f0 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 5 May 2022 19:30:45 +0200 Subject: [PATCH 3/3] review comments --- spec.bs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec.bs b/spec.bs index 933e64a..ecf370e 100644 --- a/spec.bs +++ b/spec.bs @@ -831,9 +831,9 @@ Note: The {{URLPatternList}} is a utility that combines many patterns into one matcher. This functionality can also be achieved in a naive userland implementation that iterates over a list of patterns and matches each one individually. This has the unfortunate effect of not being very fast. With the -built-in {{URLPatternList}} implementers can use a radix tree for matching under -the hood which can drastically improve performance over the naive userland -implementation. +built-in {{URLPatternList}} implementers can use an optimized implementation for +matching under the hood which can drastically improve performance over the naive +userland implementation. Each {{URLPatternList}} has an associated pattern list, a [=list=] of {{URLPattern}}s.