/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! DOM bindings for `CharacterData`. use std::cell::LazyCell; use atomic_refcell::{AtomicRef, AtomicRefCell}; use dom_struct::dom_struct; use js::context::JSContext; use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId, TextTypeId}; use servo_base::text::{AssumeUnder4GB, RangeAny, Utf16CodeUnits, Utf32CodeUnits}; use crate::dom::bindings::cell::AtomicSafeBorrowMut; use crate::dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods; use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods; use crate::dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods; use crate::dom::bindings::codegen::UnionTypes::NodeOrString; use crate::dom::bindings::error::{Error, ErrorResult, Fallible}; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::root::{DomRoot, LayoutDom}; use crate::dom::bindings::str::DOMString; use crate::dom::cdatasection::CDATASection; use crate::dom::comment::Comment; use crate::dom::document::Document; use crate::dom::element::Element; use crate::dom::mutationobserver::{Mutation, MutationObserver}; use crate::dom::node::virtualmethods::vtable_for; use crate::dom::node::{ChildrenMutation, Node, NodeDamage}; use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::text::Text; // https://dom.spec.whatwg.org/#characterdata #[dom_struct] pub(crate) struct CharacterData { node: Node, #[no_trace] data: AtomicRefCell, } impl CharacterData { pub(crate) fn new_inherited(data: DOMString, document: &Document) -> CharacterData { CharacterData { node: Node::new_inherited(document), data: AtomicRefCell::new(String::from(data)), } } pub(crate) fn clone_with_data( &self, cx: &mut js::context::JSContext, data: DOMString, document: &Document, ) -> DomRoot { match self.upcast::().type_id() { NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => { DomRoot::upcast(Comment::new(cx, data, document, None)) }, NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => { let pi = self.downcast::().unwrap(); DomRoot::upcast(ProcessingInstruction::new(cx, pi.Target(), data, document)) }, NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => { DomRoot::upcast(CDATASection::new(cx, data, document)) }, NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => { DomRoot::upcast(Text::new(cx, data, document)) }, _ => unreachable!(), } } #[inline] pub(crate) fn data(&self) -> AtomicRef<'_, String> { self.data.borrow() } #[inline] pub(crate) fn append_data(&self, cx: &mut JSContext, data: &str) { self.queue_mutation_record(cx); self.data.safe_borrow_mut(cx.no_gc()).push_str(data); self.content_changed(cx); } fn content_changed(&self, cx: &mut JSContext) { let node = self.upcast::(); node.dirty(cx.no_gc(), NodeDamage::Other); // If this is a Text node, we might need to re-parse (say, if our parent // is a