From fb98384fa534f555245f43cdfd8152a59345ec41 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Wed, 15 Oct 2014 03:41:06 +0900 Subject: [PATCH] Use DOMRefCell in Element. --- components/layout/wrapper.rs | 6 ++---- components/script/dom/element.rs | 25 +++++++++++-------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs index 3a9425c99c6..0b78a2f97ac 100644 --- a/components/layout/wrapper.rs +++ b/components/layout/wrapper.rs @@ -35,6 +35,7 @@ use css::node_style::StyledNode; use util::{LayoutDataAccess, LayoutDataWrapper, PrivateLayoutData, OpaqueNodeMethods}; use gfx::display_list::OpaqueNode; +use script::dom::bindings::cell::{Ref, RefMut}; use script::dom::bindings::codegen::InheritTypes::{ElementCast, HTMLIFrameElementCast, HTMLImageElementCast}; use script::dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, TextCast}; use script::dom::bindings::js::JS; @@ -50,7 +51,6 @@ use script::dom::text::Text; use script::layout_interface::LayoutChan; use servo_msg::constellation_msg::{PipelineId, SubpageId}; use servo_util::str::{LengthOrPercentageOrAuto, is_whitespace}; -use std::cell::{RefCell, Ref, RefMut}; use std::kinds::marker::ContravariantLifetime; use std::mem; use style::computed_values::{content, display, white_space}; @@ -445,9 +445,7 @@ pub struct LayoutElement<'le> { impl<'le> LayoutElement<'le> { pub fn style_attribute(&self) -> &'le Option { let style: &Option = unsafe { - let style: &RefCell> = self.element.style_attribute(); - // cast to the direct reference to T placed on the head of RefCell - mem::transmute(style) + &*self.element.style_attribute().borrow_for_layout() }; style } diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index cb6a3c21800..d9f995720a8 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -7,6 +7,7 @@ use dom::attr::{Attr, ReplacedAttr, FirstSetAttr, AttrHelpers, AttrHelpersForLayout}; use dom::attr::{AttrValue, StringAttrValue, UIntAttrValue, AtomAttrValue}; use dom::namednodemap::NamedNodeMap; +use dom::bindings::cell::{DOMRefCell, Ref, RefMut}; use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods; use dom::bindings::codegen::Bindings::ElementBinding; use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; @@ -39,7 +40,6 @@ use servo_util::namespace; use servo_util::str::{DOMString, LengthOrPercentageOrAuto}; use std::ascii::StrAsciiExt; -use std::cell::{Ref, RefMut, RefCell}; use std::default::Default; use std::mem; use string_cache::{Atom, Namespace}; @@ -53,8 +53,8 @@ pub struct Element { local_name: Atom, namespace: Namespace, prefix: Option, - attrs: RefCell>>, - style_attribute: RefCell>, + attrs: DOMRefCell>>, + style_attribute: DOMRefCell>, attr_list: MutNullableJS, class_list: MutNullableJS, } @@ -160,10 +160,10 @@ impl Element { local_name: Atom::from_slice(local_name.as_slice()), namespace: namespace, prefix: prefix, - attrs: RefCell::new(vec!()), + attrs: DOMRefCell::new(vec!()), attr_list: Default::default(), class_list: Default::default(), - style_attribute: RefCell::new(None), + style_attribute: DOMRefCell::new(None), } } @@ -203,7 +203,7 @@ impl Element { } #[inline] - pub fn style_attribute<'a>(&'a self) -> &'a RefCell> { + pub fn style_attribute<'a>(&'a self) -> &'a DOMRefCell> { &self.style_attribute } } @@ -226,8 +226,7 @@ impl RawLayoutElementHelpers for Element { #[allow(unrooted_must_root)] unsafe fn get_attr_val_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom) -> Option<&'a str> { - // cast to point to T in RefCell directly - let attrs: *const Vec> = mem::transmute(&self.attrs); + let attrs = self.attrs.borrow_for_layout(); (*attrs).iter().find(|attr: & &JS| { let attr = attr.unsafe_get(); *name == (*attr).local_name_atom_forever() && @@ -241,8 +240,7 @@ impl RawLayoutElementHelpers for Element { #[inline] #[allow(unrooted_must_root)] unsafe fn get_attr_vals_for_layout<'a>(&'a self, name: &Atom) -> Vec<&'a str> { - // cast to point to T in RefCell directly - let attrs: *const Vec> = mem::transmute(&self.attrs); + let attrs = self.attrs.borrow_for_layout(); (*attrs).iter().filter_map(|attr: &JS| { let attr = attr.unsafe_get(); if *name == (*attr).local_name_atom_forever() { @@ -257,8 +255,7 @@ impl RawLayoutElementHelpers for Element { #[allow(unrooted_must_root)] unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom) -> Option { - // cast to point to T in RefCell directly - let attrs: *const Vec> = mem::transmute(&self.attrs); + let attrs = self.attrs.borrow_for_layout(); (*attrs).iter().find(|attr: & &JS| { let attr = attr.unsafe_get(); *name == (*attr).local_name_atom_forever() && @@ -272,7 +269,7 @@ impl RawLayoutElementHelpers for Element { #[inline] #[allow(unrooted_must_root)] unsafe fn has_class_for_layout(&self, name: &Atom) -> bool { - let attrs: *const Vec> = mem::transmute(&self.attrs); + let attrs = self.attrs.borrow_for_layout(); (*attrs).iter().find(|attr: & &JS| { let attr = attr.unsafe_get(); (*attr).local_name_atom_forever() == atom!("class") @@ -287,7 +284,7 @@ impl RawLayoutElementHelpers for Element { #[inline] #[allow(unrooted_must_root)] unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> { - let attrs: *const Vec> = mem::transmute(&self.attrs); + let attrs = self.attrs.borrow_for_layout(); (*attrs).iter().find(|attr: & &JS| { let attr = attr.unsafe_get(); (*attr).local_name_atom_forever() == atom!("class")