script/dom/node/context.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use crate::dom::Node;
6
7/// The context of the binding to tree of a node.
8pub(crate) struct BindContext<'a> {
9 /// The parent of the inclusive ancestor that was inserted.
10 pub(crate) parent: &'a Node,
11
12 /// Whether the tree is connected.
13 ///
14 /// <https://dom.spec.whatwg.org/#connected>
15 pub(crate) tree_connected: bool,
16
17 /// Whether the tree's root is a document.
18 ///
19 /// <https://dom.spec.whatwg.org/#in-a-document-tree>
20 pub(crate) tree_is_in_a_document_tree: bool,
21
22 /// Whether the tree's root is a shadow root
23 pub(crate) tree_is_in_a_shadow_tree: bool,
24
25 /// Whether the root of the subtree that is being bound to the parent is a shadow root.
26 ///
27 /// This implies that all elements whose "bind_to_tree" method are called were already
28 /// in a shadow tree beforehand.
29 pub(crate) is_shadow_tree: IsShadowTree,
30}
31
32#[derive(Debug, Eq, PartialEq)]
33pub(crate) enum IsShadowTree {
34 Yes,
35 No,
36}
37
38impl<'a> BindContext<'a> {
39 /// Create a new `BindContext` value.
40 pub(crate) fn new(parent: &'a Node, is_shadow_tree: IsShadowTree) -> Self {
41 BindContext {
42 parent,
43 tree_connected: parent.is_connected(),
44 tree_is_in_a_document_tree: parent.is_in_a_document_tree(),
45 tree_is_in_a_shadow_tree: parent.is_in_a_shadow_tree(),
46 is_shadow_tree,
47 }
48 }
49
50 /// Return true iff the tree is inside either a document- or a shadow tree.
51 pub(crate) fn is_in_tree(&self) -> bool {
52 self.tree_is_in_a_document_tree || self.tree_is_in_a_shadow_tree
53 }
54}
55
56/// The context of the unbinding from a tree of a node when one of its
57/// inclusive ancestors is removed.
58pub(crate) struct UnbindContext<'a> {
59 /// The parent of the inclusive ancestor that was removed.
60 pub(crate) parent: &'a Node,
61
62 /// The next sibling of the inclusive ancestor that was removed.
63 pub(crate) next_sibling: Option<&'a Node>,
64
65 /// Whether the tree is connected.
66 ///
67 /// <https://dom.spec.whatwg.org/#connected>
68 pub(crate) tree_connected: bool,
69
70 /// Whether the tree's root is a document.
71 ///
72 /// <https://dom.spec.whatwg.org/#in-a-document-tree>
73 pub(crate) tree_is_in_a_document_tree: bool,
74
75 /// Whether the tree's root is a shadow root
76 pub(crate) tree_is_in_a_shadow_tree: bool,
77}
78
79impl<'a> UnbindContext<'a> {
80 /// Create a new `UnbindContext` value.
81 pub(crate) fn new(parent: &'a Node, next_sibling: Option<&'a Node>) -> Self {
82 UnbindContext {
83 parent,
84 next_sibling,
85 tree_connected: parent.is_connected(),
86 tree_is_in_a_document_tree: parent.is_in_a_document_tree(),
87 tree_is_in_a_shadow_tree: parent.is_in_a_shadow_tree(),
88 }
89 }
90}
91
92/// The context of the moving from a tree of a node when one of its
93/// inclusive ancestors is moved.
94pub(crate) struct MoveContext<'a> {
95 /// The old parent, if any, of the inclusive ancestor that was moved.
96 pub(crate) old_parent: Option<&'a Node>,
97}
98
99impl<'a> MoveContext<'a> {
100 /// Create a new `MoveContext` value.
101 pub(crate) fn new(old_parent: Option<&'a Node>) -> Self {
102 MoveContext { old_parent }
103 }
104}