Skip to content

Commit

Permalink
Use "inherit left, wildcard right" behavior in base URL application a…
Browse files Browse the repository at this point in the history
…nd constructor string parsing

The following changes apply to patterns which are constructed using a base URL, the constructor string syntax, or both -- but not any pattern which explicitly specifies components separately without a base URL.

* Components are not inherited from a base URL if an "earlier" component is explicitly specified.
* In the string format, unspecified "later" components are implicitly wildcarded, rather than required to be empty (with the exception of the port, which is always taken to be specified when the hostname is).
* Username and password are never implicitly specified or inherited.

For example:
1. `"https://example.com/*"` also matches with any username, password, search, and hash. Previously this would be written `"https://*:*@example.com/*\\?*#*"`.
2. `new URLPattern({ pathname: "/login" }, "https://example.com/?q=hello")` accepts any query string and hash, not only `"?q=hello"` and `""`.
3. `"https://*:*"` or `{protocol: "https"}` means "any HTTPS URL, on any port", and `"https://*"` means "any HTTPS URL on the default port (443)". These have the same meaning whether or not a base URL is provided, since specifying the protocol prohibits inheritance of other components.

This makes patterns more expansive than before, in cases where wildcards are likely to be desirable.

The logic of inheriting components from a base URL dictionary is also similarly changed in a way that may make it _not_ match where it did before, but more consistently with the above and with how relative URL strings are resolved. For example, `new URLPattern("https://example.com/foo?q=1#hello").test({pathname: "/foo", hash: "hello", baseURL: "https://example.com/bar?q=1"})` previously returned `true` but will now be `false`, since the search component is not inherited when the pathname is specified. This is analogous to how `new URL("/foo#hello", "https://example.com/bar?q=1")` works. The reverse is also possible; in both cases this is quite niche.

Fixes #179.
  • Loading branch information
jeremyroman authored Nov 7, 2023
1 parent 0343b0b commit ed205a4
Showing 1 changed file with 138 additions and 39 deletions.
Loading

0 comments on commit ed205a4

Please sign in to comment.