script/dom/characterdata/
characterdata.rs1use std::cell::LazyCell;
7
8use atomic_refcell::{AtomicRef, AtomicRefCell};
9use dom_struct::dom_struct;
10use js::context::JSContext;
11use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId, TextTypeId};
12use servo_base::text::Utf16CodeUnits;
13
14use crate::dom::bindings::cell::AtomicSafeBorrowMut;
15use crate::dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
16use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
17use crate::dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods;
18use crate::dom::bindings::codegen::UnionTypes::NodeOrString;
19use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
20use crate::dom::bindings::inheritance::Castable;
21use crate::dom::bindings::root::{DomRoot, LayoutDom};
22use crate::dom::bindings::str::DOMString;
23use crate::dom::cdatasection::CDATASection;
24use crate::dom::comment::Comment;
25use crate::dom::document::Document;
26use crate::dom::element::Element;
27use crate::dom::mutationobserver::{Mutation, MutationObserver};
28use crate::dom::node::virtualmethods::vtable_for;
29use crate::dom::node::{ChildrenMutation, Node, NodeDamage};
30use crate::dom::processinginstruction::ProcessingInstruction;
31use crate::dom::text::Text;
32
33#[dom_struct]
35pub(crate) struct CharacterData {
36 node: Node,
37 #[no_trace]
38 data: AtomicRefCell<String>,
39}
40
41impl CharacterData {
42 pub(crate) fn new_inherited(data: DOMString, document: &Document) -> CharacterData {
43 CharacterData {
44 node: Node::new_inherited(document),
45 data: AtomicRefCell::new(String::from(data)),
46 }
47 }
48
49 pub(crate) fn clone_with_data(
50 &self,
51 cx: &mut js::context::JSContext,
52 data: DOMString,
53 document: &Document,
54 ) -> DomRoot<Node> {
55 match self.upcast::<Node>().type_id() {
56 NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
57 DomRoot::upcast(Comment::new(cx, data, document, None))
58 },
59 NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
60 let pi = self.downcast::<ProcessingInstruction>().unwrap();
61 DomRoot::upcast(ProcessingInstruction::new(cx, pi.Target(), data, document))
62 },
63 NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => {
64 DomRoot::upcast(CDATASection::new(cx, data, document))
65 },
66 NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => {
67 DomRoot::upcast(Text::new(cx, data, document))
68 },
69 _ => unreachable!(),
70 }
71 }
72
73 #[inline]
74 pub(crate) fn data(&self) -> AtomicRef<'_, String> {
75 self.data.borrow()
76 }
77
78 #[inline]
79 pub(crate) fn append_data(&self, cx: &mut JSContext, data: &str) {
80 self.queue_mutation_record(cx);
81 self.data.safe_borrow_mut(cx.no_gc()).push_str(data);
82 self.content_changed(cx);
83 }
84
85 fn content_changed(&self, cx: &mut JSContext) {
86 let node = self.upcast::<Node>();
87 node.dirty(cx.no_gc(), NodeDamage::Other);
88
89 if self.is::<Text>() &&
93 let Some(parent_node) = node.GetParentNode()
94 {
95 let mutation = ChildrenMutation::ChangeText;
96 vtable_for(&parent_node).children_changed(cx, &mutation);
97 }
98 }
99
100 fn queue_mutation_record(&self, cx: &mut JSContext) {
102 let mutation = LazyCell::new(|| Mutation::CharacterData {
103 old_value: self.data.borrow().clone(),
104 });
105 MutationObserver::queue_a_mutation_record(cx, self.upcast::<Node>(), mutation);
106 }
107}
108
109impl CharacterDataMethods<crate::DomTypeHolder> for CharacterData {
110 fn Data(&self) -> DOMString {
112 DOMString::from(self.data.borrow().clone())
113 }
114
115 fn SetData(&self, cx: &mut JSContext, data: DOMString) {
117 self.queue_mutation_record(cx);
118 let old_length = self.Length();
119 let new_length = Utf16CodeUnits::length_of(&data.str()).0 as u32;
120 *self.data.safe_borrow_mut(cx.no_gc()) = String::from(data.str());
121 self.content_changed(cx);
122
123 let node = self.upcast::<Node>();
124 if let Some(weak_ranges) = node.weak_ranges_mut() {
125 weak_ranges.replace_code_units(node, 0, old_length, new_length);
126 }
127 }
128
129 fn Length(&self) -> u32 {
131 Utf16CodeUnits::length_of(&self.data.borrow()).0 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, cx: &mut JSContext, data: DOMString) {
171 self.append_data(cx, &data.str());
175 }
176
177 fn InsertData(&self, cx: &mut JSContext, offset: u32, arg: DOMString) -> ErrorResult {
179 self.ReplaceData(cx, offset, 0, arg)
181 }
182
183 fn DeleteData(&self, cx: &mut JSContext, offset: u32, count: u32) -> ErrorResult {
185 self.ReplaceData(cx, offset, count, DOMString::new())
187 }
188
189 fn ReplaceData(
191 &self,
192 cx: &mut JSContext,
193 offset: u32,
194 count: u32,
195 arg: DOMString,
196 ) -> ErrorResult {
197 let mut new_data;
198 {
199 let data = self.data.borrow();
200 let prefix;
201 let replacement_before;
202 let remaining;
203 match split_at_utf16_code_unit_offset(&data, offset) {
204 Ok((p, astral, r)) => {
205 prefix = p;
206 replacement_before = if astral.is_some() { "\u{FFFD}" } else { "" };
210 remaining = r;
211 },
212 Err(()) => return Err(Error::IndexSize(None)),
214 };
215 let replacement_after;
216 let suffix;
217 match split_at_utf16_code_unit_offset(remaining, count) {
218 Err(()) => {
220 replacement_after = "";
221 suffix = "";
222 },
223 Ok((_, astral, s)) => {
224 replacement_after = if astral.is_some() { "\u{FFFD}" } else { "" };
228 suffix = s;
229 },
230 };
231 self.queue_mutation_record(cx);
233
234 new_data = String::with_capacity(
236 prefix.len() +
237 replacement_before.len() +
238 arg.len() +
239 replacement_after.len() +
240 suffix.len(),
241 );
242 new_data.push_str(prefix);
243 new_data.push_str(replacement_before);
244 new_data.push_str(&arg.str());
245 new_data.push_str(replacement_after);
246 new_data.push_str(suffix);
247 }
248 *self.data.safe_borrow_mut(cx.no_gc()) = new_data;
249 self.content_changed(cx);
250
251 let node = self.upcast::<Node>();
267 if let Some(weak_ranges) = node.weak_ranges_mut() {
268 weak_ranges.replace_code_units(
269 node,
270 offset,
271 count,
272 Utf16CodeUnits::length_of(&arg.str()).0 as u32,
273 );
274 }
275
276 Ok(())
285 }
286
287 fn Before(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
289 self.upcast::<Node>().before(cx, nodes)
290 }
291
292 fn After(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
294 self.upcast::<Node>().after(cx, nodes)
295 }
296
297 fn ReplaceWith(&self, cx: &mut JSContext, nodes: Vec<NodeOrString>) -> ErrorResult {
299 self.upcast::<Node>().replace_with(cx, nodes)
300 }
301
302 fn Remove(&self, cx: &mut JSContext) {
304 self.upcast::<Node>().remove_self(cx);
305 }
306
307 fn GetPreviousElementSibling(&self) -> Option<DomRoot<Element>> {
309 self.upcast::<Node>()
310 .preceding_siblings()
311 .find_map(DomRoot::downcast)
312 }
313
314 fn GetNextElementSibling(&self) -> Option<DomRoot<Element>> {
316 self.upcast::<Node>()
317 .following_siblings()
318 .find_map(DomRoot::downcast)
319 }
320}
321
322impl<'dom> LayoutDom<'dom, CharacterData> {
323 #[inline]
324 pub(crate) fn data_for_layout(self) -> AtomicRef<'dom, str> {
325 AtomicRef::map(self.unsafe_get().data.borrow(), |data| &**data)
326 }
327}
328
329fn split_at_utf16_code_unit_offset(s: &str, offset: u32) -> Result<(&str, Option<char>, &str), ()> {
344 let mut code_units = 0;
345 for (i, c) in s.char_indices() {
346 if code_units == offset {
347 let (a, b) = s.split_at(i);
348 return Ok((a, None, b));
349 }
350 code_units += 1;
351 if c > '\u{FFFF}' {
352 if code_units == offset {
353 debug_assert_eq!(c.len_utf8(), 4);
354 warn!("Splitting a surrogate pair in CharacterData API.");
355 return Ok((&s[..i], Some(c), &s[i + c.len_utf8()..]));
356 }
357 code_units += 1;
358 }
359 }
360 if code_units == offset {
361 Ok((s, None, ""))
362 } else {
363 Err(())
364 }
365}