tree: d5a2b0b7fef33c5ed66054264ebc1d798ddc4cdd [path history] [tgz]
  1. events/
  2. testing/
  3. abort_controller.cc
  4. abort_controller.h
  5. abort_controller.idl
  6. abort_signal.cc
  7. abort_signal.h
  8. abort_signal.idl
  9. abort_signal_composition_manager.cc
  10. abort_signal_composition_manager.h
  11. abort_signal_composition_type.h
  12. abort_signal_registry.cc
  13. abort_signal_registry.h
  14. abort_signal_test.cc
  15. abstract_range.cc
  16. abstract_range.h
  17. abstract_range.idl
  18. accessibility_role.idl
  19. aria_attributes.idl
  20. aria_relationship_attributes.idl
  21. attr.cc
  22. attr.h
  23. attr.idl
  24. attr_test.cc
  25. attribute.h
  26. attribute_collection.h
  27. attribute_part.cc
  28. attribute_part.h
  29. attribute_part.idl
  30. beforeunload_event_listener.cc
  31. beforeunload_event_listener.h
  32. build.gni
  33. cdata_section.cc
  34. cdata_section.h
  35. cdata_section.idl
  36. character_data.cc
  37. character_data.h
  38. character_data.idl
  39. child_frame_disconnector.cc
  40. child_frame_disconnector.h
  41. child_list_mutation_scope.cc
  42. child_list_mutation_scope.h
  43. child_node.idl
  44. child_node_list.cc
  45. child_node_list.h
  46. child_node_part.cc
  47. child_node_part.h
  48. child_node_part.idl
  49. class_collection.cc
  50. class_collection.h
  51. collection_index_cache.h
  52. comment.cc
  53. comment.h
  54. comment.idl
  55. common_definitions.idl
  56. COMMON_METADATA
  57. container_node.cc
  58. container_node.h
  59. container_node_test.cc
  60. create_element_flags.h
  61. dataset_dom_string_map.cc
  62. dataset_dom_string_map.h
  63. decoded_data_document_parser.cc
  64. decoded_data_document_parser.h
  65. DIR_METADATA
  66. document.cc
  67. document.h
  68. document.idl
  69. document_and_element_event_handlers.idl
  70. document_data.h
  71. document_encoding_data.cc
  72. document_encoding_data.h
  73. document_fragment.cc
  74. document_fragment.h
  75. document_fragment.idl
  76. document_init.cc
  77. document_init.h
  78. document_lifecycle.cc
  79. document_lifecycle.h
  80. document_or_shadow_root.idl
  81. document_parser.cc
  82. document_parser.h
  83. document_parser_client.h
  84. document_parser_timing.cc
  85. document_parser_timing.h
  86. document_part_root.cc
  87. document_part_root.h
  88. document_part_root.idl
  89. document_statistics_collector.cc
  90. document_statistics_collector.h
  91. document_statistics_collector_test.cc
  92. document_test.cc
  93. document_timing.cc
  94. document_timing.h
  95. document_type.cc
  96. document_type.h
  97. document_type.idl
  98. dom_exception.cc
  99. dom_exception.h
  100. dom_exception.idl
  101. dom_high_res_time_stamp.h
  102. dom_implementation.cc
  103. dom_implementation.h
  104. dom_implementation.idl
  105. dom_node_ids.cc
  106. dom_node_ids.h
  107. dom_node_ids_test.cc
  108. dom_string_list.cc
  109. dom_string_list.h
  110. dom_string_list.idl
  111. dom_string_map.cc
  112. dom_string_map.h
  113. dom_string_map.idl
  114. dom_time_stamp.h
  115. dom_token_list.cc
  116. dom_token_list.h
  117. dom_token_list.idl
  118. element.cc
  119. element.h
  120. element.idl
  121. element_creation_options.idl
  122. element_css_inline_style.idl
  123. element_data.cc
  124. element_data.h
  125. element_data_cache.cc
  126. element_data_cache.h
  127. element_definition_options.idl
  128. element_rare_data_field.h
  129. element_rare_data_vector.cc
  130. element_rare_data_vector.h
  131. element_rare_data_vector_test.cc
  132. element_registration_options.idl
  133. element_test.cc
  134. element_traversal.h
  135. empty_node_list.cc
  136. empty_node_list.h
  137. first_letter_pseudo_element.cc
  138. first_letter_pseudo_element.h
  139. first_letter_pseudo_element_test.cc
  140. flat_tree_node_data.cc
  141. flat_tree_node_data.h
  142. flat_tree_traversal.cc
  143. flat_tree_traversal.h
  144. flat_tree_traversal_forbidden_scope.h
  145. flat_tree_traversal_test.cc
  146. focus_params.h
  147. focused_element_change_observer.h
  148. focusgroup_flags.cc
  149. focusgroup_flags.h
  150. frame_request_callback.idl
  151. frame_request_callback_collection.cc
  152. frame_request_callback_collection.h
  153. function_string_callback.idl
  154. get_html_options.idl
  155. get_inner_html_options.idl
  156. get_root_node_options.idl
  157. global_event_handlers.idl
  158. has_invalidation_flags.h
  159. icon_url.cc
  160. icon_url.h
  161. id_target_observer.cc
  162. id_target_observer.h
  163. id_target_observer_registry.cc
  164. id_target_observer_registry.h
  165. idle_deadline.cc
  166. idle_deadline.h
  167. idle_deadline.idl
  168. idle_deadline_test.cc
  169. idle_request_callback.idl
  170. idle_request_options.idl
  171. ignore_opens_during_unload_count_incrementer.h
  172. increment_load_event_delay_count.cc
  173. increment_load_event_delay_count.h
  174. interest_invoker_element.idl
  175. invoker_element.idl
  176. layout_tree_builder.cc
  177. layout_tree_builder.h
  178. layout_tree_builder_traversal.cc
  179. layout_tree_builder_traversal.h
  180. layout_tree_builder_traversal_test.cc
  181. live_node_list.cc
  182. live_node_list.h
  183. live_node_list_base.cc
  184. live_node_list_base.h
  185. live_node_list_registry.cc
  186. live_node_list_registry.h
  187. live_node_list_registry_test.cc
  188. mutation_observer.cc
  189. mutation_observer.h
  190. mutation_observer.idl
  191. mutation_observer_init.idl
  192. mutation_observer_interest_group.cc
  193. mutation_observer_interest_group.h
  194. mutation_observer_options.h
  195. mutation_observer_registration.cc
  196. mutation_observer_registration.h
  197. mutation_observer_test.cc
  198. mutation_record.cc
  199. mutation_record.h
  200. mutation_record.idl
  201. name_node_list.cc
  202. name_node_list.h
  203. named_node_map.cc
  204. named_node_map.h
  205. named_node_map.idl
  206. names_map.cc
  207. names_map.h
  208. names_map_test.cc
  209. node.cc
  210. node.h
  211. node.idl
  212. node_child_removal_tracker.cc
  213. node_child_removal_tracker.h
  214. node_cloning_data.h
  215. node_computed_style.h
  216. node_filter.idl
  217. node_iterator.cc
  218. node_iterator.h
  219. node_iterator.idl
  220. node_iterator_base.cc
  221. node_iterator_base.h
  222. node_list.h
  223. node_list.idl
  224. node_list_invalidation_type.h
  225. node_lists_node_data.cc
  226. node_lists_node_data.h
  227. node_part.cc
  228. node_part.h
  229. node_part.idl
  230. node_rare_data.cc
  231. node_rare_data.h
  232. node_test.cc
  233. node_traversal.cc
  234. node_traversal.h
  235. node_traversal_strategy.h
  236. node_traversal_test.cc
  237. node_with_index.h
  238. non_document_type_child_node.idl
  239. non_element_parent_node.idl
  240. nth_index_cache.cc
  241. nth_index_cache.h
  242. nth_index_cache_test.cc
  243. observable.cc
  244. observable.h
  245. observable.idl
  246. observable_internal_observer.h
  247. OWNERS
  248. parent_node.idl
  249. parser_content_policy.h
  250. part.cc
  251. part.h
  252. part.idl
  253. part_root.cc
  254. part_root.h
  255. part_root.idl
  256. pointer_lock_options.idl
  257. popover_data.h
  258. popover_invoker_element.idl
  259. presentation_attribute_style.cc
  260. presentation_attribute_style.h
  261. processing_instruction.cc
  262. processing_instruction.h
  263. processing_instruction.idl
  264. pseudo_element.cc
  265. pseudo_element.h
  266. pseudo_element_data.h
  267. pseudo_element_test.cc
  268. qualified_name.cc
  269. qualified_name.h
  270. qualified_name_test.cc
  271. range.cc
  272. range.h
  273. range.idl
  274. range_boundary_point.h
  275. range_test.cc
  276. raw_data_document_parser.h
  277. README.md
  278. scoped_abort_state.h
  279. scoped_window_focus_allowed_indicator.h
  280. scriptable_document_parser.cc
  281. scriptable_document_parser.h
  282. scripted_animation_controller.cc
  283. scripted_animation_controller.h
  284. scripted_animation_controller_test.cc
  285. scripted_idle_task_controller.cc
  286. scripted_idle_task_controller.h
  287. scripted_idle_task_controller_test.cc
  288. shadow_including_tree_order_traversal.h
  289. shadow_including_tree_order_traversal_test.cc
  290. shadow_root.cc
  291. shadow_root.h
  292. shadow_root.idl
  293. shadow_root_init.idl
  294. sink_document.cc
  295. sink_document.h
  296. slot_assignment.cc
  297. slot_assignment.h
  298. slot_assignment_engine.cc
  299. slot_assignment_engine.h
  300. slot_assignment_recalc_forbidden_scope.h
  301. slot_assignment_test.cc
  302. space_split_string.cc
  303. space_split_string.h
  304. space_split_string_test.cc
  305. static_node_list.h
  306. static_range.cc
  307. static_range.h
  308. static_range.idl
  309. static_range_init.idl
  310. static_range_test.cc
  311. subscriber.cc
  312. subscriber.h
  313. subscriber.idl
  314. synchronous_mutation_observer.cc
  315. synchronous_mutation_observer.h
  316. tag_collection.cc
  317. tag_collection.h
  318. template_content_document_fragment.h
  319. text.cc
  320. text.h
  321. text.idl
  322. text_diff_range.cc
  323. text_diff_range.h
  324. text_link_colors.cc
  325. text_link_colors.h
  326. text_test.cc
  327. text_visitor.h
  328. throw_on_dynamic_markup_insertion_count_incrementer.h
  329. transform_source.h
  330. transform_source_libxslt.cc
  331. transition_pseudo_element_data.h
  332. traversal_range.h
  333. tree_ordered_list.cc
  334. tree_ordered_list.h
  335. tree_ordered_list_test.cc
  336. tree_ordered_map.cc
  337. tree_ordered_map.h
  338. tree_ordered_map_test.cc
  339. tree_scope.cc
  340. tree_scope.h
  341. tree_scope_adopter.cc
  342. tree_scope_adopter.h
  343. tree_scope_adopter_test.cc
  344. tree_scope_test.cc
  345. tree_walker.cc
  346. tree_walker.h
  347. tree_walker.idl
  348. user_action_element_set.cc
  349. user_action_element_set.h
  350. visited_link_state.cc
  351. visited_link_state.h
  352. void_function.idl
  353. weak_identifier_map.h
  354. weak_identifier_map_test.cc
  355. whitespace_attacher.cc
  356. whitespace_attacher.h
  357. whitespace_attacher_test.cc
  358. WhitespaceLayoutObjects.md
  359. xml_document.cc
  360. xml_document.h
  361. xml_document.idl
