From fd0dadbddf2b80825f50dad348c29c405cf968c8 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Fri, 3 Jan 2014 02:02:14 +0900 Subject: [PATCH] Enable only remove operation for Document::update_idmap(). --- src/components/script/dom/document.rs | 30 ++++++++++++------- src/components/script/dom/element.rs | 2 +- .../content/test_document_getElementById.html | 5 ++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 15a952cd418..7e051d2a24a 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -406,23 +406,33 @@ impl Document { pub fn update_idmap(&mut self, abstract_self: AbstractNode, - new_id: DOMString, + new_id: Option, old_id: Option) { - // remove old ids if the old ones are not same as the new one. + // remove old ids: + // * if the old ones are not same as the new one, + // * OR if the new one is none. match old_id { - Some(ref old_id) if new_id != *old_id => { + Some(ref old_id) if new_id.is_none() || + (*new_id.get_ref() != *old_id) => { self.idmap.remove(old_id); } _ => () } - // TODO: support the case if multiple elements which haves same id are in the same document. - self.idmap.mangle(new_id, abstract_self, |_, new_node: AbstractNode| -> AbstractNode { - new_node - }, - |_, old_node: &mut AbstractNode, new_node: AbstractNode| { - *old_node = new_node; - }); + match new_id { + Some(new_id) => { + // TODO: support the case if multiple elements + // which haves same id are in the same document. + self.idmap.mangle(new_id, abstract_self, + |_, new_node: AbstractNode| -> AbstractNode { + new_node + }, + |_, old_node: &mut AbstractNode, new_node: AbstractNode| { + *old_node = new_node; + }); + } + None => () + } } } diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index e2746440c5f..aae0a7c3563 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -221,7 +221,7 @@ impl Element { "id" => { let doc = self.node.owner_doc(); let doc = doc.mut_document(); - doc.update_idmap(abstract_self, value.clone(), old_value); + doc.update_idmap(abstract_self, Some(value.clone()), old_value); } _ => () } diff --git a/src/test/html/content/test_document_getElementById.html b/src/test/html/content/test_document_getElementById.html index 069db0ee4b7..dcb1d5bb08c 100644 --- a/src/test/html/content/test_document_getElementById.html +++ b/src/test/html/content/test_document_getElementById.html @@ -59,6 +59,11 @@ let old = document.getElementById(TEST_ID); is(old, null, "test3-1, the method shouldn't get the element by the old id."); + + // remove id. + test.removeAttribute("id"); + let e2 = document.getElementById(UPDATED_ID); + is(e2, null, "test3-2, the method should return null when the passed id is none in document."); } // TODO: