Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibWeb/DOM/Utils.cpp
Line
Count
Source (jump to first uncovered line)
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
8
#include <LibWeb/DOM/EventTarget.h>
9
#include <LibWeb/DOM/Node.h>
10
#include <LibWeb/DOM/ShadowRoot.h>
11
#include <LibWeb/DOM/Utils.h>
12
13
namespace Web::DOM {
14
15
// https://dom.spec.whatwg.org/#retarget
16
EventTarget* retarget(EventTarget* a, EventTarget* b)
17
0
{
18
    // To retarget an object A against an object B, repeat these steps until they return an object:
19
0
    for (;;) {
20
        // 1. If one of the following is true then return A.
21
        // - A is not a node
22
0
        if (!is<Node>(a))
23
0
            return a;
24
25
        // - A’s root is not a shadow root
26
0
        auto* a_node = verify_cast<Node>(a);
27
0
        auto& a_root = a_node->root();
28
0
        if (!is<ShadowRoot>(a_root))
29
0
            return a;
30
31
        // - B is a node and A’s root is a shadow-including inclusive ancestor of B
32
0
        if (is<Node>(b) && a_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*b)))
33
0
            return a;
34
35
        // 2. Set A to A’s root’s host.
36
0
        auto& a_shadow_root = verify_cast<ShadowRoot>(a_root);
37
0
        a = a_shadow_root.host();
38
0
    }
39
0
}
40
41
}