HTMLFieldSetElement.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <LibWeb/Layout/FieldSetBox.h>
  17. namespace Web::HTML {
  18. GC_DEFINE_ALLOCATOR(HTMLFieldSetElement);
  19. HTMLFieldSetElement::HTMLFieldSetElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  20. : HTMLElement(document, move(qualified_name))
  21. {
  22. }
  23. HTMLFieldSetElement::~HTMLFieldSetElement() = default;
  24. void HTMLFieldSetElement::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLFieldSetElement);
  28. }
  29. void HTMLFieldSetElement::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_elements);
  33. }
  34. // https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
  35. bool HTMLFieldSetElement::is_disabled() const
  36. {
  37. // A fieldset element is a disabled fieldset if it matches any of the following conditions:
  38. // - Its disabled attribute is specified
  39. if (has_attribute(AttributeNames::disabled))
  40. return true;
  41. // - 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.
  42. for (auto* fieldset_ancestor = first_ancestor_of_type<HTMLFieldSetElement>(); fieldset_ancestor; fieldset_ancestor = fieldset_ancestor->first_ancestor_of_type<HTMLFieldSetElement>()) {
  43. if (fieldset_ancestor->has_attribute(HTML::AttributeNames::disabled)) {
  44. auto* first_legend_element_child = fieldset_ancestor->first_child_of_type<HTMLLegendElement>();
  45. if (!first_legend_element_child || !is_descendant_of(*first_legend_element_child))
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. // https://html.spec.whatwg.org/multipage/form-elements.html#dom-fieldset-elements
  52. GC::Ptr<DOM::HTMLCollection> const& HTMLFieldSetElement::elements()
  53. {
  54. // The elements IDL attribute must return an HTMLCollection rooted at the fieldset element, whose filter matches listed elements.
  55. if (!m_elements) {
  56. m_elements = DOM::HTMLCollection::create(*this, DOM::HTMLCollection::Scope::Descendants, [](DOM::Element const& element) {
  57. // FIXME: Form-associated custom elements return also true
  58. return is<HTMLButtonElement>(element)
  59. || is<HTMLFieldSetElement>(element)
  60. || is<HTMLInputElement>(element)
  61. || is<HTMLObjectElement>(element)
  62. || is<HTMLOutputElement>(element)
  63. || is<HTMLSelectElement>(element)
  64. || is<HTMLTextAreaElement>(element);
  65. });
  66. }
  67. return m_elements;
  68. }
  69. Layout::FieldSetBox* HTMLFieldSetElement::layout_node()
  70. {
  71. return static_cast<Layout::FieldSetBox*>(Node::layout_node());
  72. }
  73. GC::Ptr<Layout::Node> HTMLFieldSetElement::create_layout_node(CSS::StyleProperties style)
  74. {
  75. return heap().allocate<Layout::FieldSetBox>(document(), *this, style);
  76. }
  77. }