LibWeb: Set context for parsing against <foo-percentage> syntax

This means that we correctly parse dimension percentage mixes (i.e.
`calc(10px + 10%)` is a valid `<length-percentage>`)
This commit is contained in:
Callum Law
2026-03-08 12:43:20 +13:00
committed by Sam Atkins
parent ed2909674f
commit 614a5cf33e
Notes: github-actions[bot] 2026-03-26 01:13:21 +00:00
6 changed files with 28 additions and 10 deletions

View File

@@ -62,6 +62,9 @@ struct NegateNode {
}
struct SyntaxParsingContext {
ValueType type;
};
struct FunctionContext {
StringView name;
};
@@ -85,7 +88,7 @@ enum SpecialContext : u8 {
TranslateZArgument,
};
// FIXME: Use PropertyNameAndID instead of PropertyID as the context, for registered custom properties.
using ValueParsingContext = Variant<PropertyID, FunctionContext, DescriptorContext, SpecialContext>;
using ValueParsingContext = Variant<PropertyID, FunctionContext, DescriptorContext, SpecialContext, SyntaxParsingContext>;
enum class ParsingMode {
Normal,

View File

@@ -246,6 +246,7 @@ RefPtr<StyleValue const> Parser::parse_according_to_syntax_node(TokenStream<Comp
auto const& type_node = as<TypeSyntaxNode>(syntax_node);
auto const& type_name = type_node.type_name();
if (auto value_type = value_type_from_string(type_name); value_type.has_value()) {
auto scope_guard = push_temporary_value_parsing_context(SyntaxParsingContext { *value_type });
if (auto result = parse_value(*value_type, tokens)) {
transaction.commit();
return result.release_nonnull();

View File

@@ -5162,6 +5162,20 @@ RefPtr<CalculatedStyleValue const> Parser::parse_calculated_value(ComponentValue
return {};
}
VERIFY_NOT_REACHED();
},
[](SyntaxParsingContext const& syntax_context) -> Optional<CalculationContext> {
switch (syntax_context.type) {
case ValueType::AnglePercentage:
return CalculationContext { .percentages_resolve_as = ValueType::Angle };
case ValueType::FrequencyPercentage:
return CalculationContext { .percentages_resolve_as = ValueType::Frequency };
case ValueType::LengthPercentage:
return CalculationContext { .percentages_resolve_as = ValueType::Length };
case ValueType::TimePercentage:
return CalculationContext { .percentages_resolve_as = ValueType::Time };
default:
return {};
}
});
if (maybe_context.has_value()) {
context = maybe_context.release_value();