Utils.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2024, circl <circl.lastname@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/EventTarget.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/DOM/Utils.h>
  11. namespace Web::DOM {
  12. // https://dom.spec.whatwg.org/#retarget
  13. EventTarget* retarget(EventTarget* a, EventTarget* b)
  14. {
  15. // To retarget an object A against an object B, repeat these steps until they return an object:
  16. for (;;) {
  17. // 1. If one of the following is true then return A.
  18. // - A is not a node
  19. if (!is<Node>(a))
  20. return a;
  21. // - A’s root is not a shadow root
  22. auto* a_node = verify_cast<Node>(a);
  23. auto& a_root = a_node->root();
  24. if (!is<ShadowRoot>(a_root))
  25. return a;
  26. // - B is a node and A’s root is a shadow-including inclusive ancestor of B
  27. if (is<Node>(b) && a_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*b)))
  28. return a;
  29. // 2. Set A to A’s root’s host.
  30. auto& a_shadow_root = verify_cast<ShadowRoot>(a_root);
  31. a = a_shadow_root.host();
  32. }
  33. }
  34. }