Abstract

This is a work in progress! For the latest updates from the Web Applications (WebApps) Working Group possibly including important bug fixes, please look at the draft on GitHub.

This specification describes a method of combining multiple DOM trees into one hierarchy and how these trees interact with each other within a document, thus enabling better composition of the DOM.

Status of This Document

This is a work in progress! This specification is for review and not for implementation! For the latest updates, including important bug fixes, please look at the draft on GitHub instead.

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document was published by the W3C Web Applications (WebApps) as a Working Draft. If you wish to make comments regarding this document, please send them to public-webapps@w3.org (subscribe, archives). All comments are welcome.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

1. Conformance

All diagrams, examples, notes, are non-normative, as well as sections explicitly marked as non-normative. Everything else in this specification is normative.

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 [RFC2119]. For readability, these words do not appear in all uppercase letters in this specification.

To help with layering and to avoid circular dependencies between various parts of specification, this document consists of three consecutive narratives:

  1. setting up the stage for the specification,
  2. explaining of the conceptual model and algorithms behind it, and
  3. expressing this model with DOM interfaces and HTML elements.

In a sense, these parts can be viewed as math, which sets up the reasoning environment, physics, which is the theoretical reasoning about the concept, and mechanics, which is the practical application of this reasoning.

Any point, at which a conforming UA must make decisions about the state or reaction to the state of the conceptual model, is captured as algorithm. The algorithms are defined in terms of processing equivalence. The processing equivalence is a constraint imposed on the algorithm implementors, requiring the output of the both UA-implemented and the specified algorithm to be exactly the same for all inputs.

2. Concepts

2.1 Introduction

This section is non-normative.

See the Shadow DOM section of Introduction to Web Components as a non-normative intruduction.

2.2 Shadow trees

A document tree is a node tree [DOM] whose root node is a document.

Any element can have an associated ordered list of zero or more node trees.

An element hosts a node tree if the node tree is a member of this associated list.

A shadow host is an element that hosts one or more node trees.

A shadow tree is a node tree hosted by a shadow host.

A shadow root is the root node of a shadow tree.

If more than one shadow tree is hosted by the same shadow host, the more recently added shadow tree is called the younger shadow tree and the less recently added shadow tree is called the older shadow tree.

If there is no older shadow tree than a given shadow tree, the shadow tree is called the oldest shadow tree.

If there is no younger shadow tree than a given shadow tree, the shadow tree is called the youngest shadow tree.

The older shadow root is the root node of the older shadow tree.

The younger shadow root is the root node of the younger shadow tree.

The oldest shadow root is the root node of the oldest shadow tree.

The youngest shadow root is the root node of the youngest shadow tree.

Note

For convenience, the shadow root provides its own set of DOM tree accessor methods. No nodes other than the shadow root descendants are accessible with these methods.

2.3 Trees of trees

A tree of trees is a tree of node trees.

Note

The purpose of introducing a tree of trees here is to define algorithms easily in the following sections. This is a kind of a notation techchique to make the this specification simpler.

Just like a node tree is defined as a set of relationships between nodes, a tree of trees is similarly defined as a set of relationships between node trees:

The ownerDocument property of a node in a shadow tree must refers to the document of the shadow host which hosts the shadow tree.

Window object named properties [HTML] must access the nodes in the document tree.

2.3.1 Example tree of trees

This section is non-normative.

Fig. 1 A tree of trees.

In the figure, there are seven node trees, named A, B, C, D, E and F. The node trees, C, D and E, are hosted by the same shadow host, which participates in the node tree A. The node tree C is the oldest shadow tree. The node tree E is the youngest shadow tree. The following set of relationships holds in the figure:

2.4 Composed trees

A composed tree is a node tree which is constructed out of nodes from multiple node trees in a tree of trees. The exact algorithm of constructing a composed tree is specified later.

Fig. 2 A composed tree

In rendering a document tree, or presenting it visually, the composed tree must be used instead of the document tree.

The composed tree must be updated before the rendering occurs.

3. Distributions

3.1 Insertion Points

An insertion point is a defined location where nodes in a different node tree appear instead of the nodes's original position when constructing a composed tree.

Fig. 3 A distribution

A distribution is the mechanism that determines which nodes appear at each insertion point. The exact algorithm of a distribution is specified later.

3.2 Content Insertion Points

A content insertion point is an insertion point to where the child nodes of the shadow host are distributed. The content element that satisfies all of the following conditions represents a content insertion point:

3.3 Shadow Insertion Points

A shadow insertion point is an insertion point to where the children of the older shadow root are distributed. The shadow element that satisfies of the following conditions represents a shadow insertion point:

3.4 Distribution Results

Each tree of trees has the distribution result which describes the result of distributions. The distribution result must be equivalent of the following:

  1. Each insertion point has an ordered list, called distributed nodes, which consists of nodes which are distributed into the insertion point.
  2. Each node that is not an insertion point has an ordered list, called destination insertion points, which consists of insertion points to where the node is distributed

