script/dom/characterdata/
text.rs1use 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#[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 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 fn SplitText(&self, cx: &mut JSContext, offset: u32) -> Fallible<DomRoot<Text>> {
75 let cdata = self.upcast::<CharacterData>();
76 let length = cdata.Length();
78 if offset > length {
80 return Err(Error::IndexSize(None));
81 }
82 let count = length - offset;
84 let new_data = cdata.SubstringData(offset, count).unwrap();
86 let node = self.upcast::<Node>();
88 let document = node.owner_doc();
89 let new_text_node = document.CreateTextNode(cx, new_data);
90 let parent = node.GetParentNode();
92 if let Some(ref parent) = parent {
94 let new_node = new_text_node.upcast();
96 parent
97 .InsertBefore(cx, new_node, node.GetNextSibling().as_deref())
98 .unwrap();
99
100 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 cdata.DeleteData(cx, offset, count).unwrap();
108 Ok(new_text_node)
110 }
111
112 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 fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
133 rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
136 slottable.find_a_slot(cx.no_gc(), true)
137 }
138}