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: Let length be node’s length.
77        let length = cdata.Length();
78        // Step 2: If offset is greater than length, then throw an "IndexSizeError" DOMException.
79        if offset > length {
80            return Err(Error::IndexSize(None));
81        }
82        // Step 3: Let count be length − offset.
83        let count = length - offset;
84        // Step 4: Let newData be the result of substringing data of node with offset and count.
85        let new_data = cdata.SubstringData(offset, count).unwrap();
86        // Step 5: Let newNode be the result of creating a text node given node’s node document and newData.
87        let node = self.upcast::<Node>();
88        let document = node.owner_doc();
89        let new_text_node = document.CreateTextNode(cx, new_data);
90        // Step 6: Let parent be node’s parent.
91        let parent = node.GetParentNode();
92        // Step 7: If parent is non-null:
93        if let Some(ref parent) = parent {
94            // Step 7.1: Insert newNode into parent before node’s next sibling.
95            let new_node = new_text_node.upcast();
96            parent
97                .InsertBefore(cx, new_node, node.GetNextSibling().as_deref())
98                .unwrap();
99
100            // Steps 7.2-7.5: The live range update steps.
101            if let Some(selection) = document.selection() {
102                selection.text_split_steps(node, offset, parent, new_node);
103            }
104            document.live_range_text_split_steps(cx.no_gc(), parent, node, offset, new_node);
105        }
106        // Step 8.
107        cdata.DeleteData(cx, offset, count).unwrap();
108        // Step 9.
109        Ok(new_text_node)
110    }
111
112    /// <https://dom.spec.whatwg.org/#dom-text-wholetext>
113    fn WholeText(&self, cx: &JSContext) -> DOMString {
114        let first = self
115            .upcast::<Node>()
116            .inclusively_preceding_siblings_unrooted(cx.no_gc())
117            .take_while(|node| node.is::<Text>())
118            .last()
119            .unwrap();
120        let nodes = first
121            .inclusively_following_siblings_unrooted(cx.no_gc())
122            .take_while(|node| node.is::<Text>());
123        let mut text = String::new();
124        for ref node in nodes {
125            let cdata = node.downcast::<CharacterData>().unwrap();
126            text.push_str(&cdata.data());
127        }
128        DOMString::from(text)
129    }
130
131    /// <https://dom.spec.whatwg.org/#dom-slotable-assignedslot>
132    fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
133        // > The assignedSlot getter steps are to return the result of
134        // > find a slot given this and with the open flag set.
135        rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
136        slottable.find_a_slot(cx.no_gc(), true)
137    }
138}