mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-18 18:00:31 +00:00
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:
parent
784911fb6d
commit
d998a0aeeb
Notes:
github-actions[bot]
2026-02-17 12:26:43 +00:00
Author: https://github.com/Calme1709
Commit: d998a0aeeb
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/7979
Reviewed-by: https://github.com/AtkinsSJ ✅
7 changed files with 25 additions and 56 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue