script/dom/
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::globalscope::GlobalScope;
21use crate::dom::html::htmlslotelement::{HTMLSlotElement, Slottable};
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.
78        let length = cdata.Length();
79        if offset > length {
80            // Step 2.
81            return Err(Error::IndexSize(None));
82        }
83        // Step 3.
84        let count = length - offset;
85        // Step 4.
86        let new_data = cdata.SubstringData(offset, count).unwrap();
87        // Step 5.
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.
92        let parent = node.GetParentNode();
93        if let Some(ref parent) = parent {
94            // Step 7.1.
95            parent
96                .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
97                .unwrap();
98            // Steps 7.2-3.
99            node.ranges()
100                .move_to_following_text_sibling_above(node, offset, new_node.upcast());
101            // Steps 7.4-5.
102            parent.ranges().increment_at(parent, node.index() + 1);
103        }
104        // Step 8.
105        cdata.DeleteData(offset, count).unwrap();
106        // Step 9.
107        Ok(new_node)
108    }
109
110    /// <https://dom.spec.whatwg.org/#dom-text-wholetext>
111    fn WholeText(&self, cx: &JSContext) -> DOMString {
112        let first = self
113            .upcast::<Node>()
114            .inclusively_preceding_siblings_unrooted(cx.no_gc())
115            .take_while(|node| node.is::<Text>())
116            .last()
117            .unwrap();
118        let nodes = first
119            .inclusively_following_siblings_unrooted(cx.no_gc())
120            .take_while(|node| node.is::<Text>());
121        let mut text = String::new();
122        for ref node in nodes {
123            let cdata = node.downcast::<CharacterData>().unwrap();
124            text.push_str(&cdata.data());
125        }
126        DOMString::from(text)
127    }
128
129    /// <https://dom.spec.whatwg.org/#dom-slotable-assignedslot>
130    fn GetAssignedSlot(&self) -> Option<DomRoot<HTMLSlotElement>> {
131        let cx = GlobalScope::get_cx();
132
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(true)
137    }
138}