Bug 1849122, allowing getting and modifying the autofill state directly within the input and select elements, and reset this state when its value changes, r=webidl,emilio

Resetting the state when the value is modified is bug 1359355.

Differential Revision: https://phabricator.services.mozilla.com/D210999
This commit is contained in:
Neil Deakin 2024-05-28 23:39:59 +00:00
parent 702be51774
commit 1515db9b71
10 changed files with 72 additions and 1 deletions

View File

@ -6975,6 +6975,10 @@ void HTMLInputElement::OnValueChanged(ValueChangeKind aKind,
MOZ_ASSERT_IF(aKnownNewValue, aKnownNewValue->IsEmpty() == aNewValueEmpty);
if (aKind != ValueChangeKind::Internal) {
mLastValueChangeWasInteractive = aKind == ValueChangeKind::UserInteraction;
if (State().HasState(ElementState::AUTOFILL)) {
RemoveStates(ElementState::AUTOFILL | ElementState::AUTOFILL_PREVIEW);
}
}
if (aNewValueEmpty != IsValueEmpty()) {

View File

@ -248,6 +248,12 @@ class HTMLInputElement final : public TextControlElement,
MOZ_CAN_RUN_SCRIPT nsresult CreateEditor() override;
void SetPreviewValue(const nsAString& aValue) override;
void GetPreviewValue(nsAString& aValue) override;
void SetAutofillState(const nsAString& aState) override {
SetFormAutofillState(aState);
}
void GetAutofillState(nsAString& aState) override {
GetFormAutofillState(aState);
}
void EnablePreview() override;
bool IsPreviewEnabled() override;
void InitializeKeyboardEventListeners() override;

View File

@ -1595,6 +1595,11 @@ void HTMLSelectElement::OnSelectionChanged() {
if (!mDefaultSelectionSet) {
return;
}
if (State().HasState(ElementState::AUTOFILL)) {
RemoveStates(ElementState::AUTOFILL | ElementState::AUTOFILL_PREVIEW);
}
UpdateSelectedOptions();
}

View File

@ -327,6 +327,11 @@ class HTMLSelectElement final : public nsGenericHTMLFormControlElementWithState,
void GetPreviewValue(nsAString& aValue) { aValue = mPreviewValue; }
void SetPreviewValue(const nsAString& aValue);
void SetAutofillState(const nsAString& aState) {
SetFormAutofillState(aState);
}
void GetAutofillState(nsAString& aState) { GetFormAutofillState(aState); }
protected:
virtual ~HTMLSelectElement() = default;

View File

@ -95,6 +95,12 @@ class HTMLTextAreaElement final : public TextControlElement,
MOZ_CAN_RUN_SCRIPT nsresult CreateEditor() override;
void SetPreviewValue(const nsAString& aValue) override;
void GetPreviewValue(nsAString& aValue) override;
void SetAutofillState(const nsAString& aState) override {
SetFormAutofillState(aState);
}
void GetAutofillState(nsAString& aState) override {
GetFormAutofillState(aState);
}
void EnablePreview() override;
bool IsPreviewEnabled() override;
void InitializeKeyboardEventListeners() override;

View File

@ -31,7 +31,7 @@ class TextControlElement : public nsGenericHTMLFormControlElementWithState {
TextControlElement(already_AddRefed<dom::NodeInfo>&& aNodeInfo,
dom::FromParser aFromParser, FormControlType aType)
: nsGenericHTMLFormControlElementWithState(std::move(aNodeInfo),
aFromParser, aType){};
aFromParser, aType) {};
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(
@ -165,6 +165,16 @@ class TextControlElement : public nsGenericHTMLFormControlElementWithState {
*/
virtual void GetPreviewValue(nsAString& aValue) = 0;
/**
* Enable preview or autofilled state for the text control.
*/
virtual void SetAutofillState(const nsAString& aState) = 0;
/**
* Get the current preview or autofilled state for the text control.
*/
virtual void GetAutofillState(nsAString& aState) = 0;
/**
* Enable preview for text control.
*/

View File

@ -2804,6 +2804,29 @@ nsresult nsGenericHTMLFormControlElement::SubmitDirnameDir(
return NS_OK;
}
void nsGenericHTMLFormControlElement::GetFormAutofillState(
nsAString& aState) const {
if (State().HasState(ElementState::AUTOFILL_PREVIEW)) {
aState.AssignLiteral("preview");
} else if (State().HasState(ElementState::AUTOFILL)) {
aState.AssignLiteral("autofill");
} else {
aState.Truncate();
}
}
void nsGenericHTMLFormControlElement::SetFormAutofillState(
const nsAString& aState) {
if (aState.EqualsLiteral("autofill")) {
RemoveStates(ElementState::AUTOFILL_PREVIEW);
AddStates(ElementState::AUTOFILL);
} else if (aState.EqualsLiteral("preview")) {
AddStates(ElementState::AUTOFILL | ElementState::AUTOFILL_PREVIEW);
} else {
RemoveStates(ElementState::AUTOFILL | ElementState::AUTOFILL_PREVIEW);
}
}
//----------------------------------------------------------------------
static const nsAttrValue::EnumTable kPopoverTargetActionTable[] = {

View File

@ -1225,6 +1225,9 @@ class nsGenericHTMLFormControlElement : public nsGenericHTMLFormElement,
nsresult SubmitDirnameDir(mozilla::dom::FormData* aFormData);
void GetFormAutofillState(nsAString& aState) const;
void SetFormAutofillState(const nsAString& aState);
/** The form that contains this control */
mozilla::dom::HTMLFormElement* mForm;

View File

@ -191,6 +191,11 @@ partial interface HTMLInputElement {
[ChromeOnly]
attribute DOMString previewValue;
// A string indicating that the value of the element has been autofilled:
// either "filled", "preview" or "".
[ChromeOnly]
attribute DOMString autofillState;
// Last value entered by the user, not by a script.
// NOTE(emilio): As of right now some execCommand triggered changes might be
// considered interactive.

View File

@ -79,4 +79,8 @@ partial interface HTMLSelectElement {
AutocompleteInfo getAutocompleteInfo();
[ChromeOnly]
attribute DOMString previewValue;
// A string indicating that the value of the element has been autofilled:
// either "filled", "preview" or "".
[ChromeOnly]
attribute DOMString autofillState;
};