Implemented Attribute's tokens() iterator

This commit is contained in:
Bruno de Oliveira Abinader 2014-08-12 10:58:18 -04:00
parent 592454defd
commit e9f9afc324

View file

@ -16,9 +16,10 @@ use dom::virtualmethods::vtable_for;
use servo_util::atom::Atom; use servo_util::atom::Atom;
use servo_util::namespace; use servo_util::namespace;
use servo_util::namespace::Namespace; use servo_util::namespace::Namespace;
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS}; use servo_util::str::{DOMString, split_html_space_chars};
use std::cell::{Ref, RefCell}; use std::cell::{Ref, RefCell};
use std::mem; use std::mem;
use std::slice::Items;
pub enum AttrSettingType { pub enum AttrSettingType {
FirstSetAttr, FirstSetAttr,
@ -28,27 +29,16 @@ pub enum AttrSettingType {
#[deriving(PartialEq, Clone, Encodable)] #[deriving(PartialEq, Clone, Encodable)]
pub enum AttrValue { pub enum AttrValue {
StringAttrValue(DOMString), StringAttrValue(DOMString),
TokenListAttrValue(DOMString, Vec<(uint, uint)>), TokenListAttrValue(DOMString, Vec<Atom>),
UIntAttrValue(DOMString, u32), UIntAttrValue(DOMString, u32),
AtomAttrValue(Atom), AtomAttrValue(Atom),
} }
impl AttrValue { impl AttrValue {
pub fn from_tokenlist(list: DOMString) -> AttrValue { pub fn from_tokenlist(tokens: DOMString) -> AttrValue {
let mut indexes = vec![]; let atoms = split_html_space_chars(tokens.as_slice())
let mut last_index: uint = 0; .map(|token| Atom::from_slice(token)).collect();
let length = list.len() - 1; TokenListAttrValue(tokens, atoms)
for (index, ch) in list.as_slice().char_indices() {
if HTML_SPACE_CHARACTERS.iter().any(|&space| space == ch) {
if last_index != index {
indexes.push((last_index, index));
}
last_index = index + 1;
} else if index == length {
indexes.push((last_index, index + 1));
}
}
return TokenListAttrValue(list, indexes);
} }
pub fn from_u32(string: DOMString, default: u32) -> AttrValue { pub fn from_u32(string: DOMString, default: u32) -> AttrValue {
@ -61,6 +51,12 @@ impl AttrValue {
AtomAttrValue(value) AtomAttrValue(value)
} }
pub fn tokens<'a>(&'a self) -> Option<Items<'a, Atom>> {
match *self {
TokenListAttrValue(_, ref tokens) => Some(tokens.iter()),
_ => None
}
}
} }
impl Str for AttrValue { impl Str for AttrValue {