ladybird/Libraries/LibWeb/CSS/StyleValues/CounterStyleValue.h
Sam Atkins c1d4323cf7 LibWeb: Support counter-* properties on pseudo-elements
There are multiple things happening here which are interconnected:

- We now use AbstractElement to refer to the source of a counter, which
  means we also need to pass that around to compute `content`.

- Give AbstractElement new helper methods that are needed by
  CountersSet, so it doesn't have to care whether it's dealing with a
  true Element or PseudoElement.

- The CountersSet algorithms now walk the layout tree instead of DOM
  tree, so TreeBuilder needs to wait until the layout node exists
  before it resolves counters for it.

- Resolve counters when creating a pseudo-element's layout node. We
  awkwardly compute the `content` value up to twice: Once to figure out
  what kind of node we need to make, and then if it's a string, we do
  so again after counters are resolved so we can get the true value of
  any `counter()` functions. This will need adjusting in the future but
  it works for now.
2025-06-19 12:35:31 +01:00

55 lines
2 KiB
C++

/*
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibWeb/CSS/CSSStyleValue.h>
namespace Web::CSS {
// https://drafts.csswg.org/css-lists-3/#counter-functions
class CounterStyleValue : public StyleValueWithDefaultOperators<CounterStyleValue> {
public:
enum class CounterFunction {
Counter,
Counters,
};
static ValueComparingNonnullRefPtr<CounterStyleValue const> create_counter(FlyString counter_name, ValueComparingNonnullRefPtr<CSSStyleValue const> counter_style)
{
return adopt_ref(*new (nothrow) CounterStyleValue(CounterFunction::Counter, move(counter_name), move(counter_style), {}));
}
static ValueComparingNonnullRefPtr<CounterStyleValue const> create_counters(FlyString counter_name, FlyString join_string, ValueComparingNonnullRefPtr<CSSStyleValue const> counter_style)
{
return adopt_ref(*new (nothrow) CounterStyleValue(CounterFunction::Counters, move(counter_name), move(counter_style), move(join_string)));
}
virtual ~CounterStyleValue() override;
CounterFunction function_type() const { return m_properties.function; }
auto counter_name() const { return m_properties.counter_name; }
auto counter_style() const { return m_properties.counter_style; }
auto join_string() const { return m_properties.join_string; }
String resolve(DOM::AbstractElement&) const;
virtual String to_string(SerializationMode) const override;
bool properties_equal(CounterStyleValue const& other) const;
private:
explicit CounterStyleValue(CounterFunction, FlyString counter_name, ValueComparingNonnullRefPtr<CSSStyleValue const> counter_style, FlyString join_string);
struct Properties {
CounterFunction function;
FlyString counter_name;
ValueComparingNonnullRefPtr<CSSStyleValue const> counter_style;
FlyString join_string;
bool operator==(Properties const&) const = default;
} m_properties;
};
}