mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibWeb/CSS: Add CSSOM types for @layer
rules
Depending on usage, `@layer` has two forms, with two different CSSOM types. One simply lists layer names and the other defines a layer with its contained rules.
This commit is contained in:
parent
bf9d05d97a
commit
1c6133aa52
Notes:
github-actions[bot]
2024-09-06 05:51:10 +00:00
Author: https://github.com/AtkinsSJ
Commit: 1c6133aa52
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1291
17 changed files with 346 additions and 27 deletions
81
Userland/Libraries/LibWeb/CSS/CSSLayerBlockRule.cpp
Normal file
81
Userland/Libraries/LibWeb/CSS/CSSLayerBlockRule.cpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "CSSLayerBlockRule.h"
|
||||
#include <LibWeb/Bindings/CSSLayerBlockRulePrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/CSS/Serialize.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(CSSLayerBlockRule);
|
||||
|
||||
JS::NonnullGCPtr<CSSLayerBlockRule> CSSLayerBlockRule::create(JS::Realm& realm, FlyString name, CSSRuleList& rules)
|
||||
{
|
||||
return realm.heap().allocate<CSSLayerBlockRule>(realm, realm, move(name), rules);
|
||||
}
|
||||
|
||||
FlyString CSSLayerBlockRule::next_unique_anonymous_layer_name()
|
||||
{
|
||||
static u64 s_anonymous_layer_id = 0;
|
||||
return MUST(String::formatted("#{}", ++s_anonymous_layer_id));
|
||||
}
|
||||
|
||||
CSSLayerBlockRule::CSSLayerBlockRule(JS::Realm& realm, FlyString name, CSSRuleList& rules)
|
||||
: CSSGroupingRule(realm, rules)
|
||||
, m_name(move(name))
|
||||
{
|
||||
if (m_name.is_empty()) {
|
||||
m_name_internal = next_unique_anonymous_layer_name();
|
||||
} else {
|
||||
m_name_internal = m_name;
|
||||
}
|
||||
}
|
||||
|
||||
void CSSLayerBlockRule::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSLayerBlockRule);
|
||||
}
|
||||
|
||||
String CSSLayerBlockRule::serialized() const
|
||||
{
|
||||
// AD-HOC: No spec yet, so this is based on the @media serialization algorithm.
|
||||
StringBuilder builder;
|
||||
builder.append("@layer"sv);
|
||||
if (!m_name.is_empty())
|
||||
builder.appendff(" {}", m_name);
|
||||
|
||||
builder.append(" {\n"sv);
|
||||
// AD-HOC: All modern browsers omit the ending newline if there are no CSS rules, so let's do the same.
|
||||
if (css_rules().length() == 0) {
|
||||
builder.append('}');
|
||||
return builder.to_string_without_validation();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < css_rules().length(); i++) {
|
||||
auto rule = css_rules().item(i);
|
||||
if (i != 0)
|
||||
builder.append("\n"sv);
|
||||
builder.append(" "sv);
|
||||
builder.append(rule->css_text());
|
||||
}
|
||||
|
||||
builder.append("\n}"sv);
|
||||
|
||||
return builder.to_string_without_validation();
|
||||
}
|
||||
|
||||
FlyString CSSLayerBlockRule::internal_qualified_name(Badge<StyleComputer>) const
|
||||
{
|
||||
// TODO: Cache this?
|
||||
auto parent_name = parent_layer_internal_qualified_name();
|
||||
if (parent_name.is_empty())
|
||||
return m_name_internal;
|
||||
return MUST(String::formatted("{}.{}", parent_name, m_name_internal));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue