Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in a document. Selectors have been optimized for use with HTML and XML, and are designed to be usable in performance-critical code. They are a core component of CSS (Cascading Style Sheets), which uses Selectors to bind style properties to elements in the document.
Selectors Level 5 describes the selectors that already exist in [selectors-4], and further introduces new selectors for CSS and other languages that may need them.
CSS is a language for describing the rendering of structured documents
(such as HTML and XML)
on screen, on paper, etc.
Status of this document
This is a public copy of the editors’ draft.
It is provided for discussion only and may change at any moment.
Its publication here does not imply endorsement of its contents by W3C.
Don’t cite this document other than as work in progress.
Please send feedback
by filing issues in GitHub (preferred),
including the spec code “selectors” in the title, like this:
“[selectors] …summary of comment…”.
All issues and comments are archived.
Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.
The :local-link pseudo-class allows authors to style hyperlinks based on the users current location within a site and to
differentiate site-internal versus site-external links.
The (non-functional) :local-link pseudo-class represents an element that is
the source anchor of a hyperlink whose target’s absolute URL
matches the element’s own document URL.
Any fragment identifiers are stripped before matching the document’s URL against the link’s URL;
otherwise all portions of the URL are considered.
For example, the following rule prevents links targeting the
current page from being underlined when they are part of the
navigation list:
nav :local-link { text-decoration: none; }
As a functional pseudo-class, :local-link() can also accept a non-negative integer as its sole argument,
which, if the document’s URL belongs to a hierarchical scheme,
indicates the number of path levels to match:
:local-link(0) represents a link element whose target is in the same origin as the document’s URL
:local-link(1) represents a link element whose target has the same origin and first path segment
:local-link(2) represents a link element whose target has the same origin, first, and second path segments
etc.
The following example styles all site-external links with a dashed
underline.
Path segments are portions of the URL’s path that are separated by forward slashes (/).
If a segment is missing from the document’s URL,
a pseudo-class requiring that segment to match does not match anything.
If the document’s URL is http://www.example.com/2011/03/:
Link 1 would receive Style B
Link 2 would receive Styles B and C
Link 3 would receive Styles B, C, and D
Link 4 would also receive Styles A, B, C, D, and E
Link 5 would receive Styles B, C, and D
Link 6 would remain unstyled
Link 7 would remain unstyled
Style F would not be applied to anything
The "origin" of the URL is defined by RFC 6454, Section 4.
The username, password, query string, and fragment portions of the URL are not considered
when matching against :local-link(n).
If the document’s URL does not belong to a hierarchical scheme,
the functional pseudo-class matches nothing.
It’s clear that, if the document URL has at least N segments,
then :local-link(N) only matches links whose URL has at least N segments.
(This lets you assign consistent semantics to :local-link so that,
for example, :local-link(2) means a "within-repo" link on GitHub.)
What about if the document url has less than N segments,
and the link is same-page?
Should "null segments" count as matching, or not?
3. Exposing custom state: the :state() pseudo-class
Note: The "is" matching behavior compares strings by codepoint;
notably, it’s case-sensitive.
So if "foo" is in the states set, :state(FOO) will not match.
The exact matching behavior of :state() pseudo-class
is defined by the host language.
For clarity, the concepts explaining this pseudo-class
link to the HTML definition;
see HTML’s definition for more detail.
Other host languages must define how this pseudo-class matches.
4. Combinators
4.1. Reference combinators /ref/
The reference combinator consists of two slashes
with an intervening CSS qualified name,
and separates two compound selectors,
e.g. A /attr/ B.
The element represented by the first compound selector explicitly references
the element represented by the second compound selector.
Unless the host language defines a different syntax for expressing this relationship,
this relationship is considered to exist if
the value of the specified attribute on the first element is an IDREF or an ID selector referencing the second element.
Attribute matching for reference combinators follow the same rules as for attribute selectors.
The following example highlights an input element
when its <label> is focused or hovered-over:
label:is(:hover, :focus) /for/ input, /* association by "for" attribute */
label:is(:hover, :focus):not([for]) input { /* association by containment */
box-shadow: yellow 0 0 10px;
}
The CSS working group would like to thank everyone who contributed
to the previous Selectors specifications over the years,
as those specifications formed the basis for this one.
In particular, the working group would like to extend special thanks
to the following for their specific contributions to Selectors Level 5:
Joey Arhar.
Privacy Considerations
Should be copied from Level 4 when appropriate.
Security Considerations
Should be copied from Level 4 when appropriate.
Conformance
Document conventions
Conformance requirements are expressed with a combination of
descriptive assertions and RFC 2119 terminology. The key words “MUST”,
“MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”,
“RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this
document are to be interpreted as described in RFC 2119.
However, for readability, these words do not appear in all uppercase
letters in this specification.
All of the text of this specification is normative except sections
explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words “for example”
or are set apart from the normative text with class="example",
like this:
This is an example of an informative example.
Informative notes begin with the word “Note” and are set apart from the
normative text with class="note", like this:
Note, this is an informative note.
Advisements are normative sections styled to evoke special attention and are
set apart from other normative text with <strong class="advisement">, like
this: UAs MUST provide an accessible alternative.
Tests
Tests relating to the content of this specification
may be documented in “Tests” blocks like this one.
Any such block is non-normative.
Conformance classes
Conformance to this specification
is defined for three conformance classes:
A style sheet is conformant to this specification
if all of its statements that use syntax defined in this module are valid
according to the generic CSS grammar and the individual grammars of each
feature defined in this module.
A renderer is conformant to this specification
if, in addition to interpreting the style sheet as defined by the
appropriate specifications, it supports all the features defined
by this specification by parsing them correctly
and rendering the document accordingly. However, the inability of a
UA to correctly render a document due to limitations of the device
does not make the UA non-conformant. (For example, a UA is not
required to render color on a monochrome monitor.)
An authoring tool is conformant to this specification
if it writes style sheets that are syntactically correct according to the
generic CSS grammar and the individual grammars of each feature in
this module, and meet all other conformance requirements of style sheets
as described in this module.
Partial implementations
So that authors can exploit the forward-compatible parsing rules to
assign fallback values, CSS renderers must treat as invalid (and ignore
as appropriate) any at-rules, properties, property values, keywords,
and other syntactic constructs for which they have no usable level of
support. In particular, user agents must not selectively
ignore unsupported component values and honor supported values in a single
multi-value property declaration: if any value is considered invalid
(as unsupported values must be), CSS requires that the entire declaration
be ignored.
Implementations of Unstable and Proprietary Features
Once a specification reaches the Candidate Recommendation stage,
non-experimental implementations are possible, and implementors should
release an unprefixed implementation of any CR-level feature they
can demonstrate to be correctly implemented according to spec.
To establish and maintain the interoperability of CSS across
implementations, the CSS Working Group requests that non-experimental
CSS renderers submit an implementation report (and, if necessary, the
testcases used for that implementation report) to the W3C before
releasing an unprefixed implementation of any CSS features. Testcases
submitted to W3C are subject to review and correction by the CSS
Working Group.
It’s clear that, if the document URL has at least N segments,
then :local-link(N) only matches links whose URL has at least N segments.
(This lets you assign consistent semantics to :local-link so that,
for example, :local-link(2) means a "within-repo" link on GitHub.)
What about if the document url has less than N segments,
and the link is same-page?
Should "null segments" count as matching, or not? ↵