tree: 84f67abfa7be0fad4f5c80cf798d8fccc957ecd0 [path history] [tgz]
  1. events/
  2. ng/
  3. trustedtypes/
  4. AccessibleNode.cpp
  5. AccessibleNode.h
  6. AccessibleNode.idl
  7. AccessibleNodeList.cpp
  8. AccessibleNodeList.h
  9. AccessibleNodeList.idl
  10. AnimationWorkletProxyClient.cpp
  11. AnimationWorkletProxyClient.h
  12. Attr.cpp
  13. Attr.h
  14. Attr.idl
  15. Attribute.h
  16. AttributeCollection.h
  17. AttrTest.cpp
  18. AXObjectCache.cpp
  19. AXObjectCache.h
  20. AXObjectCacheBase.cpp
  21. AXObjectCacheBase.h
  23. CDATASection.cpp
  24. CDATASection.h
  25. CDATASection.idl
  26. CharacterData.cpp
  27. CharacterData.h
  28. CharacterData.idl
  29. ChildFrameDisconnector.cpp
  30. ChildFrameDisconnector.h
  31. ChildListMutationScope.cpp
  32. ChildListMutationScope.h
  33. ChildNode.h
  34. ChildNode.idl
  35. ChildNodeList.cpp
  36. ChildNodeList.h
  37. ClassCollection.cpp
  38. ClassCollection.h
  39. CollectionIndexCache.h
  40. Comment.cpp
  41. Comment.h
  42. Comment.idl
  43. CommonDefinitions.idl
  44. ComputedAccessibleNode.cpp
  45. ComputedAccessibleNode.h
  46. ComputedAccessibleNode.idl
  47. ContainerNode.cpp
  48. ContainerNode.h
  49. ContextFeatures.cpp
  50. ContextFeatures.h
  51. ContextFeaturesClientImpl.cpp
  52. ContextFeaturesClientImpl.h
  53. ContextLifecycleNotifier.cpp
  54. ContextLifecycleNotifier.h
  55. ContextLifecycleObserver.cpp
  56. ContextLifecycleObserver.h
  57. DatasetDOMStringMap.cpp
  58. DatasetDOMStringMap.h
  59. DecodedDataDocumentParser.cpp
  60. DecodedDataDocumentParser.h
  61. DistributedNodes.cpp
  62. DistributedNodes.h
  63. Document.cpp
  64. Document.h
  65. Document.idl
  66. DocumentEncodingData.cpp
  67. DocumentEncodingData.h
  68. DocumentFragment.cpp
  69. DocumentFragment.h
  70. DocumentFragment.idl
  71. DocumentInit.cpp
  72. DocumentInit.h
  73. DocumentLifecycle.cpp
  74. DocumentLifecycle.h
  75. DocumentOrShadowRoot.h
  76. DocumentOrShadowRoot.idl
  77. DocumentParser.cpp
  78. DocumentParser.h
  79. DocumentParserClient.h
  80. DocumentParserTiming.cpp
  81. DocumentParserTiming.h
  82. DocumentShutdownNotifier.cpp
  83. DocumentShutdownNotifier.h
  84. DocumentShutdownObserver.cpp
  85. DocumentShutdownObserver.h
  86. DocumentStatisticsCollector.cpp
  87. DocumentStatisticsCollector.h
  88. DocumentStatisticsCollectorTest.cpp
  89. DocumentTest.cpp
  90. DocumentTiming.cpp
  91. DocumentTiming.h
  92. DocumentType.cpp
  93. DocumentType.h
  94. DocumentType.idl
  95. DOMException.cpp
  96. DOMException.h
  97. DOMException.idl
  98. DOMHighResTimeStamp.h
  99. DOMImplementation.cpp
  100. DOMImplementation.h
  101. DOMImplementation.idl
  102. DOMImplementationTest.cpp
  103. DOMNodeIds.cpp
  104. DOMNodeIds.h
  105. DOMStringList.cpp
  106. DOMStringList.h
  107. DOMStringList.idl
  108. DOMStringMap.cpp
  109. DOMStringMap.h
  110. DOMStringMap.idl
  111. DOMTimeStamp.h
  112. DOMTokenList.cpp
  113. DOMTokenList.h
  114. DOMTokenList.idl
  115. Element.cpp
  116. Element.h
  117. Element.idl
  118. ElementCreationOptions.idl
  119. ElementData.cpp
  120. ElementData.h
  121. ElementDataCache.cpp
  122. ElementDataCache.h
  123. ElementDefinitionOptions.idl
  124. ElementRareData.cpp
  125. ElementRareData.h
  126. ElementRegistrationOptions.idl
  127. ElementShadow.cpp
  128. ElementShadow.h
  129. ElementShadowV0.cpp
  130. ElementShadowV0.h
  131. ElementTest.cpp
  132. ElementTraversal.h
  133. ElementVisibilityObserver.cpp
  134. ElementVisibilityObserver.h
  135. ElementVisibilityObserverTest.cpp
  136. EmptyNodeList.cpp
  137. EmptyNodeList.h
  138. ExceptionCode.h
  139. ExecutionContext.cpp
  140. ExecutionContext.h
  141. FirstLetterPseudoElement.cpp
  142. FirstLetterPseudoElement.h
  143. FirstLetterPseudoElementTest.cpp
  144. FlatTreeTraversal.cpp
  145. FlatTreeTraversal.h
  146. FlatTreeTraversalTest.cpp
  147. FrameRequestCallback.idl
  148. FrameRequestCallbackCollection.cpp
  149. FrameRequestCallbackCollection.h
  150. FunctionStringCallback.idl
  151. GetRootNodeOptions.idl
  152. GlobalEventHandlers.h
  153. GlobalEventHandlers.idl
  154. IconURL.cpp
  155. IconURL.h
  156. IdleDeadline.cpp
  157. IdleDeadline.h
  158. IdleDeadline.idl
  159. IdleDeadlineTest.cpp
  160. IdleRequestCallback.idl
  161. IdleRequestOptions.idl
  162. IdTargetObserver.cpp
  163. IdTargetObserver.h
  164. IdTargetObserverRegistry.cpp
  165. IdTargetObserverRegistry.h
  166. IncrementLoadEventDelayCount.cpp
  167. IncrementLoadEventDelayCount.h
  168. Iterator.h
  169. Iterator.idl
  170. LayoutTreeBuilder.cpp
  171. LayoutTreeBuilder.h
  172. LayoutTreeBuilderTraversal.cpp
  173. LayoutTreeBuilderTraversal.h
  174. LayoutTreeBuilderTraversalTest.cpp
  175. LiveNodeList.cpp
  176. LiveNodeList.h
  177. LiveNodeListBase.cpp
  178. LiveNodeListBase.h
  179. LiveNodeListRegistry.cpp
  180. LiveNodeListRegistry.h
  181. LiveNodeListRegistryTest.cpp
  182. MutationObserver.cpp
  183. MutationObserver.h
  184. MutationObserver.idl
  185. MutationObserverInit.idl
  186. MutationObserverInterestGroup.cpp
  187. MutationObserverInterestGroup.h
  188. MutationObserverRegistration.cpp
  189. MutationObserverRegistration.h
  190. MutationObserverTest.cpp
  191. MutationRecord.cpp
  192. MutationRecord.h
  193. MutationRecord.idl
  194. NamedNodeMap.cpp
  195. NamedNodeMap.h
  196. NamedNodeMap.idl
  197. NameNodeList.cpp
  198. NameNodeList.h
  199. Node.cpp
  200. Node.h
  201. Node.idl
  202. NodeChildRemovalTracker.cpp
  203. NodeChildRemovalTracker.h
  204. NodeComputedStyle.h
  205. NodeFilter.h
  206. NodeFilter.idl
  207. NodeIterator.cpp
  208. NodeIterator.h
  209. NodeIterator.idl
  210. NodeIteratorBase.cpp
  211. NodeIteratorBase.h
  212. NodeList.h
  213. NodeList.idl
  214. NodeListsNodeData.cpp
  215. NodeListsNodeData.h
  216. NodeRareData.cpp
  217. NodeRareData.h
  218. NodeTest.cpp
  219. NodeTraversal.cpp
  220. NodeTraversal.h
  221. NodeTraversalStrategy.h
  222. NodeWithIndex.h
  223. NoncedElement.idl
  224. NonDocumentTypeChildNode.h
  225. NonDocumentTypeChildNode.idl
  226. NonElementParentNode.h
  227. NonElementParentNode.idl
  228. NthIndexCache.cpp
  229. NthIndexCache.h
  230. NthIndexCacheTest.cpp
  231. OWNERS
  232. ParentNode.h
  233. ParentNode.idl
  234. ParserContentPolicy.h
  235. PausableObject.cpp
  236. PausableObject.h
  237. PausableObjectTest.cpp
  238. PresentationAttributeStyle.cpp
  239. PresentationAttributeStyle.h
  240. ProcessingInstruction.cpp
  241. ProcessingInstruction.h
  242. ProcessingInstruction.idl
  243. PseudoElement.cpp
  244. PseudoElement.h
  245. PseudoElementData.h
  246. QualifiedName.cpp
  247. QualifiedName.h
  248. Range.cpp
  249. Range.h
  250. Range.idl
  251. RangeBoundaryPoint.h
  252. RangeTest.cpp
  253. RawDataDocumentParser.h
  255. RemoteSecurityContext.cpp
  256. RemoteSecurityContext.h
  257. SandboxFlags.cpp
  258. SandboxFlags.h
  259. ScopedWindowFocusAllowedIndicator.h
  260. ScriptableDocumentParser.cpp
  261. ScriptableDocumentParser.h
  262. ScriptedAnimationController.cpp
  263. ScriptedAnimationController.h
  264. ScriptedAnimationControllerTest.cpp
  265. ScriptedIdleTaskController.cpp
  266. ScriptedIdleTaskController.h
  267. ScriptedIdleTaskControllerTest.cpp
  268. SecurityContext.cpp
  269. SecurityContext.h
  270. ShadowDOMV0Test.cpp
  271. ShadowRoot.cpp
  272. ShadowRoot.h
  273. ShadowRoot.idl
  274. ShadowRootInit.idl
  275. ShadowRootRareDataV0.h
  276. SinkDocument.cpp
  277. SinkDocument.h
  278. SlotAssignment.cpp
  279. SlotAssignment.h
  280. SpaceSplitString.cpp
  281. SpaceSplitString.h
  282. SpaceSplitStringTest.cpp
  283. StaticNodeList.h
  284. StaticRange.cpp
  285. StaticRange.h
  286. StaticRange.idl
  287. StaticRangeTest.cpp
  288. SynchronousMutationNotifier.cpp
  289. SynchronousMutationNotifier.h
  290. SynchronousMutationObserver.cpp
  291. SynchronousMutationObserver.h
  292. SyncReattachContext.cpp
  293. SyncReattachContext.h
  294. TagCollection.cpp
  295. TagCollection.h
  296. TaskTypeTraits.h
  297. TemplateContentDocumentFragment.h
  298. Text.cpp
  299. Text.h
  300. Text.idl
  301. TextLinkColors.cpp
  302. TextLinkColors.h
  303. TextTest.cpp
  304. ThrowOnDynamicMarkupInsertionCountIncrementer.h
  305. TransformSource.h
  306. TransformSourceLibxslt.cpp
  307. TreeOrderedList.cpp
  308. TreeOrderedList.h
  309. TreeOrderedMap.cpp
  310. TreeOrderedMap.h
  311. TreeScope.cpp
  312. TreeScope.h
  313. TreeScopeAdopter.cpp
  314. TreeScopeAdopter.h
  315. TreeScopeAdopterTest.cpp
  316. TreeScopeTest.cpp
  317. TreeWalker.cpp
  318. TreeWalker.h
  319. TreeWalker.idl
  320. UserActionElementSet.cpp
  321. UserActionElementSet.h
  322. UserGestureIndicator.cpp
  323. UserGestureIndicator.h
  324. UserGestureIndicatorTest.cpp
  325. V0InsertionPoint.cpp
  326. V0InsertionPoint.h
  327. ViewportDescription.cpp
  328. ViewportDescription.h
  329. VisitedLinkState.cpp
  330. VisitedLinkState.h
  331. VoidFunction.idl
  332. WeakIdentifierMap.h
  333. WhitespaceAttacher.cpp
  334. WhitespaceAttacher.h
  335. WhitespaceAttacherTest.cpp
  337. XMLDocument.cpp
  338. XMLDocument.h
  339. XMLDocument.idl




