HTMLFieldSetElement.cpp 3.0 KB

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