HTMLOptGroupElement.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLOptGroupElementPrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  9. #include <LibWeb/HTML/HTMLSelectElement.h>
  10. namespace Web::HTML {
  11. GC_DEFINE_ALLOCATOR(HTMLOptGroupElement);
  12. HTMLOptGroupElement::HTMLOptGroupElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLOptGroupElement::~HTMLOptGroupElement() = default;
  17. void HTMLOptGroupElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLOptGroupElement);
  21. }
  22. void HTMLOptGroupElement::inserted()
  23. {
  24. Base::inserted();
  25. // AD-HOC: We update the selectedness of our <select> parent here,
  26. // to ensure that the correct <option> is selected after an <optgroup> is dynamically inserted.
  27. if (is<HTMLSelectElement>(*parent()) && first_child_of_type<HTMLOptionElement>())
  28. static_cast<HTMLSelectElement&>(*parent()).update_selectedness();
  29. }
  30. void HTMLOptGroupElement::removed_from(Node* old_parent)
  31. {
  32. Base::removed_from(old_parent);
  33. // The optgroup HTML element removing steps, given removedNode and oldParent, are:
  34. // 1. If oldParent is a select element and removedNode has an option child, then run oldParent's selectedness setting algorithm.
  35. if (old_parent && is<HTMLSelectElement>(*old_parent) && first_child_of_type<HTMLOptionElement>())
  36. static_cast<HTMLSelectElement&>(*old_parent).update_selectedness();
  37. }
  38. }