LibWeb: Add generic string_from_style_value method

Reduces duplication in line with `Time::from_style_value()`,
`Angle::from_style_value()` etc
This commit is contained in:
Callum Law 2026-02-04 19:59:10 +13:00 committed by Sam Atkins
parent 784911fb6d
commit d998a0aeeb
Notes: github-actions[bot] 2026-02-17 12:26:43 +00:00
7 changed files with 25 additions and 56 deletions

View file

@ -2100,16 +2100,7 @@ Vector<ComputedProperties::AnimationProperties> ComputedProperties::animations(D
auto delay = Time::from_style_value(animation_delay_style_value, {}).to_milliseconds();
auto fill_mode = keyword_to_animation_fill_mode(animation_fill_mode_style_value->to_keyword()).value();
auto composition = keyword_to_animation_composition(animation_composition_style_value->to_keyword()).value();
auto name = [&] {
if (animation_name_style_value->is_custom_ident())
return animation_name_style_value->as_custom_ident().custom_ident();
if (animation_name_style_value->is_string())
return animation_name_style_value->as_string().string_value();
VERIFY_NOT_REACHED();
}();
auto name = string_from_style_value(animation_name_style_value);
// https://drafts.csswg.org/css-animations-2/#animation-timeline
auto const& timeline = [&]() -> GC::Ptr<Animations::AnimationTimeline> {

View file

@ -73,12 +73,8 @@ struct Traits<Web::CSS::ComputedFontCacheKey> : public DefaultTraits<Web::CSS::C
for (auto const& family_value : key.font_family->as_value_list().values()) {
if (family_value->is_keyword())
hash = pair_int_hash(hash, to_underlying(family_value->as_keyword().keyword()));
else if (family_value->is_string())
hash = pair_int_hash(hash, family_value->as_string().string_value().hash());
else if (family_value->is_custom_ident())
hash = pair_int_hash(hash, family_value->as_custom_ident().custom_ident().hash());
else
VERIFY_NOT_REACHED();
hash = string_from_style_value(family_value).hash();
}
hash = pair_int_hash(hash, to_underlying(key.font_optical_sizing));
@ -492,10 +488,8 @@ NonnullRefPtr<Gfx::FontCascadeList const> FontComputer::compute_font_for_style_v
RefPtr<Gfx::FontCascadeList const> other_font_list;
if (family->is_keyword()) {
other_font_list = find_generic_font(family->to_keyword());
} else if (family->is_string()) {
other_font_list = find_font(family->as_string().string_value());
} else if (family->is_custom_ident()) {
other_font_list = find_font(family->as_custom_ident().custom_ident());
} else {
other_font_list = find_font(string_from_style_value(family));
}
if (other_font_list)
@ -547,13 +541,10 @@ static bool style_value_references_font_family(StyleValue const& font_family_val
return false;
for (auto const& item : font_family_value.as_value_list().values()) {
FlyString item_family_name;
if (item->is_string())
item_family_name = item->as_string().string_value();
else if (item->is_custom_ident())
item_family_name = item->as_custom_ident().custom_ident();
else
continue; // Skip generic keywords (sans-serif, serif, etc.)
if (item->is_keyword())
continue; // Skip generic keywords (monospace, serif, etc.)
FlyString item_family_name = string_from_style_value(*item);
if (item_family_name.equals_ignoring_ascii_case(family_name))
return true;

View file

@ -281,12 +281,7 @@ WebIDL::ExceptionOr<void> FontFace::set_family(String const& string)
void FontFace::set_family_impl(NonnullRefPtr<StyleValue const> const& value)
{
if (value->is_custom_ident())
m_family = value->as_custom_ident().custom_ident().to_string();
else if (value->is_string())
m_family = value->as_string().string_value().to_string();
else
VERIFY_NOT_REACHED();
m_family = string_from_style_value(value).to_string();
}
// https://drafts.csswg.org/css-font-loading/#dom-fontface-style

View file

@ -230,22 +230,11 @@ static WebIDL::ExceptionOr<GC::Ref<JS::Set>> find_matching_font_faces(JS::Realm&
// that this may be more than just a single font face.
for (auto const& font_family : font_family_list.values()) {
// FIXME: The matching below is super basic. We currently just match font family names by their string value.
auto maybe_font_family_name = [&]() -> Optional<FlyString> {
if (font_family->is_string())
return font_family->as_string().string_value();
if (font_family->is_custom_ident())
return font_family->as_custom_ident().custom_ident();
return {};
}();
if (!maybe_font_family_name.has_value())
continue;
auto font_family_name = string_from_style_value(font_family);
for (auto font_face_value : *available_font_faces) {
auto& font_face = as<FontFace>(font_face_value.key.as_object());
if (font_face.family() != maybe_font_family_name.value())
if (font_face.family() != font_family_name)
continue;
matched_font_faces->set_add(font_face_value.key);

View file

@ -26,22 +26,13 @@
namespace Web::CSS {
static FlyString extract_font_name(StyleValue const& value)
{
if (value.is_string())
return value.as_string().string_value();
if (value.is_custom_ident())
return value.as_custom_ident().custom_ident();
return FlyString {};
}
Vector<ParsedFontFace::Source> ParsedFontFace::sources_from_style_value(StyleValue const& style_value)
{
Vector<Source> sources;
auto add_source = [&sources](FontSourceStyleValue const& font_source) {
font_source.source().visit(
[&](FontSourceStyleValue::Local const& local) {
sources.empend(extract_font_name(local.name), OptionalNone {}, Vector<FontTech> {});
sources.empend(string_from_style_value(local.name), OptionalNone {}, Vector<FontTech> {});
},
[&](URL const& url) {
sources.empend(url, font_source.format(), font_source.tech());
@ -74,7 +65,7 @@ ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& de
FlyString font_family;
if (auto value = descriptors.descriptor_or_initial_value(DescriptorID::FontFamily))
font_family = extract_font_name(*value);
font_family = string_from_style_value(*value);
ComputationContext computation_context {
.length_resolution_context = Length::ResolutionContext::for_document(*descriptors.parent_rule()->parent_style_sheet()->owning_document())

View file

@ -213,4 +213,15 @@ double number_from_style_value(NonnullRefPtr<StyleValue const> const& style_valu
VERIFY_NOT_REACHED();
}
FlyString const& string_from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
{
if (style_value->is_string())
return style_value->as_string().string_value();
if (style_value->is_custom_ident())
return style_value->as_custom_ident().custom_ident();
VERIFY_NOT_REACHED();
}
}

View file

@ -203,6 +203,7 @@ struct StyleValueWithDefaultOperators : public StyleValue {
};
double number_from_style_value(NonnullRefPtr<StyleValue const> const& style_value, Optional<double> percentage_basis);
FlyString const& string_from_style_value(NonnullRefPtr<StyleValue const> const& style_value);
}