Skip to main content

script/dom/characterdata/
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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8
9use crate::dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
10use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
11use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
12use crate::dom::bindings::codegen::Bindings::TextBinding::TextMethods;
13use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
14use crate::dom::bindings::error::{Error, Fallible};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::characterdata::CharacterData;
19use crate::dom::document::Document;
20use crate::dom::html::htmlslotelement::{HTMLSlotElement, Slottable};
21use crate::dom::node::Node;
22use crate::dom::window::Window;
23
24/// An HTML text node.
25#[dom_struct]
26pub(crate) struct Text {
27    characterdata: CharacterData,
28}
29
30impl Text {
31    pub(crate) fn new_inherited(text: DOMString, document: &Document) -> Text {
32        Text {
33            characterdata: CharacterData::new_inherited(text, document),
34        }
35    }
36
37    pub(crate) fn new(
38        cx: &mut js::context::JSContext,
39        text: DOMString,
40        document: &Document,
41    ) -> DomRoot<Text> {
42        Self::new_with_proto(cx, text, document, None)
43    }
44
45    fn new_with_proto(
46        cx: &mut js::context::JSContext,
47        text: DOMString,
48        document: &Document,
49        proto: Option<HandleObject>,
50    ) -> DomRoot<Text> {
51        Node::reflect_node_with_proto(
52            cx,
53            Box::new(Text::new_inherited(text, document)),
54            document,
55            proto,
56        )
57    }
58}
59
60impl TextMethods<crate::DomTypeHolder> for Text {
61    /// <https://dom.spec.whatwg.org/#dom-text-text>
62    fn Constructor(
63        cx: &mut js::context::JSContext,
64        window: &Window,
65        proto: Option<HandleObject>,
66        text: DOMString,
67    ) -> Fallible<DomRoot<Text>> {
68        let document = window.Document();
69        Ok(Text::new_with_proto(cx, text, &document, proto))
70    }
71
72    // https://dom.spec.whatwg.org/#dom-text-splittext
73    /// <https://dom.spec.whatwg.org/#concept-text-split>
74    fn SplitText(&self, cx: &mut JSContext, offset: u32) -> Fallible<DomRoot<Text>> {
75        let cdata = self.upcast::<CharacterData>();
76        // Step 1.
77        let length = cdata.Length();
78        if offset > length {
79            // Step 2.
80            return Err(Error::IndexSize(None));
81        }
82        // Step 3.
83        let count = length - offset;
84        // Step 4.
85        let new_data = cdata.SubstringData(offset, count).unwrap();
86        // Step 5.
87        let node = self.upcast::<Node>();
88        let owner_doc = node.owner_doc();
89        let new_node = owner_doc.CreateTextNode(cx, new_data);
90        // Step 6.
91        let parent = node.GetParentNode();
92        if let Some(ref parent) = parent {
93            // Step 7.1.
94            parent
95                .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
96                .unwrap();
97
98            // Step 7.2. For each live range whose start node is node and start offset is
99            // greater than offset, set its start node to newNode and decrease its start
100            // offset by offset.
101            //
102            // Step 7.3. For each live range whose end node is node and end offset is
103            // greater than offset, set its end node to newNode and decrease its end
104            // offset by offset.
105            if let Some(weak_ranges) = node.weak_ranges_mut() {
106                weak_ranges.move_to_following_text_sibling_above(node, offset, new_node.upcast());
107            }
108
109            // Step 7.4. For each live range whose start node is parent and start offset
110            // is equal to the index of node plus 1, increase its start offset by 1.
111            //
112            // Step 7.5. For each live range whose end node is parent and end offset is
113            // equal to the index of node plus 1, increase its end offset by 1.
114            if let Some(parent_weak_ranges) = parent.weak_ranges_mut() {
115                parent_weak_ranges.increment_at(parent, node.index() + 1);
116            }
117        }
118        // Step 8.
119        cdata.DeleteData(cx, offset, count).unwrap();
120        // Step 9.
121        Ok(new_node)
122    }
123
124    /// <https://dom.spec.whatwg.org/#dom-text-wholetext>
125    fn WholeText(&self, cx: &JSContext) -> DOMString {
126        let first = self
127            .upcast::<Node>()
128            .inclusively_preceding_siblings_unrooted(cx.no_gc())
129            .take_while(|node| node.is::<Text>())
130            .last()
131            .unwrap();
132        let nodes = first
133            .inclusively_following_siblings_unrooted(cx.no_gc())
134            .take_while(|node| node.is::<Text>());
135        let mut text = String::new();
136        for ref node in nodes {
137            let cdata = node.downcast::<CharacterData>().unwrap();
138            text.push_str(&cdata.data());
139        }
140        DOMString::from(text)
141    }
142
143    /// <https://dom.spec.whatwg.org/#dom-slotable-assignedslot>
144    fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
145        // > The assignedSlot getter steps are to return the result of
146        // > find a slot given this and with the open flag set.
147        rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
148        slottable.find_a_slot(true)
149    }
150}