Skip to content

Commit

Permalink
Privatize Element
Browse files Browse the repository at this point in the history
  • Loading branch information
ttaubert committed Oct 13, 2014
1 parent cd9de05 commit cbe50f1
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 41 deletions.
14 changes: 7 additions & 7 deletions components/layout/wrapper.rs
Expand Up @@ -425,7 +425,7 @@ pub struct LayoutElement<'le> {
impl<'le> LayoutElement<'le> {
pub fn style_attribute(&self) -> &'le Option<PropertyDeclarationBlock> {
let style: &Option<PropertyDeclarationBlock> = unsafe {
let style: &RefCell<Option<PropertyDeclarationBlock>> = &self.element.style_attribute;
let style: &RefCell<Option<PropertyDeclarationBlock>> = self.element.style_attribute();
// cast to the direct reference to T placed on the head of RefCell<T>
mem::transmute(style)
};
Expand All @@ -436,12 +436,12 @@ impl<'le> LayoutElement<'le> {
impl<'le> TElement<'le> for LayoutElement<'le> {
#[inline]
fn get_local_name(self) -> &'le Atom {
&self.element.local_name
self.element.local_name()
}

#[inline]
fn get_namespace(self) -> &'le Namespace {
&self.element.namespace
self.element.namespace()
}

#[inline]
Expand All @@ -456,7 +456,7 @@ impl<'le> TElement<'le> for LayoutElement<'le> {

fn get_link(self) -> Option<&'le str> {
// FIXME: This is HTML only.
match self.element.node.type_id_for_layout() {
match self.element.node().type_id_for_layout() {
// http://www.whatwg.org/specs/web-apps/current-work/multipage/selectors.html#
// selector-link
ElementNodeTypeId(HTMLAnchorElementTypeId) |
Expand All @@ -470,7 +470,7 @@ impl<'le> TElement<'le> for LayoutElement<'le> {

fn get_hover_state(self) -> bool {
unsafe {
self.element.node.get_hover_state_for_layout()
self.element.node().get_hover_state_for_layout()
}
}

Expand All @@ -481,13 +481,13 @@ impl<'le> TElement<'le> for LayoutElement<'le> {

fn get_disabled_state(self) -> bool {
unsafe {
self.element.node.get_disabled_state_for_layout()
self.element.node().get_disabled_state_for_layout()
}
}

fn get_enabled_state(self) -> bool {
unsafe {
self.element.node.get_enabled_state_for_layout()
self.element.node().get_enabled_state_for_layout()
}
}

Expand Down
47 changes: 39 additions & 8 deletions components/script/dom/element.rs
Expand Up @@ -35,7 +35,7 @@ use servo_util::namespace;
use servo_util::str::DOMString;

use std::ascii::StrAsciiExt;
use std::cell::RefCell;
use std::cell::{Ref, RefMut, RefCell};
use std::default::Default;
use std::mem;
use std::slice::Items;
Expand All @@ -44,14 +44,15 @@ use url::UrlParser;

#[jstraceable]
#[must_root]
#[privatize]
pub struct Element {
pub node: Node,
pub local_name: Atom,
pub namespace: Namespace,
pub prefix: Option<DOMString>,
pub attrs: RefCell<Vec<JS<Attr>>>,
pub style_attribute: RefCell<Option<style::PropertyDeclarationBlock>>,
pub attr_list: MutNullableJS<NamedNodeMap>,
node: Node,
local_name: Atom,
namespace: Namespace,
prefix: Option<DOMString>,
attrs: RefCell<Vec<JS<Attr>>>,
style_attribute: RefCell<Option<style::PropertyDeclarationBlock>>,
attr_list: MutNullableJS<NamedNodeMap>,
class_list: MutNullableJS<DOMTokenList>,
}

Expand Down Expand Up @@ -171,6 +172,36 @@ impl Element {
pub fn node<'a>(&'a self) -> &'a Node {
&self.node
}

#[inline]
pub fn local_name<'a>(&'a self) -> &'a Atom {
&self.local_name
}

#[inline]
pub fn namespace<'a>(&'a self) -> &'a Namespace {
&self.namespace
}

#[inline]
pub fn prefix<'a>(&'a self) -> &'a Option<DOMString> {
&self.prefix
}

#[inline]
pub fn attrs(&self) -> Ref<Vec<JS<Attr>>> {
self.attrs.borrow()
}

#[inline]
pub fn attrs_mut(&self) -> RefMut<Vec<JS<Attr>>> {
self.attrs.borrow_mut()
}

