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::live_range_text_split_steps;
22use crate::dom::node::Node;
23use crate::dom::window::Window;
24
25/// An HTML text node.
26#[dom_struct]
27pub(crate) struct Text {
28    characterdata: CharacterData,
29}
30
31impl Text {
32    pub(crate) fn new_inherited(text: DOMString, document: &Document) -> Text {
33        Text {
34            characterdata: CharacterData::new_inherited(text, document),
35        }
36    }
37
38    pub(crate) fn new(
39        cx: &mut js::context::JSContext,
40        text: DOMString,
41        document: &Document,
42    ) -> DomRoot<Text> {
43        Self::new_with_proto(cx, text, document, None)
44    }
45
46    fn new_with_proto(
47        cx: &mut js::context::JSContext,
48        text: DOMString,
49        document: &Document,
50        proto: Option<HandleObject>,
51    ) -> DomRoot<Text> {
52        Node::reflect_node_with_proto(
53            cx,
54            Box::new(Text::new_inherited(text, document)),
55            document,
56            proto,
57        )
58    }
59}
60
61impl TextMethods<crate::DomTypeHolder> for Text {
62    /// <https://dom.spec.whatwg.org/#dom-text-text>
63    fn Constructor(
64        cx: &mut js::context::JSContext,
65        window: &Window,
66        proto: Option<HandleObject>,
67        text: DOMString,
68    ) -> Fallible<DomRoot<Text>> {
69        let document = window.Document();
70        Ok(Text::new_with_proto(cx, text, &document, proto))
71    }
72
73    // https://dom.spec.whatwg.org/#dom-text-splittext
74    /// <https://dom.spec.whatwg.org/#concept-text-split>
75    fn SplitText(&self, cx: &mut JSContext, offset: u32) -> Fallible<DomRoot<Text>> {
76        let cdata = self.upcast::<CharacterData>();
77        // Step 1: Let length be node’s length.
78        let length = cdata.Length();
79        // Step 2: If offset is greater than length, then throw an "IndexSizeError" DOMException.
80        if offset > length {
81            return Err(Error::IndexSize(None));
82        }
83        // Step 3: Let count be length − offset.
84        let count = length - offset;
85        // Step 4: Let newData be the result of substringing data of node with offset and count.
86        let new_data = cdata.SubstringData(offset, count).unwrap();
87        // Step 5: Let newNode be the result of creating a text node given node’s node document and newData.
88        let node = self.upcast::<Node>();
89        let owner_doc = node.owner_doc();
90        let new_node = owner_doc.CreateTextNode(cx, new_data);
91        // Step 6: Let parent be node’s parent.
92        let parent = node.GetParentNode();
93        // Step 7: If parent is non-null:
94        if let Some(ref parent) = parent {
95            // Step 7.1: Insert newNode into parent before node’s next sibling.
96            parent
97                .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
98                .unwrap();
99            // Steps 7.2-7.5: The live range update steps.
100            live_range_text_split_steps(parent, node, offset, new_node.upcast());
101        }
102        // Step 8.
103        cdata.DeleteData(cx, offset, count).unwrap();
104        // Step 9.
105        Ok(new_node)
106    }
107
108    /// <https://dom.spec.whatwg.org/#dom-text-wholetext>
109    fn WholeText(&self, cx: &JSContext) -> DOMString {
110        let first = self
111            .upcast::<Node>()
112            .inclusively_preceding_siblings_unrooted(cx.no_gc())
113            .take_while(|node| node.is::<Text>())
114            .last()
115            .unwrap();
116        let nodes = first
117            .inclusively_following_siblings_unrooted(cx.no_gc())
118            .take_while(|node| node.is::<Text>());
119        let mut text = String::new();
120        for ref node in nodes {
121            let cdata = node.downcast::<CharacterData>().unwrap();
122            text.push_str(&cdata.data());
123        }
124        DOMString::from(text)
125    }
126
127    /// <https://dom.spec.whatwg.org/#dom-slotable-assignedslot>
128    fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
129        // > The assignedSlot getter steps are to return the result of
130        // > find a slot given this and with the open flag set.
131        rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
132        slottable.find_a_slot(cx.no_gc(), true)
133    }
134}