FormAssociatedElement.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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#reset-the-form-owner
  64. void FormAssociatedElement::reset_form_owner()
  65. {
  66. auto& html_element = form_associated_element_to_html_element();
  67. // Although these aren't in the "reset form owner" algorithm, these here as they are triggers for this algorithm:
  68. // FIXME: When a listed form-associated element's form attribute is set, changed, or removed, then the user agent must reset the form owner of that element.
  69. // 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.
  70. // 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.
  71. // 1. Unset element's parser inserted flag.
  72. m_parser_inserted = false;
  73. // 2. If all of the following conditions are true
  74. // - element's form owner is not null
  75. // - element is not listed or its form content attribute is not present
  76. // - element's form owner is its nearest form element ancestor after the change to the ancestor chain
  77. // then do nothing, and return.
  78. if (m_form
  79. && (!is_listed() || !html_element.has_attribute(HTML::AttributeNames::form))
  80. && html_element.first_ancestor_of_type<HTMLFormElement>() == m_form.ptr()) {
  81. return;
  82. }
  83. // 3. Set element's form owner to null.
  84. set_form(nullptr);
  85. // 4. If element is listed, has a form content attribute, and is connected, then:
  86. if (is_listed() && html_element.has_attribute(HTML::AttributeNames::form) && html_element.is_connected()) {
  87. // 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.
  88. auto form_value = html_element.deprecated_attribute(HTML::AttributeNames::form);
  89. html_element.root().for_each_in_inclusive_subtree_of_type<HTMLFormElement>([this, &form_value](HTMLFormElement& form_element) {
  90. if (form_element.deprecated_attribute(HTML::AttributeNames::id) == form_value) {
  91. set_form(&form_element);
  92. return IterationDecision::Break;
  93. }
  94. return IterationDecision::Continue;
  95. });
  96. }
  97. // 5. Otherwise, if element has an ancestor form element, then associate element with the nearest such ancestor form element.
  98. else {
  99. auto* form_ancestor = html_element.first_ancestor_of_type<HTMLFormElement>();
  100. if (form_ancestor)
  101. set_form(form_ancestor);
  102. }
  103. }
  104. }