An insertion point A is the final destination of a node B if A is the last item of the destination insertion points of B.

When a node A is distributed into an insertion point B, the following steps must happen:

Note

One case that deserves special consideration is the situation when an insertion point is a child node of another shadow host. In such situations, the nodes distributed into that insertion point appear as if they were child nodes of the shadow host in the context of distribution. Thus, the nodes distributed to a shadow tree could have already been distributed from its parent tree.

Despite being distributed to more than one insertion point, a node still only appears once in the composed tree at the final destination.

Fig. 4 A re-distribution. In the figure, a node child 1 is distributed into insertion point 1. Then child1 is re-distributed into insertion point 3. The destination insertion points of child 1 is [insertion point 1, insertion point 3] and insertion point 3 is the final destination of child 1. The distributed nodes of insertion point 1 and insertion point 3 is [child 1] and [child 1, child 3], respectively.

3.5 Distribution Algorithms

The distribution algorithm must be used to determine the distribution result for a tree of trees and must be equivalent to processing the following steps:

Input
TREE-OF-TREES, a tree of trees
Output
The distribution result of TREE-OF-TREES is updated
  1. Let all distributed nodes and destination insertion points owned by nodes in TREE-OF-TREES be empty
  2. Let ROOT-TREE be the root tree of TREE-OF-TREES
  3. Run the distribution resolution algorithm with ROOT-TREE as input

The distribution resolution algorithm must be used to determine the distribution result for a given node tree and its descendant trees, and must be equivalent to processing the following steps:

Input
NODE-TREE, a node tree
Output
The distribution result is updated for the inclusive descendant trees of NODE-TREE
  1. For each shadow host, SHADOW-HOST, which participates in NODE-TREE, in tree order:
    1. Let POOL be the result of the pool population algorithm with SHADOW-HOST as input
    2. For each shadow tree, SHADOW-TREE, which SHADOW-HOST hosts, in order from the youngest shadow tree to the oldest shadow tree:
      1. Run the pool distribution algorithm with SHADOW-TREE and POOL as input
    3. For each shadow tree, SHADOW-TREE, that SHADOW-HOST hosts, in order from the oldest shadow tree to the youngest shadow tree:
      1. Let SHADOW be the shadow insertion point which participates in SHADOW-TREE
      2. If such a SHADOW exits:
        1. If SHADOW-TREE is not the oldest shadow tree:
          1. Let POOL be the result of the pool population algorithm with the root node of the older shadow tree relative to SHADOW-TREE as input.
        2. For each node, CHILD, in POOL
          1. Distribute CHILD into SHADOW
      3. Run the distribution resolution algorithm, recursively, with SHADOW-TREE as input

The pool population algorithm must be used to populate nodes from the child nodes of a given node and must be equivalent to processing the following steps:

Input
NODE, a node
Output
POOL, an ordered list of nodes
  1. Let POOL be an empty ordered list.
  2. For each child node, CHILD, of NODE:
    1. If CHILD is an insertion point:
      1. Add all nodes in the distributed nodes of CHILD to POOL
    2. Otherwise:
      1. Add CHILD to POOL

The pool distribution algorithm must be used to distribute nodes in a pool into the content insertion points in a shadow tree and must be equivalent to processing the following steps:

Input
SHADOW-TREE, a shadow tree
POOL, an ordered list of nodes
Output
Nodes in POOL are distributed into the content insertion points in the tree.
  1. For each content insertion point, CONTENT, which participates in SHADOW-TREE, in tree order:
    1. For each node, NODE, in POOL
      1. If NODE satisfies CONTENT's matching criteria:
        1. Distribute NODE into CONTENT
        2. Remove NODE from POOL
    2. If no nodes are distributed to CONTENT:
      1. For each child, CHILD, of CONTENT
        1. Distribute CHILD into CONTENT
Note

If no nodes are distributed into a content insertion point CONTENT, the child nodes of CONTENT are distributed into CONTENT as fallback nodes.

If any condition which affects the distribution result changes, the distribution result must be updated before any use of the distribution result.

3.6 Satisfying Matching Criteria

The matching criteria for an insertion point is a set of compound selectors [SELECTORS4]. These compound selectors are restricted to contain only these simple selectors:

A node satisfies a matching criteria only if:

  1. all compound selectors in the set, contain only the simple selectors specified above; and
  2. a node matches at least one compound selectors in the set or the set is empty.

4. Composition

The composed tree children calculation algorithm must be used to determine the child nodes of a node in the composed tree and must be equivalent to processing the following steps:

Input
NODE, a node which participates in a composed tree
Output
CHILDREN, the child nodes of NODE in the composed tree.
  1. Let CHILDREN be an empty ordered list of nodes
  2. If NODE is a shadow host:
    1. Let CHILD-POOL be the children of the youngest shadow root which NODE hosts.
  3. Otherwise:
    1. Let CHILD-POOL be the child nodes of NODE
  4. For each node, CHILD, in CHILD-POOL:
    1. If CHILD is an insertion point:
      1. For each node, DISTRIBUTED-NODE, in the distributed nodes of the insertion point CHILD:
        1. If CHILD is the final destination of DISTRIBUTED-NODE, add DISTRIBUTED-NODE to CHILDREN
    2. Otherwise:
      1. Add CHILD to CHILDREN

