HTMLSummaryElement.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2023, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/HTML/HTMLDetailsElement.h>
  8. #include <LibWeb/HTML/HTMLSummaryElement.h>
  9. namespace Web::HTML {
  10. JS_DEFINE_ALLOCATOR(HTMLSummaryElement);
  11. HTMLSummaryElement::HTMLSummaryElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : HTMLElement(document, move(qualified_name))
  13. {
  14. }
  15. bool HTMLSummaryElement::has_activation_behavior() const
  16. {
  17. return true;
  18. }
  19. void HTMLSummaryElement::activation_behavior(DOM::Event const&)
  20. {
  21. // The activation behavior of summary elements is to run the following steps:
  22. // 1. If this summary element is not the summary for its parent details, then return.
  23. if (!is_summary_for_its_parent_details())
  24. return;
  25. // 2. Let parent be this summary element's parent.
  26. auto* parent = this->parent_element();
  27. // 3. If the open attribute is present on parent, then remove it. Otherwise, set parent's open attribute to the empty string.
  28. if (parent->has_attribute(HTML::AttributeNames::open))
  29. parent->remove_attribute(HTML::AttributeNames::open);
  30. else
  31. parent->set_attribute(HTML::AttributeNames::open, String {}).release_value_but_fixme_should_propagate_errors();
  32. }
  33. // https://html.spec.whatwg.org/multipage/interactive-elements.html#summary-for-its-parent-details
  34. bool HTMLSummaryElement::is_summary_for_its_parent_details()
  35. {
  36. // A summary element is a summary for its parent details if the following algorithm returns true:
  37. // 1. If this summary element has no parent, then return false.
  38. if (!parent_element())
  39. return false;
  40. // 2. Let parent be this summary element's parent.
  41. auto* parent = this->parent_element();
  42. // 3. If parent is not a details element, then return false.
  43. if (!is<HTMLDetailsElement>(*parent))
  44. return false;
  45. // 4. If parent's first summary element child is not this summary element, then return false.
  46. if (parent->first_child_of_type<HTMLSummaryElement>() != this)
  47. return false;
  48. // 5. Return true.
  49. return true;
  50. }
  51. HTMLSummaryElement::~HTMLSummaryElement() = default;
  52. void HTMLSummaryElement::initialize(JS::Realm& realm)
  53. {
  54. Base::initialize(realm);
  55. }
  56. }