Bug 1906015 - part 1: Add `nsIFormControl` accessors from `nsINode*` or `EventTarget*` r=smaug

Although the following patch do not use the `dom::EventTarget*` versions,
they should be available for consistency with the other `FromNode` related
utility methods.

Differential Revision: https://phabricator.services.mozilla.com/D215576
This commit is contained in:
Masayuki Nakano 2024-07-10 00:46:59 +00:00
Родитель 022adcf065
Коммит f8a1753d60
6 изменённых файлов: 96 добавлений и 0 удалений

Просмотреть файл

@ -45,6 +45,7 @@ class nsIAnimationObserver;
class nsIContent;
class nsIContentSecurityPolicy;
class nsIFrame;
class nsIFormControl;
class nsIHTMLCollection;
class nsMultiMutationObserver;
class nsINode;
@ -627,6 +628,16 @@ class nsINode : public mozilla::dom::EventTarget {
inline mozilla::dom::Text* AsText();
inline const mozilla::dom::Text* AsText() const;
/**
* Return this node if the instance type inherits nsIFormControl, or an
* nsIFormControl instance which ia associated with this node. Otherwise,
* returns nullptr.
*/
[[nodiscard]] virtual nsIFormControl* GetAsFormControl() { return nullptr; }
[[nodiscard]] virtual const nsIFormControl* GetAsFormControl() const {
return nullptr;
}
/*
* Return whether the node is a ProcessingInstruction node.
*/

Просмотреть файл

@ -431,6 +431,14 @@ ElementInternals* HTMLElement::GetElementInternals() const {
return data->GetElementInternals();
}
nsIFormControl* HTMLElement::GetAsFormControl() {
return GetElementInternals();
}
const nsIFormControl* HTMLElement::GetAsFormControl() const {
return GetElementInternals();
}
void HTMLElement::UpdateBarredFromConstraintValidation() {
CustomElementData* data = GetCustomElementData();
if (data && data->IsFormAssociated()) {

Просмотреть файл

@ -21,6 +21,9 @@ class HTMLElement final : public nsGenericHTMLFormElement {
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLElement,
nsGenericHTMLFormElement)
[[nodiscard]] nsIFormControl* GetAsFormControl() final;
[[nodiscard]] const nsIFormControl* GetAsFormControl() const final;
// EventTarget
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;

Просмотреть файл

@ -3777,3 +3777,57 @@ void nsGenericHTMLElement::GetPopover(nsString& aPopover) const {
aPopover.Assign(NS_ConvertUTF8toUTF16(kPopoverAttributeValueAuto));
}
}
/******************************************************************************
* nsIFormControl
*****************************************************************************/
// static
nsIFormControl* nsIFormControl::FromEventTarget(
mozilla::dom::EventTarget* aTarget) {
MOZ_ASSERT(aTarget);
return aTarget->IsNode() ? aTarget->AsNode()->GetAsFormControl() : nullptr;
}
// static
nsIFormControl* nsIFormControl::FromEventTargetOrNull(
mozilla::dom::EventTarget* aTarget) {
return aTarget && aTarget->IsNode() ? aTarget->AsNode()->GetAsFormControl()
: nullptr;
}
// static
const nsIFormControl* nsIFormControl::FromEventTarget(
const mozilla::dom::EventTarget* aTarget) {
MOZ_ASSERT(aTarget);
return aTarget->IsNode() ? aTarget->AsNode()->GetAsFormControl() : nullptr;
}
// static
const nsIFormControl* nsIFormControl::FromEventTargetOrNull(
const mozilla::dom::EventTarget* aTarget) {
return aTarget && aTarget->IsNode() ? aTarget->AsNode()->GetAsFormControl()
: nullptr;
}
// static
nsIFormControl* nsIFormControl::FromNode(nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->GetAsFormControl();
}
// static
nsIFormControl* nsIFormControl::FromNodeOrNull(nsINode* aNode) {
return aNode ? aNode->GetAsFormControl() : nullptr;
}
// static
const nsIFormControl* nsIFormControl::FromNode(const nsINode* aNode) {
MOZ_ASSERT(aNode);
return aNode->GetAsFormControl();
}
// static
const nsIFormControl* nsIFormControl::FromNodeOrNull(const nsINode* aNode) {
return aNode ? aNode->GetAsFormControl() : nullptr;
}

Просмотреть файл

@ -1176,6 +1176,11 @@ class nsGenericHTMLFormControlElement : public nsGenericHTMLFormElement,
NS_IMPL_FROMNODE_HELPER(nsGenericHTMLFormControlElement,
IsHTMLFormControlElement())
[[nodiscard]] nsIFormControl* GetAsFormControl() final { return this; }
[[nodiscard]] const nsIFormControl* GetAsFormControl() const final {
return this;
}
// nsINode
nsINode* GetScopeChainParent() const override;
bool IsHTMLFormControlElement() const final { return true; }

Просмотреть файл

@ -10,10 +10,12 @@
#include "mozilla/StaticPrefs_dom.h"
#include "nsISupports.h"
class nsINode;
namespace mozilla {
class PresState;
namespace dom {
class Element;
class EventTarget;
class FormData;
class HTMLFieldSetElement;
class HTMLFormElement;
@ -93,6 +95,19 @@ class nsIFormControl : public nsISupports {
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
static nsIFormControl* FromEventTarget(mozilla::dom::EventTarget* aTarget);
static nsIFormControl* FromEventTargetOrNull(
mozilla::dom::EventTarget* aTarget);
static const nsIFormControl* FromEventTarget(
const mozilla::dom::EventTarget* aTarget);
static const nsIFormControl* FromEventTargetOrNull(
const mozilla::dom::EventTarget* aTarget);
static nsIFormControl* FromNode(nsINode* aNode);
static nsIFormControl* FromNodeOrNull(nsINode* aNode);
static const nsIFormControl* FromNode(const nsINode* aNode);
static const nsIFormControl* FromNodeOrNull(const nsINode* aNode);
/**
* Get the fieldset for this form control.
* @return the fieldset