For a given tree of trees TREE-OF-TREES, the composed tree constructed from TREE-OF-TREES must be equivalent to the following tree:

5. Events

When an event is dispatched in a shadow tree, its path either crosses the shadow trees or is terminated at the shadow root. One exception are the mutation events. The mutation event types must never be dispatched in a shadow tree.

5.1 Events that are Always Stopped

The following events must always be stopped at the root node of the node tree:

5.2 Event Paths

The event path calculation algorithm must be used to determine event path and must be equivalent to processing the following steps:

Input
NODE, a node
EVENT, an event
Output
PATH, an event path, a ordered list of an event target
  1. Let PATH be the empty ordered list of nodes
  2. Let CURRENT be NODE
  3. Add CURRENT to PATH
  4. Repeat while CURRENT exists:
    1. If the destination insertion points of CURRENT is not empty:
      1. For each insertion point, INSERTION-POINT, in the destination insertion points of CURRENT:
        1. If INSERTION-POINT is a shadow insertion point:
          1. Let SHADOW-ROOT be the root node of INSERTION-POINT
          2. If SHADOW-ROOT is not the oldest shadow root:
            1. Add the older shadow root relative to SHADOW-ROOT to PATH
        2. Add INSERTION-POINT to PATH
      2. Let CURRENT be the final destination of CURRENT
    2. Otherwise:
      1. If CURRENT is a shadow root:
        1. If NODE and CURRENT are in the same node tree and EVENT is one of the events which must be stopped:
          1. Stop this algorithm
        2. Let CURRENT be the shadow host which hosts CURRENT
        3. Add CURRENT to PATH
      2. Otherwise:
        1. Let CURRENT be the parent node of CURRENT
        2. If CURRENT exists:
          1. Add CURRENT to PATH

5.3 Event Paths Example

This section is non-normative.

Suppose we have the following tree of trees:

Fig. 5 An example tree of trees. Nodes which are not involved in the example event path, which is explained later, are omitted.

This tree of trees has the following seven node trees, one document tree and six shadow trees:

Let's assume that the distribution result of this tree of trees is:

In this case, if an event is dispatched on node D, the event path will be:

[D, C, I, M, L, P, R, Q, O, N, K, J, H, G, U, T, S, F, E, X, W, V, B, A]

Note that the event path calculation algorithm is designed to achieve the following goals:

  1. If there is a node, CHILD, in the event path and CHILD has a parent node, PARENT, in the node tree, the event path also includes PARENT. PARENT always appears somewhere after CHILD in the event path.
  2. Nodes in the event path form a linear ancestor chain in each node tree. There are no branch points in each node tree.
>
Fig. 6 The relationship between an event path and node trees. In the figure, a number shown in a left-side of each node represents a zero-based position of each node in the event path. A parent node always have a larger number than that of its child node in each node tree.

That means if we focus on one node tree and forget all other node trees, the event path would be seen as if the event happened only on the node tree which we are focused on. This is an important aspect in a sense that hosting shadow trees doesn't have any effect to the event path within the node tree the shadow host participate in as long as the event is not stopped somewhere in the descendant trees.

For example, from the view of the document tree 1, the event path would be seen as [D, C, B, A]. From the view of the shadow tree 2, the event path would be seen as [I, H, G, F, E]. The similar things also apply to other node trees.

It is also worth pointing out that if we exclude all insertion points and shadow roots from an event path, the result would be equivalent to the inclusive ancestors of the node on which the event is dispatched, in the composed tree.

Fig. 7 The relationship between an event path and the composed tree. The event path used in the example is shown in the left-hand side and the composed tree is shown in the right-hand side. If we exclude all insertion points and shadow roots from the event path, the result would be equivalent to the inclusive ancestors of the node, D, in the composed tree.

5.4 Event Retargeting

In the cases where event path is across multiple node trees, the event's information about the target of the event is adjusted in order to maintain encapsulation. Event retargeting is a process of computing relative targets for each ancestor of the node at which the event is dispatched. A relative target is a node that most accurately represents the target of a dispatched event at a given ancestor while maintaining the encapsulation.

The retargeting algorithm is used to determine relative targets, and it must be equivalent to processing the following steps:

Input
EVENT-PATH, an event path
CURRENT-TARGET, a node where the event listener is invoked.
Output
RELATIVE-TARGET, adjusted target
  1. Let CURRENT-TARGET-TREE be the node tree which CURRENT-TARGET participates in
  2. Let ORIGINAL-TARGET be the first item in EVENT-PATH
  3. Let ORIGINAL-TARGET-TREE be the node tree which ORIGINAL-TARGET participates in
  4. Let RELATIVE-TARGET-TREE be the lowest common inclusive ancestor tree of CURRENT-TARGET-TREE and ORIGINAL-TARGET-TREE
  5. Let RELATIVE-TARGET be the first node in EVENT-PATH which satisfies the following condition:
    1. The node participates in RELATIVE-TARGET-TREE

