Merge pull request #3432 from gilles-leblanc/issue-3361

Handle dynamic addition of attributes

Reviewed-by: jdm
This commit is contained in:
bors-servo 2014-09-26 19:00:35 -06:00
commit 1e5770a138
4 changed files with 52 additions and 19 deletions

View file

@ -48,6 +48,21 @@ impl HTMLLinkElement {
}
}
fn get_attr(element: JSRef<Element>, name: &str) -> Option<String> {
let elem = element.get_attribute(Null, name).root();
elem.map(|e| e.deref().value().as_slice().to_string())
}
fn is_stylesheet(value: &Option<String>) -> bool {
match *value {
Some(ref value) => {
value.as_slice().split(HTML_SPACE_CHARACTERS.as_slice())
.any(|s| s.as_slice().eq_ignore_ascii_case("stylesheet"))
},
None => false,
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> {
fn super_type<'a>(&'a self) -> Option<&'a VirtualMethods> {
let htmlelement: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);
@ -60,10 +75,19 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> {
_ => (),
}
let element: JSRef<Element> = ElementCast::from_ref(*self);
let rel = get_attr(element, "rel");
match (rel, name.as_slice()) {
(ref rel, "href") => {
if is_stylesheet(rel) {
self.handle_stylesheet_url(value.as_slice());
}
let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"href" => node.set_enabled_state(true),
_ => ()
node.set_enabled_state(true)
}
(_, _) => ()
}
}
@ -89,24 +113,13 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> {
if tree_in_doc {
let element: JSRef<Element> = ElementCast::from_ref(*self);
// FIXME: workaround for https://github.com/mozilla/rust/issues/13246;
// we get unrooting order failures if these are inside the match.
let rel = {
let rel = element.get_attribute(Null, "rel").root();
rel.map(|rel| rel.deref().value().as_slice().to_string())
};
let href = {
let href = element.get_attribute(Null, "href").root();
href.map(|href| href.deref().value().as_slice().to_string())
};
let rel = get_attr(element, "rel");
let href = get_attr(element, "href");
match (rel, href) {
(Some(ref rel), Some(ref href)) => {
if rel.as_slice().split(HTML_SPACE_CHARACTERS.as_slice())
.any(|s| s.as_slice().eq_ignore_ascii_case("stylesheet")) {
(ref rel, Some(ref href)) if is_stylesheet(rel) => {
self.handle_stylesheet_url(href.as_slice());
}
}
_ => {}
}
}
@ -135,3 +148,4 @@ impl Reflectable for HTMLLinkElement {
self.htmlelement.reflector()
}
}

View file

@ -129,6 +129,7 @@ flaky_gpu,flaky_linux == acid2_noscroll.html acid2_ref_broken.html
== float_table_a.html float_table_ref.html
== table_containing_block_a.html table_containing_block_ref.html
== link_style_order.html link_style_order_ref.html
== link_style_dynamic_addition.html link_style_dynamic_addition_ref.html
== percent_height.html percent_height_ref.html
== inline_block_with_margin_a.html inline_block_with_margin_ref.html
== table_padding_a.html table_padding_ref.html

View file

@ -0,0 +1,11 @@
<html>
<head></head>
<body>
<script>
var link = document.createElement("link");
document.head.appendChild(link);
link.setAttribute("rel", "stylesheet");
link.setAttribute("href", "data:text/css,body{background:green}");
</script>
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<head>
<link rel="stylesheet" href="data:text/css,body{background:green}">
</head>
<body>
</body>
</html>