Skip to content

Commit

Permalink
Handle dynamic addition of attributes
Browse files Browse the repository at this point in the history
Handles dynamic addition of attributes to <link rel=stylesheet>
elements.

Fixes #3361
  • Loading branch information
gilles-leblanc committed Sep 26, 2014
1 parent ab17d31 commit 8e77422
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
52 changes: 33 additions & 19 deletions components/script/dom/htmllinkelement.rs
Expand Up @@ -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);
Expand All @@ -60,10 +75,19 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> {
_ => (),
}

let node: JSRef<Node> = NodeCast::from_ref(*self);
match name.as_slice() {
"href" => node.set_enabled_state(true),
_ => ()
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);
node.set_enabled_state(true)
}
(_, _) => ()
}
}

Expand All @@ -89,23 +113,12 @@ 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")) {
self.handle_stylesheet_url(href.as_slice());
}
(ref rel, Some(ref href)) if is_stylesheet(rel) => {
self.handle_stylesheet_url(href.as_slice());
}
_ => {}
}
Expand Down Expand Up @@ -135,3 +148,4 @@ impl Reflectable for HTMLLinkElement {
self.htmlelement.reflector()
}
}

1 change: 1 addition & 0 deletions tests/ref/basic.list
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/ref/link_style_dynamic_addition.html
@@ -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>
7 changes: 7 additions & 0 deletions tests/ref/link_style_dynamic_addition_ref.html
@@ -0,0 +1,7 @@
<html>
<head>
<link rel="stylesheet" href="data:text/css,body{background:green}">
</head>
<body>
</body>
</html>

5 comments on commit 8e77422

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at gilles-leblanc@8e77422

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gilles-leblanc/servo/issue-3361 = 8e77422 into bors-servo-integration-3432-issue-3361

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gilles-leblanc/servo/issue-3361 = 8e77422 merged ok, testing candidate = 1e5770a

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to bors-servo-integration-3432-issue-3361 = 1e5770a

Please sign in to comment.