Skip to content

Commit

Permalink
Attr is a Node, with consequences for many Node methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pshaughn committed Dec 24, 2019
1 parent 3e77a0a commit 67e9fc8
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 301 deletions.
48 changes: 15 additions & 33 deletions components/script/dom/attr.rs
Expand Up @@ -5,15 +5,14 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::customelementregistry::CallbackReaction;
use crate::dom::document::Document;
use crate::dom::element::{AttributeMutation, Element};
use crate::dom::mutationobserver::{Mutation, MutationObserver};
use crate::dom::node::Node;
use crate::dom::virtualmethods::vtable_for;
use crate::dom::window::Window;
use crate::script_thread::ScriptThread;
use devtools_traits::AttrInfo;
use dom_struct::dom_struct;
Expand All @@ -27,7 +26,7 @@ use style::attr::{AttrIdentifier, AttrValue};
// https://dom.spec.whatwg.org/#interface-attr
#[dom_struct]
pub struct Attr {
reflector_: Reflector,
node_: Node,
identifier: AttrIdentifier,
value: DomRefCell<AttrValue>,

Expand All @@ -37,6 +36,7 @@ pub struct Attr {

impl Attr {
fn new_inherited(
document: &Document,
local_name: LocalName,
value: AttrValue,
name: LocalName,
Expand All @@ -45,7 +45,7 @@ impl Attr {
owner: Option<&Element>,
) -> Attr {
Attr {
reflector_: Reflector::new(),
node_: Node::new_inherited(document),
identifier: AttrIdentifier {
local_name: local_name,
name: name,
Expand All @@ -58,19 +58,19 @@ impl Attr {
}

pub fn new(
window: &Window,
document: &Document,
local_name: LocalName,
value: AttrValue,
name: LocalName,
namespace: Namespace,
prefix: Option<Prefix>,
owner: Option<&Element>,
) -> DomRoot<Attr> {
reflect_dom_object(
Node::reflect_node(
Box::new(Attr::new_inherited(
local_name, value, name, namespace, prefix, owner,
document, local_name, value, name, namespace, prefix, owner,
)),
window,
document,
AttrBinding::Wrap,
)
}
Expand Down Expand Up @@ -114,37 +114,12 @@ impl AttrMethods for Attr {
}
}

// https://dom.spec.whatwg.org/#dom-attr-textcontent
fn TextContent(&self) -> DOMString {
self.Value()
}

// https://dom.spec.whatwg.org/#dom-attr-textcontent
fn SetTextContent(&self, value: DOMString) {
self.SetValue(value)
}

// https://dom.spec.whatwg.org/#dom-attr-nodevalue
fn NodeValue(&self) -> DOMString {
self.Value()
}

// https://dom.spec.whatwg.org/#dom-attr-nodevalue
fn SetNodeValue(&self, value: DOMString) {
self.SetValue(value)
}

// https://dom.spec.whatwg.org/#dom-attr-name
fn Name(&self) -> DOMString {
// FIXME(ajeffrey): convert directly from LocalName to DOMString
DOMString::from(&*self.identifier.name)
}

// https://dom.spec.whatwg.org/#dom-attr-nodename
fn NodeName(&self) -> DOMString {
self.Name()
}

// https://dom.spec.whatwg.org/#dom-attr-namespaceuri
fn GetNamespaceURI(&self) -> Option<DOMString> {
match self.identifier.namespace {
Expand Down Expand Up @@ -250,6 +225,13 @@ impl Attr {
value: String::from(self.Value()),
}
}

pub fn qualified_name(&self) -> DOMString {
match self.prefix() {
Some(ref prefix) => DOMString::from(format!("{}:{}", prefix, &**self.local_name())),
None => DOMString::from(&**self.local_name()),
}
}
}

#[allow(unsafe_code)]
Expand Down
4 changes: 2 additions & 2 deletions components/script/dom/document.rs
Expand Up @@ -3685,7 +3685,7 @@ impl DocumentMethods for Document {
let value = AttrValue::String("".to_owned());

Ok(Attr::new(
&self.window,
&self,
name.clone(),
value,
name,
Expand All @@ -3705,7 +3705,7 @@ impl DocumentMethods for Document {
let value = AttrValue::String("".to_owned());
let qualified_name = LocalName::from(qualified_name);
Ok(Attr::new(
&self.window,
&self,
local_name,
value,
qualified_name,
Expand Down
3 changes: 1 addition & 2 deletions components/script/dom/element.rs
Expand Up @@ -1336,9 +1336,8 @@ impl Element {
namespace: Namespace,
prefix: Option<Prefix>,
) {
let window = window_from_node(self);
let attr = Attr::new(
&window,
&self.node.owner_doc(),
local_name,
value,
name,
Expand Down

0 comments on commit 67e9fc8

Please sign in to comment.