Auto merge of #5575 - nox:namednodemap-remove, r=Ms2ger

This commit is contained in:
bors-servo 2015-04-08 13:22:29 -05:00
commit 0f0dd02daa
4 changed files with 43 additions and 22 deletions

View file

@ -627,11 +627,13 @@ pub trait AttributeHandlers {
/// Removes the first attribute with any given namespace and case-sensitive local /// Removes the first attribute with any given namespace and case-sensitive local
/// name, if any. /// name, if any.
fn remove_attribute(self, namespace: &Namespace, local_name: &Atom); fn remove_attribute(self, namespace: &Namespace, local_name: &Atom)
-> Option<Temporary<Attr>>;
/// Removes the first attribute with any namespace and given case-sensitive name. /// Removes the first attribute with any namespace and given case-sensitive name.
fn remove_attribute_by_name(self, name: &Atom); fn remove_attribute_by_name(self, name: &Atom) -> Option<Temporary<Attr>>;
/// Removes the first attribute that satisfies `find`. /// Removes the first attribute that satisfies `find`.
fn do_remove_attribute<F>(self, find: F) where F: Fn(JSRef<Attr>) -> bool; fn do_remove_attribute<F>(self, find: F) -> Option<Temporary<Attr>>
where F: Fn(JSRef<Attr>) -> bool;
fn has_class(self, name: &Atom) -> bool; fn has_class(self, name: &Atom) -> bool;
@ -764,22 +766,25 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
} }
} }
fn remove_attribute(self, namespace: &Namespace, local_name: &Atom) { fn remove_attribute(self, namespace: &Namespace, local_name: &Atom)
-> Option<Temporary<Attr>> {
self.do_remove_attribute(|attr| { self.do_remove_attribute(|attr| {
attr.namespace() == namespace && attr.local_name() == local_name attr.namespace() == namespace && attr.local_name() == local_name
}); })
} }
fn remove_attribute_by_name(self, name: &Atom) { fn remove_attribute_by_name(self, name: &Atom) -> Option<Temporary<Attr>> {
self.do_remove_attribute(|attr| attr.name() == name); self.do_remove_attribute(|attr| attr.name() == name)
} }
fn do_remove_attribute<F>(self, find: F) where F: Fn(JSRef<Attr>) -> bool { fn do_remove_attribute<F>(self, find: F) -> Option<Temporary<Attr>>
where F: Fn(JSRef<Attr>) -> bool
{
let idx = self.attrs.borrow().iter() let idx = self.attrs.borrow().iter()
.map(|attr| attr.root()) .map(|attr| attr.root())
.position(|attr| find(attr.r())); .position(|attr| find(attr.r()));
if let Some(idx) = idx { idx.map(|idx| {
let attr = (*self.attrs.borrow())[idx].root(); let attr = (*self.attrs.borrow())[idx].root();
if attr.r().namespace() == &ns!("") { if attr.r().namespace() == &ns!("") {
vtable_for(&NodeCast::from_ref(self)).before_remove_attr(attr.r()); vtable_for(&NodeCast::from_ref(self)).before_remove_attr(attr.r());
@ -798,7 +803,8 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
}; };
document.r().content_changed(node, damage); document.r().content_changed(node, damage);
} }
}; Temporary::from_rooted(attr.r())
})
} }
fn has_class(self, name: &Atom) -> bool { fn has_class(self, name: &Atom) -> bool {

View file

@ -5,6 +5,7 @@
use dom::attr::Attr; use dom::attr::Attr;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding; use dom::bindings::codegen::Bindings::NamedNodeMapBinding;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods; use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, JSRef, Temporary}; use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::utils::{Reflector, reflect_dom_object}; use dom::bindings::utils::{Reflector, reflect_dom_object};
@ -64,12 +65,32 @@ impl<'a> NamedNodeMapMethods for JSRef<'a, NamedNodeMap> {
} }
// https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns // https://dom.spec.whatwg.org/#dom-namednodemap-getnameditemns
fn GetNamedItemNS(self, namespace: Option<DOMString>, name: DOMString) -> Option<Temporary<Attr>> { fn GetNamedItemNS(self, namespace: Option<DOMString>, local_name: DOMString)
-> Option<Temporary<Attr>> {
let owner = self.owner.root(); let owner = self.owner.root();
// FIXME(https://github.com/rust-lang/rust/issues/23338) // FIXME(https://github.com/rust-lang/rust/issues/23338)
let owner = owner.r(); let owner = owner.r();
let ns = namespace::from_domstring(namespace); let ns = namespace::from_domstring(namespace);
owner.get_attribute(&ns, &Atom::from_slice(&name)) owner.get_attribute(&ns, &Atom::from_slice(&local_name))
}
// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
fn RemoveNamedItem(self, name: DOMString) -> Fallible<Temporary<Attr>> {
let owner = self.owner.root();
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let owner = owner.r();
let name = owner.parsed_name(name);
owner.remove_attribute_by_name(&Atom::from_slice(&name)).ok_or(Error::NotFound)
}
// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns
fn RemoveNamedItemNS(self, namespace: Option<DOMString>, local_name: DOMString)
-> Fallible<Temporary<Attr>> {
let owner = self.owner.root();
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let owner = owner.r();
let ns = namespace::from_domstring(namespace);
owner.remove_attribute(&ns, &Atom::from_slice(&local_name)).ok_or(Error::NotFound)
} }
fn IndexedGetter(self, index: u32, found: &mut bool) -> Option<Temporary<Attr>> { fn IndexedGetter(self, index: u32, found: &mut bool) -> Option<Temporary<Attr>> {

View file

@ -11,8 +11,8 @@ interface NamedNodeMap {
//Attr? setNamedItem(Attr attr); //Attr? setNamedItem(Attr attr);
//[Throws] //[Throws]
//Attr? setNamedItemNS(Attr attr); //Attr? setNamedItemNS(Attr attr);
//[Throws] [Throws]
//Attr removeNamedItem(DOMString name); Attr removeNamedItem(DOMString name);
//[Throws] [Throws]
//Attr removeNamedItemNS(DOMString? namespace, DOMString name); Attr removeNamedItemNS(DOMString? namespace, DOMString name);
}; };

View file

@ -345,12 +345,6 @@
[NamedNodeMap interface: operation setNamedItemNS(Attr)] [NamedNodeMap interface: operation setNamedItemNS(Attr)]
expected: FAIL expected: FAIL
[NamedNodeMap interface: operation removeNamedItem(DOMString)]
expected: FAIL
[NamedNodeMap interface: operation removeNamedItemNS(DOMString,DOMString)]
expected: FAIL
[CharacterData interface: attribute previousElementSibling] [CharacterData interface: attribute previousElementSibling]
expected: FAIL expected: FAIL