From 9b1c4ebd8d5dc27e42e72aa703db529bf4832509 Mon Sep 17 00:00:00 2001 From: Edgar Chen Date: Wed, 4 Jan 2023 13:08:39 +0000 Subject: [PATCH] Bug 1800370 - Make form-associate custom element behave the same as other form control element on event dispatching; r=smaug Differential Revision: https://phabricator.services.mozilla.com/D165292 --- dom/html/HTMLElement.cpp | 19 +++++++ dom/html/HTMLElement.h | 4 ++ .../disabled-event-dispatch.tentative.html | 50 ++++++++++++------- .../event-propagate-disabled.tentative.html | 6 +++ 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/dom/html/HTMLElement.cpp b/dom/html/HTMLElement.cpp index 7768671fd13e..67096bcb7f46 100644 --- a/dom/html/HTMLElement.cpp +++ b/dom/html/HTMLElement.cpp @@ -8,6 +8,7 @@ #include "mozilla/dom/CustomElementRegistry.h" #include "mozilla/dom/HTMLElementBinding.h" +#include "mozilla/EventDispatcher.h" #include "nsContentUtils.h" namespace mozilla::dom { @@ -38,6 +39,16 @@ JSObject* HTMLElement::WrapNode(JSContext* aCx, return dom::HTMLElement_Binding::Wrap(aCx, this, aGivenProto); } +void HTMLElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) { + if (IsDisabledForEvents(aVisitor.mEvent)) { + // Do not process any DOM events if the element is disabled + aVisitor.mCanHandle = false; + return; + } + + nsGenericHTMLFormElement::GetEventTargetParent(aVisitor); +} + nsresult HTMLElement::BindToTree(BindContext& aContext, nsINode& aParent) { nsresult rv = nsGenericHTMLFormElement::BindToTree(aContext, aParent); NS_ENSURE_SUCCESS(rv, rv); @@ -179,6 +190,14 @@ void HTMLElement::UpdateFormOwner() { UpdateBarredFromConstraintValidation(); } +bool HTMLElement::IsDisabledForEvents(WidgetEvent* aEvent) { + if (IsFormAssociatedElement()) { + return IsElementDisabledForEvents(aEvent, GetPrimaryFrame()); + } + + return false; +} + nsresult HTMLElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, const nsAttrValue* aValue, const nsAttrValue* aOldValue, diff --git a/dom/html/HTMLElement.h b/dom/html/HTMLElement.h index 7355d3ab663f..4e2e585b6d05 100644 --- a/dom/html/HTMLElement.h +++ b/dom/html/HTMLElement.h @@ -20,6 +20,9 @@ class HTMLElement final : public nsGenericHTMLFormElement { NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLElement, nsGenericHTMLFormElement) + // EventTarget + void GetEventTargetParent(EventChainPreVisitor& aVisitor) override; + // nsINode nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override; @@ -36,6 +39,7 @@ class HTMLElement final : public nsGenericHTMLFormElement { // https://html.spec.whatwg.org/multipage/custom-elements.html#dom-attachinternals already_AddRefed AttachInternals( ErrorResult& aRv) override; + bool IsDisabledForEvents(WidgetEvent* aEvent) override; // nsGenericHTMLFormElement bool IsFormAssociatedElement() const override; diff --git a/testing/web-platform/tests/html/semantics/disabled-elements/disabled-event-dispatch.tentative.html b/testing/web-platform/tests/html/semantics/disabled-elements/disabled-event-dispatch.tentative.html index c5e34a2341b4..e2b8846fc3db 100644 --- a/testing/web-platform/tests/html/semantics/disabled-elements/disabled-event-dispatch.tentative.html +++ b/testing/web-platform/tests/html/semantics/disabled-elements/disabled-event-dispatch.tentative.html @@ -1,4 +1,5 @@ + @@ -9,37 +10,48 @@
- + + hello world + child +
diff --git a/testing/web-platform/tests/html/semantics/disabled-elements/event-propagate-disabled.tentative.html b/testing/web-platform/tests/html/semantics/disabled-elements/event-propagate-disabled.tentative.html index 0c00cc8db078..9c8642d10f99 100644 --- a/testing/web-platform/tests/html/semantics/disabled-elements/event-propagate-disabled.tentative.html +++ b/testing/web-platform/tests/html/semantics/disabled-elements/event-propagate-disabled.tentative.html @@ -47,9 +47,15 @@ + Text