#[inline]
pub fn style_attribute<'a>(&'a self) -> &'a RefCell<Option<style::PropertyDeclarationBlock>> {
&self.style_attribute
}
}

pub trait RawLayoutElementHelpers {
Expand Down
10 changes: 5 additions & 5 deletions components/script/dom/htmlcollection.rs
Expand Up @@ -67,7 +67,7 @@ impl HTMLCollection {
fn filter(&self, elem: JSRef<Element>, _root: JSRef<Node>) -> bool {
match self.namespace_filter {
None => true,
Some(ref namespace) => elem.namespace == *namespace
Some(ref namespace) => *elem.namespace() == *namespace
}
}
}
Expand All @@ -89,9 +89,9 @@ impl HTMLCollection {
impl CollectionFilter for TagNameFilter {
fn filter(&self, elem: JSRef<Element>, _root: JSRef<Node>) -> bool {
if elem.html_element_in_html_document() {
elem.local_name == self.ascii_lower_tag
*elem.local_name() == self.ascii_lower_tag
} else {
elem.local_name == self.tag
*elem.local_name() == self.tag
}
}
}
Expand Down Expand Up @@ -121,11 +121,11 @@ impl HTMLCollection {
fn filter(&self, elem: JSRef<Element>, _root: JSRef<Node>) -> bool {
let ns_match = match self.namespace_filter {
Some(ref namespace) => {
elem.namespace == *namespace
*elem.namespace() == *namespace
},
None => true
};
ns_match && elem.local_name == self.tag
ns_match && *elem.local_name() == self.tag
}
}
let filter = TagNameNSFilter {
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmlfieldsetelement.rs
Expand Up @@ -58,7 +58,7 @@ impl<'a> HTMLFieldSetElementMethods for JSRef<'a, HTMLFieldSetElement> {
static tag_names: StaticStringVec = &["button", "fieldset", "input",
"keygen", "object", "output", "select", "textarea"];
let root: JSRef<Element> = ElementCast::to_ref(root).unwrap();
elem != root && tag_names.iter().any(|&tag_name| tag_name == elem.local_name.as_slice())
elem != root && tag_names.iter().any(|&tag_name| tag_name == elem.local_name().as_slice())
}
}
let node: JSRef<Node> = NodeCast::from_ref(self);
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/htmloptionelement.rs
Expand Up @@ -51,7 +51,7 @@ impl HTMLOptionElement {

fn collect_text(node: &JSRef<Node>, value: &mut DOMString) {
let elem: JSRef<Element> = ElementCast::to_ref(*node).unwrap();
let svg_script = elem.namespace == ns!(SVG) && elem.local_name.as_slice() == "script";
let svg_script = *elem.namespace() == ns!(SVG) && elem.local_name().as_slice() == "script";
let html_script = node.is_htmlscriptelement();
if svg_script || html_script {
return;
Expand Down
14 changes: 7 additions & 7 deletions components/script/dom/htmlserializer.rs
Expand Up @@ -78,10 +78,10 @@ fn serialize_text(text: JSRef<Text>, html: &mut String) {
match text_node.parent_node().map(|node| node.root()) {
Some(ref parent) if parent.is_element() => {
let elem: JSRef<Element> = ElementCast::to_ref(**parent).unwrap();
match elem.local_name.as_slice() {
match elem.local_name().as_slice() {
"style" | "script" | "xmp" | "iframe" |
"noembed" | "noframes" | "plaintext" |
"noscript" if elem.namespace == ns!(HTML)
"noscript" if *elem.namespace() == ns!(HTML)
=> html.push_str(text.characterdata().data().as_slice()),
_ => escape(text.characterdata().data().as_slice(), false, html)
}
Expand All @@ -107,15 +107,15 @@ fn serialize_doctype(doctype: JSRef<DocumentType>, html: &mut String) {

fn serialize_elem(elem: JSRef<Element>, open_elements: &mut Vec<String>, html: &mut String) {
html.push_char('<');
html.push_str(elem.local_name.as_slice());
for attr in elem.attrs.borrow().iter() {
html.push_str(elem.local_name().as_slice());
for attr in elem.attrs().iter() {
let attr = attr.root();
serialize_attr(*attr, html);
};
html.push_char('>');

match elem.local_name.as_slice() {
"pre" | "listing" | "textarea" if elem.namespace == ns!(HTML) => {
match elem.local_name().as_slice() {
"pre" | "listing" | "textarea" if *elem.namespace() == ns!(HTML) => {
let node: JSRef<Node> = NodeCast::from_ref(elem);
match node.first_child().map(|child| child.root()) {
Some(ref child) if child.is_text() => {
Expand All @@ -131,7 +131,7 @@ fn serialize_elem(elem: JSRef<Element>, open_elements: &mut Vec<String>, html: &
}

if !(elem.is_void()) {
open_elements.push(elem.local_name.as_slice().to_string());
open_elements.push(elem.local_name().as_slice().to_string());
}
}

Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/namednodemap.rs
Expand Up @@ -35,11 +35,11 @@ impl NamedNodeMap {

impl<'a> NamedNodeMapMethods for JSRef<'a, NamedNodeMap> {
fn Length(self) -> u32 {
self.owner.root().attrs.borrow().len() as u32
self.owner.root().attrs().len() as u32
}

fn Item(self, index: u32) -> Option<Temporary<Attr>> {
self.owner.root().attrs.borrow().as_slice().get(index as uint).map(|x| Temporary::new(x.clone()))
self.owner.root().attrs().as_slice().get(index as uint).map(|x| Temporary::new(x.clone()))
}

fn IndexedGetter(self, index: u32, found: &mut bool) -> Option<Temporary<Attr>> {
Expand Down
20 changes: 10 additions & 10 deletions components/script/dom/node.rs
Expand Up @@ -1501,8 +1501,8 @@ impl Node {
},
ElementNodeTypeId(..) => {
let element: JSRef<Element> = ElementCast::to_ref(node).unwrap();
let element = build_element_from_tag(element.local_name.as_slice().to_string(),
element.namespace.clone(), Some(element.prefix.as_slice().to_string()), *document);
let element = build_element_from_tag(element.local_name().as_slice().to_string(),
element.namespace().clone(), Some(element.prefix().as_slice().to_string()), *document);
NodeCast::from_temporary(element)
},
TextNodeTypeId => {
Expand Down Expand Up @@ -1541,8 +1541,8 @@ impl Node {

// FIXME: https://github.com/mozilla/servo/issues/1737
let window = document.window().root();
for attr in node_elem.attrs.borrow().iter().map(|attr| attr.root()) {
copy_elem.attrs.borrow_mut().push_unrooted(
for attr in node_elem.attrs().iter().map(|attr| attr.root()) {
copy_elem.attrs_mut().push_unrooted(
&Attr::new(*window,
attr.local_name().clone(), attr.value().clone(),
attr.name().clone(), attr.namespace().clone(),
Expand Down Expand Up @@ -1983,9 +1983,9 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
let element: JSRef<Element> = ElementCast::to_ref(node).unwrap();
let other_element: JSRef<Element> = ElementCast::to_ref(other).unwrap();
// FIXME: namespace prefix
(element.namespace == other_element.namespace) &&
(element.local_name == other_element.local_name) &&
(element.attrs.borrow().len() == other_element.attrs.borrow().len())
(*element.namespace() == *other_element.namespace()) &&
(*element.local_name() == *other_element.local_name()) &&
(element.attrs().len() == other_element.attrs().len())
}
fn is_equal_processinginstruction(node: JSRef<Node>, other: JSRef<Node>) -> bool {
let pi: JSRef<ProcessingInstruction> = ProcessingInstructionCast::to_ref(node).unwrap();
Expand All @@ -2001,9 +2001,9 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
fn is_equal_element_attrs(node: JSRef<Node>, other: JSRef<Node>) -> bool {
let element: JSRef<Element> = ElementCast::to_ref(node).unwrap();
let other_element: JSRef<Element> = ElementCast::to_ref(other).unwrap();
assert!(element.attrs.borrow().len() == other_element.attrs.borrow().len());
element.attrs.borrow().iter().map(|attr| attr.root()).all(|attr| {
other_element.attrs.borrow().iter().map(|attr| attr.root()).any(|other_attr| {
assert!(element.attrs().len() == other_element.attrs().len());
element.attrs().iter().map(|attr| attr.root()).all(|attr| {
other_element.attrs().iter().map(|attr| attr.root()).any(|other_attr| {
(*attr.namespace() == *other_attr.namespace()) &&
(attr.local_name() == other_attr.local_name()) &&
(attr.value().as_slice() == other_attr.value().as_slice())
Expand Down

9 comments on commit cbe50f1

@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.

merging ttaubert/servo/issue/3644-privatize-dom = cbe50f1 into auto

@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.

ttaubert/servo/issue/3644-privatize-dom = cbe50f1 merged ok, testing candidate = 5b8ee94

@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.

@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 ttaubert/servo/issue/3644-privatize-dom = cbe50f1 into auto

@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.

ttaubert/servo/issue/3644-privatize-dom = cbe50f1 merged ok, testing candidate = f350879

@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 auto = f350879

Please sign in to comment.