Cache the first base element with an href attribute on the Document.

This commit is contained in:
Ms2ger 2015-07-28 19:33:20 +02:00 committed by Ms2ger
parent bd31b51a87
commit 6951119f5e
2 changed files with 56 additions and 3 deletions

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::attr::AttrHelpers;
use dom::attr::{Attr, AttrHelpers};
use dom::bindings::codegen::Bindings::HTMLBaseElementBinding;
use dom::bindings::codegen::InheritTypes::ElementCast;
use dom::bindings::codegen::InheritTypes::HTMLBaseElementDerived;
@ -55,10 +55,52 @@ impl HTMLBaseElement {
let parsed = UrlParser::new().base_url(&base).parse(&href.value());
parsed.unwrap_or(base)
}
/// Update the cached base element in response to adding or removing an
/// attribute.
pub fn add_remove_attr(&self, attr: &Attr) {
if *attr.local_name() == atom!("href") {
let document = document_from_node(self);
document.refresh_base_element();
}
}
/// Update the cached base element in response to binding or unbinding from
/// a tree.
pub fn bind_unbind(&self, tree_in_doc: bool) {
if !tree_in_doc {
return;
}
if ElementCast::from_ref(self).has_attribute(&atom!("href")) {
let document = document_from_node(self);
document.refresh_base_element();
}
}
}
impl<'a> VirtualMethods for &'a HTMLBaseElement {
fn super_type<'b>(&'b self) -> Option<&'b VirtualMethods> {
Some(HTMLElementCast::from_borrowed_ref(self) as &VirtualMethods)
}
fn after_set_attr(&self, attr: &Attr) {
self.super_type().unwrap().after_set_attr(attr);
self.add_remove_attr(attr);
}
fn before_remove_attr(&self, attr: &Attr) {
self.super_type().unwrap().before_remove_attr(attr);
self.add_remove_attr(attr);
}
fn bind_to_tree(&self, tree_in_doc: bool) {
self.super_type().unwrap().bind_to_tree(tree_in_doc);
self.bind_unbind(tree_in_doc);
}
fn unbind_from_tree(&self, tree_in_doc: bool) {
self.super_type().unwrap().unbind_from_tree(tree_in_doc);
self.bind_unbind(tree_in_doc);
}
}