gecko-dev/dom/html/HTMLLabelElement.h
Jessica Jong 3342b6100d 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
2017-07-20 02:15:00 -04:00

86 lines
2.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
/**
* Declaration of HTML <label> elements.
*/
#ifndef HTMLLabelElement_h
#define HTMLLabelElement_h
#include "mozilla/Attributes.h"
#include "nsGenericHTMLElement.h"
#include "nsIDOMHTMLLabelElement.h"
namespace mozilla {
class EventChainPostVisitor;
namespace dom {
class HTMLLabelElement final : public nsGenericHTMLElement,
public nsIDOMHTMLLabelElement
{
public:
explicit HTMLLabelElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsGenericHTMLElement(aNodeInfo),
mHandlingEvent(false)
{
}
NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLLabelElement, label)
// nsISupports
NS_DECL_ISUPPORTS_INHERITED
// Element
virtual bool IsInteractiveHTMLContent(bool aIgnoreTabindex) const override
{
return true;
}
// nsIDOMHTMLLabelElement
NS_DECL_NSIDOMHTMLLABELELEMENT
HTMLFormElement* GetForm() const;
void GetHtmlFor(nsString& aHtmlFor)
{
GetHTMLAttr(nsGkAtoms::_for, aHtmlFor);
}
void SetHtmlFor(const nsAString& aHtmlFor, ErrorResult& aError)
{
SetHTMLAttr(nsGkAtoms::_for, aHtmlFor, aError);
}
nsGenericHTMLElement* GetControl() const
{
return GetLabeledElement();
}
using nsGenericHTMLElement::Focus;
virtual void Focus(mozilla::ErrorResult& aError) override;
// nsIContent
virtual nsresult PostHandleEvent(
EventChainPostVisitor& aVisitor) override;
virtual bool PerformAccesskey(bool aKeyCausesActivation,
bool aIsTrustedEvent) override;
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
bool aPreallocateChildren) const override;
nsGenericHTMLElement* GetLabeledElement() const;
protected:
virtual ~HTMLLabelElement();
virtual JSObject* WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
nsGenericHTMLElement* GetFirstLabelableDescendant() const;
// XXX It would be nice if we could use an event flag instead.
bool mHandlingEvent;
};
} // namespace dom
} // namespace mozilla
#endif /* HTMLLabelElement_h */