/* 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 dom_struct::dom_struct; use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId, TextTypeId}; use crate::dom::bindings::cell::{DomRefCell, Ref}; 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::{ChildrenMutation, Node, NodeDamage}; use crate::dom::processinginstruction::ProcessingInstruction; use crate::dom::text::Text; use crate::dom::virtualmethods::vtable_for; use crate::script_runtime::CanGc; // https://dom.spec.whatwg.org/#characterdata #[dom_struct] pub(crate) struct CharacterData { node: Node, data: DomRefCell, } impl CharacterData { pub(crate) fn new_inherited(data: DOMString, document: &Document) -> CharacterData { CharacterData { node: Node::new_inherited(document), data: DomRefCell::new(data), } } pub(crate) fn clone_with_data( &self, data: DOMString, document: &Document, can_gc: CanGc, ) -> DomRoot { match self.upcast::().type_id() { NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => { DomRoot::upcast(Comment::new(data, document, None, can_gc)) }, NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => { let pi = self.downcast::().unwrap(); DomRoot::upcast(ProcessingInstruction::new( pi.Target(), data, document, can_gc, )) }, NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::CDATASection)) => { DomRoot::upcast(CDATASection::new(data, document, can_gc)) }, NodeTypeId::CharacterData(CharacterDataTypeId::Text(TextTypeId::Text)) => { DomRoot::upcast(Text::new(data, document, can_gc)) }, _ => unreachable!(), } } #[inline] pub(crate) fn data(&self) -> Ref { self.data.borrow() } #[inline] pub(crate) fn append_data(&self, data: &str) { self.queue_mutation_record(); self.data.borrow_mut().push_str(data); self.content_changed(); } fn content_changed(&self) { let node = self.upcast::(); node.dirty(NodeDamage::OtherNodeDamage); // If this is a Text node, we might need to re-parse (say, if our parent // is a