Skip to content

Commit 8118410

Browse files
committed
Use the URLPattern specification instead of custom URL pattern parsing
1 parent e40b28a commit 8118410

1 file changed

Lines changed: 31 additions & 273 deletions

File tree

index.bs

Lines changed: 31 additions & 273 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ spec: STREAMS; urlPrefix: https://streams.spec.whatwg.org/
337337
spec: TOUCH-EVENTS; urlPrefix: https://www.w3.org/community/reports/touchevents/CG-FINAL-touch-events-20240704/
338338
type: dfn
339339
text: expose legacy touch event APIs; url: #conditionally-exposing-legacy-touch-event-apis
340+
spec: URLPATTERN; urlPrefix: https://urlpattern.spec.whatwg.org/
341+
type: dfn
342+
text: create a URLPattern; url: #url-pattern-create
343+
text: URL pattern match; url: #url-pattern-match
344+
text: URLPatternInit; url: #dictdef-urlpatterninit
340345
spec: WebSockets; urlPrefix: https://websockets.spec.whatwg.org/
341346
type: dfn
342347
text: WebSocket; url: #websocket
@@ -8859,14 +8864,13 @@ network.UrlPattern = (
88598864

88608865
network.UrlPatternPattern = {
88618866
type: "pattern",
8862-
?protocol: text,
8863-
?hostname: text,
8864-
?port: text,
8865-
?pathname: text,
8866-
?search: text,
8867+
? protocol: text,
8868+
? hostname: text,
8869+
? port: text,
8870+
? pathname: text,
8871+
? search: text,
88678872
}
88688873

8869-
88708874
network.UrlPatternString = {
88718875
type: "string",
88728876
pattern: text,
@@ -8875,296 +8879,50 @@ network.UrlPatternString = {
88758879
</pre>
88768880

88778881
A <code>network.UrlPattern</code> represents a pattern used for matching request
8878-
URLs for [=network intercepts=].
8879-
8880-
When URLs are matched against a <code>network.UrlPattern</code> the URL is
8881-
parsed, and each component is compared for equality with the corresponding field
8882-
in the pattern, if present. Missing fields from the pattern always match.
8883-
8884-
Note: This syntax is designed with future extensibility in mind. In particular
8885-
the syntax forbids characters that are treated specially in the [[URLPattern]]
8886-
specification. These can be escaped by prefixing them with a U+005C (\) character.
8887-
8888-
<div algorithm>
8889-
To <dfn>unescape URL pattern</dfn> given |pattern|
8890-
8891-
1. Let |forbidden characters| be the [=/set=] of codepoints «U+0028 ((), U+0029
8892-
()), U+002A (*), U+007B ({), U+007D (})»
8893-
8894-
1. Let |result| be the empty string.
8895-
8896-
1. Let |is escaped character| be false.
8897-
8898-
1. For each |codepoint| in |pattern|:
8899-
8900-
1. If |is escaped character| is false:
8901-
8902-
1. If |forbidden characters| [=set/contains=] |codepoint|, return [=error=]
8903-
with [=error code=] [=invalid argument=].
8904-
8905-
1. If |codepoint| is U+005C (\):
8906-
8907-
1. Set |is escaped character| to true.
8908-
8909-
1. Continue.
8910-
8911-
1. Append |codepoint| to result.
8912-
8913-
1. Set |is escaped character| to false.
8914-
8915-
1. Return [=success=] with data |result|.
8916-
8917-
</div>
8882+
URLs for [=network intercepts=]. URL patterns use the syntax and semantics
8883+
defined by [[!URLPattern]].
89188884

89198885
<div algorithm>
89208886
To <dfn>parse URL pattern</dfn>, given |pattern|:
89218887

8922-
1. Let |has protocol| be true.
8923-
8924-
1. Let |has hostname| be true.
8925-
8926-
1. Let |has port| be true.
8927-
8928-
1. Let |has pathname| be true.
8929-
8930-
1. Let |has search| be true.
8931-
89328888
1. If |pattern| matches the <code>network.UrlPatternPattern</code> production:
89338889

8934-
1. Let |pattern url| be the empty string.
8935-
8936-
1. If |pattern| [=map/contains=] "<code>protocol</code>":
8937-
8938-
1. If |pattern|["<code>protocol</code>"] is the empty string, return [=error=]
8939-
with [=error code=] [=invalid argument=].
8940-
8941-
1. Let |protocol| be the result of [=trying=] to [=unescape
8942-
URL Pattern=] with |pattern|["<code>protocol</code>"].
8943-
8944-
1. For each |codepoint| in |protocol|:
8890+
1. Let |init| be a new [=URLPatternInit=].
89458891

8946-
1. If |codepoint| is not [=ASCII alphanumeric=] and «U+002B (+), U+002D
8947-
(-), U+002E (.)» does not [=set/contains|contain=] |codepoint|:
8892+
1. If |pattern| [=map/contains=] "<code>protocol</code>", set
8893+
|init|["<code>protocol</code>"] to |pattern|["<code>protocol</code>"].
89488894

8949-
1. Return [=error=] with [=error code=] invalid argument.
8895+
1. If |pattern| [=map/contains=] "<code>hostname</code>", set
8896+
|init|["<code>hostname</code>"] to |pattern|["<code>hostname</code>"].
89508897

8951-
1. Append |protocol| to |pattern url|.
8898+
1. If |pattern| [=map/contains=] "<code>port</code>", set
8899+
|init|["<code>port</code>"] to |pattern|["<code>port</code>"].
89528900

8953-
1. Otherwise:
8954-
8955-
1. Set |has protocol| to false.
8956-
8957-
1. Append "<code>http</code>" to |pattern url|.
8958-
8959-
1. Let |scheme| be [=ASCII lowercase=] with |pattern url|.
8960-
8961-
1. Append "<code>:</code>" to |pattern url|.
8962-
8963-
1. If |scheme| [=is special=], append "<code>//</code>" to |pattern url|.
8964-
8965-
1. If |pattern| [=map/contains=] "<code>hostname</code>":
8966-
8967-
1. If |pattern|["<code>hostname</code>"] is the empty string, return [=error=]
8968-
with [=error code=] [=invalid argument=].
8969-
8970-
1. If |scheme| is "<code>file</code>" return [=error=] with [=error code=]
8971-
[=invalid argument=].
8972-
8973-
1. Let |hostname| be the result of [=trying=] to [=unescape
8974-
URL Pattern=] with |pattern|["<code>hostname</code>"].
8975-
8976-
1. Let |inside brackets| be false.
8977-
8978-
1. For each |codepoint| in |hostname|:
8979-
8980-
1. If «U+002F (/), U+003F (?), U+0023 (#)» [=set/contains=]
8981-
|codepoint|:
8982-
8983-
1. Return [=error=] with [=error code=] [=invalid argument=].
8984-
8985-
1. If |inside brackets| is false and |codepoint| is U+003A (:):
8901+
1. If |pattern| [=map/contains=] "<code>pathname</code>", set
8902+
|init|["<code>pathname</code>"] to |pattern|["<code>pathname</code>"].
89868903

8987-
1. Return [=error=] with [=error code=] [=invalid argument=].
8904+
1. If |pattern| [=map/contains=] "<code>search</code>", set
8905+
|init|["<code>search</code>"] to |pattern|["<code>search</code>"].
89888906

8989-
1. If |codepoint| is U+005B ([), set |inside brackets| to true.
8990-
8991-
1. If |codepoint| is U+005D (]), set |inside brackets| to false.
8992-
8993-
1. Append |hostname| to |pattern url|.
8994-
8995-
1. Otherwise:
8996-
8997-
1. If |scheme| is not "<code>file</code>", append "<code>placeholder</code>" to
8998-
|pattern url|.
8999-
9000-
1. Set |has hostname| to false.
9001-
9002-
1. If |pattern| [=map/contains=] "<code>port</code>":
9003-
9004-
1. If |pattern|["<code>port</code>"] is the empty string, return [=error=]
9005-
with [=error code=] [=invalid argument=].
9006-
9007-
1. Let |port| be the result of [=trying=] to [=unescape
9008-
URL Pattern=] with |pattern|["<code>port</code>"].
9009-
9010-
1. Append "<code>:</code>" to |pattern url|.
9011-
9012-
1. For each |codepoint| in |port|:
9013-
9014-
1. If |codepoint| is not an [=ASCII digit=]:
9015-
9016-
1. Return [=error=] with [=error code=] [=invalid argument=].
9017-
9018-
1. Append |port| to |pattern url|.
9019-
9020-
1. Otherwise:
9021-
9022-
1. Set |has port| to false.
9023-
9024-
1. If |pattern| [=map/contains=] "<code>pathname</code>":
9025-
9026-
1. Let |pathname| be the result of [=trying=] to [=unescape URL Pattern=] with
9027-
|pattern|["<code>pathname</code>"].
9028-
9029-
1. If |pathname| does not [=string/starts with|start with=] U+002F (/), then
9030-
append "<code>/</code>" to |pattern url|.
9031-
9032-
1. For each |codepoint| in |pathname|:
9033-
9034-
1. If «U+003F (?), U+0023 (#)» [=set/contains=]
9035-
|codepoint|:
9036-
9037-
1. Return [=error=] with [=error code=] [=invalid argument=].
9038-
9039-
1. Append |pathname| to |pattern url|.
9040-
9041-
1. Otherwise:
9042-
9043-
1. Set |has pathname| to false.
9044-
9045-
1. If |pattern| [=map/contains=] "<code>search</code>":
9046-
9047-
1. Let |search| be the result of [=trying=] to [=unescape URL pattern=] with
9048-
|pattern|["<code>search</code>"].
9049-
9050-
1. If |search| does not [=string/starts with|start with=] U+003F (?), then
9051-
append "<code>?</code>" to |pattern url|.
9052-
9053-
1. For each |codepoint| in |search|:
9054-
9055-
1. If |codepoint| is U+0023 (#):
9056-
9057-
1. Return [=error=] with [=error code=] [=invalid argument=].
9058-
9059-
1. Append |search| to |pattern url|.
9060-
9061-
1. Otherwise:
9062-
9063-
1. Set |has search| to false.
8907+
1. Let |url pattern| be the result of [=creating a URLPattern=] given |init|.
90648908

90658909
1. Otherwise, if |pattern| matches the <code>network.UrlPatternString</code> production:
90668910

9067-
1. Let |pattern url| be the result of [=trying=] to [=unescape URL
9068-
pattern=] with |pattern|["<code>pattern</code>"].
9069-
9070-
1. Let |url| be the result of [=URL Parser|parsing=] |pattern url|.
9071-
9072-
1. If |url| is failure, return [=error=] with [=error code=] [=invalid argument=].
9073-
9074-
1. Let |parsed| be a struct with the following fields:
9075-
<dl>
9076-
<dt>protocol
9077-
<dd>|url|'s [=url/scheme=] if |has protocol| is true, or null otherwise.
9078-
<dt>hostname
9079-
<dd>|url|'s [=url/host=] if |has hostname| is true, or null otherwise.
9080-
<dt>port
9081-
<dd>
9082-
1. If |has port| is false:
9083-
9084-
1. null.
9085-
9086-
1. Otherwise:
9087-
9088-
1. If |url|'s [=url/scheme=] [=is special=] and |url|'s [=url/scheme=]'s
9089-
[=default port=] is not null, and |url|'s [=url/port=] is null or is
9090-
equal to [=url/scheme=]'s [=default port=]:
9091-
9092-
1. The empty string.
8911+
1. Let |url pattern| be the result of [=creating a URLPattern=] given
8912+
|pattern|["<code>pattern</code>"].
90938913

9094-
1. Otherwise, if |url|'s [=url/port=] is not null:
9095-
9096-
1. [=Serialize an integer=] with |url|'s [=url/port=].
9097-
9098-
1. Otherwise:
9099-
9100-
1. null.
9101-
9102-
<dt>pathname
9103-
<dd>
9104-
1. If |has pathname| is false:
9105-
9106-
1. null.
9107-
9108-
1. Otherwise:
9109-
9110-
1. The result of running the [=URL path serializer=] with |url|, if
9111-
|url|'s [=url/path=] is not the empty string and is not [=list/empty=],
9112-
or null otherwise.
9113-
9114-
<dt>search
9115-
<dd>
9116-
1. If |has search| is false:
9117-
9118-
1. null.
9119-
9120-
1. Otherwise:
9121-
9122-
1. The empty string if |url|'s [=url/query=] is null, or |url|'s [=url/query=] otherwise.
9123-
</dl>
8914+
1. If |url pattern| is failure, return [=error=] with [=error code=]
8915+
[=invalid argument=].
91248916

9125-
1. Return [=success=] with data |parsed|.
8917+
1. Return [=success=] with data |url pattern|.
91268918

91278919
</div>
91288920

91298921
<div algorithm>
91308922
To <dfn>match URL pattern</dfn> given |url pattern| and |url string|:
91318923

9132-
1. Let |url| be the result of [=URL parser|parsing=] |url string|.
9133-
9134-
1. If |url pattern|'s protocol is not null and is not equal to |url|'s
9135-
[=url/scheme=], return false.
9136-
9137-
1. If |url pattern|'s hostname is not null and is not equal to |url|'s
9138-
[=url/host=], return false.
9139-
9140-
1. If |url pattern|'s port is not null:
9141-
9142-
1. Let |port| be null.
9143-
9144-
1. If |url|'s [=url/scheme=] [=is special=] and |url|'s
9145-
[=url/scheme=]'s [=default port=] is not null, and |url|'s [=url/port=] is
9146-
null or is equal to [=url/scheme=]'s [=default port=]:
9147-
9148-
1. Set |port| to the empty string.
9149-
9150-
1. Otherwise, if |url|'s [=url/port=], is not null:
9151-
9152-
1. Set |port| to [=serialize an integer=] with |url|'s [=url/port=].
9153-
9154-
1. If |url pattern|'s port is not equal to |port|, return false.
9155-
9156-
1. If |url pattern|'s pathname is not null and is not equal to the result of
9157-
running the [=URL path serializer=] with |url|, return false.
9158-
9159-
1. If |url pattern|'s search is not null:
9160-
9161-
1. Let |url query| be |url|'s [=url/query=].
9162-
9163-
1. If |url query| is null, set |url query| to the empty string.
9164-
9165-
1. If |url pattern|'s search is not equal to |url query|, return false.
9166-
9167-
1. Return true.
8924+
1. Return the result of running [=URL pattern match=] given |url pattern| and
8925+
|url string|.
91688926

91698927
</div>
91708928

0 commit comments

Comments
 (0)