script/dom/execcommand/contenteditable/
element.rs1use html5ever::{LocalName, local_name, ns};
6use js::context::JSContext;
7use script_bindings::inheritance::Castable;
8use style::attr::AttrValue;
9use style::properties::{LonghandId, PropertyDeclaration, PropertyDeclarationId, ShorthandId};
10use style::values::specified::TextDecorationLine;
11use style::values::specified::box_::DisplayOutside;
12
13use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
14use crate::dom::bindings::inheritance::{ElementTypeId, HTMLElementTypeId, NodeTypeId};
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::element::Element;
18use crate::dom::execcommand::basecommand::{CommandName, CssPropertyName};
19use crate::dom::execcommand::commands::fontsize::font_size_to_css_font;
20use crate::dom::execcommand::contenteditable::node::move_preserving_ranges;
21use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
22use crate::dom::html::htmlfontelement::HTMLFontElement;
23use crate::dom::node::node::{Node, NodeTraits};
24
25impl Element {
26 pub(crate) fn resolved_display_value(&self, cx: &mut JSContext) -> Option<DisplayOutside> {
27 self.style(cx)
28 .map(|style| style.get_box().display.outside())
29 }
30
31 pub(crate) fn specified_command_value(
33 &self,
34 cx: &mut JSContext,
35 command: &CommandName,
36 ) -> Option<DOMString> {
37 match command {
38 CommandName::BackColor | CommandName::HiliteColor
40 if self
41 .resolved_display_value(cx)
42 .is_none_or(|display| display != DisplayOutside::Inline) =>
43 {
44 return None;
45 },
46 CommandName::CreateLink | CommandName::Unlink => {
48 if let Some(anchor) = self.downcast::<HTMLAnchorElement>() {
51 return anchor
52 .upcast::<Element>()
53 .get_attribute_string_value(&local_name!("href"))
54 .map(|value| value.into());
55 }
56
57 return None;
59 },
60 CommandName::Subscript | CommandName::Superscript => {
62 if matches!(*self.local_name(), local_name!("sup")) {
64 return Some(DOMString::from_static("superscript"));
65 }
66 if matches!(*self.local_name(), local_name!("sub")) {
68 return Some(DOMString::from_static("subscript"));
69 }
70 return None;
72 },
73 CommandName::Strikethrough => {
74 if let Some(value) = CssPropertyName::TextDecorationLine.value_set_for_style(self) {
76 return value
79 .contains("line-through")
80 .then_some(DOMString::from_static("line-through"));
81 }
82 if matches!(*self.local_name(), local_name!("s") | local_name!("strike")) {
84 return Some(DOMString::from_static("line-through"));
85 }
86 },
87 CommandName::Underline => {
88 if let Some(value) = CssPropertyName::TextDecorationLine.value_set_for_style(self) {
90 return value
93 .contains("underline")
94 .then_some(DOMString::from_static("underline"));
95 }
96 if *self.local_name() == local_name!("u") {
98 return Some(DOMString::from_static("underline"));
99 }
100 },
101 _ => {},
102 };
103 let property = command.relevant_css_property()?;
106 if let Some(value) = property.value_set_for_style(self) {
109 return Some(value);
110 }
111 if self.is::<HTMLFontElement>() &&
114 let Some(font_size) = self
115 .with_attribute(&ns!(), &local_name!("size"), |attribute| {
116 if let AttrValue::UInt(_, value) = *attribute.value() {
117 Some(value)
118 } else {
119 None
120 }
121 })
122 .flatten()
123 {
124 return Some(font_size_to_css_font(&font_size).into());
125 }
126
127 let element_name = self.local_name();
130 match property {
131 CssPropertyName::FontWeight
132 if element_name == &local_name!("b") || element_name == &local_name!("strong") =>
133 {
134 Some(DOMString::from_static("bold"))
135 },
136 CssPropertyName::FontStyle
137 if element_name == &local_name!("i") || element_name == &local_name!("em") =>
138 {
139 Some(DOMString::from_static("italic"))
140 },
141 _ => None,
143 }
144 }
145
146 pub(crate) fn is_modifiable_element(&self) -> bool {
148 let attrs = self.attrs().borrow();
149 let mut attrs = attrs.iter();
150 let type_id = self.upcast::<Node>().type_id();
151
152 if matches!(
155 type_id,
156 NodeTypeId::Element(ElementTypeId::HTMLElement(
157 HTMLElementTypeId::HTMLSpanElement,
158 ))
159 ) || matches!(
160 *self.local_name(),
161 local_name!("b") |
162 local_name!("em") |
163 local_name!("i") |
164 local_name!("s") |
165 local_name!("strike") |
166 local_name!("strong") |
167 local_name!("sub") |
168 local_name!("sup") |
169 local_name!("u")
170 ) {
171 return attrs.all(|attr| attr.local_name() == &local_name!("style"));
172 }
173
174 if matches!(
176 type_id,
177 NodeTypeId::Element(ElementTypeId::HTMLElement(
178 HTMLElementTypeId::HTMLFontElement,
179 ))
180 ) {
181 return attrs.all(|attr| {
182 matches!(
183 *attr.local_name(),
184 local_name!("style") |
185 local_name!("color") |
186 local_name!("face") |
187 local_name!("size")
188 )
189 });
190 }
191
192 if matches!(
194 type_id,
195 NodeTypeId::Element(ElementTypeId::HTMLElement(
196 HTMLElementTypeId::HTMLAnchorElement,
197 ))
198 ) {
199 return attrs.all(|attr| {
200 matches!(
201 *attr.local_name(),
202 local_name!("style") | local_name!("href")
203 )
204 });
205 }
206
207 false
208 }
209
210 pub(crate) fn is_indentation_element(&self) -> bool {
212 if !matches!(
214 *self.local_name(),
215 local_name!("blockquote") | local_name!("div")
216 ) {
217 return false;
218 }
219
220 let style_attribute = self.style_attribute().borrow();
222 let Some(declarations) = style_attribute.as_ref() else {
223 return false;
224 };
225 let document = self.owner_document();
226 let shared_lock = document.style_shared_author_lock();
227 let read_lock = shared_lock.read();
228 let style = declarations.read_with(&read_lock);
229
230 ShorthandId::Margin
231 .longhands()
232 .any(|longhand| style.contains(PropertyDeclarationId::Longhand(longhand)))
233 }
234
235 pub(crate) fn has_empty_style_attribute(&self) -> bool {
236 let style_attribute = self.style_attribute().borrow();
237 style_attribute.as_ref().is_some_and(|declarations| {
238 let document = self.owner_document();
239 let shared_lock = document.style_shared_author_lock();
240 let read_lock = shared_lock.read();
241 let style = declarations.read_with(&read_lock);
242
243 style.is_empty()
244 })
245 }
246
247 pub(crate) fn is_non_list_single_line_container(&self) -> bool {
249 matches!(
252 *self.local_name(),
253 local_name!("address") |
254 local_name!("div") |
255 local_name!("h1") |
256 local_name!("h2") |
257 local_name!("h3") |
258 local_name!("h4") |
259 local_name!("h5") |
260 local_name!("h6") |
261 local_name!("listing") |
262 local_name!("p") |
263 local_name!("pre") |
264 local_name!("xmp")
265 )
266 }
267
268 pub(crate) fn is_simple_modifiable_element(&self) -> bool {
270 let attrs = self.attrs().borrow();
271 let attr_count = attrs.len();
272 let type_id = self.upcast::<Node>().type_id();
273
274 if matches!(
275 type_id,
276 NodeTypeId::Element(ElementTypeId::HTMLElement(
277 HTMLElementTypeId::HTMLAnchorElement,
278 )) | NodeTypeId::Element(ElementTypeId::HTMLElement(
279 HTMLElementTypeId::HTMLFontElement,
280 )) | NodeTypeId::Element(ElementTypeId::HTMLElement(
281 HTMLElementTypeId::HTMLSpanElement,
282 ))
283 ) || matches!(
284 *self.local_name(),
285 local_name!("b") |
286 local_name!("em") |
287 local_name!("i") |
288 local_name!("s") |
289 local_name!("strike") |
290 local_name!("strong") |
291 local_name!("sub") |
292 local_name!("sup") |
293 local_name!("u")
294 ) {
295 if attr_count == 0 {
297 return true;
298 }
299
300 if attr_count == 1 &&
304 attrs.first().expect("Size is 1").local_name() == &local_name!("style") &&
305 self.has_empty_style_attribute()
306 {
307 return true;
308 }
309 }
310
311 if attr_count != 1 {
312 return false;
313 }
314
315 let first_attr = attrs.first().expect("Size is 1");
316 let only_attribute = first_attr.local_name();
317
318 if matches!(
320 type_id,
321 NodeTypeId::Element(ElementTypeId::HTMLElement(
322 HTMLElementTypeId::HTMLAnchorElement,
323 ))
324 ) {
325 return only_attribute == &local_name!("href");
326 }
327
328 if matches!(
330 type_id,
331 NodeTypeId::Element(ElementTypeId::HTMLElement(
332 HTMLElementTypeId::HTMLFontElement,
333 ))
334 ) {
335 return only_attribute == &local_name!("color") ||
336 only_attribute == &local_name!("face") ||
337 only_attribute == &local_name!("size");
338 }
339
340 if only_attribute != &local_name!("style") {
341 return false;
342 }
343 let style_attribute = self.style_attribute().borrow();
344 let Some(declarations) = style_attribute.as_ref() else {
345 return false;
346 };
347 let document = self.owner_document();
348 let shared_lock = document.style_shared_author_lock();
349 let read_lock = shared_lock.read();
350 let style = declarations.read_with(&read_lock);
351
352 if matches!(*self.local_name(), local_name!("b") | local_name!("strong")) {
356 return style.len() == 1 &&
357 style.contains(PropertyDeclarationId::Longhand(LonghandId::FontWeight));
358 }
359
360 if matches!(*self.local_name(), local_name!("i") | local_name!("em")) {
364 return style.len() == 1 &&
365 style.contains(PropertyDeclarationId::Longhand(LonghandId::FontStyle));
366 }
367
368 let a_font_or_span = matches!(
369 type_id,
370 NodeTypeId::Element(ElementTypeId::HTMLElement(
371 HTMLElementTypeId::HTMLAnchorElement,
372 )) | NodeTypeId::Element(ElementTypeId::HTMLElement(
373 HTMLElementTypeId::HTMLFontElement,
374 )) | NodeTypeId::Element(ElementTypeId::HTMLElement(
375 HTMLElementTypeId::HTMLSpanElement,
376 ))
377 );
378 let s_strike_or_u = matches!(
379 *self.local_name(),
380 local_name!("s") | local_name!("strike") | local_name!("u")
381 );
382 if a_font_or_span || s_strike_or_u {
383 if style.len() == ShorthandId::TextDecoration.longhands().count() &&
386 style
387 .shorthand_to_css(ShorthandId::TextDecoration, &mut String::new())
388 .is_ok()
389 {
390 if let Some((text_decoration, _)) = style.get(PropertyDeclarationId::Longhand(
391 LonghandId::TextDecorationLine,
392 )) {
393 return matches!(
398 text_decoration,
399 PropertyDeclaration::TextDecorationLine(
400 TextDecorationLine::LINE_THROUGH |
401 TextDecorationLine::UNDERLINE |
402 TextDecorationLine::OVERLINE |
403 TextDecorationLine::NONE
404 )
405 );
406 }
407 } else if a_font_or_span {
408 return style.len() == 1;
412 }
413 }
414
415 false
416 }
417
418 pub(crate) fn is_simple_indentation_element(&self) -> bool {
419 if !self.is_indentation_element() {
421 return false;
422 }
423
424 let attrs = self.attrs().borrow();
426 let mut attrs = attrs.iter();
427 attrs.all(|attr| {
428 if matches!(*attr.local_name(), local_name!("style")) {
430 let style_attribute = self.style_attribute().borrow();
431 let Some(declarations) = style_attribute.as_ref() else {
432 return false;
433 };
434 let document = self.owner_document();
435 let shared_lock = document.style_shared_author_lock();
436 let read_lock = shared_lock.read();
437 let style = declarations.read_with(&read_lock);
438
439 let properties: Vec<_> = ShorthandId::Margin
440 .longhands()
441 .chain(ShorthandId::Border.longhands())
442 .chain(ShorthandId::Padding.longhands())
443 .collect();
444 return style.declarations().iter().all(|declaration| {
445 declaration
446 .id()
447 .as_longhand()
448 .is_some_and(|longhand| properties.contains(&longhand))
449 });
450 }
451
452 if matches!(*attr.local_name(), local_name!("dir")) {
454 return true;
455 }
456
457 false
458 })
459 }
460
461 pub(crate) fn set_the_tag_name(&self, cx: &mut JSContext, new_name: &str) -> DomRoot<Node> {
463 if self.local_name() == &LocalName::from(new_name) {
465 return DomRoot::upcast(DomRoot::from_ref(self));
466 }
467 let node = self.upcast::<Node>();
469 let Some(parent) = node.GetParentNode() else {
470 return DomRoot::upcast(DomRoot::from_ref(self));
471 };
472 let document = node.owner_document();
474 let replacement = document.create_element(cx, new_name);
475 let replacement_node = replacement.upcast::<Node>();
476 if parent
478 .InsertBefore(cx, replacement_node, Some(node))
479 .is_err()
480 {
481 unreachable!("Must always be able to insert");
482 }
483 self.copy_all_attributes_to_other_element(cx, &replacement);
485 for child in node.children() {
487 move_preserving_ranges(cx, &child, |cx| replacement_node.AppendChild(cx, &child));
488 }
489 node.remove_self(cx);
491 DomRoot::upcast(replacement)
493 }
494}