The Source/core/dom directory contains the implementation of DOM.

Basically, this directory should contain only a file which is related to DOM Standard. However, for historical reasons, Source/core/dom directory has been used as if it were misc directory. As a result, unfortunately, this directory contains a lot of files which are not directly related to DOM.

Please don‘t add unrelated files to this directory any more. We are trying to organize the files so that developers wouldn’t get confused at seeing this directory.

  • See the spreadsheet, as a rough plan to organize Source/core/dom files.

    The classification in the spreadsheet might be wrong. Please update the spreadsheet, and move files if you can, if you know more appropriate places for each file.

  • See for tracking our efforts.

Node and Node Tree

In this README, we draw a tree in left-to-right direction. A is the root of the tree.


Node is a base class of all kinds of nodes in a node tree. Each Node has following 3 pointers (but not limited to):

  • parent_or_shadow_host_node_: Points to the parent (or the shadow host if it is a shadow root; explained later)
  • previous_: Points to the previous sibling
  • next_: Points to the next sibling

ContainerNode, from which Element extends, has additional pointers for its child:

  • first_child_: The meaning is obvious.
  • last_child_: Nit.

That means:

  • Siblings are stored as a linked list. It takes O(N) to access a parent's n-th child.
  • Parent can't tell how many children it has in O(1).

