2024-07-21 15:06:05 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
|
|
|
|
|
* Copyright (c) 2024, circl <circl.lastname@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
|
|
|
|
#include <LibWeb/DOM/Node.h>
|
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
|
|
|
|
#include <LibWeb/DOM/Utils.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#retarget
|
|
|
|
|
EventTarget* retarget(EventTarget* a, EventTarget* b)
|
|
|
|
|
{
|
|
|
|
|
// To retarget an object A against an object B, repeat these steps until they return an object:
|
|
|
|
|
for (;;) {
|
|
|
|
|
// 1. If one of the following is true then return A.
|
|
|
|
|
// - A is not a node
|
|
|
|
|
if (!is<Node>(a))
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
// - A’s root is not a shadow root
|
2025-01-21 09:12:05 -05:00
|
|
|
|
auto* a_node = as<Node>(a);
|
2024-07-21 15:06:05 +02:00
|
|
|
|
auto& a_root = a_node->root();
|
|
|
|
|
if (!is<ShadowRoot>(a_root))
|
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
// - B is a node and A’s root is a shadow-including inclusive ancestor of B
|
2025-01-21 09:12:05 -05:00
|
|
|
|
if (is<Node>(b) && a_root.is_shadow_including_inclusive_ancestor_of(as<Node>(*b)))
|
2024-07-21 15:06:05 +02:00
|
|
|
|
return a;
|
|
|
|
|
|
|
|
|
|
// 2. Set A to A’s root’s host.
|
2025-01-21 09:12:05 -05:00
|
|
|
|
auto& a_shadow_root = as<ShadowRoot>(a_root);
|
2024-07-21 15:06:05 +02:00
|
|
|
|
a = a_shadow_root.host();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|