third_party/blink/renderer/core/dom/README.md

DOM

Rendered

Author: hayato@chromium.org, some edits by masonf@chromium.org

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

Basically, this directory should contain only files related to the DOM Standard. However, for historical reasons, the renderer/core/dom directory has been used as if it were a 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 by 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 in ascii-art notation. 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.
  • A parent can't tell how many children it has in O(1).

next sibling and previous sibling

Further info:

  • Node, ContainerNode

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

You can traverse a tree manually:

// In C++

// Traverse a node's 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
  }
}

Tree order is:

tree order

However, traversing a tree in this way might be error-prone. Instead, you can use NodeTraversal and ElementTraversal. They provide C++11 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.

shadow 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 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. Both Document and ShadowRoot implement TreeScope.

TreeScope maintains a lot of information about the underlying tree for efficiency. For example, TreeScope has an 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 it is used in a node tree whose root is not TreeScope.

Each Node has a 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 different node trees, e.g., a document tree and a shadow tree, were connected to each other. That is true in some sense. We call this super tree as composed tree, which is a tree of trees.

super tree

The following is a 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 node.

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.

flat tree

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 we explain the concept of slots and slot assignment. If we ignore the effect of <slot>, we can define a flat tree as:

  • A root of a flat tree: document
  • Given a 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

