1use cssparser::match_ignore_ascii_case;
6use js::context::JSContext;
7use script_bindings::inheritance::Castable;
8
9use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
10use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
11use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
12use crate::dom::bindings::codegen::Bindings::RangeBinding::RangeMethods;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::comment::Comment;
16use crate::dom::document::Document;
17use crate::dom::event::Event;
18use crate::dom::event::inputevent::{InputEvent, InputEventFlags};
19use crate::dom::execcommand::basecommand::CommandName;
20use crate::dom::execcommand::commands::fontsize::maybe_normalize_pixels;
21use crate::dom::html::htmlelement::HTMLElement;
22use crate::dom::node::Node;
23use crate::dom::processinginstruction::ProcessingInstruction;
24use crate::dom::selection::Selection;
25
26fn is_command_listed_in_miscellaneous_section(command_name: CommandName) -> bool {
28 matches!(
29 command_name,
30 CommandName::DefaultParagraphSeparator |
31 CommandName::Redo |
32 CommandName::SelectAll |
33 CommandName::StyleWithCss |
34 CommandName::Undo |
35 CommandName::Usecss
36 )
37}
38
39fn bump_selection_out_of_invalid_node(cx: &mut JSContext, selection: &Selection) -> Result<(), ()> {
40 let active_range = selection
46 .active_range(cx)
47 .expect("Must always have an active range");
48 if let start_container = active_range.start_container() &&
49 (start_container.is::<Comment>() || start_container.is::<ProcessingInstruction>())
50 {
51 let Some(parent) = start_container.GetParentNode() else {
52 return Err(());
53 };
54 let _ = active_range.SetStart(cx.no_gc(), &parent, start_container.index());
55 }
56 if let end_container = active_range.end_container() &&
57 (end_container.is::<Comment>() || end_container.is::<ProcessingInstruction>())
58 {
59 let Some(parent) = end_container.GetParentNode() else {
60 return Err(());
61 };
62 let _ = active_range.SetEnd(cx.no_gc(), &parent, end_container.index());
63 }
64 Ok(())
65}
66
67fn mapped_value_of_command(command: CommandName) -> DOMString {
69 match command {
70 CommandName::BackColor => "formatBackColor",
71 CommandName::Bold => "formatBold",
72 CommandName::CreateLink => "insertLink",
73 CommandName::Cut => "deleteByCut",
74 CommandName::Delete => "deleteContentBackward",
75 CommandName::FontName => "formatFontName",
76 CommandName::ForeColor => "formatFontColor",
77 CommandName::ForwardDelete => "deleteContentForward",
78 CommandName::Indent => "formatIndent",
79 CommandName::InsertHorizontalRule => "insertHorizontalRule",
80 CommandName::InsertLineBreak => "insertLineBreak",
81 CommandName::InsertOrderedList => "insertOrderedList",
82 CommandName::InsertParagraph => "insertParagraph",
83 CommandName::InsertText => "insertText",
84 CommandName::InsertUnorderedList => "insertUnorderedList",
85 CommandName::JustifyCenter => "formatJustifyCenter",
86 CommandName::JustifyFull => "formatJustifyFull",
87 CommandName::JustifyLeft => "formatJustifyLeft",
88 CommandName::JustifyRight => "formatJustifyRight",
89 CommandName::Outdent => "formatOutdent",
90 CommandName::Paste => "insertFromPaste",
91 CommandName::Redo => "historyRedo",
92 CommandName::Strikethrough => "formatStrikeThrough",
93 CommandName::Superscript => "formatSuperscript",
94 CommandName::Undo => "historyUndo",
95 _ => "",
96 }
97 .into()
98}
99
100impl Node {
101 fn is_in_plaintext_only_state(&self) -> bool {
102 self.downcast::<HTMLElement>()
103 .is_some_and(|el| el.ContentEditable().str() == "plaintext-only")
104 }
105}
106
107impl Document {
108 fn selection_if_command_is_enabled(
110 &self,
111 cx: &mut JSContext,
112 command_name: CommandName,
113 ) -> Option<DomRoot<Selection>> {
114 let selection = self.GetSelection(cx)?;
115 if is_command_listed_in_miscellaneous_section(command_name) {
120 return Some(selection);
121 }
122 let range = selection.active_range(cx)?;
124 let start_container_editing_host = range.start_container().editing_host_of()?;
126 let end_container_editing_host = range.end_container().editing_host_of()?;
130 if !command_name.is_enabled(cx, &range, &start_container_editing_host) {
136 return None;
137 }
138
139 if !command_name.is_enabled_in_plaintext_only_state() &&
141 (start_container_editing_host.is_in_plaintext_only_state() ||
142 end_container_editing_host.is_in_plaintext_only_state())
143 {
144 None
145 } else {
146 Some(selection)
147 }
148 }
149
150 fn command_if_command_is_supported(&self, command_id: &DOMString) -> Option<CommandName> {
152 Some(match_ignore_ascii_case! { &command_id.str(),
155 "backcolor" => CommandName::BackColor,
156 "bold" => CommandName::Bold,
157 "createlink" => CommandName::CreateLink,
158 "delete" => CommandName::Delete,
159 "defaultparagraphseparator" => CommandName::DefaultParagraphSeparator,
160 "fontname" => CommandName::FontName,
161 "fontsize" => CommandName::FontSize,
162 "forecolor" => CommandName::ForeColor,
163 "forwarddelete" => CommandName::ForwardDelete,
164 "hilitecolor" => CommandName::HiliteColor,
165 "indent" => CommandName::Indent,
166 "inserthorizontalrule" => CommandName::InsertHorizontalRule,
167 "insertimage" => CommandName::InsertImage,
168 "insertlinebreak" => CommandName::InsertLineBreak,
169 "insertparagraph" => CommandName::InsertParagraph,
170 "inserttext" => CommandName::InsertText,
171 "italic" => CommandName::Italic,
172 "outdent" => CommandName::Outdent,
173 "removeformat" => CommandName::RemoveFormat,
174 "strikethrough" => CommandName::Strikethrough,
175 "stylewithcss" => CommandName::StyleWithCss,
176 "subscript" => CommandName::Subscript,
177 "superscript" => CommandName::Superscript,
178 "underline" => CommandName::Underline,
179 "unlink" => CommandName::Unlink,
180 _ => return None,
181 })
182 }
183}
184
185pub(crate) trait DocumentExecCommandSupport {
186 fn is_command_supported(&self, command_id: DOMString) -> bool;
187 fn is_command_indeterminate(&self, cx: &mut JSContext, command_id: DOMString) -> bool;
188 fn command_state_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> bool;
189 fn command_value_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> DOMString;
190 fn check_support_and_enabled(
191 &self,
192 cx: &mut JSContext,
193 command_id: &DOMString,
194 ) -> Option<(CommandName, DomRoot<Selection>)>;
195 fn exec_command_for_command_id(
196 &self,
197 cx: &mut JSContext,
198 command_id: DOMString,
199 value: DOMString,
200 ) -> bool;
201}
202
203impl DocumentExecCommandSupport for Document {
204 fn is_command_supported(&self, command_id: DOMString) -> bool {
206 self.command_if_command_is_supported(&command_id).is_some()
207 }
208
209 fn is_command_indeterminate(&self, cx: &mut JSContext, command_id: DOMString) -> bool {
211 self.command_if_command_is_supported(&command_id)
214 .is_some_and(|command| command.is_indeterminate(cx, self))
215 }
216
217 fn command_state_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> bool {
219 let Some(command) = self.command_if_command_is_supported(&command_id) else {
221 return false;
222 };
223 let Some(state) = command.current_state(cx, self) else {
224 return false;
225 };
226 self.state_override(&command).unwrap_or(state)
229 }
230
231 fn command_value_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> DOMString {
233 let Some(command) = self.command_if_command_is_supported(&command_id) else {
235 return DOMString::new();
236 };
237 let Some(value) = command.current_value(cx, self) else {
238 return DOMString::new();
239 };
240 self.value_override(&command)
242 .map(|value_override| {
243 if command == CommandName::FontSize {
246 maybe_normalize_pixels(&value_override, self).unwrap_or(value_override)
247 } else {
248 value_override
249 }
250 })
251 .unwrap_or(value)
253 }
254
255 fn check_support_and_enabled(
257 &self,
258 cx: &mut JSContext,
259 command_id: &DOMString,
260 ) -> Option<(CommandName, DomRoot<Selection>)> {
261 let command = self.command_if_command_is_supported(command_id)?;
263 let selection = self.selection_if_command_is_enabled(cx, command)?;
264 Some((command, selection))
265 }
266
267 fn exec_command_for_command_id(
269 &self,
270 cx: &mut JSContext,
271 command_id: DOMString,
272 value: DOMString,
273 ) -> bool {
274 let window = self.window();
275 let Some((command, mut selection)) = self.check_support_and_enabled(cx, &command_id) else {
277 return false;
278 };
279 let affected_editing_host = if !is_command_listed_in_miscellaneous_section(command) {
281 let Some(affected_editing_host) = selection
285 .active_range(cx)
286 .expect("Must always have an active range")
287 .CommonAncestorContainer()
288 .editing_host_of()
289 else {
290 return false;
291 };
292
293 let event = InputEvent::new(
296 cx,
297 window,
298 None,
299 atom!("beforeinput"),
300 InputEventFlags::BUBBLES | InputEventFlags::CANCELABLE,
301 Some(window),
302 0,
303 None,
304 mapped_value_of_command(command),
305 );
306 let event = event.upcast::<Event>();
307 if !event.fire(cx, affected_editing_host.upcast()) {
309 return false;
310 }
311
312 let Some(new_selection) = self.selection_if_command_is_enabled(cx, command) else {
314 return false;
315 };
316 selection = new_selection;
317
318 selection
322 .active_range(cx)
323 .expect("Must always have an active range")
324 .CommonAncestorContainer()
325 .editing_host_of()
326 } else {
327 None
328 };
329
330 if affected_editing_host.is_some() &&
331 bump_selection_out_of_invalid_node(cx, &selection).is_err()
332 {
333 return false;
334 }
335
336 let result = command.execute(cx, self, &selection, value);
338 if !result {
340 return false;
341 }
342 if let Some(affected_editing_host) = affected_editing_host {
346 let event = InputEvent::new(
347 cx,
348 window,
349 None,
350 atom!("input"),
351 InputEventFlags::BUBBLES,
352 Some(window),
353 0,
354 None,
355 mapped_value_of_command(command),
356 );
357 let event = event.upcast::<Event>();
358 event.set_trusted(true);
359 event.fire(cx, affected_editing_host.upcast());
360 }
361
362 true
364 }
365}