Skip to content

Commit 9b184c6

Browse files
committed
LibWeb: Support autocomplete attribute on form elements
Implement proper support for the `autocomplete` attribute in `input`, `select` and `textarea` elements.
1 parent 198cec4 commit 9b184c6

File tree

11 files changed

+644
-9
lines changed

11 files changed

+644
-9
lines changed

Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ set(SOURCES
298298
HTML/AttributeNames.cpp
299299
HTML/AudioTrack.cpp
300300
HTML/AudioTrackList.cpp
301+
HTML/AutocompleteElement.cpp
301302
HTML/BeforeUnloadEvent.cpp
302303
HTML/BroadcastChannel.cpp
303304
HTML/BrowsingContext.cpp

Libraries/LibWeb/HTML/AutocompleteElement.cpp

Lines changed: 369 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2025, Altomani Gianluca <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/HTML/FormAssociatedElement.h>
10+
11+
namespace Web::HTML {
12+
13+
#define AUTOCOMPLETE_ELEMENT(ElementBaseClass, ElementClass) \
14+
private: \
15+
virtual HTMLElement& autocomplete_element_to_html_element() override \
16+
{ \
17+
static_assert(IsBaseOf<HTMLElement, ElementClass>); \
18+
static_assert(IsBaseOf<FormAssociatedElement, ElementClass>); \
19+
return *this; \
20+
}
21+
22+
class AutocompleteElement {
23+
public:
24+
enum class AutofillMantle {
25+
Anchor,
26+
Expectation,
27+
};
28+
AutofillMantle get_autofill_mantle() const;
29+
30+
Vector<String> autocomplete_tokens() const;
31+
String autocomplete() const;
32+
WebIDL::ExceptionOr<void> set_autocomplete(String const&);
33+
34+
// Each input element to which the autocomplete attribute applies [...] has
35+
// an autofill hint set, an autofill scope, an autofill field name,
36+
// a non-autofill credential type, and an IDL-exposed autofill value.
37+
struct AttributeDetails {
38+
Vector<String> hint_set;
39+
Vector<String> scope;
40+
String field_name;
41+
Optional<String> credential_type;
42+
String value;
43+
};
44+
AttributeDetails parse_autocomplete_attribute() const;
45+
46+
virtual HTMLElement& autocomplete_element_to_html_element() = 0;
47+
HTMLElement const& autocomplete_element_to_html_element() const { return const_cast<AutocompleteElement&>(*this).autocomplete_element_to_html_element(); }
48+
49+
protected:
50+
AutocompleteElement() = default;
51+
virtual ~AutocompleteElement() = default;
52+
};
53+
54+
}

Libraries/LibWeb/HTML/HTMLInputElement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
1313
#include <LibWeb/DOM/Text.h>
1414
#include <LibWeb/FileAPI/FileList.h>
15+
#include <LibWeb/HTML/AutocompleteElement.h>
1516
#include <LibWeb/HTML/ColorPickerUpdateState.h>
1617
#include <LibWeb/HTML/FileFilter.h>
1718
#include <LibWeb/HTML/FormAssociatedElement.h>
@@ -52,10 +53,12 @@ class HTMLInputElement final
5253
: public HTMLElement
5354
, public FormAssociatedTextControlElement
5455
, public Layout::ImageProvider
55-
, public PopoverInvokerElement {
56+
, public PopoverInvokerElement
57+
, public AutocompleteElement {
5658
WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement);
5759
GC_DECLARE_ALLOCATOR(HTMLInputElement);
58-
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement)
60+
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLInputElement);
61+
AUTOCOMPLETE_ELEMENT(HTMLElement, HTMLInputElement);
5962

6063
public:
6164
virtual ~HTMLInputElement() override;

