HTMLFormElement.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibWeb/ARIA/Roles.h>
  10. #include <LibWeb/HTML/AbstractBrowsingContext.h>
  11. #include <LibWeb/HTML/HTMLElement.h>
  12. #include <LibWeb/HTML/HTMLInputElement.h>
  13. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  14. namespace Web::HTML {
  15. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
  16. #define ENUMERATE_FORM_METHOD_ATTRIBUTES \
  17. __ENUMERATE_FORM_METHOD_ATTRIBUTE(get, GET) \
  18. __ENUMERATE_FORM_METHOD_ATTRIBUTE(post, POST) \
  19. __ENUMERATE_FORM_METHOD_ATTRIBUTE(dialog, Dialog)
  20. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-enctype
  21. #define ENUMERATE_FORM_METHOD_ENCODING_TYPES \
  22. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("application/x-www-form-urlencoded", FormUrlEncoded) \
  23. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("multipart/form-data", FormData) \
  24. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("text/plain", PlainText)
  25. class HTMLFormElement final : public HTMLElement {
  26. WEB_PLATFORM_OBJECT(HTMLFormElement, HTMLElement);
  27. public:
  28. virtual ~HTMLFormElement() override;
  29. DeprecatedString action_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  30. enum class MethodAttributeState {
  31. #define __ENUMERATE_FORM_METHOD_ATTRIBUTE(_, state) state,
  32. ENUMERATE_FORM_METHOD_ATTRIBUTES
  33. #undef __ENUMERATE_FORM_METHOD_ATTRIBUTE
  34. };
  35. MethodAttributeState method_state_from_form_element(JS::NonnullGCPtr<HTMLElement const> element) const;
  36. enum class EncodingTypeAttributeState {
  37. #define __ENUMERATE_FORM_METHOD_ENCODING_TYPE(_, state) state,
  38. ENUMERATE_FORM_METHOD_ENCODING_TYPES
  39. #undef __ENUMERATE_FORM_METHOD_ENCODING_TYPE
  40. };
  41. EncodingTypeAttributeState encoding_type_state_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  42. WebIDL::ExceptionOr<void> submit_form(JS::NonnullGCPtr<HTMLElement> submitter, bool from_submit_binding = false);
  43. void reset_form();
  44. // NOTE: This is for the JS bindings. Use submit_form instead.
  45. WebIDL::ExceptionOr<void> submit();
  46. // NOTE: This is for the JS bindings. Use submit_form instead.
  47. void reset();
  48. void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  49. void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  50. ErrorOr<Vector<JS::NonnullGCPtr<DOM::Element>>> get_submittable_elements();
  51. JS::NonnullGCPtr<DOM::HTMLFormControlsCollection> elements() const;
  52. unsigned length() const;
  53. WebIDL::ExceptionOr<bool> check_validity();
  54. WebIDL::ExceptionOr<bool> report_validity();
  55. // https://www.w3.org/TR/html-aria/#el-form
  56. virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::form; }
  57. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-entry-list
  58. bool constructing_entry_list() const { return m_constructing_entry_list; }
  59. void set_constructing_entry_list(bool value) { m_constructing_entry_list = value; }
  60. StringView method() const;
  61. WebIDL::ExceptionOr<void> set_method(String const&);
  62. String action() const;
  63. WebIDL::ExceptionOr<void> set_action(String const&);
  64. private:
  65. HTMLFormElement(DOM::Document&, DOM::QualifiedName);
  66. virtual void initialize(JS::Realm&) override;
  67. virtual void visit_edges(Cell::Visitor&) override;
  68. ErrorOr<void> populate_vector_with_submittable_elements_in_tree_order(JS::NonnullGCPtr<DOM::Element> element, Vector<JS::NonnullGCPtr<DOM::Element>>& elements);
  69. ErrorOr<String> pick_an_encoding() const;
  70. ErrorOr<void> mutate_action_url(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  71. ErrorOr<void> submit_as_entity_body(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, EncodingTypeAttributeState encoding_type, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  72. void get_action_url(AK::URL parsed_action, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  73. ErrorOr<void> mail_with_headers(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  74. ErrorOr<void> mail_as_body(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, EncodingTypeAttributeState encoding_type, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  75. void plan_to_navigate_to(AK::URL url, Variant<Empty, String, POSTResource> post_resource, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  76. bool m_firing_submission_events { false };
  77. // https://html.spec.whatwg.org/multipage/forms.html#locked-for-reset
  78. bool m_locked_for_reset { false };
  79. Vector<JS::GCPtr<HTMLElement>> m_associated_elements;
  80. JS::GCPtr<DOM::HTMLFormControlsCollection> mutable m_elements;
  81. bool m_constructing_entry_list { false };
  82. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#planned-navigation
  83. // Each form element has a planned navigation, which is either null or a task; when the form is first created,
  84. // its planned navigation must be set to null.
  85. Task const* m_planned_navigation { nullptr };
  86. };
  87. }