renaming tokens(), atom() and uint() and rewriting to return or panic

This commit is contained in:
Prabhjyot Singh Sodhi 2015-09-02 14:44:39 +05:30
parent eaf90c0b1c
commit 105ea0d690
8 changed files with 31 additions and 35 deletions

View file

@ -75,28 +75,28 @@ impl AttrValue {
AttrValue::Atom(value) AttrValue::Atom(value)
} }
pub fn tokens<'a>(&'a self) -> Option<&'a [Atom]> { pub fn as_tokens<'a>(&'a self) -> &'a [Atom] {
match *self { match *self {
AttrValue::TokenList(_, ref tokens) => Some(tokens), AttrValue::TokenList(_, ref tokens) => tokens,
_ => None _ => panic!("Tokens not found"),
} }
} }
pub fn atom<'a>(&'a self) -> Option<&'a Atom> { pub fn as_atom<'a>(&'a self) -> &'a Atom {
match *self { match *self {
AttrValue::Atom(ref value) => Some(value), AttrValue::Atom(ref value) => value,
_ => None _ => panic!("Atom not found"),
} }
} }
/// Return the AttrValue as its integer representation, if any. /// Return the AttrValue as its integer representation, if any.
/// This corresponds to attribute values returned as `AttrValue::UInt(_)` /// This corresponds to attribute values returned as `AttrValue::UInt(_)`
/// by `VirtualMethods::parse_plain_attribute()`. /// by `VirtualMethods::parse_plain_attribute()`.
pub fn uint(&self) -> Option<u32> { pub fn as_uint(&self) -> u32 {
if let AttrValue::UInt(_, value) = *self { if let AttrValue::UInt(_, value) = *self {
Some(value) value
} else { } else {
None panic!("Uint not found");
} }
} }
} }

View file

