mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-29 14:32:55 +00:00
GCC 14 emits a warning when an always succeeding `dynamic_cast`'s return value is compared to NULL inside the `AK::is<T>(U)` template when `T` == `U`. While warning on tautological `is` calls seems useful, it's a bit awkward when it comes from a function template where the cast may fail in some instantiation. There is a GCC bug open for it: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115664 Work around the warning by performing the algorithm on the base type (`EventTarget`), with a wrapper that casts it to the more specialized input type.
48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
/*
|
||
* Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
|
||
* Copyright (c) 2024, circl <circl.lastname@gmail.com>
|
||
*
|
||
* SPDX-License-Identifier: BSD-2-Clause
|
||
*/
|
||
|
||
#pragma once
|
||
|
||
#include <LibWeb/DOM/Node.h>
|
||
#include <LibWeb/DOM/ShadowRoot.h>
|
||
|
||
namespace Web::DOM {
|
||
|
||
// https://dom.spec.whatwg.org/#retarget
|
||
inline EventTarget* retarget_impl(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
|
||
auto* a_node = verify_cast<Node>(a);
|
||
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
|
||
if (is<Node>(b) && a_root.is_shadow_including_inclusive_ancestor_of(verify_cast<Node>(*b)))
|
||
return a;
|
||
|
||
// 2. Set A to A’s root’s host.
|
||
auto& a_shadow_root = verify_cast<ShadowRoot>(a_root);
|
||
a = a_shadow_root.host();
|
||
}
|
||
}
|
||
|
||
// https://dom.spec.whatwg.org/#retarget
|
||
template<typename T>
|
||
T* retarget(T* a, T* b)
|
||
{
|
||
return static_cast<T*>(retarget_impl(a, b));
|
||
}
|
||
|
||
}
|