The retargeting process must occur prior to dispatch of an event.

5.5 Retargeting relatedTarget

Some events have a relatedTarget [DOM-Level-3-Events] property, which holds a node that's not the event's target, but is related to the event.

For instance, a mouseover event's relatedTarget may hold the node from which the mouse has moved to event's target. In the case where relatedTarget is in a shadow tree, the conforming UAs must not leak its actual value outside of this tree. In cases where both relatedTarget and target are part of the same shadow tree, the conforming UAs must stop events at the shadow root to avoid the appearance of spurious mouseover and mouseout events firing from the same node.

Thus, if an event has a relatedTarget, its value and extent of event dispatch must be adjusted. In general:

  1. For a given node, the relatedTarget must be changed to its ancestor (or self) that is in the same shadow tree as the node
  2. Event listeners must not be invoked on a node for which the target and relatedTarget are the same.

The related target resolution algorithm must be used to determine the value of the relatedTarget property and must be equivalent to processing the following steps:

Input
EVENT, an event
CURRENT-TARGET, the node on which event listeners would be invoked
RELATED-TARGET, the related target for the event
Output
ADJUSTED-RELATED-TARGET, the adjusted related target for CURRENT-TARGET
  1. Let CURRENT-TARGET-TREE be the node tree which CURRENT-TARGET participates in
  2. Let RELATED-TARGET-TREE be the node tree which RELATED-TARGET participates in
  3. Let RELATED-TARGET-EVENT-PATH be the result of the event path calculation algorithm with RELATED-TARGET and EVENT as input
  4. If CURRENT-TARGET-TREE and RELATED-TARGET-TREE participate in the same tree of trees:
    1. Let LOWEST-COMMON-ANCESTOR-TREE be the lowest common inclusive ancestor tree of CURRENT-TARGET-TREE and RELATED-TARGET-TREE
  5. Otherwise:
    1. Let LOWEST-COMMON-ANCESTOR-TREE be the root tree of RELATED-TARGET-TREE
  6. For each inclusive ancestor tree, COMMON-ANCESTOR-TREE, of the LOWEST-COMMON-ANCESTOR-TREE, in ascending order:
    1. Let ADJUSTED-RELATED-TARGET be the first node in RELATED-TARGET-EVENT-PATH which satisfies the following condition:
      1. The node participates in COMMON-ANCESTOR-TREE
    2. If such a ADJUSTED-RELATED-TARGET exists:
      1. Stops this algorithm
Note

The result of the related target resolution algorithm is not always null. Unless that, you should file a bug for this specification.

The relatedTarget retargeting process must occur prior to dispatch of an event.

5.6 Retargeting Touch Events

The Touch target [TOUCH-EVENTS] attribute must be adjusted in the same way as an event with a relatedTarget. Each Touch target in the TouchList returned from TouchEvent touches(), changedTouches() and targetTouches() must be the result of related target resolution algorithm, given NODE and Touch target as arguments.

5.7 Retargeting Focus Events

The focus, DOMFocusIn, blur, and DOMFocusOut events must be treated in the same way as events with a relatedTarget, where the corresponding node that is losing focus as a result of target gaining focus or the node that is gaining focus, and thus causing the blurring of target acts as the related target.

5.8 Event Dispatch

At the time of event dispatch:

Upon completion of the event dispatch, the Event object's target and currentTarget must be to the highest ancestor's relative target. Since it is possible for a script to hold on to the Event object past the scope of event dispatch, this step is necessary to avoid revealing the nodes in shadow trees.

5.9 Event Retargeting Example

This section is non-normative.

Suppose we have a user interface for a media controller, represented by this tree, composed of both document tree and the shadow trees. In this example, we will assume that selectors are allowed to cross the shadow boundaries and we will use these selectors to identify the elements. Also, we will invent a fictional shadow-root element to demarcate the shadow boundaries and represent shadow roots:

Example 1
<div id="player">
    <shadow-root id="player-shadow-root">
        <div id="controls">
            <button id="play-button">PLAY</button>
            <input type="range" id="timeline">
                <shadow-root id="timeline-shadow-root">
                    <div id="slider-thumb" id="timeline-slider-thumb"></div>
                </shadow-root>
            </input>
            <div id="volume-slider-container">
                <input type="range" id="volume-slider">
                    <shadow-root id="volume-shadow-root">
                        <div id="slider-thumb" id="volume-slider-thumb"></div>
                    </shadow-root>
                </input>
            </div>
        </div>
    </shadow-root>
</div>

