LibWeb: Handle percentage font-size values

This commit is contained in:
Gingeh 2025-06-24 16:03:27 +10:00 committed by Sam Atkins
commit b85a8a23a7
Notes: github-actions[bot] 2025-06-24 11:44:06 +00:00
5 changed files with 77 additions and 2 deletions

View file

@ -218,6 +218,8 @@ public:
[[nodiscard]] CSSPixels line_height() const { return *m_line_height; }
void set_line_height(Badge<StyleComputer> const&, CSSPixels line_height) { m_line_height = line_height; }
[[nodiscard]] CSSPixels font_size() const { return *m_font_size; }
void set_font_size(Badge<StyleComputer> const&, CSSPixels font_size) { m_font_size = font_size; }
bool operator==(ComputedProperties const&) const;
@ -271,6 +273,7 @@ private:
RefPtr<Gfx::Font const> m_first_available_computed_font;
Optional<CSSPixels> m_line_height;
Optional<CSSPixels> m_font_size;
PseudoClassBitmap m_attempted_pseudo_class_matches;
};

View file

@ -2492,8 +2492,17 @@ void StyleComputer::absolutize_values(ComputedProperties& style, GC::Ptr<DOM::El
style.first_available_computed_font().pixel_metrics()
};
auto font_size = style.property(CSS::PropertyID::FontSize).as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics);
// "A percentage value specifies an absolute font size relative to the parent elements computed font-size. Negative percentages are invalid."
auto& font_size_value_slot = style.m_property_values[to_underlying(CSS::PropertyID::FontSize)];
if (font_size_value_slot && font_size_value_slot->is_percentage()) {
auto parent_font_size = get_inherit_value(CSS::PropertyID::FontSize, element)->as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics);
font_size_value_slot = LengthStyleValue::create(
Length::make_px(CSSPixels::nearest_value_for(parent_font_size * font_size_value_slot->as_percentage().percentage().as_fraction())));
}
auto font_size = font_size_value_slot->as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics);
font_metrics.font_size = font_size;
style.set_font_size({}, font_size);
// NOTE: Percentage line-height values are relative to the font-size of the element.
// We have to resolve them right away, so that the *computed* line-height is ready for inheritance.