From 7bb7d878070df22914ff0a8f659c559f4a054fc9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 12 Mar 2023 16:09:30 +0100 Subject: [PATCH] LibWeb: Resolve percentage line-height values before CSS inheritance Percentage line-height values are relative to 1em (i.e the font-size of the element). We have to resolve their computed values before proceeding with inheritance. --- .../css-line-height-percentage-inheritance.txt | 8 ++++++++ .../css-line-height-percentage-inheritance.html | 13 +++++++++++++ Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 10 ++++++++++ 3 files changed, 31 insertions(+) create mode 100644 Tests/LibWeb/Layout/expected/css-line-height-percentage-inheritance.txt create mode 100644 Tests/LibWeb/Layout/input/css-line-height-percentage-inheritance.html diff --git a/Tests/LibWeb/Layout/expected/css-line-height-percentage-inheritance.txt b/Tests/LibWeb/Layout/expected/css-line-height-percentage-inheritance.txt new file mode 100644 index 00000000000..be7033268ca --- /dev/null +++ b/Tests/LibWeb/Layout/expected/css-line-height-percentage-inheritance.txt @@ -0,0 +1,8 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (1,1) content-size 798x52 children: not-inline + BlockContainer at (10,10) content-size 780x34 children: not-inline + BlockContainer
at (11,11) content-size 778x32 children: inline + line 0 width: 552.109375, height: 32, bottom: 32, baseline: 27.992187 + frag 0 from TextNode start: 0, length: 25, rect: [11,11 552.109375x32] + "The Linux Kernel Archives" + TextNode <#text> diff --git a/Tests/LibWeb/Layout/input/css-line-height-percentage-inheritance.html b/Tests/LibWeb/Layout/input/css-line-height-percentage-inheritance.html new file mode 100644 index 00000000000..831ca6adaf8 --- /dev/null +++ b/Tests/LibWeb/Layout/input/css-line-height-percentage-inheritance.html @@ -0,0 +1,13 @@ +
The Linux Kernel Archives \ No newline at end of file diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 751cf0d73c0..183f77a1620 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -1299,6 +1299,16 @@ void StyleComputer::absolutize_values(StyleProperties& style, DOM::Element const auto root_font_size = root_element_font_size(); auto font_size = style.property(CSS::PropertyID::FontSize)->to_length().to_px(viewport_rect(), font_metrics, root_font_size, root_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. + // We can't simply absolutize *all* percentage values against the font size, + // because most percentages are relative to containing block metrics. + auto& line_height_value_slot = style.m_property_values[to_underlying(CSS::PropertyID::LineHeight)]; + if (line_height_value_slot && line_height_value_slot->is_percentage()) { + line_height_value_slot = LengthStyleValue::create( + Length::make_px(font_size * line_height_value_slot->as_percentage().percentage().as_fraction())); + } + for (size_t i = 0; i < style.m_property_values.size(); ++i) { auto& value_slot = style.m_property_values[i]; if (!value_slot)