</x-foo>
For a stylesheet within the shadow tree:
:host matches the <x-foo>
element.
x-foo matches nothing.
.foo matches only the <div>
element.
.foo:host matches nothing
:host(.foo) matches the <x-foo>
element.
Ordinary, selectors within a shadow tree can’t see elements outside the shadow tree at all. Sometimes, however, it’s useful to select an ancestor that lies somewhere outside the shadow tree, above it in the document.
The :host-context() functional pseudo-class tests whether there is an ancestor, outside the shadow tree, which matches a particular selector. Its syntax is:
:host-context( <compound-selector> )
When evaluated in the context of a shadow tree, the :host-context() pseudo-class matches the shadow host, if the shadow host or one of its shadow-including ancestors matches the provided <compound-selector>. In any other context, it matches nothing.
The specificity of :host-context() is that of a pseudo-class, plus the specificity of its argument.
Note: This means that the selector pierces through shadow boundaries on the way up, looking for elements that match its argument, until it reaches the document root.
The ::slotted() pseudo-element represents the elements assigned, after flattening, to a slot. This pseudo-element only exists on slots.
The ::slotted() pseudo-element is an alias for other elements in the tree, and does not generate any boxes itself.
The grammar of the ::slotted() pseudo-element is:
::slotted( <compound-selector> )
The ::slotted() pseudo-element represents the elements that are:
assigned, after flattening, to the slot that is ::slotted’s originating element
matched by its <compound-selector> argument
The ::slotted() pseudo-element can be followed by a tree-abiding pseudo-element, like ::slotted()::before, representing the appropriate pseudo-element of the elements represented by the ::slotted() pseudo-element.
The specificity of ::slotted() is that of a pseudo-element, plus the specificity of its argument.
<x-foo> <div id="one" slot="foo" class="foo">...</div> <div id="two" slot="foo">...</div> <div id="three" class="foo"> <div id="four" slot="foo">...</div> </div> <"shadow tree"> <div id="five">...</div> <div id="six">...</div> <slot name="foo"></slot> </"shadow tree"> </x-foo>
For a stylesheet within the shadow tree, a selector like ::slotted(*) selects #one and #two only, as they’re the elements assigned to the sole slot
element. It will not select #three (no slot
attribute) nor #four (only direct children of a shadow host can be assigned to a slot).
A selector like ::slotted(.foo), on the other hand, will only select #one, as it matches .foo, but #two doesn’t.
Note: Note that a selector like ::slotted(*) is equivalent to *::slotted(*), where the * selects many more elements than just the slot
element. However, since only the slot
elements are slots, they’re the only elements with a ::slotted() pseudo-element as well.
Note: ::slotted() can only represent the elements assigned to the slot. Slots can also be assigned text nodes, which can’t be selected by ::slotted(). The only way to style assigned text nodes is by styling the slot and relying on inheritance.
The :has-slotted pseudo-class matches slot
elements which have a non-empty list of flattened slotted nodes.
When :has-slotted matches a slot with fallback content, we can conclude that the fallback content is not being displayed.
Note: Even a single whitespace text node is sufficient to make :has-slotted' apply. This is by design, so that the behavior of this pseudo-class is consistent with the behavior of the assignedNodes()
method. A future version of this specification is expected to introduce a way to exclude this case from matching.
Note: It is expected that a future version of this specification will introduce a functional :has-slotted() pseudo-class that allows more fine-grained matching by accepting a selector argument. :has-slotted is not an alias of :has-slotted(*), as the latter would not match slotted text nodes, but :has-slotted does.
See CSS Cascading 4 § 6.1 Cascade Sorting Order.
While Selectors operates on the DOM tree as the host language presents it, with separate trees that are unreachable via the standard parent/child relationship, the rest of CSS needs a single unified tree structure to work with. This is called the flattened element tree (or flat tree), and is constructed as follows:
Let pending nodes be a list of DOM nodes with associated parents, initially containing just the document’s root element with no associated parent.
Repeatedly execute the following substeps until pending nodes is empty:
Pop the first element from pending nodes, and assign it to pending node.
Insert pending node into the flat tree as a child of its associated parent. (If it has no associated parent, it’s the document root—just insert it into the flat tree as its root.)
Perform one of the following, whichever is the first that matches:
If no slottables were found for pending node, instead append its children to pending nodes, with pending node as their associated parent.
Note: In other words, the flat tree is the top-level DOM tree, but shadow hosts are filled with their shadow tree children instead of their light tree children (and this proceeds recursively if the shadow tree contains any shadow hosts), and slots get filled with the nodes that are assigned to them (and this proceeds recursively if the slots are themselves assigned to a slot in a deeper shadow tree).
A non-obvious result of this is that elements assigned to a slot inherit from that slot, not their light-tree parent or any deeper slots their slot gets assigned to. This means that text nodes are styled by the shadow tree of their parent, with nobody else capable of intervening in any way. Do we want an additional pseudo-element for targeting those text nodes so they can be styled at all slot-assignment levels, like normal elements can be? This implies it needs to work for text nodes in the light tree before they’re assigned downwards, so this can’t just be a ::slotted() variant. Luckily, this is a long-standing request!
Slots must act as if they were assigned display: contents via a rule in the UA origin. This must be possible to override via display, so they do generate boxes if desired.
Note: A non-obvious result of assigning elements to slots is that they inherit from the slot they’re assigned to. Their original light tree parent, and any deeper slots that their slot gets assigned to, don’t affect inheritance.
Shadow trees are meant to be an encapsulation boundary, allowing independent authors to share code without accidentally polluting each other’s namespaces. For example, element IDs, which are generally meant to be unique within a document, can be validly used multiple times as long as each use is in a different shadow tree.
Similarly, several at-rules in CSS, such as @keyframes or @font-face, define a name that later at-rules or properties can refer to them by. Like IDs, these names are globally exposed and unique within a document; also like IDs, this restriction is now loosened to being unique within a given shadow tree.
However, property inheritance can carry values from one tree to another, which complicates referencing the correct definition of a given name. Done naively, this can produce surprising and confusing results for authors. This section defines a set of concepts to use in defining and referencing "global" names in a way that respects encapsulation and doesn’t give surprising results.
If an at-rule or property defines a name that other CSS constructs can refer to it by, such as a @font-face font-family name or an anchor-scope name, it must be defined as a tree-scoped name. Tree-scoped names are associated with the root of the element hosting the stylesheet that the at-rule or property is declared in.
Additionally, tree-scoped names can be loosely matched or strictly matched, defaulting to loosely matched unless otherwise specified. A loosely matched tree-scoped name can be matched by tree-scoped references (see below) in the same tree or descendant trees, while a strictly matched tree-scoped name can only be matched by tree-scoped references in the exact same tree.
Properties or descriptors that reference a tree-scoped name, such as the font-family or animation-name properties, must define their value as a tree-scoped reference. These references implicitly capture a node tree root along with their specified value: unless otherwise specified, the root of the element hosting the stylesheet that the property or descriptor is declared in. This root reference stays with the tree-scoped reference as it is inherited.
If a tree-scoped name is global (such as @font-face names), then when a tree-scoped reference is dereferenced to find it, first search only the tree-scoped names associated with the same root as the tree-scoped reference. If no relevant tree-scoped name is found, and the root is a shadow root, then repeat this search in the root’s host’s node tree (recursively). (In other words, global tree-scoped names “inherit” into descendant shadow trees, so long as they don’t define the same name themselves.)
If a tree-scoped name is local to an element (such as anchor-name or anchor-scope values), then whether a tree-scoped reference matches the tree-scoped name on a given element depends on whether the tree-scoped name is strictly or loosely matched. A strictly matched tree-scoped name only matches if both names are associated with the same tree. A loosely matched tree-scoped name also matches if the tree-scoped name is associated with an ancestor tree.
If two tree-scoped names are directly compared (for example, when comparing computed values), they are considered to match only if their identifiers match, and their roots match exactly. (If one has a root that’s an ancestor of the other, for example, they do not match.)
Global names include:
@font-face, referenced by font-family
@font-feature-values, referenced by font-family
@keyframes, referenced by animation-name
@counter-style, referenced by list-style-type
@color-profile, referenced by the color() function
@font-palette-values, referenced by font-palette
others?
For example, given the following document (using the imaginary <::shadow></::shadow> markup to indicate an element’s shadow tree):
< p class = outer > Here's some text in the outer document's "foo" font.< style > @ font-face { font-family : foo ; src : url ( https :// example . com / outer . woff ); } body { font-family : foo ; } my-component :: part ( text ) { font-family : foo ; } </ style > < my-component > < ::shadow > < p class = inner-default > I'm inheriting the outer document's font-family.< p class = inner-styled > And I'm explicitly styled to be in the component's "foo" font.< p class = part-styled part = text > I'm explicitly styled by the outer document, and get the outer document's "foo" font.< style > @ font-face { font-family : foo ; src : url ( https :// example . com / inner . woff ); } . inner-styled { font-family : foo ; } </ style > </ ::shadow > </ my-component >
The .outer element references the outer @font-face, using the "outer.woff" file.
The .inner-default element inherits the font-family: foo value from the outer document, using the same tree-scoped reference as .outer, and thus also uses the "outer.woff" font file.
The .inner-style element, on the other hand, receives a font-family: foo from the stylesheet inside the shadow, and thus its tree-scoped reference refers to the shadow’s @font-family, and it uses the "inner.woff" file.
The .part-styled element also receives its style from the outer document, tho by being directly set rather than by inheritance. Thus, its tree-scoped reference also refer’s to the outer document, and it uses the "outer.woff" file.
< style > @ font-face { font-family : foo ; src : url ( https :// example . com / outer . woff ); } body { font-family : foo ; } </ style > < child-component > < ::shadow > < style > @ font-face { font-family : foo ; src : url ( https :// example . com / inner . woff ); } </ style > < grandchild-component > < ::shadow > < p class = inner-default > I'm inheriting the outer document's "foo" font.</ p > < p class = inner-search > And I can't find a local "foo" font, so I'm searching further up the tree, and find the shadow's "foo" font.</ p > < style > . inner-search { font-family : foo ; } </ style > </ ::shadow > </ grandchild-component > </ ::shadow > </ child-component >
Here, just as in the previous example, .inner-default is inheriting the font-family: foo declared in the outer document, and so it ends up referencing the outer document’s @font-face, and is rendered with the "outer.woff" file.
On the other hand, .inner-search receives its style from a stylesheet in <grandchild-component>
’s shadow tree, so it attempts to find a @font-face defining a foo font in that tree. There is no such @font-face, so it starts walking up the shadow trees, finding an appropriate @font-face in <child-component>
, so it’s rendered with the "inner.woff" file.
If a tree-scoped reference is serialized, it serializes only its value; the associated root is lost.
This implies that `el.style.foo = getComputedStyle(el).foo;` is not necessarily a no-op, like it typically was before shadow trees existed.
For example, given the following document (using the imaginary <::shadow></::shadow> markup to indicate an element’s shadow tree):
< p class = outer > Here's some text in the outer document's "foo" font.< style > @ font-face { font-family : foo ; src : url ( foo . woff ); } body { font-family : foo ; } </ style > < my-component > < ::shadow > < p class = inner-default > I'm inheriting the outer document's font-family.< p class = inner-styled > And I'm explicitly styled to be in the component's "foo" font.< style > @ font-face { font-family : foo ; src : url ( https :// example . com / foo . woff ); } . inner-styled { font-family : foo ; } </ style > < script > const innerDefault= document. querySelector( '.inner-default' ); const innerStyled= document. querySelector( '.inner-styled' ); const defaultFont= getComputedStyle( innerDefault). fontFamily; const styledFont= getComputedStyle( innerStyled). fontFamily; console. log( defaultFont== styledFont); // true! </ script > </ ::shadow > </ my-component >
The .outer
element is styled with the outer document’s "foo" @font-face. The .inner-default
element inherits font-family from the outer document, meaning it inherits a tree-scoped reference referencing that outer document, and so it’s in the same font as .outer
.
Meanwhile, .inner-styled
is explicitly styled from inside the shadow root, so it receives a fresh tree-scoped reference referencing its shadow tree, and it is instead styled the shadow’s own "foo" @font-face.
Despite that, the script running inside the component sees the two elements as having the same value for font-family, because the root-reference part of a tree-scoped reference is not preserved by serialization. If it were to set innerDefault
(thus setting the font-family property of the element’s attribute stylesheet, which lives in the shadow tree), the .inner-default
element would suddenly switch to the same font as .inner-styled
!
Note: The [css-typed-om-1] is expected to reflect the root reference of a tree-scoped reference in its reification rules for values, allowing authors to tell what node tree the reference is taking its values from, and allowing values to be transported across node trees without changing their meaning.
The following significant changes were made since the 3 April 2014 Working Draft.
Defined that tree-scoped names inherit into descendant shadow trees
Renamed ::content to ::slotted.
Define the flattened tree.
Generally reorg and rebase the Shadow DOM section on top of current DOM.
Punt @scope and related things, and ::region and related things, to the next level of the draft.
Define the specificity of :host, :host(), :host-context(), and ::slotted()
Remove the >>> (previously called /deep/) combinator.
Define that tree-abiding pseudos are allowed after ::slotted().
Allow <compound-selector-list> in all the pseudos.
Define a way to create a stylesheet of default element styles for a given element.
Make featureless elements match nothing.
Define in the context of a shadow tree.
This specification introduces Shadow DOM and some shadow-piercing capabilities, but this does not introduce any privacy issues—shadow DOM, as currently specified, is intentionally not a privacy boundary (and the parts of the UA that use shadow DOM and do have a privacy boundary implicitly rely on protections not yet specified, which protect them from the things defined in this specification).
This specification introduces Shadow DOM and some shadow-piercing capabilities, but this does not introduce any security issues—shadow DOM, as currently specified, is intentionally not a security boundary (and the parts of the UA that use shadow DOM and do have a security boundary implicitly rely on protections not yet specified, which protect them from the things defined in this specification).
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:
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 relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.
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.
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.
To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.
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.
Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.
Global names include:
@font-face, referenced by font-family
@font-feature-values, referenced by font-family
@keyframes, referenced by animation-name
@counter-style, referenced by list-style-type
@color-profile, referenced by the color() function
@font-palette-values, referenced by font-palette
others?
In all current engines.
In all current engines.
In all current engines.