# Handling shadow DOM in the various tree kinds ## Flattened tree A DOM tree may have some shadow DOM trees. A flattened tree formats all [shadow root](#shadowroot)s are connected to each [shadow host](#shadow-host). ## Shadow host It is an `Element` node which has a `ShadowRoot` attached to it. In the flattened tree, its children are treated as replaced with the children of the `ShadowRoot`. So, the children of `ShadowRoot` look like the children of the host in the flattened tree. `nsINode` APIs, such as `nsINode::GetChildAt()`, `nsINode::GetChildCount()` and `nsINode::ComputeIndexOf()`, treat the children of a shadow host are the `ShadowRoot` children if `TreeKind` is not [`TreeKind::DOM`](#treekinddom). ## ShadowRoot It is a content node, inherits `DocumentFragment`. Therefore, when you print inclusive ancestors of a node in a `ShadowRoot` with `ToString(*node).c_str()`, you'll see `#document-fragment` as the root node. In the flattened tree, the children of a `ShadowRoot` is formatted as children of the [shadow host](#shadow-host) and the children of the shadow host are not part of the flattened tree unless they are assigned to `` elements. ## HTMLSlotElement When it appears in a shadow DOM, it may have assigned nodes which are children of the host element. In the flattened tree, the assigned nodes of a `` element are formatted as its children and the children of the `` are not part of the flattened tree ([spec](https://html.spec.whatwg.org/#the-slot-element)). However, if a `` does not have assigned node, the element is is treated as a usual container element because they are the fallback content of the element when there is no assigned node. `nsINode::GetParentOrShadowHostNode()` for `TreeKind::FlatForSelection` and `TreeKind::Flat` of a `` element which has some assigned nodes return the `` element as the parent node. However, if in the other `TreeKind`s, it returns the parent node in the shadow including DOM. Similarly, `nsINode::GetChildAt()`, `nsINode::GetChildCount()` and `nsINode::ComputeIndexOf()` for `TreeKind::FlatForSelection` and `TreeKind::Flat` of a `` which has some assigned nodes treat the assigned nodes as children of the ``. Note that the assigned nodes are not treated as in the shadow tree by `nsINode::IsInShadowTree()`. So, it returns `false` for the assigned nodes if they are not a descendant of a `ShadowRoot`. ## UA shadow tree Gecko attaches internally created `ShadowRoot` to some specific elements. For example, `
`, `