Added in-place mutation to DOMString.

The methods which are currently implemented are the ones on String that are currently being used:
string.push_str(...), string.clear() and string.extend(...). We may want to revisit this API.
This commit is contained in:
Alan Jeffrey 2015-11-11 17:30:23 -06:00
parent 034769f280
commit 5db67b5981
5 changed files with 23 additions and 5 deletions

View file

@ -69,6 +69,7 @@ impl CharacterDataMethods for CharacterData {
// https://dom.spec.whatwg.org/#dom-characterdata-appenddatadata // https://dom.spec.whatwg.org/#dom-characterdata-appenddatadata
fn AppendData(&self, data: DOMString) { fn AppendData(&self, data: DOMString) {
// FIXME(ajeffrey): Efficient append on DOMStrings?
self.append_data(&*data); self.append_data(&*data);
} }
@ -149,7 +150,8 @@ impl CharacterData {
} }
#[inline] #[inline]
pub fn append_data(&self, data: &str) { pub fn append_data(&self, data: &str) {
self.data.borrow_mut().0.push_str(data); // FIXME(ajeffrey): Efficient append on DOMStrings?
self.data.borrow_mut().push_str(data);
self.content_changed(); self.content_changed();
} }

View file

@ -182,7 +182,7 @@ fn follow_hyperlink(subject: &Element, hyperlink_suffix: Option<String>) {
// Step 6. // Step 6.
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=28925 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=28925
if let Some(suffix) = hyperlink_suffix { if let Some(suffix) = hyperlink_suffix {
href.0.push_str(&suffix); href.push_str(&suffix);
} }
// Step 4-5. // Step 4-5.

View file

@ -576,10 +576,11 @@ impl VirtualMethods for HTMLInputElement {
mutation.new_value(attr).as_ref().map(|name| name.as_atom())); mutation.new_value(attr).as_ref().map(|name| name.as_atom()));
}, },
&atom!(placeholder) => { &atom!(placeholder) => {
// FIXME(ajeffrey): Should we do in-place mutation of the placeholder?
let mut placeholder = self.placeholder.borrow_mut(); let mut placeholder = self.placeholder.borrow_mut();
placeholder.0.clear(); placeholder.clear();
if let AttributeMutation::Set(_) = mutation { if let AttributeMutation::Set(_) = mutation {
placeholder.0.extend( placeholder.extend(
attr.value().chars().filter(|&c| c != '\n' && c != '\r')); attr.value().chars().filter(|&c| c != '\n' && c != '\r'));
} }
}, },

View file

@ -186,6 +186,7 @@ impl<T: ClipboardProvider> TextInput<T> {
vec!(insert) vec!(insert)
}; };
// FIXME(ajeffrey): effecient append for DOMStrings
let mut new_line = prefix.to_owned(); let mut new_line = prefix.to_owned();
new_line.push_str(&insert_lines[0]); new_line.push_str(&insert_lines[0]);
insert_lines[0] = DOMString::from(new_line); insert_lines[0] = DOMString::from(new_line);
@ -194,7 +195,8 @@ impl<T: ClipboardProvider> TextInput<T> {
self.edit_point.index = insert_lines[last_insert_lines_index].len(); self.edit_point.index = insert_lines[last_insert_lines_index].len();
self.edit_point.line = begin.line + last_insert_lines_index; self.edit_point.line = begin.line + last_insert_lines_index;
insert_lines[last_insert_lines_index].0.push_str(suffix); // FIXME(ajeffrey): effecient append for DOMStrings
insert_lines[last_insert_lines_index].push_str(suffix);
let mut new_lines = vec!(); let mut new_lines = vec!();
new_lines.push_all(lines_prefix); new_lines.push_all(lines_prefix);

View file

@ -32,6 +32,13 @@ impl DOMString {
pub fn new() -> DOMString { pub fn new() -> DOMString {
DOMString(String::new()) DOMString(String::new())
} }
// FIXME(ajeffrey): implement more of the String methods on DOMString?
pub fn push_str(&mut self, string: &str) {
self.0.push_str(string)
}
pub fn clear(&mut self) {
self.0.clear()
}
} }
impl Default for DOMString { impl Default for DOMString {
@ -181,6 +188,12 @@ impl FromJSValConvertible for DOMString {
} }
} }
impl Extend<char> for DOMString {
fn extend<I>(&mut self, iterable: I) where I: IntoIterator<Item=char> {
self.0.extend(iterable)
}
}
pub type StaticCharVec = &'static [char]; pub type StaticCharVec = &'static [char];
pub type StaticStringVec = &'static [&'static str]; pub type StaticStringVec = &'static [&'static str];