8000
Skip to content

Fix line_height() crash on calc() values - #2831

Merged
liZe merged 2 commits into
Kozea:mainfrom
apoorvdarshan:fix-2812-line-height-calc
Jul 11, 2026
Merged

Fix line_height() crash on calc() values#2831
liZe merged 2 commits into
Kozea:mainfrom
apoorvdarshan:fix-2812-line-height-calc

Conversation

@apoorvdarshan
Copy link
Copy Markdown
Contributor

Summary

line-height: calc(...) crashes when laying out text. This is the same class of bug as #2673 in length(), but in line_height(), which was missed by the fix in #2675.

<p style="line-height: calc(100% + 2px)">Hello</p>
File "weasyprint/css/computed_values.py", line 673, in line_height
    elif not value.unit:
             ^^^^^^^^^^
AttributeError: 'FunctionBlock' object has no attribute 'unit'

A calc() value is a FunctionBlock, which has no .unit attribute, so elif not value.unit: raises AttributeError. The sibling functions length() and vertical_align() already guard with check_math(value); line_height() did not.

Fix

weasyprint/css/computed_values.py — add a check_math(value) branch to line_height(), mirroring length() and vertical_align(). Because line_height() must return a resolved ('NUMBER', …) / ('PIXELS', …) value (its result is unpacked in strut()), the branch resolves the calc() via resolve_math() with percentages referring to the font size, matching the existing percentage branch. The resolved value then flows through the normal number / percentage / length handling, so calc() line-heights compute identically to their literal equivalents (e.g. calc(1.2em + 2px) → 14px at font-size: 10px, and calc(1.5 * 1) → the number 1.5).

Tests

Added test_math_functions_line_height in tests/css/test_math.py, parametrized over calc line-heights containing percentages, font units, lengths, and bare numbers. Each renders a paragraph with text (so line_height() is exercised during layout via strut()). The test fails on main with the AttributeError above and passes with the fix.

  • pytest tests/css/ — all pass
  • ruff check — clean

Disclosure: prepared with AI assistance; reviewed and verified locally.

line_height() did `elif not value.unit:`, but a calc() value is a
FunctionBlock with no `unit` attribute, so laying out text with
`line-height: calc(...)` raised
`AttributeError: 'FunctionBlock' object has no attribute 'unit'`.

Add a check_math() branch (mirroring length() and vertical_align())
that resolves the calc() to a length or number, with percentages
referring to the font size like the existing percentage branch.

Fixes Kozea#2812.
@grewn0uille grewn0uille linked an issue Jul 9, 2026 that may be closed by this pull request
Computed values are currently solved later for other properties, and we do that for
line-height for consistency purpose.

We should solve some of them here (including line-height), where the inherited value is
the computed length and not the percentage. But it should be done in a consistent way,
not for each property.

See https://www.w3.org/TR/css-values-4/#calc-computed-value.
@liZe
liZe commented Jul 11, 2026
Copy link
Copy Markdown
Member

Thanks for the pull request. I did the resolution later, not in the computed value, for consistency purpose.

@liZe
liZe merged commit d9fad05 into Kozea:main Jul 11, 2026
8 checks passed
@liZe liZe added this to the 70.0 milestone Jul 11, 2026
@liZe liZe added the crash Problems preventing documents from being rendered label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crash Problems preventing documents from being rendered

Projects

None yet

Development

Successfully merging this pull request may close these issues.

line_height() crashes on calc() — missing check_math() guard

2 participants

0