Introduce VirtualMethods::attribute_mutated()

This replaces before_remove_attr(), after_remove_attr() and after_set_attr().
The virtual method takes the mutated attribute and an AttributeMutation value
to disambiguate between "attribute is changed", "attribute is added" and
"attribute is removed".

In the case of "attribute is changed", the mutation value contains a reference
to the old value of the mutated attribute, which is used to unregister outdated
named elements when the "id" attribute is changed on an element.

This greatly simplifies the handling of attributes, which in many cases don't
have any specific behaviour whether they are removed or changed or added. It
also fixes a few bugs where things were put in before_remove_attr() instead of
after_remove_attr() (e.g. when removing an href attribute from a base element).

A few helper functions in Element were also renamed and made private.
This commit is contained in:
Anthony Ramine 2015-08-28 13:32:38 +02:00
parent 5672142042
commit 58e1bd0e57
27 changed files with 565 additions and 862 deletions

View file

@ -33,7 +33,7 @@ use dom::bindings::codegen::InheritTypes::HTMLTableSectionElementCast;
use dom::bindings::codegen::InheritTypes::HTMLTextAreaElementCast;
use dom::bindings::codegen::InheritTypes::HTMLTitleElementCast;
use dom::document::Document;
use dom::element::ElementTypeId;
use dom::element::{AttributeMutation, ElementTypeId};
use dom::event::Event;
use dom::htmlelement::HTMLElementTypeId;
use dom::node::NodeTypeId;
@ -50,27 +50,12 @@ pub trait VirtualMethods {
/// if any.
fn super_type(&self) -> Option<&VirtualMethods>;
/// Called when changing or adding attributes, after the attribute's value
/// has been updated.
fn after_set_attr(&self, attr: &Attr) {
if let Some(ref s) = self.super_type() {
s.after_set_attr(attr);
}
}
/// Called when changing or removing attributes, before any modification
/// has taken place.
fn before_remove_attr(&self, attr: &Attr) {
if let Some(ref s) = self.super_type() {
s.before_remove_attr(attr);
}
}
/// Called when changing or removing attributes, after all modification
/// has taken place.
fn after_remove_attr(&self, name: &Atom) {
if let Some(ref s) = self.super_type() {
s.after_remove_attr(name);
/// Called when attributes of a node are mutated.
/// https://dom.spec.whatwg.org/#attribute-is-set
/// https://dom.spec.whatwg.org/#attribute-is-removed
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
if let Some(s) = self.super_type() {
s.attribute_mutated(attr, mutation);
}
}