HTMLFormElement.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <AK/Time.h>
  10. #include <LibWeb/ARIA/Roles.h>
  11. #include <LibWeb/HTML/AbstractBrowsingContext.h>
  12. #include <LibWeb/HTML/HTMLElement.h>
  13. #include <LibWeb/HTML/HTMLInputElement.h>
  14. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  15. namespace Web::HTML {
  16. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
  17. #define ENUMERATE_FORM_METHOD_ATTRIBUTES \
  18. __ENUMERATE_FORM_METHOD_ATTRIBUTE(get, GET) \
  19. __ENUMERATE_FORM_METHOD_ATTRIBUTE(post, POST) \
  20. __ENUMERATE_FORM_METHOD_ATTRIBUTE(dialog, Dialog)
  21. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-enctype
  22. #define ENUMERATE_FORM_METHOD_ENCODING_TYPES \
  23. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("application/x-www-form-urlencoded", FormUrlEncoded) \
  24. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("multipart/form-data", FormData) \
  25. __ENUMERATE_FORM_METHOD_ENCODING_TYPE("text/plain", PlainText)
  26. class HTMLFormElement final : public HTMLElement {
  27. WEB_PLATFORM_OBJECT(HTMLFormElement, HTMLElement);
  28. JS_DECLARE_ALLOCATOR(HTMLFormElement);
  29. public:
  30. virtual ~HTMLFormElement() override;
  31. String action_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  32. enum class MethodAttributeState {
  33. #define __ENUMERATE_FORM_METHOD_ATTRIBUTE(_, state) state,
  34. ENUMERATE_FORM_METHOD_ATTRIBUTES
  35. #undef __ENUMERATE_FORM_METHOD_ATTRIBUTE
  36. };
  37. MethodAttributeState method_state_from_form_element(JS::NonnullGCPtr<HTMLElement const> element) const;
  38. enum class EncodingTypeAttributeState {
  39. #define __ENUMERATE_FORM_METHOD_ENCODING_TYPE(_, state) state,
  40. ENUMERATE_FORM_METHOD_ENCODING_TYPES
  41. #undef __ENUMERATE_FORM_METHOD_ENCODING_TYPE
  42. };
  43. EncodingTypeAttributeState encoding_type_state_from_form_element(JS::NonnullGCPtr<HTMLElement> element) const;
  44. WebIDL::ExceptionOr<void> submit_form(JS::NonnullGCPtr<HTMLElement> submitter, bool from_submit_binding = false);
  45. void reset_form();
  46. // NOTE: This is for the JS bindings. Use submit_form instead.
  47. WebIDL::ExceptionOr<void> submit();
  48. // NOTE: This is for the JS bindings. Use submit_form instead.
  49. void reset();
  50. void add_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  51. void remove_associated_element(Badge<FormAssociatedElement>, HTMLElement&);
  52. ErrorOr<Vector<JS::NonnullGCPtr<DOM::Element>>> get_submittable_elements();
  53. JS::NonnullGCPtr<DOM::HTMLFormControlsCollection> elements() const;
  54. unsigned length() const;
  55. WebIDL::ExceptionOr<bool> check_validity();
  56. WebIDL::ExceptionOr<bool> report_validity();
  57. // https://www.w3.org/TR/html-aria/#el-form
  58. virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::form; }
  59. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-entry-list
  60. bool constructing_entry_list() const { return m_constructing_entry_list; }
  61. void set_constructing_entry_list(bool value) { m_constructing_entry_list = value; }
  62. StringView method() const;
  63. WebIDL::ExceptionOr<void> set_method(String const&);
  64. String action() const;
  65. WebIDL::ExceptionOr<void> set_action(String const&);
  66. private:
  67. HTMLFormElement(DOM::Document&, DOM::QualifiedName);
  68. virtual bool is_html_form_element() const override { return true; }
  69. virtual void initialize(JS::Realm&) override;
  70. virtual void visit_edges(Cell::Visitor&) override;
  71. // ^PlatformObject
  72. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  73. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override;
  74. virtual Vector<FlyString> supported_property_names() const override;
  75. virtual bool is_supported_property_index(u32) const override;
  76. ErrorOr<void> populate_vector_with_submittable_elements_in_tree_order(JS::NonnullGCPtr<DOM::Element> element, Vector<JS::NonnullGCPtr<DOM::Element>>& elements);
  77. ErrorOr<String> pick_an_encoding() const;
  78. ErrorOr<void> mutate_action_url(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  79. 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);
  80. void get_action_url(AK::URL parsed_action, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  81. ErrorOr<void> mail_with_headers(AK::URL parsed_action, Vector<XHR::FormDataEntry> entry_list, String encoding, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  82. 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);
  83. void plan_to_navigate_to(AK::URL url, Variant<Empty, String, POSTResource> post_resource, JS::NonnullGCPtr<Navigable> target_navigable, HistoryHandlingBehavior history_handling);
  84. bool m_firing_submission_events { false };
  85. // https://html.spec.whatwg.org/multipage/forms.html#locked-for-reset
  86. bool m_locked_for_reset { false };
  87. Vector<JS::GCPtr<HTMLElement>> m_associated_elements;
  88. // https://html.spec.whatwg.org/multipage/forms.html#past-names-map
  89. struct PastNameEntry {
  90. JS::GCPtr<DOM::Node const> node;
  91. MonotonicTime insertion_time;
  92. };
  93. HashMap<FlyString, PastNameEntry> mutable m_past_names_map;
  94. JS::GCPtr<DOM::HTMLFormControlsCollection> mutable m_elements;
  95. bool m_constructing_entry_list { false };
  96. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#planned-navigation
  97. // Each form element has a planned navigation, which is either null or a task; when the form is first created,
  98. // its planned navigation must be set to null.
  99. Task const* m_planned_navigation { nullptr };
  100. };
  101. }
  102. namespace Web::DOM {
  103. template<>
  104. inline bool Node::fast_is<HTML::HTMLFormElement>() const { return is_html_form_element(); }
  105. }