1. Declaring named URL patterns: the @route rule
The @route rule is an at-rule that associates a name with a URL pattern. This name can be referenced in @navigation rules and in :link-to() pseudo-classes.
The syntax of the @route rule is:
@route <dashed-ident> {
[ <pattern-descriptors> | <init-descriptors> ]
}
based on the following definitions:
<pattern-descriptors> = ;* <pattern-descriptor> ;*
<pattern-descriptor> = pattern : <url-pattern()>
<init-descriptors> = ;* <init-descriptor> [ ;+ <init-descriptor> ]* ;*
<init-descriptor> = <init-descriptor-name> : <string>
<init-descriptor-name> = protocol | hostname | port | pathname |
search | hash | base-url
This associates an author-defined keyword with a URL pattern, so that any URL that matches one of the URL patterns matches the route named by the keyword.
The @route rule can be defined in one of two ways:
- with the
patterndescriptor -
in this case the URL pattern represented is the one represented by the <url-pattern()> function given as the descriptor’s value.
- with the other descriptors named by <init-descriptor-name>
-
In this case the URL pattern represented is the result of invoking create a URL pattern given input as
URLPatternInitconstructed from the descriptors and their values. Each dictionary member is given the value of the descriptor with the same name, except thebaseURLmember is given the value of thebase-urldescriptor. If abase-urldescriptor is not given then one is created from the style resource base URL of the rule.
Should this use <dashed-ident>, <custom-ident>, or <ident> for the route names?
Is there value in being able to provide a list of <url-pattern()> rather than just one?
NOTE: The list of allowed init descriptors does not include username
or password since they seem unlikely to be useful.
This would also give us the option to remove the braces from the syntax of the @route rule and make it more like @import or @namespace. This does remove a potential future extensibility point, but it could also be added back later if we need it.
or this rule:@route --movie-list{ pattern : url-pattern ( "/movie-list" ); }
define an @route rule that associates the name@route --movie-list{ pathname : "/movie-list" ; }
--movie-list
with the URL "/movie-list" resolved relative to the style sheet.
NOTE: The bracing syntax also allows for future expansion if needed.
NOTE: Some of the design discussion for this feature has been in w3c/csswg-drafts#12594.
2. Conditional rules for navigation queries
2.1. Navigation queries: the @navigation rule
The @navigation rule is a conditional group rule whose condition tests characteristics of the current URL or of the state of navigation between two URLs. These queries are called navigation queries.
Authors can use it to:
-
write style sheets that apply to multiple pages but behave somewhat differently between those pages,
-
write style sheets that apply to single page applications that change their URL over time, so that style changes when the URL changes, and
-
write style sheets that declaratively start view transitions (or make other appropriate style changes) in response to navigations.
The syntax of the condition in the @navigation rule is similar to that defined for <supports-condition> in [CSS-CONDITIONAL-3]. Negation, conjunction, and disjunction are all needed so that authors can specify the interaction of multiple styles in ways that are most intuitive and require the simplest code.
The @navigation rule can be used in simple cases to define styles that only affect a particular page:
@navigation ( at:url-pattern ( "/" )) { /* These styles only apply to the site's homepage (including any URL with a search or hash). */ }
The @navigation rule can also be used to define styles that are used when a certain navigation is in progress. This is particularly useful for defining styles that cause view transitions.
@route --search-results-page{ pattern : url-pattern ( "/search-results" ); } @route --product-page{ pattern : url-pattern ( "/product/:id" ); } @navigation ( from: --search-results-page) and( to: --product-page) { /* These styles apply when a navigation is in progress from a search results page to a product page (as defined by the @route rules above), but not in the reverse direction. */ } @navigation ( between: --search-results-page and --product-page) { /* These styles apply when a navigation is in progress between a search results page and a product page (as defined by the @route rules above), in either direction. */ }
The syntax of the @navigation rule is:
@navigation <navigation-condition> {
<rule-list>
}
with <navigation-condition> defined as:
<navigation-condition> = not <navigation-in-parens>
| <navigation-in-parens> [ and <navigation-in-parens> ]*
| <navigation-in-parens> [ or <navigation-in-parens> ]*
<navigation-in-parens> = ( <navigation-condition> ) | ( <navigation-test> ) | <general-enclosed>
<navigation-test> = <navigation-location-test> |
<navigation-location-between-test> |
<navigation-type-test>
<navigation-location-test> = <navigation-location-keyword> : <navigation-location>
<navigation-location-keyword> = at | with | from | to
<navigation-location> = <route-name> | <url-pattern()>
<route-name> = <dashed-ident>
<navigation-location-between-test> =
between : <navigation-location> and <navigation-location>
<navigation-type-test> = history : <navigation-type-keyword>
<navigation-type-keyword> = traverse | back | forward | reload
Should we use at/with/from/to or current/other/from/to?
The above grammar is purposely very loose for forwards-compatibility reasons, since the <general-enclosed> production allows for substantial future extensibility. Any @navigation rule that does not parse according to the grammar above (that is, a rule that does not match this loose grammar which includes the <general-enclosed> production) is invalid. Style sheets must not use such a rule and processors must ignore such a rule (including all of its contents).
Many of these grammar terms are associated with a boolean result, as follows:
- <navigation-condition>
-
- not <navigation-in-parens>
-
The result is the negation of the <navigation-in-parens> term.
- <navigation-in-parens> [ and ]*
-
The result is true if all of the <navigation-in-parens> child terms are true, and false otherwise.
- <navigation-in-parens> [ or ]*
-
The result is false if all of the <navigation-in-parens> child terms are false, and true otherwise.
- <navigation-in-parens>
-
The result is the result of the child subexpression.
- <navigation-location-test>
-
- at: <navigation-location>
-
The result is whether the result of match a URL pattern is non-null given urlPattern as the navigation location URL pattern of <navigation-location> and input as the document’s URL.
- with: <navigation-location>
-
The result is true if the current other URL other of the document is non-null and match a URL pattern is non-null when given urlPattern as the navigation location URL pattern of <navigation-location> and input as other.
- from: <navigation-location>
-
The result is true if the current from URL from of the document is non-null and match a URL pattern is non-null when given urlPattern as the navigation location URL pattern of <navigation-location> and input as from.
- to: <navigation-location>
-
The result is true if the current to URL to of the document is non-null and match a URL pattern is non-null when given urlPattern as the navigation location URL pattern of <navigation-location> and input as to.
- <navigation-location-between-test>
-
- between: <navigation-location> and
-
The result is true if the current from URL from of the document is non-null, the current to URL to of the document is non-null, match a URL pattern is non-null given urlPattern as the navigation location URL pattern of one of the two <navigation-location>s and input as from, and match a URL pattern is non-null given urlPattern as the of the other of the two s and input as to.
- <navigation-type-test>
-
- history: traverse
-
True if the current navigation type is
traverse. - history: back
-
True if the current navigation type is
traverseand the current navigation delta is less than 0. - history: forward
-
True if the current navigation type is
traverseand the current navigation delta is greater than 0. - history: reload
-
True if the current navigation type is
reload.
- <general-enclosed>
-
The result is false.
Authors must not use <general-enclosed> in their stylesheets. It exists only for future-compatibility, so that new syntax additions do not invalidate too much of a <navigation-condition> in older user agents.
The navigation location URL pattern of a <navigation-location> depends on the type of :
- <route-name>
-
the URL pattern represented by the @route rule referenced by the name.
- <url-pattern()>
-
The URL pattern represented by the function; see create a URL pattern for url-pattern().
Should it also be possible to reference a name defined in a routemap? See the route matching explainer for details.
A document’s navigation API is the result of the following steps on document:
-
Let window be the
Windowwhose associated Document is document, or null if there is no suchWindow. -
If window is null, return null.
-
Return window’s navigation API.
The condition of the @navigation rule is the result of the <navigation-condition> in its prelude.
NOTE: Some of the design discussion for this feature has been in w3c/csswg-drafts#12594 and w3c/csswg-drafts#8209.
2.2. The navigation() function for @when
This specification defines an additional function for the @when rule:
navigation() = navigation( <navigation-condition> )
The navigation() function is associated with the boolean result that its contained condition is associated with.
2.3. The navigation() function for if()
This specification defines an additional function for the if() function’s <if-test> production:
navigation() = navigation( <navigation-condition> )
This should probably have a more formal definition of the function, but I can’t find the formal definitions of the existing if() functions to model it after.
3. Pseudo-class for navigation-related links: :link-to()
This specification defines a new :link-to() functional pseudo-class that matches link elements that link to a certain URL.
A simple example of a :link-to() selector is this one, which matches any links that link to the site’s homepage:
:link-to( url-pattern ( "/" )) { font-weight : bold; }
A more interesting example of the :link-to() pseudo-class is this example which creates a view transition between a item in a list that contains a link (in this document) and the details page for that link (in a different document). This transition works even when the navigation is a back/forward navigation and even if the user has used a language selector UI to change the page into a different language (and thus change the URL). The use of the :link-to() pseudo-class ensures that the view transition animations from or to the correct item in the list by matching the relevant parts of the navigation URL to the link URL.
@view-transition { /* allow cross-document view transitions */ navigation: auto; } @route --movie-details-with-id{ /* match URLs like /en/movie/123 which is the English page about a movie with ID 123. Be careful to specify the language part with a "*" but the ID part with a named :id parameter so that matching using the 'match-params' keyword (the default) will require equal IDs but allow differences of language. */ pattern:url-pattern ( "/*/movie/:id" ); } /* capture the overall area representing the movie, and a sub-area for its poster image */ /* match an element with class movie-container with a child link that links to a movie whose id is the same as the movie we are currently navigating to or from. (lang can be different, though.) This depends on the --movie-details-with-id route declaring the ID but not the language with a named parameter, and the use of match-params(navigation-with). This means that both the of the link and the other URL of the current navigation match the URL pattern defined by the "@route --movie-details-with-id" rule, and that the id named group from both matches be the same. (However, the URLs can be different because the * part of the match, containing the language, can be different.) */ .movie-container:has( > .movie-title:link-to( --movie-details-with-id withmatch-params ( navigation-with))) { view-transition-name : movie-container; > .movie-poster{ view-transition-name : movie-poster; } /* leave the default cross-fade animation for both image captures */ }
The :link-to() pseudo-class takes a single argument, a <link-condition>, and the pseudo-class matches any element where:
-
the element matches :any-link
-
the target of link matches the <link-condition>, as defined below.
<link-condition> = <link-condition-base> [ with <navigation-match> ]?
<link-condition-base> = <navigation-location>
// Note: <navigation-match> is defining three functions
// with identical syntax.
<navigation-match> = <pattern-match-type>( <navigation-match-target> )
<pattern-match-type> = match-pattern | match-params | match-url
<navigation-match-target> = navigation-at | navigation-with |
navigation-from | navigation-to
Should we use navigation-(at/with/from/to) or navigation-(current/other/from/to)?
Should the :link-to() variant that has a <link-condition> have a different name, such as :navigating-link()?
A <link-condition> matches the target of the link when both:
-
the <link-condition-base> matches the target of the link, and
-
either:
-
a <navigation-match> is not present, or
-
the <navigation-match> matches the target of the link, with the URL pattern represented by the <link-condition-base> as context
-
A <link-condition-base> represents a URL pattern. If the <link-condition-base> is a <url-pattern()>, then it represents the URL pattern represented by the <url-pattern()> function (see create a URL pattern for url-pattern()). If it is a <route-name>, then it represents the URL pattern represented by the @route rule.
A <link-condition-base> matches a URL when match a URL pattern is non-null given urlPattern as the URL pattern that it represents and input as the given URL.
A <navigation-match> matches the URL linkTarget (with a URL Pattern urlPattern as context) if the following steps return true:
-
Let targetMatchResult be the result of match a URL pattern given urlPattern and linkTarget.
-
If targetMatchResult is null, return false.
NOTE: This doesn’t really matter because in this case the <link-condition-base> also doesn’t match.
-
Let matchType be the <pattern-match-type> of the <navigation-match>.
-
Let matchTarget be the <navigation-match-target> argument of the <navigation-match>.
-
Let navigationURL be:
- If matchTarget is navigation-at,
-
the document’s [URL].
- Otherwise, if matchTarget is navigation-with,
-
the current other URL of the document.
- Otherwise, if matchTarget is navigation-from,
-
the current from URL of the document.
- Otherwise (Assert: matchTarget is navigation-to),
-
the current to URL of the document.
-
If navigationURL is null, return false.
-
- If matchType is match-pattern
-
Return true if the result of match a URL pattern given urlPattern and navigationURL is not null; otherwise return false.
- If matchType is match-params
-
-
Let navigationMatchResult be the result of match a URL pattern given navigationURL and urlPattern.
-
If either navigationMatchResult is null, return false.
-
For each property prop of
URLPatternResultthat is aURLPatternComponentResult: -
Return true.
-
- If matchType is match-url
-
Return true if linkTarget equals navigationURL; otherwise return false.
NOTE: Some of the design discussion for this feature has been in w3c/csswg-drafts#13163.
4. Definitions of current navigation state
Both the @navigation rule and the :link-to() pseudo-class rely on the following definitions of the current other URL, current from URL, and current to URL.
The current from URL of a document is a URL or null. It is defined as follows:
-
If the document’s navigation API of the document is non-null and its transition is non-null, its from entry’s
url.NOTE: This part is for when the old document in the navigation is still the current document.
-
If the document’s navigation API of the document is non-null, its activation is non-null, the activation’s
fromis non-null, and the document’s has been revealed is false or was false at the start of the current task, the activation’sfrom’surl.NOTE: This part is for when the new document in the navigation has become the current document.
-
Otherwise, null.
NOTE: The previous two branches can also produce null results.
The current to URL of a document is a URL or null. It is defined as follows:
-
If the document’s navigation API of the document is non-null and its ongoing navigate event is non-null:
-
if the
pageswapevent has fired since that navigation began, and itsactivationwas non-null, and thatactivation’sentry’surlis non-null, then thaturl.NOTE: This part does expose the result of redirects.
NOTE: This part is not relevant to normal page rendering. However, it can be relevant to what is rendered when capturing the image for a cross-document view transition.
-
otherwise, the ongoing navigate event’s
destination’surlNOTE: This part does not expose the result of redirects.
This assumes that the ongoing navigate event and the transition have the same lifetime, but this isn’t really true if the event is intercepted. After whatwg/html#11690 / whatwg/html#11692. we could probably define this more like "from" above. But which lifetime is the one we want?
NOTE: This part is for when the old document in the navigation is still the current document.
-
-
If the document’s navigation API of the document is non-null and its activation is non-null, the document’s has been revealed is false or was false at the start of the current task, and the activation’s
entry’surl.NOTE: This part is for when the new document in the navigation has become the current document.
Does it make sense to expose this when the activation’s
fromis null, and thus there is no current from URL? -
Otherwise, null.
NOTE: The previous two branches can also produce null results.
The above definitions of from and to apparently don’t work right
if you start a same-document navigation (e.g., with pushState)
in the middle of a cross-document navigation.
The current other URL of a document is a URL or null. It is defined as follows:
The current navigation type of a document is a NavigationType or null.
It is defined as follows:
-
If the document’s navigation API of the document is non-null and its transition is non-null, the transition’s
navigationType.NOTE: This part is for when the old document in the navigation is still the current document.
-
If the document’s navigation API of the document is non-null and its activation is non-null, the document’s has been revealed is false or was false at the start of the current task, the activation’s
navigationType.NOTE: This part is for when the new document in the navigation has become the current document.
-
Otherwise, null.
The current navigation delta of a document is a NavigationType or null.
It is defined as follows:
-
If the document’s navigation API of the document is non-null and its transition is non-null,
-
If the transition’s
navigationTypeis nottraverse, null. -
Otherwise, the transition’s
to’sindexminus the transition’sfrom’sindex.
NOTE: This part is for when the old document in the navigation is still the current document.
-
-
If the document’s navigation API of the document is non-null, its activation is non-null, the activation’s
fromis non-null, and the document’s has been revealed is false or was false at the start of the current task,-
If the activation’s
navigationTypeis nottraverse, null. -
Otherwise, the activation’s
entry’sindexminus the activation’sfrom’sindex.
NOTE: This part is for when the new document in the navigation has become the current document.
-
-
Otherwise, null.
Generally improve integration with the HTML spec for these definitions,
instead of monkeypatching.
This includes the interaction with has been revealed
and the interaction with the pageswap event,
and other things where this section links to non-exported definitions.
Generally figure out if these definitions should care about the ongoing navigate event or the transition.
5. The url-pattern() function
The url-pattern() function represents a URL pattern, which can be used to match URLs.
<url-pattern()> = url-pattern( <string> )
This function represents a URL pattern that can be created using the steps of the create a URL pattern for url-pattern() algorithm:
-
Let arg be the <string> argument to the url-pattern() function.
-
Let baseURL be the style resource base URL of the rule or declaration block containing the url-pattern() function.
Do we want this to be the base URL all the time? For use of url-pattern() in @navigation, it’s likely more useful for the base URL to be the document URL rather than the style sheet URL. However, it would be very awkward for url-pattern() to be inconsistent with url().Should we allow the base URL of url-pattern() to be defined by the consumer? Should we introduce
document-url-pattern()? Should we do something similar to CSS Images 3 § 2.1.1 Ambiguous Reference-or-Image URLs (see w3c/csswg-drafts issue #383)?Also see other proposed uses of
URLPatternin CSS in w3c/csswg-drafts issue #10975, for :local-link. -
Return the result of create a URL pattern given arg, baseURL, and an empty map.
NOTE: This function requires that its argument is quoted. This differs from the url() function, which allows its argument to be quoted or unquoted.
To serialize a url-pattern() function f, serialize a function f, using serialize a string on the single argument to serialize f’s contents.
NOTE: This is defined this way because URLPattern
intentionally does not provide a serialization.