Skip to content

Commit

Permalink
Handle style attributes in element setter instead of html parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ILyoan authored and metajack committed Aug 15, 2013
1 parent eadda94 commit 2ad3505
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
13 changes: 10 additions & 3 deletions src/components/script/dom/element.rs
Expand Up @@ -47,6 +47,7 @@ use js::jsapi::{JSContext, JSObject};
use std::cell::Cell;
use std::comm;
use std::str::eq_slice;
use extra::net::url;

pub struct Element {
parent: Node<ScriptView>,
Expand Down Expand Up @@ -262,8 +263,7 @@ impl<'self> Element {

pub fn set_attr(&mut self, name: &DOMString, value: &DOMString) {
let name = name.to_str();
let value = value.to_str();
let value_cell = Cell::new(value);
let value_cell = Cell::new(value.to_str());
let mut found = false;
for self.attrs.mut_iter().advance |attr| {
if eq_slice(attr.name, name) {
Expand All @@ -276,6 +276,13 @@ impl<'self> Element {
self.attrs.push(Attr::new(name.to_str(), value_cell.take().clone()));
}

if "style" == name {
self.style_attribute = Some(
Stylesheet::from_attribute(
url::from_str("http://www.example.com/").unwrap(),
value.get_ref()));
}

match self.parent.owner_doc {
Some(owner) => do owner.with_base |owner| { owner.content_changed() },
None => {}
Expand Down Expand Up @@ -312,7 +319,7 @@ impl Element {
null_string
}

pub fn SetAttribute(&self, _name: &DOMString, _value: &DOMString, _rv: &mut ErrorResult) {
pub fn SetAttribute(&mut self, _name: &DOMString, _value: &DOMString, _rv: &mut ErrorResult) {
}

pub fn SetAttributeNS(&self, _namespace: &DOMString, _localname: &DOMString, _value: &DOMString, _rv: &mut ErrorResult) {
Expand Down
12 changes: 3 additions & 9 deletions src/components/script/html/hubbub_html_parser.rs
Expand Up @@ -42,10 +42,11 @@ use dom::htmltablesectionelement::HTMLTableSectionElement;
use dom::htmltextareaelement::HTMLTextAreaElement;
use dom::htmltitleelement::HTMLTitleElement;
use dom::htmlulistelement::HTMLUListElement;
use dom::element::{Element, Attr};
use dom::element::Element;
use dom::htmlelement::HTMLElement;
use dom::node::{AbstractNode, Comment, Doctype, ElementNodeTypeId, Node, ScriptView};
use dom::node::{Text};
use dom::bindings::utils::str;
use html::cssparse::{InlineProvenance, StylesheetProvenance, UrlProvenance, spawn_css_parser};
use js::jsapi::JSContext;
use newcss::stylesheet::Stylesheet;
Expand Down Expand Up @@ -343,14 +344,7 @@ pub fn parse_html(cx: *JSContext,
debug!("-- attach attrs");
do node.as_mut_element |element| {
for tag.attributes.iter().advance |attr| {
element.attrs.push(Attr::new(attr.name.clone(), attr.value.clone()));

if "style" == attr.name {
element.style_attribute = Some(
Stylesheet::from_attribute(
url::from_str("http://www.example.com/").unwrap(),
attr.value));
}
element.set_attr(&str(attr.name.clone()), &str(attr.value.clone()));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/html/color-change-text.html
Expand Up @@ -9,6 +9,7 @@
<script src="color-change-text.js"></script>
</head>
<body>
<div id="change" class="red">Hello, World!</div>
<div id="change" class="red">Hello, World!</div>
<div id="change" style="color:blue;">Hello, Servo!</div>
</body>
</html>
1 change: 1 addition & 0 deletions src/test/html/color-change-text.js
@@ -1,3 +1,4 @@
window.setTimeout(function () {
window.document.getElementsByTagName('div')[0].setAttribute('class', 'blue');
window.document.getElementsByTagName('div')[1].setAttribute('style', 'color:red;');
}, 1000);

0 comments on commit 2ad3505

Please sign in to comment.