Privatize Element

This commit is contained in:
Tim Taubert 2014-10-13 11:11:05 +02:00
parent cd9de05088
commit cbe50f1f14
8 changed files with 72 additions and 41 deletions

View file

@ -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;
@ -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>,
}
@ -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 {

View file

@ -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
}
}
}
@ -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
}
}
}
@ -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 {

View file

@ -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);

View file

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

View file

@ -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)
}
@ -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() => {
@ -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());
}
}

View file

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

View file

@ -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 => {
@ -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(),
@ -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();
@ -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())