Skip to content

Commit fc7ae45

Browse files
committed
Expand introductory text and examples.
Some simple introductory examples are added, along with some brief overview text of both URL patterns and (component-specific) pattern strings. Some more detailed examples already exist within the relevant parsing code. This contributes to resolving whatwg#127.
1 parent 80af74a commit fc7ae45

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

spec.bs

+54
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,54 @@ table {
9292
top: -0.8em;
9393
left: -0.8em;
9494
}
95+
96+
/* props from https://resources.whatwg.org/standard.css */
97+
dl.props { display: grid; grid-template-columns: max-content auto; row-gap: 0.25em; column-gap: 1em; }
98+
dl.props > dt { grid-column-start: 1; margin: 0; }
99+
dl.props > dd { grid-column-start: 2; margin: 0; }
100+
p + dl.props { margin-top: -0.5em; }
95101
</style>
96102

97103
<script src="https://resources.whatwg.org/file-issue.js" async></script>
98104

99105
<h2 id=urlpattern-class>The {{URLPattern}} class </h2>
100106

107+
A {{URLPattern}} consists of several [=components=], each of which represents a [=pattern string|pattern=] which could be matched against the corresponding component of a [=URL=].
108+
109+
It can be constructed using a string for each component, or from a shorthand string. It can optionally be resolved relative to a base URL.
110+
111+
<div class="example">
112+
The shorthand "`https://example.com/:category/*`" corresponds to the following components:
113+
114+
<dl class="props">
115+
: [=protocol component|protocol=]
116+
:: "`https`"
117+
: [=username component|username=]
118+
:: ""
119+
: [=password component|password=]
120+
:: ""
121+
: [=hostname component|hostname=]
122+
:: "`example.com`"
123+
: [=port component|port=]
124+
:: ""
125+
: [=pathname component|pathname=]
126+
:: "`/:category/*`"
127+
: [=search component|search=]
128+
:: ""
129+
: [=hash component|hash=]
130+
:: ""
131+
</dl>
132+
133+
It matches the following URLs:
134+
* `https://example.com/products/`
135+
* `https://example.com/blog/our-greatest-product-ever`
136+
137+
It does not match the following URLs:
138+
* `https://example.com/`
139+
* `http://example.com/products/`
140+
* `https://example.com:8443/blog/our-greatest-product-ever`
141+
</div>
142+
101143
<xmp class="idl">
102144
typedef (USVString or URLPatternInit) URLPatternInput;
103145

@@ -833,6 +875,18 @@ To <dfn>compute protocol matches a special scheme flag</dfn> given a [=construct
833875

834876
A <dfn>pattern string</dfn> is a string that is written to match a set of target strings. A <dfn for="pattern string">well formed</dfn> 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.
835877

878+
It can be [=parse a pattern string|parsed=] to produce a [=part list=] which describes, in order, what must appear in a component string for the pattern string to match.
879+
880+
<div class="example">
881+
Pattern strings can contain capture groups, which by default match the shortest possible string, up to a component-specific separator (`/` in the pathname, `.` in the hostname). For example, the pathname pattern "`/blog/:title`" will match "`/blog/hello-world`" but not "`/blog/2012/02"`.
882+
883+
A regular expression can also be used instead, so the pathname pattern "`/blog/:year(\\d+)/:month(\\d+)`" will match "`/blog/2012/02`".
884+
885+
A group can also be made optional, or repeated, by using a modifier. For example, the pathname pattern "`/products/:id?"` will match both "`/products`" and "`/products/2`" (but not "`/products/`"). In the pathname specifically, groups automatically require a leading `/`; to avoid this, the group can be explicitly deliminated, as in the pathname pattern "`/products/{:id}?"`.
886+
887+
A full wildcard `*` can also be used to match as much as possible, as in the pathname pattern "`/products/*`".
888+
</div>
889+
836890
<h3 id=parsing-patterns>Parsing Patterns</h3>
837891

838892
<h4 id=tokens>Tokens</h4>

0 commit comments

Comments
 (0)