From 3342b6100dee2ce3708c6a062b0d861cc6eeaf8c Mon Sep 17 00:00:00 2001 From: Jessica Jong Date: Thu, 20 Jul 2017 02:15:00 -0400 Subject: [PATCH] Bug 1375599 - Change IsDisabled() to look at NS_EVENT_STATE_DISABLED instead of the "disabled" attribute. r=bz In order to speed up IsDisabled(), instead of querying for the @disabled attribute, we're now using the NS_EVENT_STATE_DISABLED flag to know whether an element is disabled. It is safe to use the NS_EVENT_STATE_DISABLED flag for the following reasons: - For form elements, nsGenericHTMLFormElement::IsDisabled() is only called on form elements that can be disabled; form elements that can't be disabled overrides IsDisabled() to return false directly. And, before this patch, NS_EVENT_STATE_DISABLED flag is set by nsGenericHTMLFormElement::IntrinsicState() if and only if IsDisabled() in all cases when CanBeDisabled() is true, and when CanBeDisabled() is false then IsDisabled() is always false and the flag is not set. - For non form elements, optgroup and option have the flag matching IsDisabled(). Note that option's IsDisabled() should also refer to optgroup's (if it exists) disabled state, which was not done before this patch. For this to work correctly, we need to set NS_EVENT_STATE_DISABLED earlier, that is, in AfterSetAttr(), before any consumer of IsDisabled(). We also need to update the flag whenever the element's parent (e.g. fieldset or optgroup) disabled state changes and when moving into/out of a parent container. Note that NS_EVENT_STATE_DISABLED/ENABLED is now part of the EXTERNALLY_MANAGED_STATES. MozReview-Commit-ID: KSceikeqvvU --- dom/events/EventStates.h | 3 ++ dom/html/HTMLButtonElement.cpp | 13 +++++- dom/html/HTMLFieldSetElement.cpp | 25 ++++++----- dom/html/HTMLInputElement.cpp | 15 ++++++- dom/html/HTMLLabelElement.h | 2 - dom/html/HTMLOptGroupElement.cpp | 44 +++++++++--------- dom/html/HTMLOptGroupElement.h | 4 +- dom/html/HTMLOptionElement.cpp | 75 ++++++++++++++++++++----------- dom/html/HTMLOptionElement.h | 15 ++++++- dom/html/HTMLSelectElement.cpp | 15 ++++++- dom/html/HTMLTextAreaElement.cpp | 15 ++++++- dom/html/nsGenericHTMLElement.cpp | 43 ++++++++++++------ dom/html/nsGenericHTMLElement.h | 6 +++ 13 files changed, 188 insertions(+), 87 deletions(-) diff --git a/dom/events/EventStates.h b/dom/events/EventStates.h index 1cfcd4154093..8e3486b8fe3b 100644 --- a/dom/events/EventStates.h +++ b/dom/events/EventStates.h @@ -328,6 +328,8 @@ private: NS_EVENT_STATE_DIR_ATTR_RTL | \ NS_EVENT_STATE_DIR_ATTR_LIKE_AUTO) +#define DISABLED_STATES (NS_EVENT_STATE_DISABLED | NS_EVENT_STATE_ENABLED) + // Event states that can be added and removed through // Element::{Add,Remove}ManuallyManagedStates. // @@ -347,6 +349,7 @@ private: #define EXTERNALLY_MANAGED_STATES ( \ MANUALLY_MANAGED_STATES | \ DIR_ATTR_STATES | \ + DISABLED_STATES | \ NS_EVENT_STATE_ACTIVE | \ NS_EVENT_STATE_DRAGOVER | \ NS_EVENT_STATE_FOCUS | \ diff --git a/dom/html/HTMLButtonElement.cpp b/dom/html/HTMLButtonElement.cpp index 5e1fd2eacd4f..5100d696053a 100644 --- a/dom/html/HTMLButtonElement.cpp +++ b/dom/html/HTMLButtonElement.cpp @@ -106,9 +106,14 @@ HTMLButtonElement::UpdateBarredFromConstraintValidation() void HTMLButtonElement::FieldSetDisabledChanged(bool aNotify) { - UpdateBarredFromConstraintValidation(); + // FieldSetDisabledChanged *has* to be called *before* + // UpdateBarredFromConstraintValidation, because the latter depends on our + // disabled state. nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify); + + UpdateBarredFromConstraintValidation(); + UpdateState(aNotify); } // nsIDOMHTMLButtonElement @@ -436,6 +441,12 @@ HTMLButtonElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, } if (aName == nsGkAtoms::type || aName == nsGkAtoms::disabled) { + if (aName == nsGkAtoms::disabled) { + // This *has* to be called *before* validity state check because + // UpdateBarredFromConstraintValidation depends on our disabled state. + UpdateDisabledState(aNotify); + } + UpdateBarredFromConstraintValidation(); } } diff --git a/dom/html/HTMLFieldSetElement.cpp b/dom/html/HTMLFieldSetElement.cpp index b6614211701c..cf4b585f2293 100644 --- a/dom/html/HTMLFieldSetElement.cpp +++ b/dom/html/HTMLFieldSetElement.cpp @@ -83,17 +83,22 @@ HTMLFieldSetElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, const nsAttrValue* aValue, const nsAttrValue* aOldValue, bool aNotify) { - if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled && - nsINode::GetFirstChild()) { - if (!mElements) { - mElements = new nsContentList(this, MatchListedElements, nullptr, nullptr, - true); - } + if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled) { + // This *has* to be called *before* calling FieldSetDisabledChanged on our + // controls, as they may depend on our disabled state. + UpdateDisabledState(aNotify); - uint32_t length = mElements->Length(true); - for (uint32_t i=0; i(mElements->Item(i)) - ->FieldSetDisabledChanged(aNotify); + if (nsINode::GetFirstChild()) { + if (!mElements) { + mElements = new nsContentList(this, MatchListedElements, nullptr, nullptr, + true); + } + + uint32_t length = mElements->Length(true); + for (uint32_t i=0; i(mElements->Item(i)) + ->FieldSetDisabledChanged(aNotify); + } } } diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index 8cdb0746937b..bff106abbb11 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -1446,6 +1446,13 @@ HTMLInputElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, if (aName == nsGkAtoms::required || aName == nsGkAtoms::disabled || aName == nsGkAtoms::readonly) { + if (aName == nsGkAtoms::disabled) { + // This *has* to be called *before* validity state check because + // UpdateBarredFromConstraintValidation and + // UpdateValueMissingValidityState depend on our disabled state. + UpdateDisabledState(aNotify); + } + UpdateValueMissingValidityState(); // This *has* to be called *after* validity has changed. @@ -7463,10 +7470,14 @@ HTMLInputElement::HasCachedSelection() void HTMLInputElement::FieldSetDisabledChanged(bool aNotify) { + // This *has* to be called *before* UpdateBarredFromConstraintValidation and + // UpdateValueMissingValidityState because these two functions depend on our + // disabled state. + nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify); + UpdateValueMissingValidityState(); UpdateBarredFromConstraintValidation(); - - nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify); + UpdateState(aNotify); } void diff --git a/dom/html/HTMLLabelElement.h b/dom/html/HTMLLabelElement.h index 9b4906bbdc73..915c39318a84 100644 --- a/dom/html/HTMLLabelElement.h +++ b/dom/html/HTMLLabelElement.h @@ -59,8 +59,6 @@ public: using nsGenericHTMLElement::Focus; virtual void Focus(mozilla::ErrorResult& aError) override; - virtual bool IsDisabled() const override { return false; } - // nsIContent virtual nsresult PostHandleEvent( EventChainPostVisitor& aVisitor) override; diff --git a/dom/html/HTMLOptGroupElement.cpp b/dom/html/HTMLOptGroupElement.cpp index 8d122d66ca1a..da7d79d69027 100644 --- a/dom/html/HTMLOptGroupElement.cpp +++ b/dom/html/HTMLOptGroupElement.cpp @@ -102,13 +102,27 @@ HTMLOptGroupElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, const nsAttrValue* aOldValue, bool aNotify) { if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled) { - // All our children