@ -1762,10 +1762,10 @@ impl DocumentMethods for Document {
match html_elem_type { match html_elem_type {
HTMLElementTypeId::HTMLAppletElement => { HTMLElementTypeId::HTMLAppletElement => {
match elem.get_attribute(&ns!(""), &atom!("name")) { match elem.get_attribute(&ns!(""), &atom!("name")) {
Some(ref attr) if attr.r().value().atom() == Some(name) => true, Some(ref attr) if attr.r().value().as_atom() == name => true,
_ => { _ => {
match elem.get_attribute(&ns!(""), &atom!("id")) { match elem.get_attribute(&ns!(""), &atom!("id")) {
Some(ref attr) => attr.r().value().atom() == Some(name), Some(ref attr) => attr.r().value().as_atom() == name,
None => false, None => false,
} }
}, },
@ -1773,18 +1773,18 @@ impl DocumentMethods for Document {
}, },
HTMLElementTypeId::HTMLFormElement => { HTMLElementTypeId::HTMLFormElement => {
match elem.get_attribute(&ns!(""), &atom!("name")) { match elem.get_attribute(&ns!(""), &atom!("name")) {
Some(ref attr) => attr.r().value().atom() == Some(name), Some(ref attr) => attr.r().value().as_atom() == name,
None => false, None => false,
} }
}, },
HTMLElementTypeId::HTMLImageElement => { HTMLElementTypeId::HTMLImageElement => {
match elem.get_attribute(&ns!(""), &atom!("name")) { match elem.get_attribute(&ns!(""), &atom!("name")) {
Some(ref attr) => { Some(ref attr) => {
if attr.r().value().atom() == Some(name) { if attr.r().value().as_atom() == name {
true true
} else { } else {
match elem.get_attribute(&ns!(""), &atom!("id")) { match elem.get_attribute(&ns!(""), &atom!("id")) {
Some(ref attr) => attr.r().value().atom() == Some(name), Some(ref attr) => attr.r().value().as_atom() == name,
None => false, None => false,
} }
} }

View file

@ -61,7 +61,7 @@ impl DOMTokenListMethods for DOMTokenList {
fn Length(&self) -> u32 { fn Length(&self) -> u32 {
self.attribute().map(|attr| { self.attribute().map(|attr| {
let attr = attr.r(); let attr = attr.r();
attr.value().tokens().map(|tokens| tokens.len()).unwrap_or(0) attr.value().as_tokens().len()
}).unwrap_or(0) as u32 }).unwrap_or(0) as u32
} }
@ -69,7 +69,7 @@ impl DOMTokenListMethods for DOMTokenList {
fn Item(&self, index: u32) -> Option<DOMString> { fn Item(&self, index: u32) -> Option<DOMString> {
self.attribute().and_then(|attr| { self.attribute().and_then(|attr| {
let attr = attr.r(); let attr = attr.r();
attr.value().tokens().and_then(|tokens| { Some(attr.value().as_tokens()).and_then(|tokens| {
tokens.get(index as usize).map(|token| (**token).to_owned()) tokens.get(index as usize).map(|token| (**token).to_owned())
}) })
}) })
@ -81,10 +81,9 @@ impl DOMTokenListMethods for DOMTokenList {
self.attribute().map(|attr| { self.attribute().map(|attr| {
let attr = attr.r(); let attr = attr.r();
attr.value() attr.value()
.tokens() .as_tokens()
.expect("Should have parsed this attribute")
.iter() .iter()
.any(|atom| *atom == token) .any(|atom: &Atom| *atom == token)
}).unwrap_or(false) }).unwrap_or(false)
}) })
} }

View file

@ -969,9 +969,7 @@ impl Element {
Quirks => lhs.eq_ignore_ascii_case(&rhs) Quirks => lhs.eq_ignore_ascii_case(&rhs)
}; };
self.get_attribute(&ns!(""), &atom!("class")).map(|attr| { self.get_attribute(&ns!(""), &atom!("class")).map(|attr| {
attr.r().value().tokens().map(|tokens| { attr.r().value().as_tokens().iter().any(|atom| is_equal(name, atom))
tokens.iter().any(|atom| is_equal(name, atom))
}).unwrap_or(false)
}).unwrap_or(false) }).unwrap_or(false)
} }
@ -1031,8 +1029,7 @@ impl Element {
self.get_attribute(&ns!(""), local_name).map(|attr| { self.get_attribute(&ns!(""), local_name).map(|attr| {
attr.r() attr.r()
.value() .value()
.tokens() .as_tokens()
.expect("Expected a TokenListAttrValue")
.to_vec() .to_vec()
}).unwrap_or(vec!()) }).unwrap_or(vec!())
} }
@ -1469,11 +1466,11 @@ impl VirtualMethods for Element {
}, },
&atom!(id) => { &atom!(id) => {
if node.is_in_doc() { if node.is_in_doc() {
let value = attr.value().atom().unwrap().clone(); let value = attr.value().as_atom().clone();
match mutation { match mutation {
AttributeMutation::Set(old_value) => { AttributeMutation::Set(old_value) => {
if let Some(old_value) = old_value { if let Some(old_value) = old_value {
let old_value = old_value.atom().unwrap().clone(); let old_value = old_value.as_atom().clone();
doc.unregister_named_element(self, old_value); doc.unregister_named_element(self, old_value);
} }
if value != atom!("") { if value != atom!("") {
@ -1659,10 +1656,10 @@ impl<'a> ::selectors::Element for Root<Element> {
where F: FnMut(&Atom) where F: FnMut(&Atom)
{ {
if let Some(ref attr) = self.get_attribute(&ns!(""), &atom!("class")) { if let Some(ref attr) = self.get_attribute(&ns!(""), &atom!("class")) {
if let Some(tokens) = attr.r().value().tokens() { let tokens = attr.r().value();
for token in tokens { let tokens = tokens.as_tokens();
callback(token) for token in tokens {
} callback(token);
} }
} }
} }

View file

@ -362,7 +362,7 @@ impl VirtualMethods for HTMLIFrameElement {
&atom!(sandbox) => { &atom!(sandbox) => {
self.sandbox.set(mutation.new_value(attr).map(|value| { self.sandbox.set(mutation.new_value(attr).map(|value| {
let mut modes = SandboxAllowance::AllowNothing as u8; let mut modes = SandboxAllowance::AllowNothing as u8;
for token in value.tokens().unwrap() { for token in value.as_tokens() {
modes |= match &*token.to_ascii_lowercase() { modes |= match &*token.to_ascii_lowercase() {
"allow-same-origin" => SandboxAllowance::AllowSameOrigin, "allow-same-origin" => SandboxAllowance::AllowSameOrigin,
"allow-forms" => SandboxAllowance::AllowForms, "allow-forms" => SandboxAllowance::AllowForms,

View file

@ -479,7 +479,7 @@ impl VirtualMethods for HTMLInputElement {
}, },
&atom!(size) => { &atom!(size) => {
let size = mutation.new_value(attr).map(|value| { let size = mutation.new_value(attr).map(|value| {
value.uint().expect("Expected an AttrValue::UInt") value.as_uint()
}); });
self.size.set(size.unwrap_or(DEFAULT_INPUT_SIZE)); self.size.set(size.unwrap_or(DEFAULT_INPUT_SIZE));
} }

View file

@ -113,7 +113,7 @@ impl VirtualMethods for HTMLTableCellElement {
}, },
&atom!(colspan) => { &atom!(colspan) => {
self.colspan.set(mutation.new_value(attr).map(|value| { self.colspan.set(mutation.new_value(attr).map(|value| {
max(DEFAULT_COLSPAN, value.uint().unwrap()) max(DEFAULT_COLSPAN, value.as_uint())
})); }));
}, },
&atom!(width) => { &atom!(width) => {

View file

@ -265,13 +265,13 @@ impl VirtualMethods for HTMLTextAreaElement {
}, },
&atom!(cols) => { &atom!(cols) => {
let cols = mutation.new_value(attr).map(|value| { let cols = mutation.new_value(attr).map(|value| {
value.uint().expect("Expected an AttrValue::UInt") value.as_uint()
}); });
self.cols.set(cols.unwrap_or(DEFAULT_COLS)); self.cols.set(cols.unwrap_or(DEFAULT_COLS));
}, },
&atom!(rows) => { &atom!(rows) => {
let rows = mutation.new_value(attr).map(|value| { let rows = mutation.new_value(attr).map(|value| {
value.uint().expect("Expected an AttrValue::UInt") value.as_uint()
}); });
self.rows.set(rows.unwrap_or(DEFAULT_ROWS)); self.rows.set(rows.unwrap_or(DEFAULT_ROWS));
}, },