script/dom/execcommand/contenteditable/
text.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 script_bindings::inheritance::Castable;
6use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
7
8use crate::dom::bindings::cell::Ref;
9use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
10use crate::dom::characterdata::CharacterData;
11use crate::dom::element::Element;
12use crate::dom::html::htmlbrelement::HTMLBRElement;
13use crate::dom::html::htmlimageelement::HTMLImageElement;
14use crate::dom::node::Node;
15use crate::dom::text::Text;
16
17impl Text {
18    /// <https://dom.spec.whatwg.org/#concept-cd-data>
19    pub(crate) fn data(&self) -> Ref<'_, String> {
20        self.upcast::<CharacterData>().data()
21    }
22
23    /// <https://w3c.github.io/editing/docs/execCommand/#whitespace-node>
24    pub(crate) fn is_whitespace_node(&self) -> bool {
25        // > A whitespace node is either a Text node whose data is the empty string;
26        let data = self.data();
27        if data.is_empty() {
28            return true;
29        }
30        // > or a Text node whose data consists only of one or more tabs (0x0009), line feeds (0x000A),
31        // > carriage returns (0x000D), and/or spaces (0x0020),
32        // > and whose parent is an Element whose resolved value for "white-space" is "normal" or "nowrap";
33        let Some(parent) = self.upcast::<Node>().GetParentElement() else {
34            return false;
35        };
36        // TODO: Optimize the below to only do a traversal once and in the match handle the expected collapse value
37        let Some(style) = parent.style() else {
38            return false;
39        };
40        let white_space_collapse = style.get_inherited_text().white_space_collapse;
41        if data
42            .bytes()
43            .all(|byte| matches!(byte, b'\t' | b'\n' | b'\r' | b' ')) &&
44            // Note that for "normal" and "nowrap", the longhand "white-space-collapse: collapse" applies
45            // https://www.w3.org/TR/css-text-4/#white-space-property
46            white_space_collapse == WhiteSpaceCollapse::Collapse
47        {
48            return true;
49        }
50        // > or a Text node whose data consists only of one or more tabs (0x0009), carriage returns (0x000D),
51        // > and/or spaces (0x0020), and whose parent is an Element whose resolved value for "white-space" is "pre-line".
52        data.bytes()
53            .all(|byte| matches!(byte, b'\t' | b'\r' | b' ')) &&
54            // Note that for "pre-line", the longhand "white-space-collapse: preserve-breaks" applies
55            // https://www.w3.org/TR/css-text-4/#white-space-property
56            white_space_collapse == WhiteSpaceCollapse::PreserveBreaks
57    }
58
59    /// <https://w3c.github.io/editing/docs/execCommand/#collapsed-whitespace-node>
60    pub(crate) fn is_collapsed_whitespace_node(&self) -> bool {
61        // Step 1. If node is not a whitespace node, return false.
62        if !self.is_whitespace_node() {
63            return false;
64        }
65        // Step 2. If node's data is the empty string, return true.
66        if self.data().is_empty() {
67            return true;
68        }
69        // Step 3. Let ancestor be node's parent.
70        let node = self.upcast::<Node>();
71        let Some(ancestor) = node.GetParentNode() else {
72            // Step 4. If ancestor is null, return true.
73            return true;
74        };
75        let mut resolved_ancestor = ancestor.clone();
76        for parent in ancestor.ancestors() {
77            // Step 5. If the "display" property of some ancestor of node has resolved value "none", return true.
78            if parent
79                .downcast::<Element>()
80                .is_some_and(Element::is_display_none)
81            {
82                return true;
83            }
84            // Step 6. While ancestor is not a block node and its parent is not null, set ancestor to its parent.
85            //
86            // Note that the spec is written as "while not". Since this is the end-condition, we need to invert
87            // the condition to decide when to stop.
88            if parent.is_block_node() {
89                break;
90            }
91            resolved_ancestor = parent;
92        }
93        // Step 7. Let reference be node.
94        // Step 8. While reference is a descendant of ancestor:
95        // Step 8.1. Let reference be the node before it in tree order.
96        for reference in node.preceding_nodes(&resolved_ancestor) {
97            // Step 8.2. If reference is a block node or a br, return true.
98            if reference.is_block_node() || reference.is::<HTMLBRElement>() {
99                return true;
100            }
101            // Step 8.3. If reference is a Text node that is not a whitespace node, or is an img, break from this loop.
102            if reference
103                .downcast::<Text>()
104                .is_some_and(|text| !text.is_whitespace_node()) ||
105                reference.is::<HTMLImageElement>()
106            {
107                break;
108            }
109        }
110        // Step 9. Let reference be node.
111        // Step 10. While reference is a descendant of ancestor:
112        // Step 10.1. Let reference be the node after it in tree order, or null if there is no such node.
113        for reference in node.following_nodes(&resolved_ancestor) {
114            // Step 10.2. If reference is a block node or a br, return true.
115            if reference.is_block_node() || reference.is::<HTMLBRElement>() {
116                return true;
117            }
118            // Step 10.3. If reference is a Text node that is not a whitespace node, or is an img, break from this loop.
119            if reference
120                .downcast::<Text>()
121                .is_some_and(|text| !text.is_whitespace_node()) ||
122                reference.is::<HTMLImageElement>()
123            {
124                break;
125            }
126        }
127        // Step 11. Return false.
128        false
129    }
130
131    /// Part of <https://w3c.github.io/editing/docs/execCommand/#canonicalize-whitespace>
132    /// and deduplicated here, since we need to do this for both start and end nodes
133    pub(crate) fn has_whitespace_and_has_parent_with_whitespace_preserve(
134        &self,
135        offset: u32,
136        space_characters: &'static [&'static char],
137    ) -> bool {
138        // if node is a Text node and its parent's resolved value for "white-space" is neither "pre" nor "pre-wrap"
139        // and start offset is not zero and the (start offset − 1)st code unit of start node's data is a space (0x0020) or
140        // non-breaking space (0x00A0)
141        let has_preserve_space = self
142            .upcast::<Node>()
143            .GetParentNode()
144            .and_then(|parent_node| parent_node.downcast::<Element>().and_then(Element::style))
145            .is_some_and(|style| {
146                // Note that for "pre" and "pre-wrap", the longhand "white-space-collapse: preserve" applies
147                // https://www.w3.org/TR/css-text-4/#white-space-property
148                style.get_inherited_text().white_space_collapse != WhiteSpaceCollapse::Preserve
149            });
150        let has_space_character = self
151            .data()
152            .chars()
153            .nth(offset as usize)
154            .is_some_and(|c| space_characters.contains(&&c));
155        has_preserve_space && has_space_character
156    }
157}