script/dom/execcommand/commands/
indent.rs1use std::collections::{HashSet, VecDeque};
6
7use js::context::JSContext;
8use script_bindings::codegen::GenericBindings::NodeBinding::NodeMethods;
9use script_bindings::dom::UnrootedDom;
10use script_bindings::inheritance::Castable;
11
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::document::Document;
14use crate::dom::element::Element;
15use crate::dom::execcommand::contenteditable::node::{
16 NodeOrString, is_allowed_child, wrap_node_list,
17};
18use crate::dom::html::htmllielement::HTMLLIElement;
19use crate::dom::html::htmlolistelement::HTMLOListElement;
20use crate::dom::html::htmlulistelement::HTMLUListElement;
21use crate::dom::node::Node;
22use crate::dom::selection::Selection;
23
24pub(crate) fn indent(
26 cx: &mut JSContext,
27 context_object: &Document,
28 node_list: VecDeque<DomRoot<Node>>,
29) {
30 if node_list.is_empty() {
32 return;
33 }
34
35 let first_node = node_list
37 .front()
38 .expect("Must have a first node by now.")
39 .clone();
40
41 if let Some(parent) = first_node.GetParentElement() &&
43 (parent.is::<HTMLOListElement>() || parent.is::<HTMLUListElement>())
44 {
45 let tag = parent.local_name();
47
48 wrap_node_list(
52 cx,
53 node_list,
54 |_cx, sibling| {
55 sibling
56 .downcast::<Element>()
57 .is_some_and(|sibling| sibling.local_name() == tag)
58 },
59 |cx| {
60 Some(DomRoot::upcast(
61 first_node.owner_doc().create_element(cx, tag),
62 ))
63 },
64 );
65
66 return;
68 }
69
70 let new_parent = wrap_node_list(
75 cx,
76 node_list,
77 |_cx, sibling| {
78 sibling
79 .downcast::<Element>()
80 .is_some_and(|sibling| sibling.is_simple_indentation_element())
81 },
82 |cx| {
83 Some(DomRoot::upcast(
84 first_node.owner_doc().create_element(cx, "blockquote"),
85 ))
86 },
87 );
88
89 if let Some(new_parent) = new_parent {
91 new_parent.fix_disallowed_ancestors(cx, context_object);
92 }
93}
94
95pub(crate) fn execute_indent_command(
97 cx: &mut JSContext,
98 document: &Document,
99 selection: &Selection,
100) -> bool {
101 let mut active_range = selection
102 .active_range(cx)
103 .expect("Must always have an active range.");
104 let items: HashSet<DomRoot<Node>> = active_range
106 .start_container()
107 .ancestors()
108 .chain(active_range.end_container().ancestors())
109 .filter(|ancestor| ancestor.is::<HTMLLIElement>())
110 .collect();
111
112 for item in items {
114 item.normalize_sublists(cx);
115 }
116
117 active_range = selection
119 .active_range(cx)
120 .expect("Must always have an active range.");
121
122 let new_range = active_range.block_extend(cx, document);
124
125 let mut node_list: Vec<DomRoot<Node>> = vec![];
127
128 let mut contained_nodes = new_range.contained_nodes_rooted();
133 while let Some(node) = contained_nodes.next(cx.no_gc()) {
134 if node.is_editable(cx) &&
135 (is_allowed_child(
136 NodeOrString::Node(UnrootedDom::from_ref(&*node, cx.no_gc())),
137 NodeOrString::String("div".to_owned()),
138 ) || is_allowed_child(
139 NodeOrString::Node(UnrootedDom::from_ref(&*node, cx.no_gc())),
140 NodeOrString::String("ol".to_owned()),
141 )) &&
142 node_list
143 .last()
144 .is_none_or(|last| !last.is_ancestor_of(&node))
145 {
146 node_list.push(node);
147 }
148 }
149
150 if let Some(first_visible_member) = node_list.iter().find(|node| node.is_visible(cx)) &&
152 first_visible_member.is::<HTMLLIElement>() &&
153 let Some(parent) = first_visible_member.GetParentNode() &&
154 (parent.is::<HTMLOListElement>() || parent.is::<HTMLUListElement>())
155 {
156 let mut sibling = first_visible_member.GetPreviousSibling();
158
159 while let Some(ref some_sibling) = sibling &&
161 some_sibling.is_invisible(cx)
162 {
163 sibling = some_sibling.GetPreviousSibling();
164 }
165
166 if let Some(sibling) = sibling &&
168 sibling.is::<HTMLLIElement>()
169 {
170 sibling.normalize_sublists(cx);
171 }
172 }
173
174 let mut node_list_iter = node_list.iter().peekable();
176 while node_list_iter.peek().is_some() {
177 let mut sublist: VecDeque<DomRoot<Node>> = VecDeque::new();
179
180 sublist.push_back(
182 node_list_iter
183 .next()
184 .expect("Must always have a next item")
185 .clone(),
186 );
187
188 while node_list_iter.peek().is_some_and(|node| {
191 sublist
192 .back()
193 .expect("Must always have last element here.")
194 .GetNextSibling()
195 .is_some_and(|next_sibling| &&next_sibling == node)
196 }) {
197 sublist.push_back(
198 node_list_iter
199 .next()
200 .expect("Must always have a next item")
201 .clone(),
202 );
203 }
204
205 indent(cx, document, sublist);
207 }
208
209 true
212}