tree: d3f75f8156a4a9c65f9a783e19452fce885e19ec [path history] [tgz]
  1. events/
  2. abort_controller.cc
  3. abort_controller.h
  4. abort_controller.idl
  5. abort_signal.cc
  6. abort_signal.h
  7. abort_signal.idl
  8. accessibility_role.idl
  9. animation_worklet_proxy_client.cc
  10. animation_worklet_proxy_client.h
  11. aria_attributes.idl
  12. attr.cc
  13. attr.h
  14. attr.idl
  15. attr_test.cc
  16. attribute.h
  17. attribute_collection.h
  18. BUILD.gn
  19. cdata_section.cc
  20. cdata_section.h
  21. cdata_section.idl
  22. character_data.cc
  23. character_data.h
  24. character_data.idl
  25. child_frame_disconnector.cc
  26. child_frame_disconnector.h
  27. child_list_mutation_scope.cc
  28. child_list_mutation_scope.h
  29. child_node.h
  30. child_node.idl
  31. child_node_list.cc
  32. child_node_list.h
  33. class_collection.cc
  34. class_collection.h
  35. collection_index_cache.h
  36. comment.cc
  37. comment.h
  38. comment.idl
  39. common_definitions.idl
  40. container_node.cc
  41. container_node.h
  42. context_features.cc
  43. context_features.h
  44. context_features_client_impl.cc
  45. context_features_client_impl.h
  46. context_lifecycle_notifier.cc
  47. context_lifecycle_notifier.h
  48. context_lifecycle_observer.cc
  49. context_lifecycle_observer.h
  50. create_element_flags.h
  51. dataset_dom_string_map.cc
  52. dataset_dom_string_map.h
  53. decoded_data_document_parser.cc
  54. decoded_data_document_parser.h
  55. distributed_nodes.cc
  56. distributed_nodes.h
  57. document.cc
  58. document.h
  59. document.idl
  60. document_encoding_data.cc
  61. document_encoding_data.h
  62. document_fragment.cc
  63. document_fragment.h
  64. document_fragment.idl
  65. document_init.cc
  66. document_init.h
  67. document_lifecycle.cc
  68. document_lifecycle.h
  69. document_or_shadow_root.h
  70. document_or_shadow_root.idl
  71. document_parser.cc
  72. document_parser.h
  73. document_parser_client.h
  74. document_parser_timing.cc
  75. document_parser_timing.h
  76. document_shutdown_notifier.cc
  77. document_shutdown_notifier.h
  78. document_shutdown_observer.cc
  79. document_shutdown_observer.h
  80. document_statistics_collector.cc
  81. document_statistics_collector.h
  82. document_statistics_collector_test.cc
  83. document_test.cc
  84. document_timing.cc
  85. document_timing.h
  86. document_type.cc
  87. document_type.h
  88. document_type.idl
  89. dom_exception.cc
  90. dom_exception.h
  91. dom_exception.idl
  92. dom_high_res_time_stamp.h
  93. dom_implementation.cc
  94. dom_implementation.h
  95. dom_implementation.idl
  96. dom_implementation_test.cc
  97. dom_node_ids.cc
  98. dom_node_ids.h
  99. dom_string_list.cc
  100. dom_string_list.h
  101. dom_string_list.idl
  102. dom_string_map.cc
  103. dom_string_map.h
  104. dom_string_map.idl
  105. dom_time_stamp.h
  106. dom_token_list.cc
  107. dom_token_list.h
  108. dom_token_list.idl
  109. element.cc
  110. element.h
  111. element.idl
  112. element_creation_options.idl
  113. element_data.cc
  114. element_data.h
  115. element_data_cache.cc
  116. element_data_cache.h
  117. element_definition_options.idl
  118. element_rare_data.cc
  119. element_rare_data.h
  120. element_registration_options.idl
  121. element_test.cc
  122. element_traversal.h
  123. element_visibility_observer.cc
  124. element_visibility_observer.h
  125. element_visibility_observer_test.cc
  126. empty_node_list.cc
  127. empty_node_list.h
  128. first_letter_pseudo_element.cc
  129. first_letter_pseudo_element.h
  130. first_letter_pseudo_element_test.cc
  131. flat_tree_traversal.cc
  132. flat_tree_traversal.h
  133. flat_tree_traversal_ng.cc
  134. flat_tree_traversal_ng.h
  135. flat_tree_traversal_ng_test.cc
  136. flat_tree_traversal_test.cc
  137. frame_request_callback.idl
  138. frame_request_callback_collection.cc
  139. frame_request_callback_collection.h
  140. function_string_callback.idl
  141. get_root_node_options.idl
  142. global_event_handlers.h
  143. global_event_handlers.idl
  144. icon_url.cc
  145. icon_url.h
  146. id_target_observer.cc
  147. id_target_observer.h
  148. id_target_observer_registry.cc
  149. id_target_observer_registry.h
  150. idle_deadline.cc
  151. idle_deadline.h
  152. idle_deadline.idl
  153. idle_deadline_test.cc
  154. idle_request_callback.idl
  155. idle_request_options.idl
  156. increment_load_event_delay_count.cc
  157. increment_load_event_delay_count.h
  158. iterator.h
  159. iterator.idl
  160. layout_tree_builder.cc
  161. layout_tree_builder.h
  162. layout_tree_builder_traversal.cc
  163. layout_tree_builder_traversal.h
  164. layout_tree_builder_traversal_test.cc
  165. live_node_list.cc
  166. live_node_list.h
  167. live_node_list_base.cc
  168. live_node_list_base.h
  169. live_node_list_registry.cc
  170. live_node_list_registry.h
  171. live_node_list_registry_test.cc
  172. mutation_observer.cc
  173. mutation_observer.h
  174. mutation_observer.idl
  175. mutation_observer_init.idl
  176. mutation_observer_interest_group.cc
  177. mutation_observer_interest_group.h
  178. mutation_observer_options.h
  179. mutation_observer_registration.cc
  180. mutation_observer_registration.h
  181. mutation_observer_test.cc
  182. mutation_record.cc
  183. mutation_record.h
  184. mutation_record.idl
  185. name_node_list.cc
  186. name_node_list.h
  187. named_node_map.cc
  188. named_node_map.h
  189. named_node_map.idl
  190. names_map.cc
  191. names_map.h
  192. names_map_test.cc
  193. node.cc
  194. node.h
  195. node.idl
  196. node_child_removal_tracker.cc
  197. node_child_removal_tracker.h
  198. node_computed_style.h
  199. node_filter.idl
  200. node_iterator.cc
  201. node_iterator.h
  202. node_iterator.idl
  203. node_iterator_base.cc
  204. node_iterator_base.h
  205. node_list.h
  206. node_list.idl
  207. node_lists_node_data.cc
  208. node_lists_node_data.h
  209. node_rare_data.cc
  210. node_rare_data.h
  211. node_test.cc
  212. node_traversal.cc
  213. node_traversal.h
  214. node_traversal_strategy.h
  215. node_with_index.h
  216. non_document_type_child_node.h
  217. non_document_type_child_node.idl
  218. non_element_parent_node.h
  219. non_element_parent_node.idl
  220. nonced_element.idl
  221. nth_index_cache.cc
  222. nth_index_cache.h
  223. nth_index_cache_test.cc
  224. OWNERS
  225. parent_node.h
  226. parent_node.idl
  227. parser_content_policy.h
  228. pausable_object.cc
  229. pausable_object.h
  230. pausable_object_test.cc
  231. presentation_attribute_style.cc
  232. presentation_attribute_style.h
  233. processing_instruction.cc
  234. processing_instruction.h
  235. processing_instruction.idl
  236. pseudo_element.cc
  237. pseudo_element.h
  238. pseudo_element_data.h
  239. qualified_name.cc
  240. qualified_name.h
  241. range.cc
  242. range.h
  243. range.idl
  244. range_boundary_point.h
  245. range_test.cc
  246. raw_data_document_parser.h
  247. README.md
  248. scoped_window_focus_allowed_indicator.h
  249. scriptable_document_parser.cc
  250. scriptable_document_parser.h
  251. scripted_animation_controller.cc
  252. scripted_animation_controller.h
  253. scripted_animation_controller_test.cc
  254. scripted_idle_task_controller.cc
  255. scripted_idle_task_controller.h
  256. scripted_idle_task_controller_test.cc
  257. shadow_dom_v0_test.cc
  258. shadow_root.cc
  259. shadow_root.h
  260. shadow_root.idl
  261. shadow_root_init.idl
  262. shadow_root_v0.cc
  263. shadow_root_v0.h
  264. sink_document.cc
  265. sink_document.h
  266. slot_assignment.cc
  267. slot_assignment.h
  268. slot_assignment_engine.cc
  269. slot_assignment_engine.h
  270. slot_assignment_recalc_forbidden_scope.h
  271. slot_assignment_test.cc
  272. space_split_string.cc
  273. space_split_string.h
  274. space_split_string_test.cc
  275. static_node_list.h
  276. static_range.cc
  277. static_range.h
  278. static_range.idl
  279. static_range_test.cc
  280. synchronous_mutation_notifier.cc
  281. synchronous_mutation_notifier.h
  282. synchronous_mutation_observer.cc
  283. synchronous_mutation_observer.h
  284. tag_collection.cc
  285. tag_collection.h
  286. task_type_traits.h
  287. template_content_document_fragment.h
  288. text.cc
  289. text.h
  290. text.idl
  291. text_link_colors.cc
  292. text_link_colors.h
  293. text_test.cc
  294. throw_on_dynamic_markup_insertion_count_incrementer.h
  295. transform_source.h
  296. transform_source_libxslt.cc
  297. tree_ordered_list.cc
  298. tree_ordered_list.h
  299. tree_ordered_map.cc
  300. tree_ordered_map.h
  301. tree_scope.cc
  302. tree_scope.h
  303. tree_scope_adopter.cc
  304. tree_scope_adopter.h
  305. tree_scope_adopter_test.cc
  306. tree_scope_test.cc
  307. tree_walker.cc
  308. tree_walker.h
  309. tree_walker.idl
  310. user_action_element_set.cc
  311. user_action_element_set.h
  312. user_gesture_indicator.cc
  313. user_gesture_indicator.h
  314. user_gesture_indicator_test.cc
  315. v0_insertion_point.cc
  316. v0_insertion_point.h
  317. visited_link_state.cc
  318. visited_link_state.h
  319. void_function.idl
  320. weak_identifier_map.h
  321. whitespace_attacher.cc
  322. whitespace_attacher.h
  323. whitespace_attacher_test.cc
  324. WhitespaceLayoutObjects.md
  325. xml_document.cc
  326. xml_document.h
  327. xml_document.idl
third_party/blink/renderer/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.