Make CharacterData store a ~str rather than a DOMString, because its data can never be null.

This commit is contained in:
Ms2ger 2013-09-05 19:24:49 +02:00
parent 9073329737
commit efff6be86c
2 changed files with 16 additions and 15 deletions

View file

@ -111,6 +111,14 @@ impl DOMString {
null_string => &'a "", null_string => &'a "",
} }
} }
// XXX This is temporary until issue #875 is fixed.
pub fn unwrap(&self) -> ~str {
match self {
&str(ref s) => s.clone(),
&null_string => fail!("Cannot unwrap a null string.")
}
}
} }
pub struct rust_box<T> { pub struct rust_box<T> {

View file

@ -4,49 +4,42 @@
//! DOM bindings for `CharacterData`. //! DOM bindings for `CharacterData`.
use dom::bindings::utils::{DOMString, null_string, str, ErrorResult}; use dom::bindings::utils::{DOMString, str, ErrorResult};
use dom::bindings::utils::{BindingObject, CacheableWrapper, WrapperCache}; use dom::bindings::utils::{BindingObject, CacheableWrapper, WrapperCache};
use dom::node::{Node, NodeTypeId, ScriptView}; use dom::node::{Node, NodeTypeId, ScriptView};
use js::jsapi::{JSObject, JSContext}; use js::jsapi::{JSObject, JSContext};
pub struct CharacterData { pub struct CharacterData {
parent: Node<ScriptView>, parent: Node<ScriptView>,
data: DOMString data: ~str
} }
impl CharacterData { impl CharacterData {
pub fn new(id: NodeTypeId, data: ~str) -> CharacterData { pub fn new(id: NodeTypeId, data: ~str) -> CharacterData {
CharacterData { CharacterData {
parent: Node::new(id), parent: Node::new(id),
data: str(data) data: data
} }
} }
pub fn Data(&self) -> DOMString { pub fn Data(&self) -> DOMString {
self.data.clone() str(self.data.clone())
} }
pub fn SetData(&mut self, arg: &DOMString, _rv: &mut ErrorResult) { pub fn SetData(&mut self, arg: &DOMString, _rv: &mut ErrorResult) {
self.data = (*arg).clone(); self.data = arg.unwrap();
} }
pub fn Length(&self) -> u32 { pub fn Length(&self) -> u32 {
match self.data { self.data.len() as u32
str(ref s) => s.len() as u32,
null_string => 0
}
} }
pub fn SubstringData(&self, offset: u32, count: u32, _rv: &mut ErrorResult) -> DOMString { pub fn SubstringData(&self, offset: u32, count: u32, _rv: &mut ErrorResult) -> DOMString {
match self.data { str(self.data.slice(offset as uint, count as uint).to_str())
str(ref s) => str(s.slice(offset as uint, count as uint).to_str()),
null_string => null_string
}
} }
pub fn AppendData(&mut self, arg: &DOMString, _rv: &mut ErrorResult) { pub fn AppendData(&mut self, arg: &DOMString, _rv: &mut ErrorResult) {
let s = self.data.to_str(); self.data.push_str(arg.unwrap());
self.data = str(s.append(arg.to_str()));
} }
pub fn InsertData(&mut self, _offset: u32, _arg: &DOMString, _rv: &mut ErrorResult) { pub fn InsertData(&mut self, _offset: u32, _arg: &DOMString, _rv: &mut ErrorResult) {