Skip to content

Truly independent web browser

LibWeb: Report line-height’s used value before layout rounds it

sideshowbarker847035dd4578
diff --git a/Libraries/LibWeb/Rust/src/css/resolved_value.rs b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
index 62e6056ca71..fa05af1f900 100644
--- a/Libraries/LibWeb/Rust/src/css/resolved_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/resolved_value.rs
@@ -862,12 +862,23 @@ impl ResolvedValueBox<'_> {
// -> line-height
// The resolved value is normal if the computed value is normal, or the used value otherwise.
+ // NB: The used value is the computed length, or a number times the computed font size, before layout
+ // rounds the line box's height.
property_id::LINE_HEIGHT => {
let line_height = self.computed(property);
- if matches!(&*line_height, StyleValueData::Keyword { keyword } if *keyword == keyword::NORMAL) {
- return line_height;
+ match &*line_height {
+ StyleValueData::Keyword { keyword } if *keyword == keyword::NORMAL => line_height,
+ StyleValueData::Number { value } => {
+ match absolute_length_px(&self.computed(property_id::FONT_SIZE)) {
+ Some(font_size) => length_value(value * font_size),
+ None => length_value(self.element_style.font().line_height_used().to_double()),
+ }
+ }
+ computed => match absolute_length_px(computed) {
+ Some(length) => length_value(length),
+ None => length_value(self.element_style.font().line_height_used().to_double()),
+ },
}
- length_value(self.element_style.font().line_height_used().to_double())
}
property_id::HEIGHT => self.resolved_value_for_size(property, |size| size.height),
diff --git a/Tests/LibWeb/Text/expected/css/line-height-resolved-value-is-unrounded.txt b/Tests/LibWeb/Text/expected/css/line-height-resolved-value-is-unrounded.txt
new file mode 100644
index 00000000000..c154ef9ebc2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/line-height-resolved-value-is-unrounded.txt
@@ -0,0 +1,9 @@
+font-size: 17.6px, line-height: 10 -> 176px
+font-size: 13.3px, line-height: 1.15 -> 15.295px
+font-size: 16px, line-height: 1.2 -> 19.2px
+font-size: 15px, line-height: 1.333 -> 19.995px
+font-size: 11.7px, line-height: 1.5 -> 17.55px
+font-size: 1.1em, line-height: 1.4 -> 24.64px
+font-size: 16px, line-height: 38.4px -> 38.4px
+font-size: 17.6px, line-height: 1.2em -> 21.12px
+font-size: 13.3px, line-height: 120% -> 15.96px
diff --git a/Tests/LibWeb/Text/input/css/line-height-resolved-value-is-unrounded.html b/Tests/LibWeb/Text/input/css/line-height-resolved-value-is-unrounded.html
new file mode 100644
index 00000000000..162ecb22c1a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/line-height-resolved-value-is-unrounded.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<div id="root"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // The resolved value of a line-height is its computed length, or a number times the computed font size, not the
+ // line box's height, which layout rounds.
+ const cases = [
+ ["17.6px", "10"],
+ ["13.3px", "1.15"],
+ ["16px", "1.2"],
+ ["15px", "1.333"],
+ ["11.7px", "1.5"],
+ ["1.1em", "1.4"],
+ ["16px", "38.4px"],
+ ["17.6px", "1.2em"],
+ ["13.3px", "120%"],
+ ];
+ for (const [font_size, line_height] of cases) {
+ const div = document.createElement("div");
+ div.style.fontSize = font_size;
+ div.style.lineHeight = line_height;
+ div.textContent = "x";
+ root.appendChild(div);
+ println(`font-size: ${font_size}, line-height: ${line_height} -> ${getComputedStyle(div).lineHeight}`);
+ div.remove();
+ }
+ });
+</script>