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 {
79 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 owner_doc = node.owner_doc();
89 let new_node = owner_doc.CreateTextNode(cx, new_data);
90 let parent = node.GetParentNode();
92 if let Some(ref parent) = parent {
93 parent
95 .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
96 .unwrap();
97
98 if let Some(weak_ranges) = node.weak_ranges_mut() {
106 weak_ranges.move_to_following_text_sibling_above(node, offset, new_node.upcast());
107 }
108
109 if let Some(parent_weak_ranges) = parent.weak_ranges_mut() {
115 parent_weak_ranges.increment_at(parent, node.index() + 1);
116 }
117 }
118 cdata.DeleteData(cx, offset, count).unwrap();
120 Ok(new_node)
122 }
123
124 fn WholeText(&self, cx: &JSContext) -> DOMString {
126 let first = self
127 .upcast::<Node>()
128 .inclusively_preceding_siblings_unrooted(cx.no_gc())
129 .take_while(|node| node.is::<Text>())
130 .last()
131 .unwrap();
132 let nodes = first
133 .inclusively_following_siblings_unrooted(cx.no_gc())
134 .take_while(|node| node.is::<Text>());
135 let mut text = String::new();
136 for ref node in nodes {
137 let cdata = node.downcast::<CharacterData>().unwrap();
138 text.push_str(&cdata.data());
139 }
140 DOMString::from(text)
141 }
142
143 fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
145 rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
148 slottable.find_a_slot(true)
149 }
150}