mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Fix CharacterData::SubstringData()
It was not following the spec and it could panic.
This commit is contained in:
parent
8f73b452fb
commit
702cea6fc3
1 changed files with 11 additions and 2 deletions
|
@ -20,6 +20,7 @@ use util::str::DOMString;
|
|||
|
||||
use std::borrow::ToOwned;
|
||||
use std::cell::Ref;
|
||||
use std::cmp;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct CharacterData {
|
||||
|
@ -69,9 +70,17 @@ impl<'a> CharacterDataMethods for JSRef<'a, CharacterData> {
|
|||
|
||||
// https://dom.spec.whatwg.org/#dom-characterdata-substringdata
|
||||
fn SubstringData(self, offset: u32, count: u32) -> Fallible<DOMString> {
|
||||
// FIXME(https://github.com/rust-lang/rust/issues/23338)
|
||||
let data = self.data.borrow();
|
||||
Ok(data.slice_chars(offset as usize, (offset + count) as usize).to_owned())
|
||||
// Step 1.
|
||||
let len = data.chars().count();
|
||||
if len > offset as usize {
|
||||
// Step 2.
|
||||
return Err(IndexSize);
|
||||
}
|
||||
// Step 3.
|
||||
let end = cmp::min((offset + count) as usize, len);
|
||||
// Step 4.
|
||||
Ok(data.slice_chars(offset as usize, end).to_owned())
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-characterdata-appenddata
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue