Skip to content

fix font string parsing to support full CSS font shorthand syntax#226

Merged
joshmarinacci merged 1 commit into
masterfrom
font-parsing
Jul 13, 2026
Merged

fix font string parsing to support full CSS font shorthand syntax#226
joshmarinacci merged 1 commit into
masterfrom
font-parsing

Conversation

@joshmarinacci

Copy link
Copy Markdown
Owner

The old parser split on the first space, causing any font string with style/weight keywords (e.g. "bold 12px Arial") to silently corrupt the font state (size = NaN, family = "12px Arial").

Replaced it with parseFontString() which iterates tokens and correctly recognises style (italic, oblique), variant (small-caps), weight keywords and numeric weights (100–900), then requires a unit-suffixed size token (px/pt/em/rem/etc.) before taking the remainder as the family name. Invalid strings now leave _font unchanged. The getter is updated to reconstruct a proper CSS font string from all stored properties.

Added test/font-parsing.test.ts with 39 tests covering all previously untested combinations.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Description

The old parser split on the first space, causing any font string with
style/weight keywords (e.g. "bold 12px Arial") to silently corrupt the
font state (size = NaN, family = "12px Arial").

Replaced it with parseFontString() which iterates tokens and correctly
recognises style (italic, oblique), variant (small-caps), weight keywords
and numeric weights (100–900), then requires a unit-suffixed size token
(px/pt/em/rem/etc.) before taking the remainder as the family name.
Invalid strings now leave _font unchanged. The getter is updated to
reconstruct a proper CSS font string from all stored properties.

Added test/font-parsing.test.ts with 39 tests covering all previously
untested combinations.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@trunk-io

trunk-io Bot commented Jul 13, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@joshmarinacci joshmarinacci self-assigned this Jul 13, 2026
@joshmarinacci
joshmarinacci merged commit 18638df into master Jul 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant