ladybird/Tests/LibWeb/TestCSSStyleSheetInvalidation.cpp
Jelle Raaijmakers 2f39b2dd63 LibWeb: Split shadow stylesheet host-side invalidation reach
Broad shadow-root stylesheet changes already restyle the whole shadow
tree, but host-side fallout does not always need a document-wide
invalidation. Split the host-side reach classification so selectors
contained to the host subtree, such as `:host *` and `:host > *`,
invalidate the host, while sibling-escaping selectors such as
`:host + :has(*)` still invalidate the host's root.

Recognize sibling escapes through positive selector-list pseudos such as
`:is()` and `:where()` as well, including selectors like
`:is(:host) + :has(*)` and `:is(:host + .item)`.

This avoids turning host-contained shadow stylesheet changes into full
document style invalidations. On https://pomax.github.io/bezierinfo/,
this reduces the time to produce a layout tree from about 8.7s to 3.6s
on my machine.
2026-05-05 18:36:34 +02:00

61 lines
3.7 KiB
C++

/*
* Copyright (c) 2026-present, The Ladybird developers
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <LibWeb/CSS/StyleSheetInvalidation.h>
namespace Web {
TEST_CASE(selector_escape_to_light_dom_under_shadow_host)
{
EXPECT(!CSS::selector_may_match_light_dom_under_shadow_host(":host"sv));
EXPECT(!CSS::selector_may_match_light_dom_under_shadow_host(":host(.active)"sv));
EXPECT(!CSS::selector_may_match_light_dom_under_shadow_host(":is(:host)"sv));
EXPECT(!CSS::selector_may_match_light_dom_under_shadow_host(":where(:host)"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":host > *"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":host + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":host ~ .item"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":is(:host) > *"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":where(:host) .item"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host(":is(.foo, :host) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_under_shadow_host("::slotted(.item)"sv));
}
TEST_CASE(selector_escape_to_light_dom_outside_shadow_host)
{
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":host"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":host(.active)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":host > *"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":host *"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host("::slotted(.item)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":where(:host)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host) > *"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":where(:host) *"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host > .item)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":where(:host .item)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":host:has(+ .item)"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":has(:host) + .item"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":not(:host) + .item"sv));
EXPECT(!CSS::selector_may_match_light_dom_outside_shadow_host(":nth-child(1 of :host) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":host + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":host ~ .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":host + :has(*)"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":where(:host) ~ .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host) + :has(*)"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(.foo, :host) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":where(.foo, :host(.active)) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(:where(:host)) + .item"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host + .item)"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":where(:host ~ .item)"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(:host + .item) > .child"sv));
EXPECT(CSS::selector_may_match_light_dom_outside_shadow_host(":is(.foo, :where(:host + .item))"sv));
}
}