ladybird/Libraries/LibWeb/CSS/CSSRule.h
Sam Atkins 5c928eb7eb LibWeb/CSS: Implement the @scope rule
`@scope (a) to (b) {}` applies its contained style rules to elements
that have `a` as a parent, and do not have `a b` as a parent. Both the
`a` and `b` selector lists are optional.

Because it's situational whether a `@scope` will apply to a given
element, we store the ancestor scope on the `MatchingRule`, similar to
`@container`, and then determine during matching whether all the parent
`@scope`s match or not.

The rules for how selectors inside `@scope` are adjusted and interpreted
are a bit confusing. Unlike for other at-rules, nested style rules
inside `@scope` do not get a leading `&` added during parsing. To
support this, `adapt_nested_relative_selector_list()` now takes a flag
for whether its parent is a `@scope` or not.

`@scope` can also contain nested declarations without itself being
nested inside a style rule.

When determining their selectors, nested declarations rules adopt the
`@scope`'s scoping root if it has one, or otherwise fall back to the
parent element of the `<style>` element (not implemented here,) or the
`:root`. These are required to have zero specificity, so we wrap the
selector in `:where()`.
2026-05-22 10:00:42 +01:00

93 lines
2.6 KiB
C++

/*
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <LibGC/Ptr.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/CSS/CSSStyleDeclaration.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/Export.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::CSS {
class WEB_API CSSRule : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(CSSRule, Bindings::PlatformObject);
public:
virtual ~CSSRule() = default;
// https://drafts.csswg.org/cssom/#dom-cssrule-type
enum class Type : WebIDL::UnsignedShort {
Style = 1,
Import = 3,
Media = 4,
FontFace = 5,
Page = 6,
Keyframes = 7,
Keyframe = 8,
Margin = 9,
Namespace = 10,
CounterStyle = 11,
Supports = 12,
FontFeatureValues = 14,
// AD-HOC: These are not included in the spec, but we need them internally. So, their numbers are arbitrary.
LayerBlock = 100,
LayerStatement = 101,
NestedDeclarations = 102,
Property = 103,
Function = 104,
FunctionDeclarations = 105,
Container = 106,
Scope = 107,
};
Type type() const { return m_type; }
WebIDL::UnsignedShort type_for_bindings() const;
String css_text() const;
void set_css_text(StringView);
CSSRule* parent_rule() { return m_parent_rule.ptr(); }
CSSRule const* parent_rule() const { return m_parent_rule.ptr(); }
void set_parent_rule(CSSRule*);
CSSStyleSheet* parent_style_sheet() { return m_parent_style_sheet.ptr(); }
MUST_UPCALL virtual void set_parent_style_sheet(CSSStyleSheet*);
template<typename T>
bool fast_is() const = delete;
// https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
virtual String serialized() const = 0;
MUST_UPCALL virtual void dump(StringBuilder&, int indent_levels = 0) const;
MUST_UPCALL virtual void clear_caches();
protected:
CSSRule(JS::Realm&, Type);
virtual void visit_edges(Cell::Visitor&) override;
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
{
return m_cached_layer_name.ensure([&] { return parent_layer_internal_qualified_name_slow_case(); });
}
[[nodiscard]] FlyString parent_layer_internal_qualified_name_slow_case() const;
Type m_type;
GC::Ptr<CSSRule> m_parent_rule;
GC::Ptr<CSSStyleSheet> m_parent_style_sheet;
mutable Optional<FlyString> m_cached_layer_name;
};
}