Skip to content

fix(small-images): only read the width/height CSS properties (#531) - #536

Open
samiarif02 wants to merge 1 commit into
kepano:mainfrom
samiarif02:fix/small-image-style-width-boundary
Open

samiarif02 wants to merge 1 commit into
kepano:mainfrom
samiarif02:fix/small-image-style-width-boundary

Conversation

@samiarif02

Copy link
Copy Markdown

Problem

The small-image filter reads an element's inline dimensions with:

const STYLE_WIDTH_PATTERN = /width\s*:\s*(\d+)/;
const STYLE_HEIGHT_PATTERN = /height\s*:\s*(\d+)/;

Because width and height are matched anywhere in the style string, they also match inside other properties:

  • stroke-width:1px → width read as 1
  • border-width: 1px; width: 600px → width read as 1 (first match wins)
  • line-height: 1.5 → height read as 1

So an SVG with style="stroke-width:1px" whose real size comes from its viewBox, or a large image with a border-width, is treated as 1px wide and dropped by findSmallImages / removeSmallImages. Reported in #531.

Fix

Anchor each pattern to a CSS property boundary — the start of the style string or just after a ; (ignoring surrounding whitespace) — so only the width/height properties match, not stroke-width, border-width or line-height:

const STYLE_WIDTH_PATTERN = /(?:^|;)\s*width\s*:\s*(\d+)/;
const STYLE_HEIGHT_PATTERN = /(?:^|;)\s*height\s*:\s*(\d+)/;

Tests

Added tests/small-image-style-width.test.ts with two cases (an SVG sized only by viewBox plus stroke-width:1px, and an image with border-width:1px;width:600px). Both fail on main (the elements are removed) and pass with the fix. Full vitest run is green except the pre-existing website/* suites that need the @lezer/* packages, which fail the same way on main.

The small-image filter read inline dimensions with /width\s*:\s*(\d+)/
and /height\s*:\s*(\d+)/. Those also match inside stroke-width,
border-width and line-height, so an SVG with style="stroke-width:1px"
or an image with style="border-width:1px;width:600px" was read as 1px
wide and removed as a small image.

Anchor each pattern to a property boundary (start of the style string or
after a ;) so only the width/height properties match.

Fixes kepano#531

This branch has not been deployed

No deployments
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