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

DOM

Rendered

Author: hayato@chromium.org

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 crbug.com/738794 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.

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

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:

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

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');
shadowRoot.appendChild(sb);

The resulting shadow tree would be:

shadowRoot
└── sb

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

A
└── B
    ├──/shadowRoot
       └── 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

TreeScope

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.

Example

document
└── a1
    ├──/shadowRoot1
       └── s1
    └── a2
        └── a3

document-fragment
└── b1
    ├──/shadowRoot2
       └── 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)
a1documentdocument
a2documentdocument
a3documentdocument
shadowRoot1shadowRoot1 (self)shadowRoot1 (self)
s1shadowRoot1shadowRoot1
document-fragmentdocument-fragment (self)document
b1document-fragmentdocument
b2document-fragmentdocument
b3document-fragmentdocument
shadowRoot2shadowRoot2 (self)shadowRoot2 (self)
t1shadowRoot2shadowRoot2

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:

document
├── a1 (host)
   ├──/shadowRoot1
      └── b1
   └── a2 (host)
       ├──/shadowRoot2
          ├── c1
             ├── c2
             └── c3
          └── c4
       ├── a3
       └── a4
└── a5
    └── a6 (host)
        └──/shadowRoot3
            └── d1
                ├── d2
                ├── d3 (host)
                   └──/shadowRoot4
                       ├── e1
                       └── e2
                └── d4 (host)
                    └──/shadowRoot5
                        ├── 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

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

    shadowRoot1
    └── b1
    
  • shadow tree 2

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

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

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

    shadowRoot5
    ├── f1
    └── f2
    

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

document
├── 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
document---------
a1---------
a2---------
b1hiddenhiddenhidden-hiddenhiddenhiddenhiddenhidden
c1hiddenhiddenhiddenhidden-hiddenhiddenhiddenhidden
d1hiddenhiddenhiddenhiddenhidden----
d2hiddenhiddenhiddenhiddenhidden----
e1hiddenhiddenhiddenhiddenhiddenhiddenhidden-hidden
f1hiddenhiddenhiddenhiddenhiddenhiddenhiddenhidden-

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,

document
├── a1 (host)
   ├──/shadowRoot1
      └── b1
   └── a2 (host)
       ├──/shadowRoot2
          ├── c1
             ├── c2
             └── c3
          └── c4
       ├── a3
       └── a4
└── a5
    └── a6 (host)
        └──/shadowRoot3
            └── d1
                ├── d2
                ├── d3 (host)
                   └──/shadowRoot4
                       ├── e1
                       └── e2
                └── d4 (host)
                    └──/shadowRoot5
                        ├── f1
                        └── f2

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

document
├── 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.

FlatTreeTraversal

TODO(hayato): Explain.

DOM mutations

TODO(hayato): Explain.

Related flags

TODO(hayato): Explain.

Event path and Event Retargeting

TODO(hayato): Explain.