Use internal mutability for CharacterData::data.

This commit is contained in:
Ms2ger 2014-06-10 15:07:33 +02:00
parent d230077f9f
commit 288e0bc877
6 changed files with 46 additions and 45 deletions

View file

@ -188,7 +188,7 @@ impl<'ln> TLayoutNode for LayoutNode<'ln> {
fail!("not text!") fail!("not text!")
} }
let text: JS<Text> = self.get_jsmanaged().transmute_copy(); let text: JS<Text> = self.get_jsmanaged().transmute_copy();
(*text.unsafe_get()).characterdata.data.to_str() (*text.unsafe_get()).characterdata.data.deref().borrow().clone()
} }
} }
} }
@ -494,7 +494,7 @@ impl<'ln> TLayoutNode for ThreadSafeLayoutNode<'ln> {
fail!("not text!") fail!("not text!")
} }
let text: JS<Text> = self.get_jsmanaged().transmute_copy(); let text: JS<Text> = self.get_jsmanaged().transmute_copy();
(*text.unsafe_get()).characterdata.data.to_str() (*text.unsafe_get()).characterdata.data.deref().borrow().clone()
} }
} }
} }
@ -636,7 +636,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
Some(TextNodeTypeId) => { Some(TextNodeTypeId) => {
unsafe { unsafe {
let text: JS<Text> = self.get_jsmanaged().transmute_copy(); let text: JS<Text> = self.get_jsmanaged().transmute_copy();
if !is_whitespace((*text.unsafe_get()).characterdata.data.as_slice()) { if !is_whitespace((*text.unsafe_get()).characterdata.data.deref().borrow().as_slice()) {
return false return false
} }

View file

@ -5,18 +5,21 @@
//! DOM bindings for `CharacterData`. //! DOM bindings for `CharacterData`.
use dom::bindings::codegen::InheritTypes::{CharacterDataDerived, NodeCast}; use dom::bindings::codegen::InheritTypes::{CharacterDataDerived, NodeCast};
use dom::bindings::js::JSRef;
use dom::bindings::error::{Fallible, ErrorResult, IndexSize}; use dom::bindings::error::{Fallible, ErrorResult, IndexSize};
use dom::bindings::js::JSRef;
use dom::bindings::trace::Untraceable;
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::Document; use dom::document::Document;
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::node::{CommentNodeTypeId, Node, NodeTypeId, TextNodeTypeId, ProcessingInstructionNodeTypeId, NodeHelpers}; use dom::node::{CommentNodeTypeId, Node, NodeTypeId, TextNodeTypeId, ProcessingInstructionNodeTypeId, NodeHelpers};
use servo_util::str::DOMString; use servo_util::str::DOMString;
use std::cell::RefCell;
#[deriving(Encodable)] #[deriving(Encodable)]
pub struct CharacterData { pub struct CharacterData {
pub node: Node, pub node: Node,
pub data: DOMString, pub data: Untraceable<RefCell<DOMString>>,
} }
impl CharacterDataDerived for EventTarget { impl CharacterDataDerived for EventTarget {
@ -34,56 +37,56 @@ impl CharacterData {
pub fn new_inherited(id: NodeTypeId, data: DOMString, document: &JSRef<Document>) -> CharacterData { pub fn new_inherited(id: NodeTypeId, data: DOMString, document: &JSRef<Document>) -> CharacterData {
CharacterData { CharacterData {
node: Node::new_inherited(id, document), node: Node::new_inherited(id, document),
data: data data: Untraceable::new(RefCell::new(data)),
} }
} }
} }
pub trait CharacterDataMethods { pub trait CharacterDataMethods {
fn Data(&self) -> DOMString; fn Data(&self) -> DOMString;
fn SetData(&mut self, arg: DOMString) -> ErrorResult; fn SetData(&self, arg: DOMString) -> ErrorResult;
fn Length(&self) -> u32; fn Length(&self) -> u32;
fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString>; fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString>;
fn AppendData(&mut self, arg: DOMString) -> ErrorResult; fn AppendData(&self, arg: DOMString) -> ErrorResult;
fn InsertData(&mut self, _offset: u32, _arg: DOMString) -> ErrorResult; fn InsertData(&self, _offset: u32, _arg: DOMString) -> ErrorResult;
fn DeleteData(&mut self, _offset: u32, _count: u32) -> ErrorResult; fn DeleteData(&self, _offset: u32, _count: u32) -> ErrorResult;
fn ReplaceData(&mut self, _offset: u32, _count: u32, _arg: DOMString) -> ErrorResult; fn ReplaceData(&self, _offset: u32, _count: u32, _arg: DOMString) -> ErrorResult;
fn Remove(&self); fn Remove(&self);
} }
impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> { impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
fn Data(&self) -> DOMString { fn Data(&self) -> DOMString {
self.data.clone() self.data.deref().borrow().clone()
} }
fn SetData(&mut self, arg: DOMString) -> ErrorResult { fn SetData(&self, arg: DOMString) -> ErrorResult {
self.data = arg; *self.data.deref().borrow_mut() = arg;
Ok(()) Ok(())
} }
fn Length(&self) -> u32 { fn Length(&self) -> u32 {
self.data.len() as u32 self.data.deref().borrow().len() as u32
} }
fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString> { fn SubstringData(&self, offset: u32, count: u32) -> Fallible<DOMString> {
Ok(self.data.as_slice().slice(offset as uint, count as uint).to_str()) Ok(self.data.deref().borrow().as_slice().slice(offset as uint, count as uint).to_string())
} }
fn AppendData(&mut self, arg: DOMString) -> ErrorResult { fn AppendData(&self, arg: DOMString) -> ErrorResult {
self.data.push_str(arg.as_slice()); self.data.deref().borrow_mut().push_str(arg.as_slice());
Ok(()) Ok(())
} }
fn InsertData(&mut self, offset: u32, arg: DOMString) -> ErrorResult { fn InsertData(&self, offset: u32, arg: DOMString) -> ErrorResult {
self.ReplaceData(offset, 0, arg) self.ReplaceData(offset, 0, arg)
} }
fn DeleteData(&mut self, offset: u32, count: u32) -> ErrorResult { fn DeleteData(&self, offset: u32, count: u32) -> ErrorResult {
self.ReplaceData(offset, count, "".to_string()) self.ReplaceData(offset, count, "".to_string())
} }
fn ReplaceData(&mut self, offset: u32, count: u32, arg: DOMString) -> ErrorResult { fn ReplaceData(&self, offset: u32, count: u32, arg: DOMString) -> ErrorResult {
let length = self.data.len() as u32; let length = self.data.deref().borrow().len() as u32;
if offset > length { if offset > length {
return Err(IndexSize); return Err(IndexSize);
} }
@ -92,10 +95,10 @@ impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
} else { } else {
count count
}; };
let mut data = self.data.as_slice().slice(0, offset as uint).to_string(); let mut data = self.data.deref().borrow().as_slice().slice(0, offset as uint).to_string();
data.push_str(arg.as_slice()); data.push_str(arg.as_slice());
data.push_str(self.data.as_slice().slice((offset + count) as uint, length as uint)); data.push_str(self.data.deref().borrow().as_slice().slice((offset + count) as uint, length as uint));
self.data = data.into_owned(); *self.data.deref().borrow_mut() = data.into_owned();
// FIXME: Once we have `Range`, we should implement step7 to step11 // FIXME: Once we have `Range`, we should implement step7 to step11
Ok(()) Ok(())
} }

View file

@ -567,7 +567,7 @@ impl<'a> DocumentMethods for JSRef<'a, Document> {
for child in title_elem.children() { for child in title_elem.children() {
if child.is_text() { if child.is_text() {
let text: &JSRef<Text> = TextCast::to_ref(&child).unwrap(); let text: &JSRef<Text> = TextCast::to_ref(&child).unwrap();
title.push_str(text.deref().characterdata.data.as_slice()); title.push_str(text.deref().characterdata.data.deref().borrow().as_slice());
} }
} }
}); });

View file

@ -67,7 +67,7 @@ pub fn serialize(iterator: &mut NodeIterator) -> String {
fn serialize_comment(comment: &JSRef<Comment>, html: &mut String) { fn serialize_comment(comment: &JSRef<Comment>, html: &mut String) {
html.push_str("<!--"); html.push_str("<!--");
html.push_str(comment.deref().characterdata.data.as_slice()); html.push_str(comment.deref().characterdata.data.deref().borrow().as_slice());
html.push_str("-->"); html.push_str("-->");
} }
@ -80,11 +80,11 @@ fn serialize_text(text: &JSRef<Text>, html: &mut String) {
"style" | "script" | "xmp" | "iframe" | "style" | "script" | "xmp" | "iframe" |
"noembed" | "noframes" | "plaintext" | "noembed" | "noframes" | "plaintext" |
"noscript" if elem.deref().namespace == namespace::HTML "noscript" if elem.deref().namespace == namespace::HTML
=> html.push_str(text.deref().characterdata.data.as_slice()), => html.push_str(text.deref().characterdata.data.deref().borrow().as_slice()),
_ => escape(text.deref().characterdata.data.as_slice(), false, html) _ => escape(text.deref().characterdata.data.deref().borrow().as_slice(), false, html)
} }
} }
_ => escape(text.deref().characterdata.data.as_slice(), false, html) _ => escape(text.deref().characterdata.data.deref().borrow().as_slice(), false, html)
} }
} }
@ -93,7 +93,7 @@ fn serialize_processing_instruction(processing_instruction: &JSRef<ProcessingIns
html.push_str("<?"); html.push_str("<?");
html.push_str(processing_instruction.deref().target.as_slice()); html.push_str(processing_instruction.deref().target.as_slice());
html.push_char(' '); html.push_char(' ');
html.push_str(processing_instruction.deref().characterdata.data.as_slice()); html.push_str(processing_instruction.deref().characterdata.data.deref().borrow().as_slice());
html.push_str("?>"); html.push_str("?>");
} }
@ -118,7 +118,7 @@ fn serialize_elem(elem: &JSRef<Element>, open_elements: &mut Vec<String>, html:
match node.first_child().map(|child| child.root()) { match node.first_child().map(|child| child.root()) {
Some(ref child) if child.is_text() => { Some(ref child) if child.is_text() => {
let text: &JSRef<CharacterData> = CharacterDataCast::to_ref(&**child).unwrap(); let text: &JSRef<CharacterData> = CharacterDataCast::to_ref(&**child).unwrap();
if text.deref().data.len() > 0 && text.deref().data.as_slice().char_at(0) == '\n' { if text.deref().data.deref().borrow().len() > 0 && text.deref().data.deref().borrow().as_slice().char_at(0) == '\n' {
html.push_char('\x0A'); html.push_char('\x0A');
} }
}, },

View file

@ -1235,7 +1235,7 @@ impl Node {
CommentNodeTypeId => { CommentNodeTypeId => {
let comment: &JSRef<Comment> = CommentCast::to_ref(node).unwrap(); let comment: &JSRef<Comment> = CommentCast::to_ref(node).unwrap();
let comment = comment.deref(); let comment = comment.deref();
let comment = Comment::new(comment.characterdata.data.clone(), &*document); let comment = Comment::new(comment.characterdata.data.deref().borrow().clone(), &*document);
NodeCast::from_temporary(comment) NodeCast::from_temporary(comment)
}, },
DocumentNodeTypeId => { DocumentNodeTypeId => {
@ -1259,14 +1259,14 @@ impl Node {
TextNodeTypeId => { TextNodeTypeId => {
let text: &JSRef<Text> = TextCast::to_ref(node).unwrap(); let text: &JSRef<Text> = TextCast::to_ref(node).unwrap();
let text = text.deref(); let text = text.deref();
let text = Text::new(text.characterdata.data.clone(), &*document); let text = Text::new(text.characterdata.data.deref().borrow().clone(), &*document);
NodeCast::from_temporary(text) NodeCast::from_temporary(text)
}, },
ProcessingInstructionNodeTypeId => { ProcessingInstructionNodeTypeId => {
let pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(node).unwrap(); let pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(node).unwrap();
let pi = pi.deref(); let pi = pi.deref();
let pi = ProcessingInstruction::new(pi.target.clone(), let pi = ProcessingInstruction::new(pi.target.clone(),
pi.characterdata.data.clone(), &*document); pi.characterdata.data.deref().borrow().clone(), &*document);
NodeCast::from_temporary(pi) NodeCast::from_temporary(pi)
}, },
}.root(); }.root();
@ -1514,7 +1514,7 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
for node in self.traverse_preorder() { for node in self.traverse_preorder() {
if node.is_text() { if node.is_text() {
let text: &JSRef<Text> = TextCast::to_ref(&node).unwrap(); let text: &JSRef<Text> = TextCast::to_ref(&node).unwrap();
content.push_str(text.deref().characterdata.data.as_slice()); content.push_str(text.deref().characterdata.data.deref().borrow().as_slice());
} }
} }
Some(content.into_owned()) Some(content.into_owned())
@ -1555,10 +1555,8 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
ProcessingInstructionNodeTypeId => { ProcessingInstructionNodeTypeId => {
self.wait_until_safe_to_modify_dom(); self.wait_until_safe_to_modify_dom();
{ let characterdata: &JSRef<CharacterData> = CharacterDataCast::to_ref(self).unwrap();
let characterdata: &mut JSRef<CharacterData> = CharacterDataCast::to_mut_ref(self).unwrap(); *characterdata.data.deref().borrow_mut() = value;
characterdata.deref_mut().data = value;
}
// Notify the document that the content of this node is different // Notify the document that the content of this node is different
let document = self.owner_doc().root(); let document = self.owner_doc().root();
@ -1772,12 +1770,12 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
let pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(node).unwrap(); let pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(node).unwrap();
let other_pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(other).unwrap(); let other_pi: &JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(other).unwrap();
(pi.deref().target == other_pi.deref().target) && (pi.deref().target == other_pi.deref().target) &&
(pi.deref().characterdata.data == other_pi.deref().characterdata.data) (*pi.deref().characterdata.data.deref().borrow() == *other_pi.deref().characterdata.data.deref().borrow())
} }
fn is_equal_characterdata(node: &JSRef<Node>, other: &JSRef<Node>) -> bool { fn is_equal_characterdata(node: &JSRef<Node>, other: &JSRef<Node>) -> bool {
let characterdata: &JSRef<CharacterData> = CharacterDataCast::to_ref(node).unwrap(); let characterdata: &JSRef<CharacterData> = CharacterDataCast::to_ref(node).unwrap();
let other_characterdata: &JSRef<CharacterData> = CharacterDataCast::to_ref(other).unwrap(); let other_characterdata: &JSRef<CharacterData> = CharacterDataCast::to_ref(other).unwrap();
characterdata.deref().data == other_characterdata.deref().data *characterdata.deref().data.deref().borrow() == *other_characterdata.deref().data.deref().borrow()
} }
fn is_equal_element_attrs(node: &JSRef<Node>, other: &JSRef<Node>) -> bool { fn is_equal_element_attrs(node: &JSRef<Node>, other: &JSRef<Node>) -> bool {
let element: &JSRef<Element> = ElementCast::to_ref(node).unwrap(); let element: &JSRef<Element> = ElementCast::to_ref(node).unwrap();

View file

@ -506,17 +506,17 @@ pub fn parse_html(page: &Page,
js_chan2.send(JSTaskNewFile(new_url)); js_chan2.send(JSTaskNewFile(new_url));
} }
None => { None => {
let mut data = vec!(); let mut data = String::new();
let scriptnode: &JSRef<Node> = NodeCast::from_ref(script); let scriptnode: &JSRef<Node> = NodeCast::from_ref(script);
debug!("iterating over children {:?}", scriptnode.first_child()); debug!("iterating over children {:?}", scriptnode.first_child());
for child in scriptnode.children() { for child in scriptnode.children() {
debug!("child = {:?}", child); debug!("child = {:?}", child);
let text: &JSRef<Text> = TextCast::to_ref(&child).unwrap(); let text: &JSRef<Text> = TextCast::to_ref(&child).unwrap();
data.push(text.deref().characterdata.data.to_str()); // FIXME: Bad copy. data.push_str(text.deref().characterdata.data.deref().borrow().as_slice());
} }
debug!("script data = {:?}", data); debug!("script data = {:?}", data);
js_chan2.send(JSTaskNewInlineScript(data.concat(), url3.clone())); js_chan2.send(JSTaskNewInlineScript(data, url3.clone()));
} }
} }
} }