1use js::context::JSContext;
6use script_bindings::inheritance::Castable;
7
8use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
9use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
10use crate::dom::bindings::codegen::Bindings::RangeBinding::RangeMethods;
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::DOMString;
13use crate::dom::document::Document;
14use crate::dom::event::Event;
15use crate::dom::event::inputevent::InputEvent;
16use crate::dom::execcommand::basecommand::CommandName;
17use crate::dom::execcommand::commands::fontsize::maybe_normalize_pixels;
18use crate::dom::html::htmlelement::HTMLElement;
19use crate::dom::node::Node;
20use crate::dom::selection::Selection;
21
22fn is_command_listed_in_miscellaneous_section(command_name: CommandName) -> bool {
24 matches!(
25 command_name,
26 CommandName::DefaultParagraphSeparator |
27 CommandName::Redo |
28 CommandName::SelectAll |
29 CommandName::StyleWithCss |
30 CommandName::Undo |
31 CommandName::Usecss
32 )
33}
34
35fn mapped_value_of_command(command: CommandName) -> DOMString {
37 match command {
38 CommandName::BackColor => "formatBackColor",
39 CommandName::Bold => "formatBold",
40 CommandName::CreateLink => "insertLink",
41 CommandName::Cut => "deleteByCut",
42 CommandName::Delete => "deleteContentBackward",
43 CommandName::FontName => "formatFontName",
44 CommandName::ForeColor => "formatFontColor",
45 CommandName::ForwardDelete => "deleteContentForward",
46 CommandName::Indent => "formatIndent",
47 CommandName::InsertHorizontalRule => "insertHorizontalRule",
48 CommandName::InsertLineBreak => "insertLineBreak",
49 CommandName::InsertOrderedList => "insertOrderedList",
50 CommandName::InsertParagraph => "insertParagraph",
51 CommandName::InsertText => "insertText",
52 CommandName::InsertUnorderedList => "insertUnorderedList",
53 CommandName::JustifyCenter => "formatJustifyCenter",
54 CommandName::JustifyFull => "formatJustifyFull",
55 CommandName::JustifyLeft => "formatJustifyLeft",
56 CommandName::JustifyRight => "formatJustifyRight",
57 CommandName::Outdent => "formatOutdent",
58 CommandName::Paste => "insertFromPaste",
59 CommandName::Redo => "historyRedo",
60 CommandName::Strikethrough => "formatStrikeThrough",
61 CommandName::Superscript => "formatSuperscript",
62 CommandName::Undo => "historyUndo",
63 _ => "",
64 }
65 .into()
66}
67
68impl Node {
69 fn is_in_plaintext_only_state(&self) -> bool {
70 self.downcast::<HTMLElement>()
71 .is_some_and(|el| el.ContentEditable().str() == "plaintext-only")
72 }
73}
74
75impl Document {
76 fn selection_if_command_is_enabled(
78 &self,
79 cx: &mut JSContext,
80 command_name: CommandName,
81 ) -> Option<DomRoot<Selection>> {
82 let selection = self.GetSelection(cx)?;
83 if is_command_listed_in_miscellaneous_section(command_name) {
88 return Some(selection);
89 }
90 let range = selection.active_range()?;
92 let start_container_editing_host = range.start_container().editing_host_of()?;
94 let end_container_editing_host = range.end_container().editing_host_of()?;
98 if !command_name.is_enabled_in_plaintext_only_state() &&
105 (start_container_editing_host.is_in_plaintext_only_state() ||
106 end_container_editing_host.is_in_plaintext_only_state())
107 {
108 None
109 } else {
110 Some(selection)
111 }
112 }
113
114 fn command_if_command_is_supported(&self, command_id: &DOMString) -> Option<CommandName> {
116 Some(match &*command_id.str().to_lowercase() {
119 "backcolor" => CommandName::BackColor,
120 "bold" => CommandName::Bold,
121 "createlink" => CommandName::CreateLink,
122 "delete" => CommandName::Delete,
123 "defaultparagraphseparator" => CommandName::DefaultParagraphSeparator,
124 "fontname" => CommandName::FontName,
125 "fontsize" => CommandName::FontSize,
126 "forecolor" => CommandName::ForeColor,
127 "hilitecolor" => CommandName::HiliteColor,
128 "insertparagraph" => CommandName::InsertParagraph,
129 "italic" => CommandName::Italic,
130 "removeformat" => CommandName::RemoveFormat,
131 "strikethrough" => CommandName::Strikethrough,
132 "stylewithcss" => CommandName::StyleWithCss,
133 "subscript" => CommandName::Subscript,
134 "superscript" => CommandName::Superscript,
135 "underline" => CommandName::Underline,
136 "unlink" => CommandName::Unlink,
137 _ => return None,
138 })
139 }
140}
141
142pub(crate) trait DocumentExecCommandSupport {
143 fn is_command_supported(&self, command_id: DOMString) -> bool;
144 fn is_command_indeterminate(&self, cx: &mut JSContext, command_id: DOMString) -> bool;
145 fn command_state_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> bool;
146 fn command_value_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> DOMString;
147 fn check_support_and_enabled(
148 &self,
149 cx: &mut JSContext,
150 command_id: &DOMString,
151 ) -> Option<(CommandName, DomRoot<Selection>)>;
152 fn exec_command_for_command_id(
153 &self,
154 cx: &mut JSContext,
155 command_id: DOMString,
156 value: DOMString,
157 ) -> bool;
158}
159
160impl DocumentExecCommandSupport for Document {
161 fn is_command_supported(&self, command_id: DOMString) -> bool {
163 self.command_if_command_is_supported(&command_id).is_some()
164 }
165
166 fn is_command_indeterminate(&self, cx: &mut JSContext, command_id: DOMString) -> bool {
168 self.command_if_command_is_supported(&command_id)
171 .is_some_and(|command| command.is_indeterminate(cx, self))
172 }
173
174 fn command_state_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> bool {
176 let Some(command) = self.command_if_command_is_supported(&command_id) else {
178 return false;
179 };
180 let Some(state) = command.current_state(cx, self) else {
181 return false;
182 };
183 self.state_override(&command).unwrap_or(state)
186 }
187
188 fn command_value_for_command(&self, cx: &mut JSContext, command_id: DOMString) -> DOMString {
190 let Some(command) = self.command_if_command_is_supported(&command_id) else {
192 return DOMString::new();
193 };
194 let Some(value) = command.current_value(cx, self) else {
195 return DOMString::new();
196 };
197 self.value_override(&command)
199 .map(|value_override| {
200 if command == CommandName::FontSize {
203 maybe_normalize_pixels(&value_override, self).unwrap_or(value_override)
204 } else {
205 value_override
206 }
207 })
208 .unwrap_or(value)
210 }
211
212 fn check_support_and_enabled(
214 &self,
215 cx: &mut JSContext,
216 command_id: &DOMString,
217 ) -> Option<(CommandName, DomRoot<Selection>)> {
218 let command = self.command_if_command_is_supported(command_id)?;
220 let selection = self.selection_if_command_is_enabled(cx, command)?;
221 Some((command, selection))
222 }
223
224 fn exec_command_for_command_id(
226 &self,
227 cx: &mut JSContext,
228 command_id: DOMString,
229 value: DOMString,
230 ) -> bool {
231 let window = self.window();
232 let Some((command, mut selection)) = self.check_support_and_enabled(cx, &command_id) else {
234 return false;
235 };
236 let affected_editing_host = if !is_command_listed_in_miscellaneous_section(command) {
238 let Some(affected_editing_host) = selection
242 .active_range()
243 .expect("Must always have an active range")
244 .CommonAncestorContainer()
245 .editing_host_of()
246 else {
247 return false;
248 };
249
250 let event = InputEvent::new(
253 cx,
254 window,
255 None,
256 atom!("beforeinput"),
257 true,
258 true,
259 Some(window),
260 0,
261 None,
262 false,
263 "".into(),
264 );
265 let event = event.upcast::<Event>();
266 if !event.fire(cx, affected_editing_host.upcast()) {
268 return false;
269 }
270
271 let Some(new_selection) = self.selection_if_command_is_enabled(cx, command) else {
273 return false;
274 };
275 selection = new_selection;
276
277 selection
281 .active_range()
282 .expect("Must always have an active range")
283 .CommonAncestorContainer()
284 .editing_host_of()
285 } else {
286 None
287 };
288
289 let result = command.execute(cx, self, &selection, value);
291 if !result {
293 return false;
294 }
295 if let Some(affected_editing_host) = affected_editing_host {
299 let event = InputEvent::new(
300 cx,
301 window,
302 None,
303 atom!("input"),
304 true,
305 false,
306 Some(window),
307 0,
308 None,
309 false,
310 mapped_value_of_command(command),
311 );
312 let event = event.upcast::<Event>();
313 event.set_trusted(true);
314 event.fire(cx, affected_editing_host.upcast());
315 }
316
317 true
319 }
320}