Implement DOMStringMap::SupportedPropertyNames and NamedNodeMap::SupportedPropertyNames

This commit is contained in:
Nova Fallen 2015-10-25 13:05:22 -04:00
parent f6e3146de2
commit 73c4af626a
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 // https://html.spec.whatwg.org/multipage/#the-domstringmap-interface:supported-property-names
fn SupportedPropertyNames(&self) -> Vec<DOMString> { fn SupportedPropertyNames(&self) -> Vec<DOMString> {
// FIXME: unimplemented (https://github.com/servo/servo/issues/7273) self.element.supported_prop_names_custom_attr().iter().cloned().collect()
vec![]
} }
} }

View file

@ -27,6 +27,7 @@ use dom::node::{Node, SEQUENTIALLY_FOCUSABLE};
use dom::node::{document_from_node, window_from_node}; use dom::node::{document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods; use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::FocusType; use msg::constellation_msg::FocusType;
use std::ascii::AsciiExt;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::default::Default; use std::default::Default;
use std::intrinsics; use std::intrinsics;
@ -275,6 +276,45 @@ fn to_snake_case(name: DOMString) -> DOMString {
attr_name 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 { impl HTMLElement {
pub fn set_custom_attr(&self, name: DOMString, value: DOMString) -> ErrorResult { pub fn set_custom_attr(&self, name: DOMString, value: DOMString) -> ErrorResult {
if name.chars() if name.chars()
@ -316,6 +356,14 @@ impl HTMLElement {
_ => false, _ => 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 { impl VirtualMethods for HTMLElement {

View file

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

View file

@ -13281,6 +13281,14 @@
"path": "dom/collections/HTMLCollection-supported-property-names.html", "path": "dom/collections/HTMLCollection-supported-property-names.html",
"url": "/dom/collections/HTMLCollection-supported-property-names.html" "url": "/dom/collections/HTMLCollection-supported-property-names.html"
}, },
{
"path": "dom/collections/domstringmap-supported-property-names.html",
"url": "/dom/collections/domstringmap-supported-property-names.html"
},
{
"path": "dom/collections/namednodemap-supported-property-names.html",
"url": "/dom/collections/namednodemap-supported-property-names.html"
},
{ {
"path": "dom/events/Event-constants.html", "path": "dom/events/Event-constants.html",
"url": "/dom/events/Event-constants.html" "url": "/dom/events/Event-constants.html"

View file

@ -1,8 +0,0 @@
[dataset-enumeration.html]
type: testharness
[A dataset should be enumeratable.]
expected: FAIL
[Only attributes who qualify as dataset properties should be enumeratable in the dataset.]
expected: FAIL

View file

@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>DOMStringMap Test: Supported property names</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div id="edge1" data-="012">Simple</div>
<div id="edge2" data-id-="012">Simple</div>
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>
John Doe
</div>
<div id="user2" data-unique-id="1234567890"> Jane Doe </div>
<div id="user3" data-unique-id="4324324241"> Jim Doe </div>
<script>
test(function() {
var element = document.querySelector('#edge1');
assert_array_equals(Object.getOwnPropertyNames(element.dataset),
[""]);
}, "Object.getOwnPropertyNames on DOMStringMap, empty data attribute");
test(function() {
var element = document.querySelector('#edge2');
assert_array_equals(Object.getOwnPropertyNames(element.dataset),
["id-"]);
}, "Object.getOwnPropertyNames on DOMStringMap, data attribute trailing hyphen");
test(function() {
var element = document.querySelector('#user');
assert_array_equals(Object.getOwnPropertyNames(element.dataset),
['id', 'user', 'dateOfBirth']);
}, "Object.getOwnPropertyNames on DOMStringMap, multiple data attributes");
test(function() {
var element = document.querySelector('#user2');
element.dataset.middleName = "mark";
assert_array_equals(Object.getOwnPropertyNames(element.dataset),
['uniqueId', 'middleName']);
}, "Object.getOwnPropertyNames on DOMStringMap, attribute set on dataset in JS");
test(function() {
var element = document.querySelector('#user3');
element.setAttribute("data-age", 30);
assert_array_equals(Object.getOwnPropertyNames(element.dataset),
['uniqueId', 'age']);
}, "Object.getOwnPropertyNames on DOMStringMap, attribute set on element in JS");
</script>

View file

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>NamedNodeMap Test: Supported property names</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div id="simple" class="fancy">Simple</div>
<input id="result" type="text" value="" width="200px">
<script>
test(function() {
var elt = document.querySelector('#simple');
assert_array_equals(Object.getOwnPropertyNames(elt.attributes),
['0','1','id','class']);
}, "Object.getOwnPropertyNames on NamedNodeMap");
test(function() {
var result = document.getElementById("result");
assert_array_equals(Object.getOwnPropertyNames(result.attributes),
['0','1','2','3','id','type','value','width']);
}, "Object.getOwnPropertyNames on NamedNodeMap of input");
test(function() {
var result = document.getElementById("result");
result.removeAttribute("width");
assert_array_equals(Object.getOwnPropertyNames(result.attributes),
['0','1','2','id','type','value']);
}, "Object.getOwnPropertyNames on NamedNodeMap after attribute removal");
</script>