mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-30 13:19:02 +00:00
LibWeb: Move retarget function to its own file
This commit is contained in:
parent
8cf0f36f7d
commit
1aa9282103
Notes:
sideshowbarker
2024-07-18 23:45:27 +09:00
Author: https://github.com/circl-lastname
Commit: 1aa9282103
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/628
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/AtkinsSJ
2 changed files with 47 additions and 31 deletions
42
Userland/Libraries/LibWeb/DOM/Utils.h
Normal file
42
Userland/Libraries/LibWeb/DOM/Utils.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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
|
||||
template<typename T>
|
||||
T* retarget(T* a, T* 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue