Auto merge of #8114 - nfallen:7695, r=eefriedman

Implement DOMStringMap::SupportedPropertyNames and NamedNodeMap::SupportedPropertyNames #7695

Here is a draft for issue #7695 with web platform tests. 
Thanks for reviewing!
https://dom.spec.whatwg.org/#namednodemap
https://html.spec.whatwg.org/multipage/infrastructure.html#domstringmap

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8114)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-10-31 08:04:08 +05:30
commit 7512aa69c0
7 changed files with 145 additions and 12 deletions

View file

@ -61,7 +61,6 @@ impl DOMStringMapMethods for DOMStringMap {
// https://html.spec.whatwg.org/multipage/#the-domstringmap-interface:supported-property-names
fn SupportedPropertyNames(&self) -> Vec<DOMString> {
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
vec![]
self.element.supported_prop_names_custom_attr().iter().cloned().collect()
}
}

View file

@ -27,6 +27,7 @@ use dom::node::{Node, SEQUENTIALLY_FOCUSABLE};
use dom::node::{document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::FocusType;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::default::Default;
use std::intrinsics;
@ -275,6 +276,45 @@ fn to_snake_case(name: DOMString) -> DOMString {
attr_name
}
// https://html.spec.whatwg.org/multipage/#attr-data-*
// if this attribute is in snake case with a data- prefix,
// this function returns a name converted to camel case
// without the data prefix.
fn to_camel_case(name: &str) -> Option<DOMString> {
if !name.starts_with("data-") {
return None;
}
let name = &name[5..];
let has_uppercase = name.chars().any(|curr_char| {
curr_char.is_ascii() && curr_char.is_uppercase()
});
if has_uppercase {
return None;
}
let mut result = "".to_owned();
let mut name_chars = name.chars();
while let Some(curr_char) = name_chars.next() {
//check for hyphen followed by character
if curr_char == '\x2d' {
if let Some(next_char) = name_chars.next() {
if next_char.is_ascii() && next_char.is_lowercase() {
result.push(next_char.to_ascii_uppercase());
} else {
result.push(curr_char);
result.push(next_char);
}
} else {
result.push(curr_char);
}
} else {
result.push(curr_char);
}
}
Some(result)
}
impl HTMLElement {
pub fn set_custom_attr(&self, name: DOMString, value: DOMString) -> ErrorResult {
if name.chars()
@ -316,6 +356,14 @@ impl HTMLElement {
_ => false,
}
}
pub fn supported_prop_names_custom_attr(&self) -> Vec<DOMString> {
let element = self.upcast::<Element>();
element.attrs().iter().map(JS::root).filter_map(|attr| {
let raw_name = attr.r().local_name();
to_camel_case(&raw_name)
}).collect()
}
}
impl VirtualMethods for HTMLElement {

View file

@ -85,8 +85,10 @@ impl NamedNodeMapMethods for NamedNodeMap {
item
}
// https://heycam.github.io/webidl/#dfn-supported-property-names
fn SupportedPropertyNames(&self) -> Vec<DOMString> {
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273)
vec![]
self.owner.attrs().iter().map(JS::root).map(|attr| {
(**attr.name()).to_owned()
}).collect()
}
}