Libraries/LibWeb/HTML/HTMLInputElement.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface HTMLInputElement : HTMLElement {
1111

1212
[CEReactions, Reflect] attribute DOMString accept;
1313
[CEReactions, Reflect] attribute DOMString alt;
14-
[CEReactions, Enumerated=Autocomplete, Reflect] attribute DOMString autocomplete;
14+
[CEReactions] attribute DOMString autocomplete;
1515
[CEReactions, Reflect=checked] attribute boolean defaultChecked;
1616
[ImplementedAs=checked_binding] attribute boolean checked;
1717
[CEReactions, Reflect=dirname] attribute DOMString dirName;

Libraries/LibWeb/HTML/HTMLSelectElement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#pragma once
1111

12+
#include <LibWeb/HTML/AutocompleteElement.h>
1213
#include <LibWeb/HTML/FormAssociatedElement.h>
1314
#include <LibWeb/HTML/HTMLElement.h>
1415
#include <LibWeb/HTML/HTMLOptionsCollection.h>
@@ -19,10 +20,12 @@ namespace Web::HTML {
1920

2021
class HTMLSelectElement final
2122
: public HTMLElement
22-
, public FormAssociatedElement {
23+
, public FormAssociatedElement
24+
, public AutocompleteElement {
2325
WEB_PLATFORM_OBJECT(HTMLSelectElement, HTMLElement);
2426
GC_DECLARE_ALLOCATOR(HTMLSelectElement);
25-
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLSelectElement)
27+
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLSelectElement);
28+
AUTOCOMPLETE_ELEMENT(HTMLElement, HTMLSelectElement);
2629

2730
public:
2831
virtual ~HTMLSelectElement() override;

Libraries/LibWeb/HTML/HTMLSelectElement.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
interface HTMLSelectElement : HTMLElement {
99
[HTMLConstructor] constructor();
1010

11-
[CEReactions, Enumerated=Autocomplete, Reflect] attribute DOMString autocomplete;
11+
[CEReactions] attribute DOMString autocomplete;
1212
[CEReactions, Reflect] attribute boolean disabled;
1313
readonly attribute HTMLFormElement? form;
1414
[CEReactions, Reflect] attribute boolean multiple;

Libraries/LibWeb/HTML/HTMLTextAreaElement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <LibCore/Timer.h>
1313
#include <LibWeb/ARIA/Roles.h>
1414
#include <LibWeb/DOM/Text.h>
15+
#include <LibWeb/HTML/AutocompleteElement.h>
1516
#include <LibWeb/HTML/FormAssociatedElement.h>
1617
#include <LibWeb/HTML/HTMLElement.h>
1718
#include <LibWeb/WebIDL/Types.h>
@@ -20,10 +21,12 @@ namespace Web::HTML {
2021

2122
class HTMLTextAreaElement final
2223
: public HTMLElement
23-
, public FormAssociatedTextControlElement {
24+
, public FormAssociatedTextControlElement
25+
, public AutocompleteElement {
2426
WEB_PLATFORM_OBJECT(HTMLTextAreaElement, HTMLElement);
2527
GC_DECLARE_ALLOCATOR(HTMLTextAreaElement);
26-
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLTextAreaElement)
28+
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLTextAreaElement);
29+
AUTOCOMPLETE_ELEMENT(HTMLElement, HTMLTextAreaElement);
2730

2831
public:
2932
virtual ~HTMLTextAreaElement() override;

Libraries/LibWeb/HTML/HTMLTextAreaElement.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
interface HTMLTextAreaElement : HTMLElement {
88
[HTMLConstructor] constructor();
99

10-
[CEReactions, Enumerated=Autocomplete, Reflect] attribute DOMString autocomplete;
10+
[CEReactions] attribute DOMString autocomplete;
1111
[CEReactions] attribute unsigned long cols;
1212
[CEReactions, Reflect=dirname] attribute DOMString dirName;
1313
[CEReactions, Reflect] attribute boolean disabled;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Harness status: OK
2+
3+
Found 67 tests
4+
5+
67 Pass
6+
Pass form autocomplete attribute missing
7+
Pass form autocomplete attribute on
8+
Pass form autocomplete attribute off
9+
Pass form autocomplete attribute invalid
10+
Pass on is an allowed autocomplete field name
11+
Pass off is an allowed autocomplete field name
12+
Pass name is an allowed autocomplete field name
13+
Pass honorific-prefix is an allowed autocomplete field name
14+
Pass given-name is an allowed autocomplete field name
15+
Pass additional-name is an allowed autocomplete field name
16+
Pass family-name is an allowed autocomplete field name
17+
Pass honorific-suffix is an allowed autocomplete field name
18+
Pass nickname is an allowed autocomplete field name
19+
Pass username is an allowed autocomplete field name
20+
Pass new-password is an allowed autocomplete field name
21+
Pass current-password is an allowed autocomplete field name
22+
Pass one-time-code is an allowed autocomplete field name
23+
Pass organization-title is an allowed autocomplete field name
24+
Pass organization is an allowed autocomplete field name
25+
Pass street-address is an allowed autocomplete field name
26+
Pass address-line1 is an allowed autocomplete field name
27+
Pass address-line2 is an allowed autocomplete field name
28+
Pass address-line3 is an allowed autocomplete field name
29+
Pass address-level4 is an allowed autocomplete field name
30+
Pass address-level3 is an allowed autocomplete field name
31+
Pass address-level2 is an allowed autocomplete field name
32+
Pass address-level1 is an allowed autocomplete field name
33+
Pass country is an allowed autocomplete field name
34+
Pass country-name is an allowed autocomplete field name
35+
Pass postal-code is an allowed autocomplete field name
36+
Pass cc-name is an allowed autocomplete field name
37+
Pass cc-given-name is an allowed autocomplete field name
38+
Pass cc-additional-name is an allowed autocomplete field name
39+
Pass cc-family-name is an allowed autocomplete field name
40+
Pass cc-number is an allowed autocomplete field name
41+
Pass cc-exp is an allowed autocomplete field name
42+
Pass cc-exp-month is an allowed autocomplete field name
43+
Pass cc-exp-year is an allowed autocomplete field name
44+
Pass cc-csc is an allowed autocomplete field name
45+
Pass cc-type is an allowed autocomplete field name
46+
Pass transaction-currency is an allowed autocomplete field name
47+
Pass transaction-amount is an allowed autocomplete field name
48+
Pass language is an allowed autocomplete field name
49+
Pass bday is an allowed autocomplete field name
50+
Pass bday-day is an allowed autocomplete field name
51+
Pass bday-month is an allowed autocomplete field name
52+
Pass bday-year is an allowed autocomplete field name
53+
Pass sex is an allowed autocomplete field name
54+
Pass url is an allowed autocomplete field name
55+
Pass photo is an allowed autocomplete field name
56+
Pass tel is an allowed autocomplete field name
57+
Pass tel-country-code is an allowed autocomplete field name
58+
Pass tel-national is an allowed autocomplete field name
59+
Pass tel-area-code is an allowed autocomplete field name
60+
Pass tel-local is an allowed autocomplete field name
61+
Pass tel-local-prefix is an allowed autocomplete field name
62+
Pass tel-local-suffix is an allowed autocomplete field name
63+
Pass tel-extension is an allowed autocomplete field name
64+
Pass email is an allowed autocomplete field name
65+
Pass impp is an allowed autocomplete field name
66+
Pass webauthn is an allowed autocomplete field name
67+
Pass Test whitespace-only attribute value
68+
Pass Test maximum number of tokens
69+
Pass Unknown field
70+
Pass Test 'wearing the autofill anchor mantle' with off/on
71+
Pass Serialize combinations of section, mode, contact, and field
72+
Pass Serialize combinations of section, mode, contact, field, and credential

0 commit comments

Comments
 (0)