Further info:

  • Node, ContainerNode

C++11 range-based for loops for traversing a tree

You can traverse a tree manually:

// In C++

// Traverse a children.
for (Node* child = parent.firstChild(); child; child = child->nextSibling()) {

// ...

// Traverse nodes in tree order, depth-first traversal.
void foo(const Node& node) {
  for (Node* child = node.firstChild(); child; child = child->nextSibling()) {
    foo(*child);  // Recursively

However, traversing a tree in this way might be error-prone. Instead, you can use NodeTraversal and ElementTraversal. They provides a C++11's range-based for loops, such as:

// In C++
for (Node& child : NodeTraversal::childrenOf(parent) {

e.g. Given a parent A, this traverses B, C, and F in this order.

// In C++
for (Node& node : NodeTraversal::startsAt(root)) {

e.g. Given the root A, this traverses A, B, C, D, E, and F in this order.

There are several other useful range-based for loops for each purpose. The cost of using range-based for loops is zero because everything can be inlined.

Further info:

  • NodeTraversal and ElementTraversal (more type-safe version)
  • The CL, which introduced these range-based for loops.

Shadow Tree

A shadow tree is a node tree whose root is a ShadowRoot. From web developer's perspective, a shadow root can be created by calling element.attachShadow{ ... } API. The element here is called a shadow host, or just a host if the context is clear.

  • A shadow root is always attached to another node tree through its host. A shadow tree is therefore never alone.
  • The node tree of a shadow root’s host is sometimes referred to as the light tree.

For example, given the example node tree:


Web developers can create a shadow root, and manipulate the shadow tree in the following way:

// In JavaScript
const b = document.querySelector('#B');
const shadowRoot = b.attachShadow({ mode: 'open'} )
const sb = document.createElement('div');

The resulting shadow tree would be:

└── sb

The shadowRoot has one child, sb. This shadow tree is being attached to B:

└── B
       └── sb
    ├── C
       ├── D
       └── E
    └── F

In this README, a notation (──/) is used to represent a shadowhost-shadowroot relationship, in a composed tree. A composed tree will be explained later. A shadowhost-shadowroot is 1:1 relationship.

Though a shadow root has always a corresponding shadow host element, a light tree and a shadow tree should be considered separately, from a node tree's perspective. (──/) is NOT a parent-child relationship in a node tree.

For example, even though B hosts the shadow tree, shadowRoot is not considered as a child of B. The means the following traversal:

// In C++
for (Node& node : NodeTraversal::startsAt(A)) {

traverses only A, B, C, D, E and F nodes. It never visits shadowRoot nor sb. NodeTraversal never cross a shadow boundary, ──/.

Further info:

  • ShadowRoot
  • Element#attachShadow


Document and ShadowRoot are always the root of a node tree. BothDocument and ShadowRoot implements TreeScope.

TreeScope maintains a lot of information about the underlying tree for efficiency. For example, TreeScope has a id-to-element mapping, as TreeOrderedMap, so that querySelector('#foo') can find an element whose id attribute is “foo” in O(1). In other words, root.querySelector('#foo') can be slow if that is used in a node tree whose root is not TreeScope.

Each Node has tree_scope_ pointer, which points to:

  • The root node: if the node's root is either Document or ShadowRoot.
  • owner document, otherwise.

The means tree_scope_ pointer is always non-null (except for while in a DOM mutation), but it doesn‘t always point to the node’s root.

Since each node doesn't have a pointer which always points to the root, Node::getRootNode(...) may take O(N) if the node is neither in a document tree nor in a shadow tree. If the node is in TreeScope (Node#IsInTreeScope() can tell it), we can get the root in O(1).

Each node has flags, which is updated in DOM mutation, so that we can tell whether the node is in a document tree, in a shadow tree, or in none of them, by using Node::IsInDocumentTree() and/or Node::IsInShadowTree().

If you want to add new features to Document, Document might be a wrong place to add. Instead, please consider to add functionality to TreeScope. We want to treat a document tree and a shadow tree equally as much as possible.


└── a1
       └── s1
    └── a2
        └── a3

└── b1
       └── t2
    └── b2
        └── b3
  • Here, there are 4 node trees; The root node of each tree is document, shadowRoot1, document-fragment, and shadowRoot2.
  • Suppose that each node is created by document.createElement(...) (except for Document and ShadowRoot). That means each node's owner document is document.
nodenode's rootnode's _tree_scope points to:
documentdocument (self)document (self)
shadowRoot1shadowRoot1 (self)shadowRoot1 (self)
document-fragmentdocument-fragment (self)document
shadowRoot2shadowRoot2 (self)shadowRoot2 (self)

Further Info:

Composed Tree (a tree of node trees)

In the previous picture, you might think that more than one node trees, a document tree and a shadow tree, were connected to each other. That is true in some sense. The following is a more complex example:

├── a1 (host)
      └── b1
   └── a2 (host)
          ├── c1
             ├── c2
             └── c3
          └── c4
       ├── a3
       └── a4
└── a5
    └── a6 (host)
            └── d1
                ├── d2
                ├── d3 (host)
                       ├── e1
                       └── e2
                └── d4 (host)
                        ├── f1
                        └── f2

If you see this carefully, you can notice that this composed tree is composed of 6 node trees; 1 document tree and 5 shadow trees:

  • document tree

    ├── a1 (host)
       └── a2 (host)
           ├── a3
           └── a4
    └── a5
        └── a6 (host)
  • shadow tree 1

    └── b1
  • shadow tree 2

    ├── c1
       ├── c2
       └── c3
    └── c4
  • shadow tree 3

    └── d1
        ├── d2
        ├── d3 (host)
        └── d4 (host)
  • shadow tree 4

    ├── e1
    └── e2
  • shadow tree 5

    ├── f1
    └── f2

If we consider each node tree as node of a super-tree, we can draw a super-tree as such:

├── shadowRoot1
├── shadowRoot2
└── shadowRoot3
    ├── shadowRoot4
    └── shadowRoot5

Here, a root node is used as a representative of each node tree; A root node and a node tree itself can be sometimes exchangeable in explanations.

We call this kind of a super-tree (a tree of node trees) a composed tree. The concept of a composed tree is very useful to understand how Shadow DOM's encapsulation works.

DOM Standard defines the following terminologies:

For example,

  • d1's shadow-including ancestor nodes are shadowRoot3, a6, a5, and document
  • d1's shadow-including descendant nodes are d2, d3, shadowRoot4, e1, e2, d4, shadowRoot5, f1, and f2.

To honor Shadow DOM's encapsulation, we have a concept of visibility relationship between two nodes.

In the following table, “-” means that “node A is visible from node B”.

A \ Bdocumenta1a2b1c1d1d2e1f1

For example, document is visible from any nodes.

To understand visibility relationship easily, here is a rule of thumb:

  • If node B can reach node A by traversing an edge (in the first picture of this section), recursively, A is visible from B.
  • However, an edge of (──/) ( shadowhost-shadowroot relationship) is one-directional:
    • From a shadow root to the shadow host -> Okay
    • From a shadow host to the shadow root -> Forbidden

In other words, a node in an inner tree can see a node in an outer tree in a composed tree, but the opposite is not true.

We have designed (or re-designed) a bunch of Web-facing APIs to honor this basic principle. If you add a new API to the web platform and Blink, please consider this rule and don't leak a node which should be hidden to web developers.

Warning: Unfortunately, a composed tree had a different meaning in the past; it was used to specify a flat tree (which will be explained later). If you find a wrong usage of a composed tree in Blink, please fix it.

Further Info:

  • TreeScope::ParentTreeScope()
  • Node::IsConnected()
  • DOM Standard: connected
  • DOM Standard: retarget

Flat tree

A composed tree itself can‘t be rendered as is. From the rendering’s perspective, Blink has to construct a layout tree, which would be used as an input to the paint phase. A layout tree is a tree whose node is LayoutObject, which points to Node in a node tree, plus additional calculated layout information.

Before the Web Platform got Shadow DOM, the structure of a layout tree is almost similar to the structure of a document tree; where only one node tree, document tree, is being involved there.

Since the Web Platform got Shadow DOM, we now have a composed tree which is composed of multiple node trees, instead of a single node tree. That means We have to flatten the composed tree to the one node tree, called a flat tree, from which a layout tree is constructed.

For example, given the following composed tree,

├── a1 (host)
      └── b1
   └── a2 (host)
          ├── c1
             ├── c2
             └── c3
          └── c4
       ├── a3
       └── a4
└── a5
    └── a6 (host)
            └── d1
                ├── d2
                ├── d3 (host)
                       ├── e1
                       └── e2
                └── d4 (host)
                        ├── f1
                        └── f2

This composed tree would be flattened into the following flat tree (assuming there are not <slot> elements there):

├── a1 (host)
   └── b1
└── a5
    └── a6 (host)
        └── d1
            ├── d2
            ├── d3 (host)
               ├── e1
               └── e2
            └── d4 (host)
                ├── f1
                └── f2

We can't explain the exact algorithm how to flatten a composed tree into a flat tree until I explain the concept of slots and node distribution If we are ignoring the effect of <slot>, we can have the following simple definition. A flat tree can be defined as:

  • A root of a flat tree: document
  • Given node A which is in a flat tree, its children are defined, recursively, as follows:
    • If A is a shadow host, its shadow root's children
    • Otherwise, A's children

Distribution and slots

TODO(hayato): Explain.

In the meantime, please see Incremental Shadow DOM.


TODO(hayato): Explain.

DOM mutations

TODO(hayato): Explain.

Related flags

TODO(hayato): Explain.

Event path and Event Retargeting

TODO(hayato): Explain.