Factor the name-related fields of Attr into a struct and move it to style.

This commit is contained in:
Bobby Holley 2015-11-03 12:16:53 -08:00
parent 488c1291d4
commit c9704992a9
3 changed files with 31 additions and 23 deletions

View file

@ -17,18 +17,15 @@ use std::borrow::ToOwned;
use std::cell::Ref; use std::cell::Ref;
use std::mem; use std::mem;
use string_cache::{Atom, Namespace}; use string_cache::{Atom, Namespace};
pub use style::attr::AttrValue; pub use style::attr::{AttrIdentifier, AttrValue};
use util::str::DOMString; use util::str::DOMString;
// https://dom.spec.whatwg.org/#interface-attr // https://dom.spec.whatwg.org/#interface-attr
#[dom_struct] #[dom_struct]
pub struct Attr { pub struct Attr {
reflector_: Reflector, reflector_: Reflector,
local_name: Atom, identifier: AttrIdentifier,
value: DOMRefCell<AttrValue>, value: DOMRefCell<AttrValue>,
name: Atom,
namespace: Namespace,
prefix: Option<Atom>,
/// the element that owns this attribute. /// the element that owns this attribute.
owner: MutNullableHeap<JS<Element>>, owner: MutNullableHeap<JS<Element>>,
@ -39,11 +36,13 @@ impl Attr {
prefix: Option<Atom>, owner: Option<&Element>) -> Attr { prefix: Option<Atom>, owner: Option<&Element>) -> Attr {
Attr { Attr {
reflector_: Reflector::new(), reflector_: Reflector::new(),
identifier: AttrIdentifier {
local_name: local_name, local_name: local_name,
value: DOMRefCell::new(value),
name: name, name: name,
namespace: namespace, namespace: namespace,
prefix: prefix, prefix: prefix,
},
value: DOMRefCell::new(value),
owner: MutNullableHeap::new(owner), owner: MutNullableHeap::new(owner),
} }
} }
@ -59,17 +58,17 @@ impl Attr {
#[inline] #[inline]
pub fn name(&self) -> &Atom { pub fn name(&self) -> &Atom {
&self.name &self.identifier.name
} }
#[inline] #[inline]
pub fn namespace(&self) -> &Namespace { pub fn namespace(&self) -> &Namespace {
&self.namespace &self.identifier.namespace
} }
#[inline] #[inline]
pub fn prefix(&self) -> &Option<Atom> { pub fn prefix(&self) -> &Option<Atom> {
&self.prefix &self.identifier.prefix
} }
} }
@ -89,7 +88,7 @@ impl AttrMethods for Attr {
match self.owner() { match self.owner() {
None => *self.value.borrow_mut() = AttrValue::String(value), None => *self.value.borrow_mut() = AttrValue::String(value),
Some(owner) => { Some(owner) => {
let value = owner.parse_attribute(&self.namespace, self.local_name(), value); let value = owner.parse_attribute(&self.identifier.namespace, self.local_name(), value);
self.set_value(value, owner.r()); self.set_value(value, owner.r());
} }
} }
@ -117,12 +116,12 @@ impl AttrMethods for Attr {
// https://dom.spec.whatwg.org/#dom-attr-name // https://dom.spec.whatwg.org/#dom-attr-name
fn Name(&self) -> DOMString { fn Name(&self) -> DOMString {
DOMString((*self.name).to_owned()) DOMString((*self.identifier.name).to_owned())
} }
// https://dom.spec.whatwg.org/#dom-attr-namespaceuri // https://dom.spec.whatwg.org/#dom-attr-namespaceuri
fn GetNamespaceURI(&self) -> Option<DOMString> { fn GetNamespaceURI(&self) -> Option<DOMString> {
let Namespace(ref atom) = self.namespace; let Namespace(ref atom) = self.identifier.namespace;
match &**atom { match &**atom {
"" => None, "" => None,
url => Some(DOMString(url.to_owned())), url => Some(DOMString(url.to_owned())),
@ -150,7 +149,7 @@ impl Attr {
pub fn set_value(&self, mut value: AttrValue, owner: &Element) { pub fn set_value(&self, mut value: AttrValue, owner: &Element) {
assert!(Some(owner) == self.owner().r()); assert!(Some(owner) == self.owner().r());
mem::swap(&mut *self.value.borrow_mut(), &mut value); mem::swap(&mut *self.value.borrow_mut(), &mut value);
if self.namespace == ns!("") { if self.identifier.namespace == ns!("") {
vtable_for(owner.upcast()).attribute_mutated( vtable_for(owner.upcast()).attribute_mutated(
self, AttributeMutation::Set(Some(&value))); self, AttributeMutation::Set(Some(&value)));
} }
@ -161,21 +160,21 @@ impl Attr {
} }
pub fn local_name(&self) -> &Atom { pub fn local_name(&self) -> &Atom {
&self.local_name &self.identifier.local_name
} }
/// Sets the owner element. Should be called after the attribute is added /// Sets the owner element. Should be called after the attribute is added
/// or removed from its older parent. /// or removed from its older parent.
pub fn set_owner(&self, owner: Option<&Element>) { pub fn set_owner(&self, owner: Option<&Element>) {
let ref ns = self.namespace; let ref ns = self.identifier.namespace;
match (self.owner().r(), owner) { match (self.owner().r(), owner) {
(None, Some(new)) => { (None, Some(new)) => {
// Already in the list of attributes of new owner. // Already in the list of attributes of new owner.
assert!(new.get_attribute(&ns, &self.local_name) == Some(Root::from_ref(self))) assert!(new.get_attribute(&ns, &self.identifier.local_name) == Some(Root::from_ref(self)))
} }
(Some(old), None) => { (Some(old), None) => {
// Already gone from the list of attributes of old owner. // Already gone from the list of attributes of old owner.
assert!(old.get_attribute(&ns, &self.local_name).is_none()) assert!(old.get_attribute(&ns, &self.identifier.local_name).is_none())
} }
(old, new) => assert!(old == new) (old, new) => assert!(old == new)
} }
@ -187,7 +186,7 @@ impl Attr {
} }
pub fn summarize(&self) -> AttrInfo { pub fn summarize(&self) -> AttrInfo {
let Namespace(ref ns) = self.namespace; let Namespace(ref ns) = self.identifier.namespace;
AttrInfo { AttrInfo {
namespace: (**ns).to_owned(), namespace: (**ns).to_owned(),
name: self.Name().0, name: self.Name().0,
@ -239,7 +238,7 @@ impl AttrHelpersForLayout for LayoutJS<Attr> {
#[inline] #[inline]
unsafe fn local_name_atom_forever(&self) -> Atom { unsafe fn local_name_atom_forever(&self) -> Atom {
(*self.unsafe_get()).local_name.clone() (*self.unsafe_get()).identifier.local_name.clone()
} }
#[inline] #[inline]

View file

@ -82,7 +82,7 @@ use std::sync::Arc;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use string_cache::{Atom, Namespace, QualName}; use string_cache::{Atom, Namespace, QualName};
use style::attr::AttrValue; use style::attr::{AttrIdentifier, AttrValue};
use style::properties::PropertyDeclarationBlock; use style::properties::PropertyDeclarationBlock;
use style::values::specified::Length; use style::values::specified::Length;
use url::Url; use url::Url;
@ -290,6 +290,7 @@ no_jsmanaged_fields!(Length);
no_jsmanaged_fields!(ElementState); no_jsmanaged_fields!(ElementState);
no_jsmanaged_fields!(DOMString); no_jsmanaged_fields!(DOMString);
no_jsmanaged_fields!(Mime); no_jsmanaged_fields!(Mime);
no_jsmanaged_fields!(AttrIdentifier);
no_jsmanaged_fields!(AttrValue); no_jsmanaged_fields!(AttrValue);
impl JSTraceable for Box<ScriptChan + Send> { impl JSTraceable for Box<ScriptChan + Send> {

View file

@ -168,3 +168,11 @@ impl Deref for AttrValue {
} }
} }
} }
#[derive(Clone, HeapSizeOf, Debug)]
pub struct AttrIdentifier {
pub local_name: Atom,
pub name: Atom,
pub namespace: Namespace,
pub prefix: Option<Atom>,
}