Slots and node assignments

Please see this nice article how <slot> elements work in general.

Slots are placeholders inside your component that users can fill with their own markup.

Here, I'll show some examples.

Example 1

Given the following composed tree and slot assignments,

Composed tree:

A
├──/shadowRoot1
   ├── slot1
   └── slot2
├── B
└── C

Slot Assignments:

slotslot's assigned nodes
slot1[C]
slot2[B]

The flat tree would be:

A
├── slot1
   └── C
└── slot2
    └── B

Example 2

More complex example is here.

Composed tree:

A
├──/shadowRoot1
   ├── B
      └── slot1
   ├── slot2
      └── C
   ├── D
   └── slot3
       ├── E
       └── F
├── G
├── H
├── I
└── J

Slot Assignments:

slotslot's assigned nodes
slot1[H]
slot2[G, I]
slot3[] (nothing is assigned)

The flat tree would be:

A
├── B
   └── slot1
       └── H
├── slot2
   ├── G
   └── I
├── D
└── slot3
    ├── E
    └── F
  • slot2's child, C, is not shown in this flat tree because slot2 has non-empty assigned nodes, [G, I], which are used as slot2's children in the flat tree.
  • If a slot doesn‘t have any assigned nodes, the slot’s children are used as fallback contents in the flat tree. e.g. slot3s children in the flat tree are E and F.
  • If a host's child node is assigned to nowhere, the child is not used, as is the case, e.g., for J.

