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) RefPtr<StyleValue const> Parser::parse_font_family_value(TokenStream<ComponentValue>& tokens)
{ {
// [ <family-name> | <generic-family> ]# // [ <family-name> | <generic-family> ]#
// FIXME: We currently require font-family to always be a list, even with one item. return parse_comma_separated_value_list(tokens, [this](auto& inner_tokens) -> RefPtr<StyleValue const> {
// Maybe change that?
auto result = parse_comma_separated_value_list(tokens, [this](auto& inner_tokens) -> RefPtr<StyleValue const> {
inner_tokens.discard_whitespace(); inner_tokens.discard_whitespace();
// <generic-family> // <generic-family>
@ -2883,15 +2881,6 @@ RefPtr<StyleValue const> Parser::parse_font_family_value(TokenStream<ComponentVa
// <family-name> // <family-name>
return parse_family_name_value(inner_tokens); 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) RefPtr<StyleValue const> Parser::parse_font_language_override_value(TokenStream<ComponentValue>& tokens)

View file

@ -1901,28 +1901,18 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
}; };
auto font_list = Gfx::FontCascadeList::create(); 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 : font_family.as_value_list().values()) {
for (auto const& family : family_list) { RefPtr<Gfx::FontCascadeList const> other_font_list;
RefPtr<Gfx::FontCascadeList const> other_font_list; if (family->is_keyword()) {
if (family->is_keyword()) { other_font_list = find_generic_font(family->to_keyword());
other_font_list = find_generic_font(family->to_keyword()); } else if (family->is_string()) {
} else if (family->is_string()) { other_font_list = find_font(family->as_string().string_value());
other_font_list = find_font(family->as_string().string_value()); } else if (family->is_custom_ident()) {
} else if (family->is_custom_ident()) { other_font_list = find_font(family->as_custom_ident().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())) if (other_font_list)
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); font_list->extend(*other_font_list);
} }
@ -2430,14 +2420,12 @@ GC::Ptr<ComputedProperties> StyleComputer::compute_style_impl(DOM::AbstractEleme
static bool is_monospace(StyleValue const& value) static bool is_monospace(StyleValue const& value)
{ {
if (value.to_keyword() == Keyword::Monospace) if (!value.is_value_list())
return true; return false;
if (value.is_value_list()) {
auto const& values = value.as_value_list().values(); auto const& values = value.as_value_list().values();
if (values.size() == 1 && values[0]->to_keyword() == Keyword::Monospace)
return true; return values.size() == 1 && values[0]->to_keyword() == Keyword::Monospace;
}
return false;
} }
// HACK: This function implements time-travelling inheritance for the font-size property // HACK: This function implements time-travelling inheritance for the font-size property