HTMLSummaryElement.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. HTMLSummaryElement::HTMLSummaryElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  11. : HTMLElement(document, move(qualified_name))
  12. {
  13. activation_behavior = [this](auto&) {
  14. // The activation behavior of summary elements is to run the following steps:
  15. // 1. If this summary element is not the summary for its parent details, then return.
  16. if (!is_summary_for_its_parent_details())
  17. return;
  18. // 2. Let parent be this summary element's parent.
  19. auto* parent = this->parent_element();
  20. // 3. If the open attribute is present on parent, then remove it. Otherwise, set parent's open attribute to the empty string.
  21. if (parent->has_attribute(HTML::AttributeNames::open))
  22. parent->remove_attribute(HTML::AttributeNames::open);
  23. else
  24. parent->set_attribute(HTML::AttributeNames::open, "").release_value_but_fixme_should_propagate_errors();
  25. };
  26. }
  27. // https://html.spec.whatwg.org/multipage/interactive-elements.html#summary-for-its-parent-details
  28. bool HTMLSummaryElement::is_summary_for_its_parent_details()
  29. {
  30. // A summary element is a summary for its parent details if the following algorithm returns true:
  31. // 1. If this summary element has no parent, then return false.
  32. if (!parent_element())
  33. return false;
  34. // 2. Let parent be this summary element's parent.
  35. auto* parent = this->parent_element();
  36. // 3. If parent is not a details element, then return false.
  37. if (!is<HTMLDetailsElement>(*parent))
  38. return false;
  39. // 4. If parent's first summary element child is not this summary element, then return false.
  40. if (parent->first_child_of_type<HTMLSummaryElement>() != this)
  41. return false;
  42. // 5. Return true.
  43. return true;
  44. }
  45. HTMLSummaryElement::~HTMLSummaryElement() = default;
  46. void HTMLSummaryElement::initialize(JS::Realm& realm)
  47. {
  48. Base::initialize(realm);
  49. }
  50. }