Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 31, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Implement DOMStringMap::SupportedPropertyNames and NamedNodeMap::Supp…

…ortedPropertyNames
  • Loading branch information
Nova Fallen
Nova Fallen committed Oct 31, 2015
commit 73c4af626ad261225d0fba23c78b1a8bceeac411
@@ -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()
}
}
@@ -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 {
@@ -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()
}
}
"path": "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",
"url": "/dom/events/Event-constants.html"

This file was deleted.

@@ -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>
@@ -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>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.