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;
24use crate::script_runtime::CanGc;
25
26#[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 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 fn SplitText(&self, cx: &mut JSContext, offset: u32) -> Fallible<DomRoot<Text>> {
73 let cdata = self.upcast::<CharacterData>();
74 let length = cdata.Length();
76 if offset > length {
77 return Err(Error::IndexSize(None));
79 }
80 let count = length - offset;
82 let new_data = cdata.SubstringData(offset, count).unwrap();
84 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 let parent = node.GetParentNode();
90 if let Some(ref parent) = parent {
91 parent
93 .InsertBefore(cx, new_node.upcast(), node.GetNextSibling().as_deref())
94 .unwrap();
95 node.ranges()
97 .move_to_following_text_sibling_above(node, offset, new_node.upcast());
98 parent.ranges().increment_at(parent, node.index() + 1);
100 }
101 cdata.DeleteData(offset, count).unwrap();
103 Ok(new_node)
105 }
106
107 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 fn GetAssignedSlot(&self) -> Option<DomRoot<HTMLSlotElement>> {
128 let cx = GlobalScope::get_cx();
129
130 rooted!(in(*cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
133 slottable.find_a_slot(true)
134 }
135}