LibWeb: Resolve absolute calc() values in color functions
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

Currently, `calc()` values with relative units are not handled
correctly and will output an error to the console.
This commit is contained in:
Tim Ledbetter 2025-04-20 22:55:04 +01:00 committed by Andreas Kling
commit 6f5b107fcc
Notes: github-actions[bot] 2025-04-22 10:55:10 +00:00
2 changed files with 45 additions and 27 deletions

View file

@ -7,6 +7,7 @@
#include "ColorFunctionStyleValue.h"
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
@ -93,9 +94,26 @@ ColorFunctionStyleValue::Resolved ColorFunctionStyleValue::resolve_properties()
// https://www.w3.org/TR/css-color-4/#serializing-color-function-values
String ColorFunctionStyleValue::to_string(SerializationMode mode) const
{
auto convert_percentage = [](ValueComparingNonnullRefPtr<CSSStyleValue const> const& value) -> RemoveReference<decltype(value)> {
auto convert_percentage = [&](ValueComparingNonnullRefPtr<CSSStyleValue const> const& value) -> RemoveReference<decltype(value)> {
if (value->is_percentage())
return NumberStyleValue::create(value->as_percentage().value() / 100);
if (mode == SerializationMode::ResolvedValue && value->is_calculated()) {
// FIXME: Figure out how to get the proper calculation resolution context here
CalculationResolutionContext context {};
auto const& calculated = value->as_calculated();
if (calculated.resolves_to_percentage()) {
if (auto resolved_percentage = calculated.resolve_percentage(context); resolved_percentage.has_value()) {
auto resolved_number = resolved_percentage->value() / 100;
if (!isfinite(resolved_number))
resolved_number = 0;
return NumberStyleValue::create(resolved_number);
}
} else if (calculated.resolves_to_number()) {
if (auto resolved_number = calculated.resolve_number(context); resolved_number.has_value())
return NumberStyleValue::create(*resolved_number);
}
}
return value;
};