HTMLFieldSetElement.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/HTML/HTMLButtonElement.h>
  8. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  9. #include <LibWeb/HTML/HTMLInputElement.h>
  10. #include <LibWeb/HTML/HTMLLegendElement.h>
  11. #include <LibWeb/HTML/HTMLObjectElement.h>
  12. #include <LibWeb/HTML/HTMLOutputElement.h>
  13. #include <LibWeb/HTML/HTMLSelectElement.h>
  14. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(HTMLFieldSetElement);
  17. HTMLFieldSetElement::HTMLFieldSetElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : HTMLElement(document, move(qualified_name))
  19. {
  20. }
  21. HTMLFieldSetElement::~HTMLFieldSetElement() = default;
  22. void HTMLFieldSetElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFieldSetElement);
  26. }
  27. void HTMLFieldSetElement::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_elements);
  31. }
  32. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
  33. bool HTMLFieldSetElement::is_disabled() const
  34. {
  35. // A fieldset element is a disabled fieldset if it matches any of the following conditions:
  36. // - Its disabled attribute is specified
  37. if (has_attribute(AttributeNames::disabled))
  38. return true;
  39. // - It is a descendant of another fieldset element whose disabled attribute is specified, and is not a descendant of that fieldset element's first legend element child, if any.
  40. for (auto* fieldset_ancestor = first_ancestor_of_type<HTMLFieldSetElement>(); fieldset_ancestor; fieldset_ancestor = fieldset_ancestor->first_ancestor_of_type<HTMLFieldSetElement>()) {
  41. if (fieldset_ancestor->has_attribute(HTML::AttributeNames::disabled)) {
  42. auto* first_legend_element_child = fieldset_ancestor->first_child_of_type<HTMLLegendElement>();
  43. if (!first_legend_element_child || !is_descendant_of(*first_legend_element_child))
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-fieldset-elements
  50. JS::GCPtr<DOM::HTMLCollection> const& HTMLFieldSetElement::elements()
  51. {
  52. // The elements IDL attribute must return an HTMLCollection rooted at the fieldset element, whose filter matches listed elements.
  53. if (!m_elements) {
  54. m_elements = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Descendants, [](DOM::Element const& element) {
  55. // FIXME: Form-associated custom elements return also true
  56. return is<HTMLButtonElement>(element)
  57. || is<HTMLFieldSetElement>(element)
  58. || is<HTMLInputElement>(element)
  59. || is<HTMLObjectElement>(element)
  60. || is<HTMLOutputElement>(element)
  61. || is<HTMLSelectElement>(element)
  62. || is<HTMLTextAreaElement>(element);
  63. });
  64. }
  65. return m_elements;
  66. }
  67. }