FormAssociatedElement.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. namespace Web::HTML {
  15. void FormAssociatedElement::set_form(HTMLFormElement* form)
  16. {
  17. if (m_form)
  18. m_form->remove_associated_element({}, *this);
  19. m_form = form;
  20. if (m_form)
  21. m_form->add_associated_element({}, *this);
  22. }
  23. bool FormAssociatedElement::enabled() const
  24. {
  25. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled
  26. // A form control is disabled if any of the following conditions are met:
  27. // 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).
  28. // FIXME: This doesn't check for form-associated custom elements.
  29. if ((is<HTMLButtonElement>(this) || is<HTMLInputElement>(this) || is<HTMLSelectElement>(this) || is<HTMLTextAreaElement>(this)) && has_attribute(HTML::AttributeNames::disabled))
  30. return false;
  31. // 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.
  32. auto* fieldset_ancestor = first_ancestor_of_type<HTMLFieldSetElement>();
  33. if (fieldset_ancestor && fieldset_ancestor->has_attribute(HTML::AttributeNames::disabled)) {
  34. auto* first_legend_element_child = fieldset_ancestor->first_child_of_type<HTMLLegendElement>();
  35. if (!first_legend_element_child || !is_descendant_of(*first_legend_element_child))
  36. return false;
  37. }
  38. return true;
  39. }
  40. }