LibWeb: Don't deduplicate font-variation-settings values at parse time

We now only deduplicate and sort the computed value of the
`font-variation-settings` property.
This commit is contained in:
Tim Ledbetter 2025-09-19 16:44:18 +01:00 committed by Sam Atkins
commit 83ad5ce8a2
Notes: github-actions[bot] 2025-09-26 10:22:16 +00:00
4 changed files with 38 additions and 20 deletions

View file

@ -3098,11 +3098,7 @@ RefPtr<StyleValue const> Parser::parse_font_variation_settings_value(TokenStream
auto transaction = tokens.begin_transaction();
auto tag_values = parse_a_comma_separated_list_of_component_values(tokens);
// "If the same axis name appears more than once, the value associated with the last appearance supersedes any
// previous value for that axis. This deduplication is observable by accessing the computed value of this property."
// So, we deduplicate them here using a HashSet.
OrderedHashMap<FlyString, NonnullRefPtr<OpenTypeTaggedStyleValue const>> axis_tags_map;
StyleValueVector axis_tags;
for (auto const& values : tag_values) {
TokenStream tag_tokens { values };
tag_tokens.discard_whitespace();
@ -3114,19 +3110,9 @@ RefPtr<StyleValue const> Parser::parse_font_variation_settings_value(TokenStream
if (!opentype_tag || !number || tag_tokens.has_next_token())
return nullptr;
axis_tags_map.set(opentype_tag->string_value(), OpenTypeTaggedStyleValue::create(OpenTypeTaggedStyleValue::Mode::FontVariationSettings, opentype_tag->string_value(), number.release_nonnull()));
axis_tags.append(OpenTypeTaggedStyleValue::create(OpenTypeTaggedStyleValue::Mode::FontVariationSettings, opentype_tag->string_value(), number.release_nonnull()));
}
// "The computed value contains the de-duplicated axis names, sorted in ascending order by code unit."
StyleValueVector axis_tags;
axis_tags.ensure_capacity(axis_tags_map.size());
for (auto const& [key, axis_tag] : axis_tags_map)
axis_tags.append(axis_tag);
quick_sort(axis_tags, [](auto& a, auto& b) {
return a->as_open_type_tagged().tag() < b->as_open_type_tagged().tag();
});
transaction.commit();
return StyleValueList::create(move(axis_tags), StyleValueList::Separator::Comma);
}

View file

@ -62,6 +62,7 @@
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
#include <LibWeb/CSS/StyleValues/PendingSubstitutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
@ -3211,6 +3212,8 @@ NonnullRefPtr<StyleValue const> StyleComputer::compute_value_of_property(Propert
return compute_border_or_outline_width(specified_value, get_property_specified_value(PropertyID::BorderTopStyle), computation_context);
case PropertyID::OutlineWidth:
return compute_border_or_outline_width(specified_value, get_property_specified_value(PropertyID::OutlineStyle), computation_context);
case PropertyID::FontVariationSettings:
return compute_font_variation_settings(specified_value);
case PropertyID::LetterSpacing:
case PropertyID::WordSpacing:
if (specified_value->to_keyword() == Keyword::Normal)
@ -3257,6 +3260,35 @@ NonnullRefPtr<StyleValue const> StyleComputer::compute_animation_name(NonnullRef
});
}
NonnullRefPtr<StyleValue const> StyleComputer::compute_font_variation_settings(NonnullRefPtr<StyleValue const> const& specified_value)
{
if (specified_value->is_keyword())
return specified_value;
// https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def
// If the same axis name appears more than once, the value associated with the last appearance supersedes any
// previous value for that axis. This deduplication is observable by accessing the computed value of this property."
// So, we deduplicate them here using a HashSet.
auto const& value_list = specified_value->as_value_list();
OrderedHashMap<FlyString, NonnullRefPtr<OpenTypeTaggedStyleValue const>> axis_tags_map;
for (size_t i = 0; i < value_list.values().size(); i++) {
auto const& axis_tag = value_list.values().at(i)->as_open_type_tagged();
axis_tags_map.set(axis_tag.tag(), axis_tag);
}
StyleValueVector axis_tags;
// The computed value contains the de-duplicated axis names, sorted in ascending order by code unit.
for (auto const& [key, axis_tag] : axis_tags_map)
axis_tags.append(axis_tag);
quick_sort(axis_tags, [](auto& a, auto& b) {
return a->as_open_type_tagged().tag() < b->as_open_type_tagged().tag();
});
return StyleValueList::create(move(axis_tags), StyleValueList::Separator::Comma);
}
NonnullRefPtr<StyleValue const> StyleComputer::compute_border_or_outline_width(NonnullRefPtr<StyleValue const> const& specified_value, NonnullRefPtr<StyleValue const> const& style_specified_value, PropertyValueComputationContext const& computation_context)
{
// https://drafts.csswg.org/css-backgrounds/#border-width

View file

@ -209,6 +209,7 @@ public:
static NonnullRefPtr<StyleValue const> compute_font_style(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const& parent_length_resolution_context);
static NonnullRefPtr<StyleValue const> compute_font_weight(NonnullRefPtr<StyleValue const> const& specified_value, double inherited_font_weight, Length::ResolutionContext const& parent_length_resolution_context);
static NonnullRefPtr<StyleValue const> compute_font_width(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const& parent_length_resolution_context);
static NonnullRefPtr<StyleValue const> compute_font_variation_settings(NonnullRefPtr<StyleValue const> const& specified_value);
static NonnullRefPtr<StyleValue const> compute_line_height(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const&);
static NonnullRefPtr<StyleValue const> compute_opacity(NonnullRefPtr<StyleValue const> const& specified_value, PropertyValueComputationContext const&);
static NonnullRefPtr<StyleValue const> compute_text_underline_offset(NonnullRefPtr<StyleValue const> const& specified_value, PropertyValueComputationContext const&);

View file

@ -2,12 +2,11 @@ Harness status: OK
Found 7 tests
5 Pass
2 Fail
7 Pass
Pass e.style['font-variation-settings'] = "normal" should set the property value
Pass e.style['font-variation-settings'] = "\"wght\" 700" should set the property value
Pass e.style['font-variation-settings'] = "'wght' 700" should set the property value
Fail e.style['font-variation-settings'] = "\"wght\" 700, \"XHGT\" 0.7" should set the property value
Pass e.style['font-variation-settings'] = "\"wght\" 700, \"XHGT\" 0.7" should set the property value
Pass e.style['font-variation-settings'] = "\"a cd\" 0.5" should set the property value
Pass e.style['font-variation-settings'] = "\"ab@d\" 0.5" should set the property value
Fail e.style['font-variation-settings'] = "'wght' 1e3, 'slnt' -450.0e-1" should set the property value
Pass e.style['font-variation-settings'] = "'wght' 1e3, 'slnt' -450.0e-1" should set the property value