FormAssociatedElement.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. auto* fieldset_ancestor = html_element.first_ancestor_of_type<HTMLFieldSetElement>();
  35. if (fieldset_ancestor && 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. return true;
  41. }
  42. void FormAssociatedElement::set_parser_inserted(Badge<HTMLParser>)
  43. {
  44. m_parser_inserted = true;
  45. }
  46. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-inserted
  47. void FormAssociatedElement::form_node_was_inserted()
  48. {
  49. // 1. If the form-associated element's parser inserted flag is set, then return.
  50. if (m_parser_inserted)
  51. return;
  52. // 2. Reset the form owner of the form-associated element.
  53. reset_form_owner();
  54. }
  55. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-removed
  56. void FormAssociatedElement::form_node_was_removed()
  57. {
  58. // 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.
  59. if (m_form && &form_associated_element_to_html_element().root() != &m_form->root())
  60. reset_form_owner();
  61. }
  62. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#reset-the-form-owner
  63. void FormAssociatedElement::reset_form_owner()
  64. {
  65. auto& html_element = form_associated_element_to_html_element();
  66. // Although these aren't in the "reset form owner" algorithm, these here as they are triggers for this algorithm:
  67. // 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.
  68. // 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.
  69. // 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.
  70. // 1. Unset element's parser inserted flag.
  71. m_parser_inserted = false;
  72. // 2. If all of the following conditions are true
  73. // - element's form owner is not null
  74. // - element is not listed or its form content attribute is not present
  75. // - element's form owner is its nearest form element ancestor after the change to the ancestor chain
  76. // then do nothing, and return.
  77. if (m_form
  78. && (!is_listed() || !html_element.has_attribute(HTML::AttributeNames::form))
  79. && html_element.first_ancestor_of_type<HTMLFormElement>() == m_form.ptr()) {
  80. return;
  81. }
  82. // 3. Set element's form owner to null.
  83. set_form(nullptr);
  84. // 4. If element is listed, has a form content attribute, and is connected, then:
  85. if (is_listed() && html_element.has_attribute(HTML::AttributeNames::form) && html_element.is_connected()) {
  86. // 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.
  87. auto form_value = html_element.attribute(HTML::AttributeNames::form);
  88. html_element.root().for_each_in_inclusive_subtree_of_type<HTMLFormElement>([this, &form_value](HTMLFormElement& form_element) mutable {
  89. if (form_element.attribute(HTML::AttributeNames::id) == form_value) {
  90. set_form(&form_element);
  91. return IterationDecision::Break;
  92. }
  93. return IterationDecision::Continue;
  94. });
  95. }
  96. // 5. Otherwise, if element has an ancestor form element, then associate element with the nearest such ancestor form element.
  97. else {
  98. auto* form_ancestor = html_element.first_ancestor_of_type<HTMLFormElement>();
  99. if (form_ancestor)
  100. set_form(form_ancestor);
  101. }
  102. }
  103. }