Introduce CharacterData::clone_with_data()

This commit is contained in:
Anthony Ramine 2015-11-13 13:21:31 +01:00
parent 7e1e34e7d5
commit 1dd7c8cf01
3 changed files with 35 additions and 33 deletions

View file

@ -6,13 +6,18 @@
use dom::bindings::cell::DOMRefCell; use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods; use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
use dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods;
use dom::bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
use dom::bindings::codegen::UnionTypes::NodeOrString; use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
use dom::bindings::js::{LayoutJS, Root}; use dom::bindings::js::{LayoutJS, Root};
use dom::comment::Comment;
use dom::document::Document; use dom::document::Document;
use dom::element::Element; use dom::element::Element;
use dom::node::{Node, NodeDamage}; use dom::node::{Node, NodeDamage};
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use std::cell::Ref; use std::cell::Ref;
use util::str::DOMString; use util::str::DOMString;
@ -31,6 +36,22 @@ impl CharacterData {
} }
} }
pub fn clone_with_data(&self, data: DOMString, document: &Document) -> Root<Node> {
match self.upcast::<Node>().type_id() {
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
Root::upcast(Comment::new(data, &document))
}
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
let pi = self.downcast::<ProcessingInstruction>().unwrap();
Root::upcast(ProcessingInstruction::new(pi.Target(), data, &document))
},
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
Root::upcast(Text::new(data, &document))
},
_ => unreachable!(),
}
}
#[inline] #[inline]
pub fn data(&self) -> Ref<DOMString> { pub fn data(&self) -> Ref<DOMString> {
self.data.borrow() self.data.borrow()

View file

@ -33,7 +33,6 @@ use dom::bindings::trace::JSTraceable;
use dom::bindings::trace::RootedVec; use dom::bindings::trace::RootedVec;
use dom::bindings::xmlname::namespace_from_domstring; use dom::bindings::xmlname::namespace_from_domstring;
use dom::characterdata::CharacterData; use dom::characterdata::CharacterData;
use dom::comment::Comment;
use dom::document::{Document, DocumentSource, IsHTMLDocument}; use dom::document::{Document, DocumentSource, IsHTMLDocument};
use dom::documentfragment::DocumentFragment; use dom::documentfragment::DocumentFragment;
use dom::documenttype::DocumentType; use dom::documenttype::DocumentType;
@ -1610,10 +1609,9 @@ impl Node {
let doc_fragment = DocumentFragment::new(document.r()); let doc_fragment = DocumentFragment::new(document.r());
Root::upcast::<Node>(doc_fragment) Root::upcast::<Node>(doc_fragment)
}, },
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => { NodeTypeId::CharacterData(_) => {
let cdata = node.downcast::<CharacterData>().unwrap(); let cdata = node.downcast::<CharacterData>().unwrap();
let comment = Comment::new(cdata.Data(), document.r()); cdata.clone_with_data(cdata.Data(), &document)
Root::upcast::<Node>(comment)
}, },
NodeTypeId::Document(_) => { NodeTypeId::Document(_) => {
let document = node.downcast::<Document>().unwrap(); let document = node.downcast::<Document>().unwrap();
@ -1639,17 +1637,6 @@ impl Node {
document.r(), ElementCreator::ScriptCreated); document.r(), ElementCreator::ScriptCreated);
Root::upcast::<Node>(element) Root::upcast::<Node>(element)
}, },
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
let cdata = node.downcast::<CharacterData>().unwrap();
let text = Text::new(cdata.Data(), document.r());
Root::upcast::<Node>(text)
},
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
let pi = node.downcast::<ProcessingInstruction>().unwrap();
let pi = ProcessingInstruction::new(pi.Target(),
pi.upcast::<CharacterData>().Data(), document.r());
Root::upcast::<Node>(pi)
},
}; };
// Step 3. // Step 3.

View file

@ -410,12 +410,10 @@ impl RangeMethods for Range {
} }
if end_node == start_node { if end_node == start_node {
if let Some(text) = start_node.downcast::<CharacterData>() { if let Some(cdata) = start_node.downcast::<CharacterData>() {
// Step 4.1. // Steps 4.1-2.
let clone = start_node.CloneNode(true); let data = cdata.SubstringData(start_offset, end_offset - start_offset).unwrap();
// Step 4.2. let clone = cdata.clone_with_data(data, &start_node.owner_doc());
let text = text.SubstringData(start_offset, end_offset - start_offset);
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
// Step 4.3. // Step 4.3.
try!(fragment.upcast::<Node>().AppendChild(&clone)); try!(fragment.upcast::<Node>().AppendChild(&clone));
// Step 4.4 // Step 4.4
@ -429,13 +427,11 @@ impl RangeMethods for Range {
if let Some(child) = first_contained_child { if let Some(child) = first_contained_child {
// Step 13. // Step 13.
if let Some(text) = child.downcast::<CharacterData>() { if let Some(cdata) = child.downcast::<CharacterData>() {
assert!(child == start_node); assert!(child == start_node);
// Step 13.1. // Steps 13.1-2.
let clone = start_node.CloneNode(true); // CharacterData has no children. let data = cdata.SubstringData(start_offset, start_node.len() - start_offset).unwrap();
// Step 13.2 let clone = cdata.clone_with_data(data, &start_node.owner_doc());
let text = text.SubstringData(start_offset, start_node.len() - start_offset);
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
// Step 13.3. // Step 13.3.
try!(fragment.upcast::<Node>().AppendChild(&clone)); try!(fragment.upcast::<Node>().AppendChild(&clone));
} else { } else {
@ -466,13 +462,11 @@ impl RangeMethods for Range {
if let Some(child) = last_contained_child { if let Some(child) = last_contained_child {
// Step 16. // Step 16.
if let Some(text) = child.downcast::<CharacterData>() { if let Some(cdata) = child.downcast::<CharacterData>() {
assert!(child == end_node); assert!(child == end_node);
// Step 16.1. // Steps 16.1-2.
let clone = end_node.CloneNode(true); // CharacterData has no children. let data = cdata.SubstringData(0, end_offset).unwrap();
// Step 16.2. let clone = cdata.clone_with_data(data, &start_node.owner_doc());
let text = text.SubstringData(0, end_offset);
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
// Step 16.3. // Step 16.3.
try!(fragment.upcast::<Node>().AppendChild(&clone)); try!(fragment.upcast::<Node>().AppendChild(&clone));
} else { } else {