Serenity Operating System
1/*
2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/WeakPtr.h>
10#include <LibWeb/Forward.h>
11
12namespace Web::HTML {
13
14// Form-associated elements should invoke this macro to inject overridden FormAssociatedElement and HTMLElement
15// methods as needed. If your class wished to override an HTMLElement method that is overridden here, use the
16// following methods instead:
17//
18// HTMLElement::inserted() -> Use form_associated_element_was_inserted()
19// HTMLElement::removed_from() -> Use form_associated_element_was_removed()
20//
21#define FORM_ASSOCIATED_ELEMENT(ElementBaseClass, ElementClass) \
22private: \
23 virtual HTMLElement& form_associated_element_to_html_element() override \
24 { \
25 static_assert(IsBaseOf<HTMLElement, ElementClass>); \
26 return *this; \
27 } \
28 \
29 virtual void inserted() override \
30 { \
31 ElementBaseClass::inserted(); \
32 form_node_was_inserted(); \
33 form_associated_element_was_inserted(); \
34 } \
35 \
36 virtual void removed_from(DOM::Node* node) override \
37 { \
38 ElementBaseClass::removed_from(node); \
39 form_node_was_removed(); \
40 form_associated_element_was_removed(node); \
41 }
42
43class FormAssociatedElement {
44public:
45 HTMLFormElement* form() { return m_form; }
46 HTMLFormElement const* form() const { return m_form; }
47
48 void set_form(HTMLFormElement*);
49
50 bool enabled() const;
51
52 void set_parser_inserted(Badge<HTMLParser>);
53
54 // https://html.spec.whatwg.org/multipage/forms.html#category-listed
55 virtual bool is_listed() const { return false; }
56
57 // https://html.spec.whatwg.org/multipage/forms.html#category-submit
58 virtual bool is_submittable() const { return false; }
59
60 // https://html.spec.whatwg.org/multipage/forms.html#category-reset
61 virtual bool is_resettable() const { return false; }
62
63 // https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
64 virtual bool is_auto_capitalize_inheriting() const { return false; }
65
66 virtual HTMLElement& form_associated_element_to_html_element() = 0;
67
68 // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
69 virtual void reset_algorithm() {};
70
71protected:
72 FormAssociatedElement() = default;
73 virtual ~FormAssociatedElement() = default;
74
75 virtual void form_associated_element_was_inserted() { }
76 virtual void form_associated_element_was_removed(DOM::Node*) { }
77
78 void form_node_was_inserted();
79 void form_node_was_removed();
80
81private:
82 WeakPtr<HTMLFormElement> m_form;
83
84 // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
85 bool m_parser_inserted { false };
86
87 void reset_form_owner();
88};
89
90}