mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibWeb: Support top-level tree counting functions
Adds support for `sibling-index()` and `sibling-count()` when parsing
`<number>` and `<integer>`. This is achieved by a new
`TreeCountingFunctionStyleValue` class which is converted within
`absolutized` to `NumberStyleValue` and `IntegerStyleValue` respectively
There are still a few kinks to work out in order to support these
everywhere, namely:
- There are some `StyleValue`s which aren't absolutized (i.e. those
which are stored within another `StyleValue` without an
`absolutize()` method.
- We don't have a way to represent this new `StyleValue` within
`{Number,Integer}OrCalculated`. This would be fixed if we were to
instead just use the `StyleValue` classes until style computation at
which time they would be absolutized into their respective
primitives (double, i64, etc) bypassing the need for *OrCalculated
entirely.
This commit is contained in:
parent
9cd23e3ae5
commit
831e471444
Notes:
github-actions[bot]
2025-10-20 15:13:44 +00:00
Author: https://github.com/Calme1709
Commit: 831e471444
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6426
Reviewed-by: https://github.com/AtkinsSJ ✅
17 changed files with 184 additions and 32 deletions
|
|
@ -71,6 +71,7 @@
|
|||
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/TransitionStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/TreeCountingFunctionStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/URLStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ namespace Web::CSS {
|
|||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(Time, time, TimeStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(Transformation, transformation, TransformationStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(Transition, transition, TransitionStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(TreeCountingFunction, tree_counting_function, TreeCountingFunctionStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(UnicodeRange, unicode_range, UnicodeRangeStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(Unresolved, unresolved, UnresolvedStyleValue) \
|
||||
__ENUMERATE_CSS_STYLE_VALUE_TYPE(URL, url, URLStyleValue) \
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Callum Law <callumlaw1709@outlook.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "TreeCountingFunctionStyleValue.h"
|
||||
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
|
||||
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
String TreeCountingFunctionStyleValue::to_string(SerializationMode) const
|
||||
{
|
||||
switch (m_function) {
|
||||
case TreeCountingFunction::SiblingCount:
|
||||
return "sibling-count()"_string;
|
||||
case TreeCountingFunction::SiblingIndex:
|
||||
return "sibling-index()"_string;
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
size_t TreeCountingFunctionStyleValue::resolve(TreeCountingFunctionResolutionContext const& tree_counting_function_resolution_context) const
|
||||
{
|
||||
switch (m_function) {
|
||||
case TreeCountingFunction::SiblingCount:
|
||||
return tree_counting_function_resolution_context.sibling_count;
|
||||
case TreeCountingFunction::SiblingIndex:
|
||||
return tree_counting_function_resolution_context.sibling_index;
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
ValueComparingNonnullRefPtr<StyleValue const> TreeCountingFunctionStyleValue::absolutized(ComputationContext const& computation_context) const
|
||||
{
|
||||
// FIXME: We should clamp this value in case it falls outside the valid range for the context it is in
|
||||
VERIFY(computation_context.tree_counting_function_resolution_context.has_value());
|
||||
|
||||
size_t value = resolve(computation_context.tree_counting_function_resolution_context.value());
|
||||
|
||||
switch (m_computed_type) {
|
||||
case ComputedType::Integer:
|
||||
return IntegerStyleValue::create(value);
|
||||
case ComputedType::Number:
|
||||
return NumberStyleValue::create(static_cast<double>(value));
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool TreeCountingFunctionStyleValue::equals(StyleValue const& other) const
|
||||
{
|
||||
if (type() != other.type())
|
||||
return false;
|
||||
|
||||
auto const& other_tree_counting_function = other.as_tree_counting_function();
|
||||
|
||||
return m_function == other_tree_counting_function.m_function && m_computed_type == other_tree_counting_function.m_computed_type;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Callum Law <callumlaw1709@outlook.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/CSS/StyleValues/StyleValue.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
class TreeCountingFunctionStyleValue final : public StyleValue {
|
||||
public:
|
||||
enum class TreeCountingFunction : u8 {
|
||||
SiblingCount,
|
||||
SiblingIndex
|
||||
};
|
||||
|
||||
enum class ComputedType : u8 {
|
||||
Number,
|
||||
Integer
|
||||
};
|
||||
|
||||
static ValueComparingNonnullRefPtr<TreeCountingFunctionStyleValue const> create(TreeCountingFunction function, ComputedType computed_type)
|
||||
{
|
||||
return adopt_ref(*new (nothrow) TreeCountingFunctionStyleValue(function, computed_type));
|
||||
}
|
||||
virtual ~TreeCountingFunctionStyleValue() override = default;
|
||||
|
||||
virtual String to_string(SerializationMode) const override;
|
||||
|
||||
size_t resolve(TreeCountingFunctionResolutionContext const&) const;
|
||||
|
||||
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
|
||||
|
||||
virtual bool equals(StyleValue const& other) const override;
|
||||
|
||||
private:
|
||||
TreeCountingFunctionStyleValue(TreeCountingFunction function, ComputedType computed_type)
|
||||
: StyleValue(Type::TreeCountingFunction)
|
||||
, m_function(function)
|
||||
, m_computed_type(computed_type)
|
||||
{
|
||||
}
|
||||
|
||||
TreeCountingFunction m_function;
|
||||
ComputedType m_computed_type;
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue