LibWeb: Simplify handling of font-family

We know `parse_comma_separated_value_list` always returns a
`StyleValueList`
This commit is contained in:
Callum Law 2025-11-29 15:09:09 +13:00 committed by Sam Atkins
parent 0595d52be2
commit 11a9b5a67b
Notes: github-actions[bot] 2025-12-01 10:18:39 +00:00
2 changed files with 18 additions and 41 deletions

View file

@ -2865,9 +2865,7 @@ RefPtr<StyleValue const> Parser::parse_font_value(TokenStream<ComponentValue>& t
RefPtr<StyleValue const> Parser::parse_font_family_value(TokenStream<ComponentValue>& tokens)
{
// [ <family-name> | <generic-family> ]#
// FIXME: We currently require font-family to always be a list, even with one item.
// Maybe change that?
auto result = parse_comma_separated_value_list(tokens, [this](auto& inner_tokens) -> RefPtr<StyleValue const> {
return parse_comma_separated_value_list(tokens, [this](auto& inner_tokens) -> RefPtr<StyleValue const> {
inner_tokens.discard_whitespace();
// <generic-family>
@ -2883,15 +2881,6 @@ RefPtr<StyleValue const> Parser::parse_font_family_value(TokenStream<ComponentVa
// <family-name>
return parse_family_name_value(inner_tokens);
});
if (!result)
return nullptr;
if (result->is_value_list())
return result.release_nonnull();
// It's a single value, so wrap it in a list - see FIXME above.
return StyleValueList::create(StyleValueVector { result.release_nonnull() }, StyleValueList::Separator::Comma);
}
RefPtr<StyleValue const> Parser::parse_font_language_override_value(TokenStream<ComponentValue>& tokens)

View file

@ -1901,9 +1901,8 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
};
auto font_list = Gfx::FontCascadeList::create();
if (font_family.is_value_list()) {
auto const& family_list = static_cast<StyleValueList const&>(font_family).values();
for (auto const& family : family_list) {
for (auto const& family : font_family.as_value_list().values()) {
RefPtr<Gfx::FontCascadeList const> other_font_list;
if (family->is_keyword()) {
other_font_list = find_generic_font(family->to_keyword());
@ -1912,19 +1911,10 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
} else if (family->is_custom_ident()) {
other_font_list = find_font(family->as_custom_ident().custom_ident());
}
if (other_font_list)
font_list->extend(*other_font_list);
}
} else if (font_family.is_keyword()) {
if (auto other_font_list = find_generic_font(font_family.to_keyword()))
font_list->extend(*other_font_list);
} else if (font_family.is_string()) {
if (auto other_font_list = find_font(font_family.as_string().string_value()))
font_list->extend(*other_font_list);
} else if (font_family.is_custom_ident()) {
if (auto other_font_list = find_font(font_family.as_custom_ident().custom_ident()))
font_list->extend(*other_font_list);
}
auto default_font = Platform::FontPlugin::the().default_font(font_size_in_pt);
if (font_list->is_empty()) {
@ -2430,14 +2420,12 @@ GC::Ptr<ComputedProperties> StyleComputer::compute_style_impl(DOM::AbstractEleme
static bool is_monospace(StyleValue const& value)
{
if (value.to_keyword() == Keyword::Monospace)
return true;
if (value.is_value_list()) {
auto const& values = value.as_value_list().values();
if (values.size() == 1 && values[0]->to_keyword() == Keyword::Monospace)
return true;
}
if (!value.is_value_list())
return false;
auto const& values = value.as_value_list().values();
return values.size() == 1 && values[0]->to_keyword() == Keyword::Monospace;
}
// HACK: This function implements time-travelling inheritance for the font-size property