Let's have a user position their pointing device over the volume slider's thumb (#volume-slider-thumb), thus triggering a mouseover event on that node. For this event, let's pretend it has no associated relatedTarget.

Per the retargeting algorithm, we should have the following set of ancestors and relative targets:

Ancestor Relative Target
#player #player
#player-shadow-root #volume-slider
#controls #volume-slider
#volume-slider-container #volume-slider
#volume-slider #volume-slider
#volume-shadow-root #volume-slider-thumb
#volume-slider-thumb #volume-slider-thumb

After we dispatch the mouseover event using these newly computed relative targets, the user decides to move their pointing device over the thumb of the timeline (#timeline-slider-thumb). This triggers both a mouseout event for the volume slider thumb and the mouseover event for the timeline thumb.

Let's see how the relatedTarget value of the volume thumb's mouseout event is affected. For this event, the relatedTarget is the timeline thumb (#timeline-slider-thumb). Per the related target resolution algorithm, we should have the following set of ancestors and adjusted related targets:

Ancestor Relative Target Adjusted related Target
#player #player #player
#player-shadow-root #volume-slider #timeline
#controls #volume-slider #timeline
#volume-slider-container #volume-slider #timeline
#volume-slider #volume-slider #timeline
#volume-shadow-root #volume-slider-thumb #timeline
#volume-slider-thumb #volume-slider-thumb #timeline

The node, #player, has both target and relatedTarget being the same value (#player), which means that we do not dispatch the event on this node and its ancestors.

6. User Interaction

6.1 Ranges and Selections

This section is non-normative.

Selection [EDITING] is not defined. Implementation should do their best to do what's best for them. Here's one possible, admittedly naive way:

Since nodes which are in the different node trees never have the same root, there may never exist a valid DOM range that spans multiple node trees.

Accordingly, selections may only exist within one node tree, because they are defined by a single range. The selection, returned by the window.getSelection() method never returns a selection within a shadow tree.

The getSelection() method of the shadow root object returns the current selection in this shadow tree.

6.2 Focus Navigation

If a node doesn’t participate in the composed tree, the node must be skipped from the navigation order [CSS3UI] sequence.

For sequential focus navigation, the navigation order sequence for a given shadow tree A must be inserted into the navigation order for other node tree as follow:

  1. If A is the youngest shadow tree:
    1. Let HOST be the shadow host which hosts A
    2. Let B be the node tree which HOST participates in
    3. The navigation order for A must be inserted into the navigation order for B:
      1. immediately after HOST, if HOST is focusable; or
      2. in place of the HOST as if HOST were assigned the value of auto for determining its position.
  2. Otherwise:
    1. Let B be the younger shadow tree relative to A
    2. Let SHADOW be the shadow insertion point in B
    3. If SHADOW exists, the navigation order for A must be inserted into the navigation order for B immediately after SHADOW as if SHADOW were assigned the value of auto for determining its position.

For directional focus navigation, it is up to the user agent to integrate the navigation orders for shadow trees into the document navigation order.

6.3 Active Element

To maintain encapsulation, the value of the Document object's focus API property activeElement must be adjusted. To prevent loss of information when adjusting this value, each shadow root must also have an activeElement property to store the value of the focused element in the shadow tree.

The active element adjustment algorithm is used to determine the value of the activeElement property, and it must be equivalent to processing the following steps:

Input
ELEMENT, the focused element
ROOT, either a document or a shadow root
Output
ADJUSTED, an adjusted activeElement property of ROOT.
  1. Let PATH be the result of the event path calculation algorithm with ELEMENT and null as input
  2. Let ADJUSTED be the result of the retargeting algorithm with PATH and ROOT as input

6.4 Editing

The value of the contenteditable attribute must not propagate from shadow host to its shadow trees.

6.5 Assistive Technology

User agents with assistive technology traverse the composed tree, and thus enable full use of WAI-ARIA [WAI-ARIA] semantics in the shadow trees.

7. HTML Elements in Shadow Trees

Comparatively, a shadow tree can be seen as somewhere between just part of a document and itself being a document fragment. Since it is rendered, a shadow tree aims to retain the traits of a typical tree in a document. At the same time, it is an encapsulation abstraction, so it has to avoid affecting the document tree. Thus, the HTML elements must behave as specified [HTML] in the shadow trees, with a few exceptions.

7.1 Inert HTML Elements

A subset of HTML elements must behave as inert, or not part of the document tree. This is consistent how the HTML elements would behave in a document fragment. These elements are:

All other HTML elements in the shadow trees must behave as if they were part of the document tree.

8. HTML Elements and Their Shadow Trees

Per specification, some HTML elements are designed to either not render their contents or have special requirements in regard to contents rendering. In order to reconcile these differences in rendering behavior with the composed tree, all HTML elements must have an equivalent of a shadow tree that is created and populated at the time of element instantiation. It is up to a user agent to define the content of these trees. However, all conforming user agents must satisfy the following requirements:

HTML Element Shadow Tree Requirements
img, iframe, embed, object, video, audio, canvas, map, input, textarea, progress, meter If the element can have fallback content, contains one content insertion point. The matching criteria value is the universal selector only when the element needs to show fallback content. Otherwise, contains no content insertion points or an content insertion point that matches nothing.
fieldset Contains two content insertion points with the following matching criteria:
  1. legend:first-of-type
  2. universal selector
details Contains two content insertion points with the following matching criteria:
  1. summary:first-of-type
  2. universal selector
All other elements Contains one content insertion point with the universal selector as the matching criteria

9. Elements and DOM Objects

9.1 ShadowRoot Object

The ShadowRoot object represents the shadow root.

interface ShadowRoot : DocumentFragment {
    HTMLElement getElementById (DOMString elementId);
    NodeList    getElementsByClassName (DOMString className);
    NodeList    getElementsByTagName (DOMString tagName);
    NodeList    getElementsByTagNameNS (DOMString? namespace, DOMString localName);
    Selection?  getSelection ();
    Element?    elementFromPoint (double x, double y);
    readonly    attribute Element?       activeElement;
    readonly    attribute Element        host;
    readonly    attribute ShadowRoot?    olderShadowRoot;
                attribute DOMString      innerHTML;
    readonly    attribute StyleSheetList styleSheets;
};

9.1.1 Attributes

activeElement of type Element, readonly , nullable

Represents the currently focused element in the shadow tree.

On getting, the attribute must return the currently focused element in the shadow tree or null, if there is none.

host of type Element, readonly

Represents the shadow host which hosts the context object.

On getting, the attribute must return the shadow host which hosts the context object.

innerHTML of type DOMString,

Represents the markup of ShadowRoot's contents.

On getting, the attribute must return the result of running the HTML fragment serialization algorithm with the context object as shadow host.

On setting, these steps must be run:

  1. Let FRAGMENT be the result of invoking the fragment parsing algorithm [DOMPARSING] with the new value as MARKUP, and the context object as shadow host
  2. Replace all with FRAGMENT within the shadow root.
olderShadowRoot of type ShadowRoot, readonly , nullable

Represents the older shadow root relative to the context object

On getting, the attribute must return a result that is equivalent to running the following steps:

  1. If the context object is the oldest shadow root, return null.
  2. Return the older shadow root relative to the context object.

For HTML elements, the UA-provided shadow trees must not be accessible.

styleSheets of type StyleSheetList, readonly

Represents the shadow root style sheets.

On getting, the attribute must return a StyleSheetList sequence containing the shadow root style sheets.

9.1.2 Methods

elementFromPoint

Returns an element at specified coordinates.

Note

Eventually, this needs to be part of CSSOM View Module specification [CSSOM-VIEW]

When invoked, it must return result of running the following steps:

  1. If context object is not a ShadowRoot instance, throw an InvalidNodeTypeError.
  2. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll bar (if any), or if y is greater than the viewport height excluding the size of a rendered scroll bar (if any), return null.
  3. Let HIT be the element at coordinates x and y in the viewport, determined through hit testing
  4. Let PATH be the result of running the event path calculation algorithm with HIT and null as input
  5. Return the result of running the retargeting algorithm with PATH and context object as input
ParameterTypeNullableOptionalDescription
xdouble
ydouble
Return type: Element, nullable
getElementById
Must behave exactly like document.getElementById, except scoped to the shadow tree.
ParameterTypeNullableOptionalDescription
elementIdDOMString
Return type: HTMLElement
getElementsByClassName
Must behave exactly like document.getElementsByClassName, except scoped to the shadow tree.
ParameterTypeNullableOptionalDescription
classNameDOMString
Return type: NodeList
getElementsByTagName
Must behave exactly like document.getElementsByTagName, except scoped to the shadow tree.
ParameterTypeNullableOptionalDescription
tagNameDOMString
Return type: NodeList
getElementsByTagNameNS
Must behave exactly like document.getElementsByTagNameNS, except scoped to the shadow tree.
ParameterTypeNullableOptionalDescription
namespaceDOMString
localNameDOMString
Return type: NodeList
getSelection

Returns the current selection in the shadow tree.

When invoked, it must return the selection in the shadow tree.

No parameters.
Return type: Selection, nullable

The nodeType attribute of a ShadowRoot instance must return DOCUMENT_FRAGMENT_NODE. Accordingly, the nodeName attribute of a ShadowRoot instance must return "#document-fragment".

Invoking the cloneNode() method on a ShadowRoot instance must always throw a DATA_CLONE_ERR exception.

9.2 Extensions to Element Interface

partial interface Element {
    ShadowRoot createShadowRoot ();
    NodeList   getDestinationInsertionPoints ();
    readonly    attribute ShadowRoot? shadowRoot;
};

9.2.1 Attributes

shadowRoot of type ShadowRoot, readonly , nullable

Represents the youngest shadow root that context object hosts.

On getting, the attribute must return the youngest shadow root that context object hosts, or null if no such shadow root is accessible.

For HTML elements, the UA-provided shadow trees must not be accessible.

9.2.2 Methods

createShadowRoot
When invoked, these steps must be run:
  1. Create a new instance of the ShadowRoot object
  2. Add the ShadowRoot object to the ordered list of shadow roots associated with the context object as the youngest shadow root
  3. Return ShadowRoot object.
No parameters.
Return type: ShadowRoot
getDestinationInsertionPoints
When invoked, the method must return a static NodeList consisting of insertion points in the destination insertion points of the context object.
No parameters.
Return type: NodeList

9.3 The content element

The content element represents an insertion point in the shadow tree.

If a content element does not satisfy the condition of an insertion point, it must have the same rendering behavior as the HTMLUnknownElement.

Context
Where flow content is expected.
Content model
Transparent
Children
Anything as fallback content
Content attributes
Global attributes
select, a set of comma-separated tokens
Represents the matching criteria for distributing child nodes of the shadow host. Each token must be a compound selector.
DOM Interface
interface HTMLContentElement : HTMLElement {
                attribute DOMString select;
    NodeList getDistributedNodes ();
};

Attributes

select of type DOMString,
Must reflect the select attribute.

Methods

getDistributedNodes
When invoked, it must return result of running the following steps:
  1. If the context object is a content insertion point:
    1. Return a static NodeList consisting of nodes in the distributed nodes of the context object.
  2. Otherwise:
    1. Return an empty static NodeList.
No parameters.
Return type: NodeList

9.4 The shadow element

The shadow element represents an shadow insertion point in a shadow tree.

If a shadow element does not satisfy the condition of an insertion point, it must have the same rendering behavior as the HTMLUnknownElement.

Context
Where flow content is expected.
Content model
Transparent
Children
Anything
DOM Interface
interface HTMLShadowElement : HTMLElement {
    NodeList getDistributedNodes ();
};

Methods

getDistributedNodes
When invoked, it must return result of running the following steps:
  1. If the context object is a shadow insertion point:
    1. Return a static NodeList consisting of nodes in the distributed nodes of the context object.
  2. Otherwise:
    1. Return an empty static NodeList.
No parameters.
Return type: NodeList

9.5 Extensions to Event Interface

partial interface Event {
    readonly    attribute object path;
};

9.5.1 Attributes

path of type object, readonly

Represents the event path.

On getting, the attribute must create and return a new JavaScript Array object, that is a copy of the event path for the context object.

Issue 1

Use Array as the return type of the path attribute in WebIDL. WebIDL bugs: Array subclassing and class, not interface.

10. Shadow DOM Example

Bob was asked to turn a simple list of links into a News Widget, which has links organized into two categories: breaking news and just news. The current document markup for the stories looks like this:

Example 2
<ul class="stories">
    <li><a href="//example.com/stories/1">A story</a></li>
    <li><a href="//example.com/stories/2">Another story</a></li>
    <li class="breaking"><a href="//example.com/stories/3">Also a story</a></li>
    <li><a href="//example.com/stories/4">Yet another story</a></li>
    <li><a href="//example.com/stories/4">Awesome story</a></li>
    <li class="breaking"><a href="//example.com/stories/5">Horrible story</a></li>
</ul>

To organize the stories, Bob decides to use shadow DOM. Doing so will allow Bob to keep the document markup uncluttered, and harnessing the power of insertion point makes sorting stories by class name a very simple task. After getting another cup of Green Eye, he quickly mocks up the following shadow tree, to be hosted by the ul element:

Example 3
<div class="breaking">
    <ul>
        <content select=".breaking"></content> <!-- insertion point for breaking news -->
    </ul>
</div>
<div class="other">
    <ul>
        <content></content> <!-- insertion point for the rest of the news -->
    </ul>
</div>

Bob then styles the newborn widget according to comps from the designer by adding this to the shadow tree mockup:

Example 4
<style>
    div.breaking {
        color: Red;
        font-size: 20px;
        border: 1px dashed Purple;
    }
    div.other {
        padding: 2px 0 0 0;
        border: 1px solid Cyan;
    }
</style>

While pondering if his company should start looking for a new designer, Bob converts the mockup to code:

Example 5
function createStoryGroup(className, contentSelector)
{
    var group = document.createElement('div');
    group.className = className;
    // Empty string in select attribute or absence thereof work the same, so no need for special handling.
    group.innerHTML = '<ul><content select="' + contentSelector + '"></content></ul>';
    return group;
}

function createStyle()
{
    var style = document.createElement('style');
    style.textContent = 'div.breaking { color: Red;font-size: 20px; border: 1px dashed Purple; }' +
        'div.other { padding: 2px 0 0 0; border: 1px solid Cyan; }';
    return style;
}

function makeShadowTree(storyList)
{
    var root = storyList.createShadowRoot();
    root.appendChild(createStyle());
    root.appendChild(createStoryGroup('breaking', '.breaking'));
    root.appendChild(createStoryGroup('other', ''));
}

document.addEventListener('DOMContentLoaded', function() {
    [].forEach.call(document.querySelectorAll('ul.stories'), makeShadowTree);
});

Well done, Bob! With the cup of coffee still half-full, the work is complete. Recognizing his awesomeness, Bob returns to teaching n00bs the ways of WoW.

A few months pass.

It's election time. With Bob at his annual conference, Alice is charged with adding another, temporary box to the news widget, filled with election-related stories. Alice studies Bob's code, reads up on the shadow DOM spec and realizes that, thanks to multiple shadow tree support, she doesn't have to touch his code. As usual, her solution is elegant and simple, fitting neatly right under Bob's code:

Example 6
// TODO(alice): BEGIN -- DELETE THIS CODE AFTER ELECTIONS ARE OVER.
var ELECTION_BOX_REMOVAL_DEADLINE = ...;

function createElectionStyle()
{
    var style = document.createElement('style');
    // TODO(alice): Check designer's desk for hallucinogens.
    style.textContent = 'div.election { color: Magenta; font-size: 24px; border: 2px dotted Fuchsia; }';
    return style;
}

function makeElectionShadowTree(storyList)
{
    var root = storyList.createShadowRoot();
    // Add and style election story box.
    root.appendChild(createElectionStyle());
    root.appendChild(createStoryGroup('election', '.election'));
    // Insert Bob's shadow tree under the election story box.
    root.appendChild(document.createElement('shadow'));
}

if (Date.now() < ELECTION_BOX_REMOVAL_DEADLINE) {
    document.addEventListener('DOMContentLoaded', function() {
        [].forEach.call(document.querySelectorAll('ul.stories'), makeElectionShadowTree);
    });
}
// TODO(alice): END -- DELETE THIS CODE AFTER ELECTIONS ARE OVER.

Using the shadow element allows Alice to compose Bob's widget inside of hers—without having to change a line of production code. Smiling to herself, Alice realizes that Bob may have come up with a way to keep the document markup clean, but she is the one who takes the cake for using shadow tree composition in such a cool way.

A. Acknowledgements

David Hyatt developed XBL 1.0, and Ian Hickson co-wrote XBL 2.0. These documents provided tremendous insight into the problem of functional encapsulation and greatly influenced this specification.

Alex Russell and his considerable forethought triggered a new wave of enthusiasm around the subject of shadow DOM and how it can be applied practically on the Web.

Dominic Cooney, Hajime Morrita, and Roland Steiner worked tirelessly to scope the problem of functional encapsulation within the confines of the Web platform and provided a solid foundation for this document.

The editor would also like to thank Alex Komoroske, Anne van Kesteren, Brandon Payton, Brian Kardell, Darin Fisher, Eric Bidelman, Deepak Sherveghar, Edward O'Connor, Elisée Maurer, Elliott Sprehn, Erik Arvidsson, Glenn Adams, Jonas Sicking, Malte Ubl, Mike Taylor, Oliver Nightingale, Olli Pettay, Rafael Weinstein, Richard Bradshaw, Ruud Steltenpool, Sam Dutton, Sergey G. Grekhov, Shinya Kawanaka, Tab Atkins, Takashi Sakamoto, and Yoshinori Sano for their comments and contributions to this specification.

This list is too short. There's a lot of work left to do. Please contribute by reviewing and filing bugs—and don't forget to ask the editor to add your name into this section.

B. References

B.1 Normative references

[CSS3UI]
Tantek Çelik. CSS3 Basic User Interface Module. 17 January 2012. W3C Working Draft. URL: http://www.w3.org/TR/css3-ui/
[CSSOM-VIEW]
Simon Pieters; Glenn Adams. CSSOM View Module. 17 December 2013. W3C Working Draft. URL: http://www.w3.org/TR/cssom-view/
[DOM]
Anne van Kesteren; Aryeh Gregor; Ms2ger. DOM Standard. Living Standard. URL: http://dom.spec.whatwg.org
[DOM-Level-3-Events]
Gary Kacmarcik; Travis Leithead; Jacob Rossi; Doug Schepers; Björn Höhrmann; Philippe Le Hégaret; Tom Pixley. Document Object Model (DOM) Level 3 Events Specification. 5 November 2013. W3C Working Draft. URL: http://www.w3.org/TR/DOM-Level-3-Events/
[DOMPARSING]
Ms2ger. DOM Parsing and Serialization. URL: http://domparsing.spec.whatwg.org/
[EDITING]
Aryeh Gregor. HTML Editing APIs. URL: https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html
[HTML]
Ian Hickson. HTML. Living Standard. URL: http://www.whatwg.org/specs/web-apps/current-work/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Internet RFC 2119. URL: http://www.ietf.org/rfc/rfc2119.txt
[SELECTORS4]
Elika Etemad; Tab Atkins Jr. Selectors Level 4. W3C Editor's Draft. URL: http://dev.w3.org/csswg/selectors4/
[TOUCH-EVENTS]
Doug Schepers; Sangwhan Moon; Matt Brubeck; Arthur Barstow. Touch Events. 10 October 2013. W3C Recommendation. URL: http://www.w3.org/TR/touch-events/
[WAI-ARIA]
James Craig; Michael Cooper et al. Accessible Rich Internet Applications (WAI-ARIA) 1.0. 20 March 2014. W3C Recommendation. URL: http://www.w3.org/TR/wai-aria/