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