mirror of
https://github.com/servo/servo.git
synced 2025-06-21 07:38:59 +01:00
Introduce CharacterData::clone_with_data()
This commit is contained in:
parent
7e1e34e7d5
commit
1dd7c8cf01
3 changed files with 35 additions and 33 deletions
|
@ -6,13 +6,18 @@
|
|||
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
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::error::{Error, ErrorResult, Fallible};
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::{LayoutJS, Root};
|
||||
use dom::comment::Comment;
|
||||
use dom::document::Document;
|
||||
use dom::element::Element;
|
||||
use dom::node::{Node, NodeDamage};
|
||||
use dom::processinginstruction::ProcessingInstruction;
|
||||
use dom::text::Text;
|
||||
use std::cell::Ref;
|
||||
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]
|
||||
pub fn data(&self) -> Ref<DOMString> {
|
||||
self.data.borrow()
|
||||
|
|
|
@ -33,7 +33,6 @@ use dom::bindings::trace::JSTraceable;
|
|||
use dom::bindings::trace::RootedVec;
|
||||
use dom::bindings::xmlname::namespace_from_domstring;
|
||||
use dom::characterdata::CharacterData;
|
||||
use dom::comment::Comment;
|
||||
use dom::document::{Document, DocumentSource, IsHTMLDocument};
|
||||
use dom::documentfragment::DocumentFragment;
|
||||
use dom::documenttype::DocumentType;
|
||||
|
@ -1610,10 +1609,9 @@ impl Node {
|
|||
let doc_fragment = DocumentFragment::new(document.r());
|
||||
Root::upcast::<Node>(doc_fragment)
|
||||
},
|
||||
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
|
||||
NodeTypeId::CharacterData(_) => {
|
||||
let cdata = node.downcast::<CharacterData>().unwrap();
|
||||
let comment = Comment::new(cdata.Data(), document.r());
|
||||
Root::upcast::<Node>(comment)
|
||||
cdata.clone_with_data(cdata.Data(), &document)
|
||||
},
|
||||
NodeTypeId::Document(_) => {
|
||||
let document = node.downcast::<Document>().unwrap();
|
||||
|
@ -1639,17 +1637,6 @@ impl Node {
|
|||
document.r(), ElementCreator::ScriptCreated);
|
||||
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.
|
||||
|
|
|
@ -410,12 +410,10 @@ impl RangeMethods for Range {
|
|||
}
|
||||
|
||||
if end_node == start_node {
|
||||
if let Some(text) = start_node.downcast::<CharacterData>() {
|
||||
// Step 4.1.
|
||||
let clone = start_node.CloneNode(true);
|
||||
// Step 4.2.
|
||||
let text = text.SubstringData(start_offset, end_offset - start_offset);
|
||||
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
|
||||
if let Some(cdata) = start_node.downcast::<CharacterData>() {
|
||||
// Steps 4.1-2.
|
||||
let data = cdata.SubstringData(start_offset, end_offset - start_offset).unwrap();
|
||||
let clone = cdata.clone_with_data(data, &start_node.owner_doc());
|
||||
// Step 4.3.
|
||||
try!(fragment.upcast::<Node>().AppendChild(&clone));
|
||||
// Step 4.4
|
||||
|
@ -429,13 +427,11 @@ impl RangeMethods for Range {
|
|||
|
||||
if let Some(child) = first_contained_child {
|
||||
// Step 13.
|
||||
if let Some(text) = child.downcast::<CharacterData>() {
|
||||
if let Some(cdata) = child.downcast::<CharacterData>() {
|
||||
assert!(child == start_node);
|
||||
// Step 13.1.
|
||||
let clone = start_node.CloneNode(true); // CharacterData has no children.
|
||||
// Step 13.2
|
||||
let text = text.SubstringData(start_offset, start_node.len() - start_offset);
|
||||
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
|
||||
// Steps 13.1-2.
|
||||
let data = cdata.SubstringData(start_offset, start_node.len() - start_offset).unwrap();
|
||||
let clone = cdata.clone_with_data(data, &start_node.owner_doc());
|
||||
// Step 13.3.
|
||||
try!(fragment.upcast::<Node>().AppendChild(&clone));
|
||||
} else {
|
||||
|
@ -466,13 +462,11 @@ impl RangeMethods for Range {
|
|||
|
||||
if let Some(child) = last_contained_child {
|
||||
// Step 16.
|
||||
if let Some(text) = child.downcast::<CharacterData>() {
|
||||
if let Some(cdata) = child.downcast::<CharacterData>() {
|
||||
assert!(child == end_node);
|
||||
// Step 16.1.
|
||||
let clone = end_node.CloneNode(true); // CharacterData has no children.
|
||||
// Step 16.2.
|
||||
let text = text.SubstringData(0, end_offset);
|
||||
clone.downcast::<CharacterData>().unwrap().SetData(text.unwrap());
|
||||
// Steps 16.1-2.
|
||||
let data = cdata.SubstringData(0, end_offset).unwrap();
|
||||
let clone = cdata.clone_with_data(data, &start_node.owner_doc());
|
||||
// Step 16.3.
|
||||
try!(fragment.upcast::<Node>().AppendChild(&clone));
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue