1use 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#[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 {
80 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 {
94 parent
96 .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
97 .unwrap();
98 node.ranges()
100 .move_to_following_text_sibling_above(node, offset, new_node.upcast());
101 parent.ranges().increment_at(parent, node.index() + 1);
103 }
104 cdata.DeleteData(offset, count).unwrap();
106 Ok(new_node)
108 }
109
110 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 fn GetAssignedSlot(&self) -> Option<DomRoot<HTMLSlotElement>> {
131 let cx = GlobalScope::get_cx();
132
133 rooted!(in(*cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
136 slottable.find_a_slot(true)
137 }
138}