LibWeb: Implement CSSNestedDeclarations type

This is basically a list of properties, without a block around it. It'll
be part of CSSStyleRules' contents.
This commit is contained in:
Sam Atkins 2024-10-15 15:59:31 +01:00 committed by Andreas Kling
commit 9c66ab356a
Notes: github-actions[bot] 2024-10-17 18:57:40 +00:00
13 changed files with 155 additions and 26 deletions

View file

@ -42,6 +42,7 @@ CSSLayerBlockRule
CSSLayerStatementRule
CSSMediaRule
CSSNamespaceRule
CSSNestedDeclarations
CSSRule
CSSRuleList
CSSStyleDeclaration

View file

@ -47,6 +47,7 @@ set(SOURCES
CSS/CSSLayerBlockRule.cpp
CSS/CSSLayerStatementRule.cpp
CSS/CSSMediaRule.cpp
CSS/CSSNestedDeclarations.cpp
CSS/CSSNumericType.cpp
CSS/CSSNamespaceRule.cpp
CSS/CSSRule.cpp

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSNestedDeclarations.h"
#include <LibWeb/Bindings/CSSNestedDeclarationsPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
namespace Web::CSS {
JS_DEFINE_ALLOCATOR(CSSNestedDeclarations);
JS::NonnullGCPtr<CSSNestedDeclarations> CSSNestedDeclarations::create(JS::Realm& realm, PropertyOwningCSSStyleDeclaration& declaration)
{
return realm.heap().allocate<CSSNestedDeclarations>(realm, realm, declaration);
}
CSSNestedDeclarations::CSSNestedDeclarations(JS::Realm& realm, PropertyOwningCSSStyleDeclaration& declaration)
: CSSRule(realm)
, m_declaration(declaration)
{
m_declaration->set_parent_rule(*this);
}
void CSSNestedDeclarations::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSNestedDeclarations);
}
void CSSNestedDeclarations::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_declaration);
}
CSSStyleDeclaration* CSSNestedDeclarations::style()
{
return m_declaration;
}
String CSSNestedDeclarations::serialized() const
{
// NOTE: There's no proper spec for this yet, only this note:
// "The CSSNestedDeclarations rule serializes as if its declaration block had been serialized directly."
// - https://drafts.csswg.org/css-nesting-1/#ref-for-cssnesteddeclarations%E2%91%A1
// So, we'll do the simple thing and hope it's good.
return m_declaration->serialized();
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSRule.h>
namespace Web::CSS {
class CSSNestedDeclarations final : public CSSRule {
WEB_PLATFORM_OBJECT(CSSNestedDeclarations, CSSRule);
JS_DECLARE_ALLOCATOR(CSSNestedDeclarations);
public:
[[nodiscard]] static JS::NonnullGCPtr<CSSNestedDeclarations> create(JS::Realm&, PropertyOwningCSSStyleDeclaration&);
virtual ~CSSNestedDeclarations() override = default;
virtual Type type() const override { return Type::NestedDeclarations; }
PropertyOwningCSSStyleDeclaration const& declaration() const { return m_declaration; }
CSSStyleDeclaration* style();
private:
CSSNestedDeclarations(JS::Realm&, PropertyOwningCSSStyleDeclaration&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
virtual String serialized() const override;
JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> m_declaration;
};
template<>
inline bool CSSRule::fast_is<CSSNestedDeclarations>() const { return type() == CSSRule::Type::NestedDeclarations; }
}

View file

@ -0,0 +1,9 @@
#import <CSS/CSSRule.idl>
#import <CSS/CSSStyleDeclaration.idl>
// https://drafts.csswg.org/css-nesting-1/#cssnesteddeclarations
[Exposed=Window]
interface CSSNestedDeclarations : CSSRule {
// FIXME: Should be a CSSStyleProperties, once we have that
[SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
};

View file

@ -53,11 +53,11 @@ FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
Vector<FlyString> layer_names;
for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
switch (rule->type()) {
case CSSRule::Type::Import:
case Type::Import:
// TODO: Handle `layer(foo)` in import rules once we implement that.
break;
case CSSRule::Type::LayerBlock: {
case Type::LayerBlock: {
auto& layer_block = static_cast<CSSLayerBlockRule const&>(*rule);
layer_names.append(layer_block.internal_name());
break;
@ -65,14 +65,15 @@ FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
// Ignore everything else
// Note that LayerStatement cannot have child rules so we still ignore it here.
case CSSRule::Type::LayerStatement:
case CSSRule::Type::Style:
case CSSRule::Type::Media:
case CSSRule::Type::FontFace:
case CSSRule::Type::Keyframes:
case CSSRule::Type::Keyframe:
case CSSRule::Type::Namespace:
case CSSRule::Type::Supports:
case Type::LayerStatement:
case Type::Style:
case Type::Media:
case Type::FontFace:
case Type::Keyframes:
case Type::Keyframe:
case Type::Namespace:
case Type::Supports:
case Type::NestedDeclarations:
break;
}
}

View file

@ -34,6 +34,7 @@ public:
// 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,
};
virtual Type type() const = 0;

View file

@ -144,6 +144,7 @@ void CSSRuleList::for_each_effective_rule(TraversalOrder order, Function<void(We
case CSSRule::Type::Keyframes:
case CSSRule::Type::LayerStatement:
case CSSRule::Type::Namespace:
case CSSRule::Type::NestedDeclarations:
case CSSRule::Type::Style:
break;
}
@ -192,6 +193,7 @@ bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
case CSSRule::Type::Keyframes:
case CSSRule::Type::LayerStatement:
case CSSRule::Type::Namespace:
case CSSRule::Type::NestedDeclarations:
case CSSRule::Type::Style:
break;
}

View file

@ -2664,6 +2664,7 @@ void StyleComputer::build_qualified_layer_names_cache()
case CSSRule::Type::Keyframes:
case CSSRule::Type::Keyframe:
case CSSRule::Type::Namespace:
case CSSRule::Type::NestedDeclarations:
case CSSRule::Type::Supports:
break;
}

View file

@ -14,6 +14,7 @@
#include <LibWeb/CSS/CSSLayerBlockRule.h>
#include <LibWeb/CSS/CSSLayerStatementRule.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSNestedDeclarations.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
@ -641,6 +642,9 @@ void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_leve
case CSS::CSSRule::Type::Namespace:
dump_namespace_rule(builder, verify_cast<CSS::CSSNamespaceRule const>(rule), indent_levels);
break;
case CSS::CSSRule::Type::NestedDeclarations:
dump_nested_declarations(builder, verify_cast<CSS::CSSNestedDeclarations const>(rule), indent_levels);
break;
case CSS::CSSRule::Type::Style:
dump_style_rule(builder, verify_cast<CSS::CSSStyleRule const>(rule), indent_levels);
break;
@ -780,21 +784,18 @@ void dump_supports_rule(StringBuilder& builder, CSS::CSSSupportsRule const& supp
dump_rule(builder, rule, indent_levels + 2);
}
void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels)
void dump_declaration(StringBuilder& builder, CSS::PropertyOwningCSSStyleDeclaration const& declaration, int indent_levels)
{
for (auto& selector : rule.selectors()) {
dump_selector(builder, selector, indent_levels + 1);
}
indent(builder, indent_levels);
builder.appendff(" Declarations ({}):\n", rule.declaration().length());
for (auto& property : rule.declaration().properties()) {
builder.appendff("Declarations ({}):\n", declaration.length());
for (auto& property : declaration.properties()) {
indent(builder, indent_levels);
builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string());
if (property.important == CSS::Important::Yes)
builder.append(" \033[31;1m!important\033[0m"sv);
builder.append('\n');
}
for (auto& property : rule.declaration().custom_properties()) {
for (auto& property : declaration.custom_properties()) {
indent(builder, indent_levels);
builder.appendff(" {}: '{}'", property.key, property.value.value->to_string());
if (property.value.important == CSS::Important::Yes)
@ -803,6 +804,14 @@ void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int
}
}
void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels)
{
for (auto& selector : rule.selectors()) {
dump_selector(builder, selector, indent_levels + 1);
}
dump_declaration(builder, rule.declaration(), indent_levels + 1);
}
void dump_sheet(CSS::StyleSheet const& sheet)
{
StringBuilder builder;
@ -883,4 +892,11 @@ void dump_namespace_rule(StringBuilder& builder, CSS::CSSNamespaceRule const& na
builder.appendff(" Prefix: {}\n", namespace_.prefix());
}
void dump_nested_declarations(StringBuilder& builder, CSS::CSSNestedDeclarations const& declarations, int indent_levels)
{
indent(builder, indent_levels);
builder.append(" Nested declarations:\n"sv);
dump_declaration(builder, declarations.declaration(), indent_levels + 1);
}
}

View file

@ -24,12 +24,14 @@ void dump_sheet(StringBuilder&, CSS::StyleSheet const&);
void dump_sheet(CSS::StyleSheet const&);
void dump_rule(StringBuilder&, CSS::CSSRule const&, int indent_levels = 0);
void dump_rule(CSS::CSSRule const&);
void dump_declaration(StringBuilder&, CSS::PropertyOwningCSSStyleDeclaration const&, int indent_levels = 0);
void dump_font_face_rule(StringBuilder&, CSS::CSSFontFaceRule const&, int indent_levels = 0);
void dump_import_rule(StringBuilder&, CSS::CSSImportRule const&, int indent_levels = 0);
void dump_media_rule(StringBuilder&, CSS::CSSMediaRule const&, int indent_levels = 0);
void dump_style_rule(StringBuilder&, CSS::CSSStyleRule const&, int indent_levels = 0);
void dump_supports_rule(StringBuilder&, CSS::CSSSupportsRule const&, int indent_levels = 0);
void dump_namespace_rule(StringBuilder&, CSS::CSSNamespaceRule const&, int indent_levels = 0);
void dump_nested_declarations(StringBuilder&, CSS::CSSNestedDeclarations const&, int indent_levels = 0);
void dump_layer_block_rule(StringBuilder&, CSS::CSSLayerBlockRule const&, int indent_levels = 0);
void dump_layer_statement_rule(StringBuilder&, CSS::CSSLayerStatementRule const&, int indent_levels = 0);
void dump_selector(StringBuilder&, CSS::Selector const&, int indent_levels = 0);

View file

@ -117,6 +117,7 @@ class CSSLayerBlockRule;
class CSSLayerStatementRule;
class CSSMathValue;
class CSSMediaRule;
class CSSNestedDeclarations;
class CSSOKLab;
class CSSOKLCH;
class CSSRGB;

View file

@ -25,6 +25,7 @@ libweb_js_bindings(CSS/CSSLayerStatementRule)
libweb_js_bindings(CSS/CSSMediaRule)
libweb_js_bindings(CSS/CSS NAMESPACE)
libweb_js_bindings(CSS/CSSNamespaceRule)
libweb_js_bindings(CSS/CSSNestedDeclarations)
libweb_js_bindings(CSS/CSSRule)
libweb_js_bindings(CSS/CSSRuleList)
libweb_js_bindings(CSS/CSSStyleDeclaration)