FormAssociatedElement.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/FormAssociatedElement.h>
  7. #include <LibWeb/HTML/HTMLButtonElement.h>
  8. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  9. #include <LibWeb/HTML/HTMLFormElement.h>
  10. #include <LibWeb/HTML/HTMLInputElement.h>
  11. #include <LibWeb/HTML/HTMLLegendElement.h>
  12. #include <LibWeb/HTML/HTMLSelectElement.h>
  13. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  14. #include <LibWeb/HTML/Parser/HTMLParser.h>
  15. namespace Web::HTML {
  16. void FormAssociatedElement::set_form(HTMLFormElement* form)
  17. {
  18. if (m_form)
  19. m_form->remove_associated_element({}, form_associated_element_to_html_element());
  20. m_form = form;
  21. if (m_form)
  22. m_form->add_associated_element({}, form_associated_element_to_html_element());
  23. }
  24. bool FormAssociatedElement::enabled() const
  25. {
  26. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled
  27. auto const& html_element = const_cast<FormAssociatedElement&>(*this).form_associated_element_to_html_element();
  28. // A form control is disabled if any of the following conditions are met:
  29. // 1. The element is a button, input, select, textarea, or form-associated custom element, and the disabled attribute is specified on this element (regardless of its value).
  30. // FIXME: This doesn't check for form-associated custom elements.
  31. if ((is<HTMLButtonElement>(html_element) || is<HTMLInputElement>(html_element) || is<HTMLSelectElement>(html_element) || is<HTMLTextAreaElement>(html_element)) && html_element.has_attribute(HTML::AttributeNames::disabled))
  32. return false;
  33. // 2. The element is a descendant of a fieldset element whose disabled attribute is specified, and is not a descendant of that fieldset element's first legend element child, if any.
  34. for (auto* fieldset_ancestor = html_element.first_ancestor_of_type<HTMLFieldSetElement>(); fieldset_ancestor; fieldset_ancestor = fieldset_ancestor->first_ancestor_of_type<HTMLFieldSetElement>()) {
  35. if (fieldset_ancestor->has_attribute(HTML::AttributeNames::disabled)) {
  36. auto* first_legend_element_child = fieldset_ancestor->first_child_of_type<HTMLLegendElement>();
  37. if (!first_legend_element_child || !html_element.is_descendant_of(*first_legend_element_child))
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. void FormAssociatedElement::set_parser_inserted(Badge<HTMLParser>)
  44. {
  45. m_parser_inserted = true;
  46. }
  47. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-inserted
  48. void FormAssociatedElement::form_node_was_inserted()
  49. {
  50. // 1. If the form-associated element's parser inserted flag is set, then return.
  51. if (m_parser_inserted)
  52. return;
  53. // 2. Reset the form owner of the form-associated element.
  54. reset_form_owner();
  55. }
  56. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-removed
  57. void FormAssociatedElement::form_node_was_removed()
  58. {
  59. // 1. If the form-associated element has a form owner and the form-associated element and its form owner are no longer in the same tree, then reset the form owner of the form-associated element.
  60. if (m_form && &form_associated_element_to_html_element().root() != &m_form->root())
  61. reset_form_owner();
  62. }
  63. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:category-listed-3
  64. void FormAssociatedElement::form_node_attribute_changed(FlyString const& name, Optional<String> const&)
  65. {
  66. // When a listed form-associated element's form attribute is set, changed, or removed, then the user agent must
  67. // reset the form owner of that element.
  68. if (name == HTML::AttributeNames::form) {
  69. reset_form_owner();
  70. }
  71. }
  72. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#reset-the-form-owner
  73. void FormAssociatedElement::reset_form_owner()
  74. {
  75. auto& html_element = form_associated_element_to_html_element();
  76. // Although these aren't in the "reset form owner" algorithm, these here as they are triggers for this algorithm:
  77. // FIXME: When a listed form-associated element has a form attribute and the ID of any of the elements in the tree changes, then the user agent must reset the form owner of that form-associated element.
  78. // FIXME: When a listed form-associated element has a form attribute and an element with an ID is inserted into or removed from the Document, then the user agent must reset the form owner of that form-associated element.
  79. // 1. Unset element's parser inserted flag.
  80. m_parser_inserted = false;
  81. // 2. If all of the following conditions are true
  82. // - element's form owner is not null
  83. // - element is not listed or its form content attribute is not present
  84. // - element's form owner is its nearest form element ancestor after the change to the ancestor chain
  85. // then do nothing, and return.
  86. if (m_form
  87. && (!is_listed() || !html_element.has_attribute(HTML::AttributeNames::form))
  88. && html_element.first_ancestor_of_type<HTMLFormElement>() == m_form.ptr()) {
  89. return;
  90. }
  91. // 3. Set element's form owner to null.
  92. set_form(nullptr);
  93. // 4. If element is listed, has a form content attribute, and is connected, then:
  94. if (is_listed() && html_element.has_attribute(HTML::AttributeNames::form) && html_element.is_connected()) {
  95. // 1. If the first element in element's tree, in tree order, to have an ID that is identical to element's form content attribute's value, is a form element, then associate the element with that form element.
  96. auto form_value = html_element.attribute(HTML::AttributeNames::form);
  97. html_element.root().for_each_in_inclusive_subtree_of_type<HTMLFormElement>([this, &form_value](HTMLFormElement& form_element) {
  98. if (form_element.id() == form_value) {
  99. set_form(&form_element);
  100. return IterationDecision::Break;
  101. }
  102. return IterationDecision::Continue;
  103. });
  104. }
  105. // 5. Otherwise, if element has an ancestor form element, then associate element with the nearest such ancestor form element.
  106. else {
  107. auto* form_ancestor = html_element.first_ancestor_of_type<HTMLFormElement>();
  108. if (form_ancestor)
  109. set_form(form_ancestor);
  110. }
  111. }
  112. }