1use std::cell::LazyCell;
7
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId, TextTypeId};
11
12use crate::dom::bindings::cell::{DomRefCell, Ref};
13use crate::dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
14use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
15use crate::dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods;
16use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
17use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::root::{DomRoot, LayoutDom};
20use crate::dom::bindings::str::DOMString;
21use crate::dom::cdatasection::CDATASection;
22use crate::dom::comment::Comment;
23use crate::dom::document::Document;
24use crate::dom::element::Element;
25use crate::dom::mutationobserver::{Mutation, MutationObserver};
26use crate::dom::node::{ChildrenMutation, Node, NodeDamage};
27use crate::dom::processinginstruction::ProcessingInstruction;
28use crate::dom::text::Text;
29use crate::dom::virtualmethods::vtable_for;
30
31#[dom_struct]
33pub(crate) struct CharacterData {
34 node: Node,
35 data: DomRefCell<String>,
36}
37
38impl CharacterData {
39 pub(crate) fn new_inherited(data: DOMString, document: &Document) -> CharacterData {
40 CharacterData {
41 node: Node::new_inherited(document),
42 data: DomRefCell::new(String::from(data.str())),
43 }
44 }
45
46 pub(crate) fn clone_with_data(
47 &self,
48 cx: &mut js::context::JSContext,
49 data: DOMString,
50 document: &Document,
51 ) -> DomRoot<Node> {
52 match self.upcast::<Node>().type_id() {
53 NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
54 DomRoot::upcast(Comment::new(cx, data, document, None))
55 },
56 NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
57 let pi = self.downcast::<ProcessingInstruction>().unwrap();
58 DomRoot::upcast(ProcessingInstruction::new(cx, pi.Target(), data, document))
59 },
60 NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => {
61 DomRoot::upcast(CDATASection::new(cx, data, document))
62 },
63 NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
64 DomRoot::upcast(Text::new(cx, data, document))
65 },
66 _ => unreachable!(),
67 }
68 }
69
70 #[inline]
71 pub(crate) fn data(&self) -> Ref<'_, String> {
72 self.data.borrow()
73 }
74
75 #[inline]
76 pub(crate) fn append_data(&self, data: &str) {
77 self.queue_mutation_record();
78 self.data.borrow_mut().push_str(data);
79 self.content_changed();
80 }
81
82 #[expect(unsafe_code)]
83 fn content_changed(&self) {
84 let mut cx = unsafe { script_bindings::script_runtime::temp_cx() };
86 let cx = &mut cx;
87
88 let node = self.upcast::<Node>();
89 node.dirty(NodeDamage::Other);
90
91 if self.is::<Text>() {
95 if let Some(parent_node) = node.GetParentNode() {
96 let mutation = ChildrenMutation::ChangeText;
97 vtable_for(&parent_node).children_changed(cx, &mutation);
98 }
99 }
100 }
101
102 fn queue_mutation_record(&self) {
104 let mutation = LazyCell::new(|| Mutation::CharacterData {
105 old_value: self.data.borrow().clone(),
106 });
107 MutationObserver::queue_a_mutation_record(self.upcast::<Node>(), mutation);
108 }
109}
110
111impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
112 fn Data(&self) -> DOMString {
114 DOMString::from(self.data.borrow().clone())
115 }
116
117 fn SetData(&self, data: DOMString) {
119 self.queue_mutation_record();
120 let old_length = self.Length();
121 let new_length = data.str().encode_utf16().count() as u32;
122 *self.data.borrow_mut() = String::from(data.str());
123 self.content_changed();
124 let node = self.upcast::<Node>();
125 node.ranges()
126 .replace_code_units(node, 0, old_length, new_length);
127 }
128
129 fn Length(&self) -> u32 {
131 self.data.borrow().encode_utf16().count() as u32
132 }
133
134 fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString> {
136 let data = self.data.borrow();
137 let mut substring = String::new();
139 let remaining = match split_at_utf16_code_unit_offset(&data, offset) {
140 Ok((_, astral, s)) => {
141 if astral.is_some() {
145 substring += "\u{FFFD}";
146 }
147 s
148 },
149 Err(()) => return Err(Error::IndexSize(None)),
151 };
152 match split_at_utf16_code_unit_offset(remaining, count) {
153 Err(()) => substring += remaining,
155 Ok((s, astral, _)) => {
157 substring += s;
158 if astral.is_some() {
162 substring += "\u{FFFD}";
163 }
164 },
165 };
166 Ok(DOMString::from(substring))
167 }
168
169 fn AppendData(&self, data: DOMString) {
171 self.append_data(&data.str());
175 }
176
177 fn InsertData(&self, offset: u32, arg: DOMString) -> ErrorResult {
179 self.ReplaceData(offset, 0, arg)
181 }
182
183 fn DeleteData(&self, offset: u32, count: u32) -> ErrorResult {
185 self.ReplaceData(offset, count, DOMString::new())
187 }
188
189 fn ReplaceData(&self, offset: u32, count: u32, arg: DOMString) -> ErrorResult {
191 let mut new_data;
192 {
193 let data = self.data.borrow();
194 let prefix;
195 let replacement_before;
196 let remaining;
197 match split_at_utf16_code_unit_offset(&data, offset) {
198 Ok((p, astral, r)) => {
199 prefix = p;
200 replacement_before = if astral.is_some() { "\u{FFFD}" } else { "" };
204 remaining = r;
205 },
206 Err(()) => return Err(Error::IndexSize(None)),
208 };
209 let replacement_after;
210 let suffix;
211 match split_at_utf16_code_unit_offset(remaining, count) {
212 Err(()) => {
214 replacement_after = "";
215 suffix = "";
216 },
217 Ok((_, astral, s)) => {
218 replacement_after = if astral.is_some() { "\u{FFFD}" } else { "" };
222 suffix = s;
223 },
224 };
225 self.queue_mutation_record();
227
228 new_data = String::with_capacity(
230 prefix.len() +
231 replacement_before.len() +
232 arg.len() +
233 replacement_after.len() +
234 suffix.len(),
235 );
236 new_data.push_str(prefix);
237 new_data.push_str(replacement_before);
238 new_data.push_str(&arg.str());
239 new_data.push_str(replacement_after);
240 new_data.push_str(suffix);
241 }
242 *self.data.borrow_mut() = new_data;
243 self.content_changed();
244 let node = self.upcast::<Node>();
246 node.ranges().replace_code_units(
247 node,
248 offset,
249 count,
250 arg.str().encode_utf16().count() as u32,
251 );
252 Ok(())
253 }
254
255 fn Before(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
257 self.upcast::<Node>().before(cx, nodes)
258 }
259
260 fn After(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
262 self.upcast::<Node>().after(cx, nodes)
263 }
264
265 fn ReplaceWith(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
267 self.upcast::<Node>().replace_with(cx, nodes)
268 }
269
270 fn Remove(&self, cx: &mut JSContext) {
272 self.upcast::<Node>().remove_self(cx);
273 }
274
275 fn GetPreviousElementSibling(&self) -> Option<DomRoot<Element>> {
277 self.upcast::<Node>()
278 .preceding_siblings()
279 .find_map(DomRoot::downcast)
280 }
281
282 fn GetNextElementSibling(&self) -> Option<DomRoot<Element>> {
284 self.upcast::<Node>()
285 .following_siblings()
286 .find_map(DomRoot::downcast)
287 }
288}
289
290pub(crate) trait LayoutCharacterDataHelpers<'dom> {
291 fn data_for_layout(self) -> &'dom str;
292}
293
294impl<'dom> LayoutCharacterDataHelpers<'dom> for LayoutDom<'dom, CharacterData> {
295 #[expect(unsafe_code)]
296 #[inline]
297 fn data_for_layout(self) -> &'dom str {
298 unsafe { self.unsafe_get().data.borrow_for_layout() }
299 }
300}
301
302fn split_at_utf16_code_unit_offset(s: &str, offset: u32) -> Result<(&str, Option<char>, &str), ()> {
317 let mut code_units = 0;
318 for (i, c) in s.char_indices() {
319 if code_units == offset {
320 let (a, b) = s.split_at(i);
321 return Ok((a, None, b));
322 }
323 code_units += 1;
324 if c > '\u{FFFF}' {
325 if code_units == offset {
326 debug_assert_eq!(c.len_utf8(), 4);
327 warn!("Splitting a surrogate pair in CharacterData API.");
328 return Ok((&s[..i], Some(c), &s[i + c.len_utf8()..]));
329 }
330 code_units += 1;
331 }
332 }
333 if code_units == offset {
334 Ok((s, None, ""))
335 } else {
336 Err(())
337 }
338}