Use RefCell for mutability of Element::style_attribute.

This commit is contained in:
Tetsuharu OHZEKI 2014-06-08 23:34:25 +09:00
parent 18a4d46070
commit a2a94d9421
2 changed files with 13 additions and 8 deletions

View file

@ -50,7 +50,7 @@ use servo_msg::constellation_msg::{PipelineId, SubpageId};
use servo_util::namespace::Namespace;
use servo_util::namespace;
use servo_util::str::is_whitespace;
use std::cell::{Ref, RefMut};
use std::cell::{RefCell, Ref, RefMut};
use std::kinds::marker::ContravariantLifetime;
use std::mem;
use style::computed_values::{content, display, white_space};
@ -355,7 +355,12 @@ pub struct LayoutElement<'le> {
impl<'le> LayoutElement<'le> {
pub fn style_attribute(&self) -> &'le Option<PropertyDeclarationBlock> {
&self.element.style_attribute
let style: &Option<PropertyDeclarationBlock> = unsafe {
let style: &RefCell<Option<PropertyDeclarationBlock>> = self.element.style_attribute.deref();
// cast to the direct reference to T placed on the head of RefCell<T>
mem::transmute(style)
};
style
}
}

View file

@ -10,6 +10,7 @@ use dom::bindings::codegen::Bindings::ElementBinding;
use dom::bindings::codegen::InheritTypes::{ElementDerived, NodeCast};
use dom::bindings::js::{JS, JSRef, Temporary, TemporaryPushable};
use dom::bindings::js::{OptionalSettable, OptionalRootable, Root};
use dom::bindings::trace::Traceable;
use dom::bindings::utils::{Reflectable, Reflector};
use dom::bindings::error::{ErrorResult, Fallible, NamespaceError, InvalidCharacter};
use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type};
@ -40,7 +41,7 @@ pub struct Element {
pub namespace: Namespace,
pub prefix: Option<DOMString>,
pub attrs: RefCell<Vec<JS<Attr>>>,
pub style_attribute: Option<style::PropertyDeclarationBlock>,
pub style_attribute: Traceable<RefCell<Option<style::PropertyDeclarationBlock>>>,
pub attr_list: Cell<Option<JS<AttrList>>>
}
@ -149,7 +150,7 @@ impl Element {
prefix: prefix,
attrs: RefCell::new(vec!()),
attr_list: Cell::new(None),
style_attribute: None,
style_attribute: Traceable::new(RefCell::new(None)),
}
}
@ -747,8 +748,8 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
"style" => {
let doc = document_from_node(self).root();
let base_url = doc.deref().url().clone();
let mut self_alias = self.clone();
self_alias.deref_mut().style_attribute = Some(style::parse_style_attribute(value.as_slice(), &base_url))
let style = Some(style::parse_style_attribute(value.as_slice(), &base_url));
*self.deref().style_attribute.deref().borrow_mut() = style;
}
"id" => {
let node: &JSRef<Node> = NodeCast::from_ref(self);
@ -771,8 +772,7 @@ impl<'a> VirtualMethods for JSRef<'a, Element> {
match name.as_slice() {
"style" => {
let mut self_alias = self.clone();
self_alias.deref_mut().style_attribute = None
*self.deref().style_attribute.deref().borrow_mut() = None;
}
"id" => {
let node: &JSRef<Node> = NodeCast::from_ref(self);