Example 3

A slot itself can be assigned to another slot.

For example, if we attach a shadow root to B, and put a <slot>, slot4, inside of the shadow tree.

A
├──/shadowRoot1
   ├── B
      ├──/shadowRoot2
         └── K
             └── slot4
      └── slot1
   ├── slot2
      └── C
   ├── D
   └── slot3
       ├── E
       └── F
├── G
├── H
├── I
└── J
slotslot's assigned nodes
slot1[H]
slot2[G, I]
slot3[] (nothing is assigned)
slot4[slot1]

The flat tree would be:

A
├── B
   └── K
       └── slot4
           └── slot1
               └── H
├── slot2
   ├── G
   └── I
├── D
└── slot3
    ├── E
    └── F

Slot Assignment Recalc

Please see Incremental Shadow DOM to know how assignments are recalc-ed.

FlatTreeTraversal

Blink doesn't store nor maintain a flat tree data structure in the memory. Instead, Blink provides a utility class, FlatTreeTraversal, which traverses a composed tree in a flat tree order.

e.g. in the above example 3,

  • FlatTreeTraversal::firstChild(slot1) returns H
  • FlatTreeTraversal::parent(H) returns slot1
  • FlatTreeTraversal::nextSibling(G) returns I
  • FlatTreeTraversal::previousSibling(I) returns G

The APIs which FlatTreeTraversal provides are very similar to ones other traversal utility classes provide, such as NodeTraversal and ElementTraversal.

Event path and Event Retargeting

The DOM Standard defines how an event should be dispatched here, including how the event path should be calculated. However, I wouldn't be surprised if the steps described there might appear cryptic to you.

In this README, I'll explain how an event is dispatched and how its event path is calculated briefly by using some relatively-understandable examples.

Basically, an event is dispatched across shadow trees.

event dispatch

Let me show more complex example composed tree, involving a slot:

A
└── B
    ├──/shadowroot-C
       └── D
           ├──/shadowroot-E
              └── F
                  └── slot-G
           └── H
               └── I
                   ├──/shadowroot-J
                      └── K
                          ├──/shadowroot-L
                             └── M
                                 ├──/shadowroot-N
                                    └── slot-O
                                 └── slot-P
                          └── Q
                              └── slot-R
                   └── slot-S
    └── T
        └── U

Slot Assignments:

slotslot's assigned nodes
slot-G[H]
slot-O[slot-P]
slot-P[Q]
slot-R[slot-S]
slot-S[T]

Given that, suppose that an event is fired on U. Then an event path would be (in reverse order):

[U => T => slot-S => slot-R => Q => slot-P => slot-O => shadowroot-N => M
=> shadowroot-L => K => shadowroot-J => I => H => slot-G => F => shadowroot-E
=> D => shadowroot-C => B => A]

Roughly speaking, an event's parent (the next node in event path) is calculated as follows:

  • If a node is assigned to a slot, the parent is the node's asigned slot.
  • If a node is a shadow root, the parent is its shadow host.
  • In other cases, the parent is node's parent.

In the above case, event.target, U, doesn't change in its lifetime because U can be seen from every nodes there. However, if an event is fired on node Q, for example, event.target would be adjusted for some nodes in event path to honor encapsulation. That is called event re-targeting.

Here is an event path for an event which is fired on Q :

event.currenttarget(re-targeted) event.target
QQ
slot-PQ
slot-OQ
shadowroot-NQ
MQ
shadowroot-LQ
KQ
shadowroot-JQ
II
HI
slot-GI
FI
shadowroot-EI
DI
shadowroot-CI
BB
AB

Design goal of event path calculation

TODO(hayato): Explain.

Composed events

TODO(hayato): Explain.

Event path and related targets

TODO(hayato): Explain.

DOM mutations

TODO(hayato): Explain.

Related flags

TODO(hayato): Explain.