HTMLFormElement.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. JS_DECLARE_ALLOCATOR(HTMLFormElement);
  28. public:
  29. virtual ~HTMLFormElement() override;
  30. String action_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  31. enum class MethodAttributeState {
  32. #define __ENUMERATE_FORM_METHOD_ATTRIBUTE(_, state) state,
  33. ENUMERATE_FORM_METHOD_ATTRIBUTES
  34. #undef __ENUMERATE_FORM_METHOD_ATTRIBUTE
  35. };
  36. MethodAttributeState method_state_from_form_element(JS::NonnullGCPtr<HTMLElement const> element) const;
  37. enum class EncodingTypeAttributeState {
  38. #define __ENUMERATE_FORM_METHOD_ENCODING_TYPE(_, state) state,
  39. ENUMERATE_FORM_METHOD_ENCODING_TYPES
  40. #undef __ENUMERATE_FORM_METHOD_ENCODING_TYPE
  41. };
  42. EncodingTypeAttributeState encoding_type_state_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  43. WebIDL::ExceptionOr<void> submit_form(JS::NonnullGCPtr<HTMLElement> submitter, bool from_submit_binding = false);
  44. void reset_form();
  45. // NOTE: This is for the JS bindings. Use submit_form instead.
  46. WebIDL::ExceptionOr<void> submit();
  47. // NOTE: This is for the JS bindings. Use submit_form instead.
  48. void reset();
  49. void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  50. void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  51. ErrorOr<Vector<JS::NonnullGCPtr<DOM::Element>>> get_submittable_elements();
  52. JS::NonnullGCPtr<DOM::HTMLFormControlsCollection> elements() const;
  53. unsigned length() const;
  54. WebIDL::ExceptionOr<bool> check_validity();
  55. WebIDL::ExceptionOr<bool> report_validity();
  56. // https://www.w3.org/TR/html-aria/#el-form
  57. virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::form; }
  58. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-entry-list
  59. bool constructing_entry_list() const { return m_constructing_entry_list; }
  60. void set_constructing_entry_list(bool value) { m_constructing_entry_list = value; }
  61. StringView method() const;
  62. WebIDL::ExceptionOr<void> set_method(String const&);
  63. String action() const;
  64. WebIDL::ExceptionOr<void> set_action(String const&);
  65. private:
  66. HTMLFormElement(DOM::Document&, DOM::QualifiedName);
  67. virtual void initialize(JS::Realm&) override;
  68. virtual void visit_edges(Cell::Visitor&) override;
  69. ErrorOr<void> populate_vector_with_submittable_elements_in_tree_order(JS::NonnullGCPtr<DOM::Element> element, Vector<JS::NonnullGCPtr<DOM::Element>>& elements);
  70. ErrorOr<String> pick_an_encoding() const;
  71. ErrorOr<void> mutate_action_url(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  72. 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);
  73. void get_action_url(AK::URL parsed_action, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  74. ErrorOr<void> mail_with_headers(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  75. 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);
  76. void plan_to_navigate_to(AK::URL url, Variant<Empty, String, POSTResource> post_resource, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  77. bool m_firing_submission_events { false };
  78. // https://html.spec.whatwg.org/multipage/forms.html#locked-for-reset
  79. bool m_locked_for_reset { false };
  80. Vector<JS::GCPtr<HTMLElement>> m_associated_elements;
  81. JS::GCPtr<DOM::HTMLFormControlsCollection> mutable m_elements;
  82. bool m_constructing_entry_list { false };
  83. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#planned-navigation
  84. // Each form element has a planned navigation, which is either null or a task; when the form is first created,
  85. // its planned navigation must be set to null.
  86. Task const* m_planned_navigation { nullptr };
  87. };
  88. }