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::live_range_text_split_steps;
22use crate::dom::node::Node;
23use crate::dom::window::Window;
24
25#[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 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 fn SplitText(&self, cx: &mut JSContext, offset: u32) -> Fallible<DomRoot<Text>> {
76 let cdata = self.upcast::<CharacterData>();
77 let length = cdata.Length();
79 if offset > length {
81 return Err(Error::IndexSize(None));
82 }
83 let count = length - offset;
85 let new_data = cdata.SubstringData(offset, count).unwrap();
87 let node = self.upcast::<Node>();
89 let owner_doc = node.owner_doc();
90 let new_node = owner_doc.CreateTextNode(cx, new_data);
91 let parent = node.GetParentNode();
93 if let Some(ref parent) = parent {
95 parent
97 .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
98 .unwrap();
99 live_range_text_split_steps(parent, node, offset, new_node.upcast());
101 }
102 cdata.DeleteData(cx, offset, count).unwrap();
104 Ok(new_node)
106 }
107
108 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 fn GetAssignedSlot(&self, cx: &JSContext) -> Option<DomRoot<HTMLSlotElement>> {
129 rooted!(&in(cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
132 slottable.find_a_slot(cx.no_gc(), true)
133 }
134}