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

View file

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