Skip to content

fix(svg): support valid SVGs without root width/height or viewBox - #348

Open
tranhoangtu-it wants to merge 1 commit into
unjs:mainfrom
tranhoangtu-it:fix/svg-dimensionless-root
Open

tranhoangtu-it wants to merge 1 commit into
unjs:mainfrom
tranhoangtu-it:fix/svg-dimensionless-root

Conversation

@tranhoangtu-it

@tranhoangtu-it tranhoangtu-it commented Nov 30, 2026 •

Copy link
Copy Markdown

Fixes #287

Summary

  • Added a linear single-pass XML prolog and root <svg> detector (isSVGInput in src/svg.ts) so SVGs without width, height, or viewBox attributes on the root <svg> element (e.g., <symbol viewBox="..."> + <use> icon files or bare <svg>) are recognized when image-meta rejects them for missing dimensions.
  • Kept IPX's security guarantees intact: allowedInputFormats is still checked before touching SVGO/sharp, non-SVG HTML documents (<html><body><svg></svg></body></html>) are still rejected with 400 IPX_INVALID_IMAGE, and SVG passthrough remains sanitized by sanitizeSVGPlugin.
  • When rasterizing an SVG whose dimensions were missing from imageMeta, the Sharp metadata probe resolves width/height for downstream dimension clamping while preserving the existing multiPage height limit guard.

Verification

  • pnpm build (1440ms)
  • pnpm lint (eslint . && prettier -c src test scripts automd.config.ts) — passed
  • pnpm test:types (tsc --noEmit) — passed
  • pnpm test (vitest run --coverage) — 9 test files passed, 609 tests passed (10 pre-existing skips on Windows in server.test.ts, fs-dirs.test.ts, unstorage.test.ts), including new unit & integration tests in test/svg.test.ts and test/index.test.ts

Summary by CodeRabbit

  • Bug Fixes
    • SVG images without explicit width or height are now recognized and can be passed through or resized.
    • SVG content, including symbol elements, is preserved during processing.
    • SVG images continue to follow allowed input format settings, and malformed SVG or non-SVG input receives an appropriate error.

@tranhoangtu-it
tranhoangtu-it requested a review from pi0 as a code owner November 30, 2026 22:27
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

IPX now recognizes SVG input that lacks root dimensions when image metadata parsing fails. It probes Sharp for missing dimensions and continues through existing SVG or raster processing. Tests cover SVG recognition, processing, format restrictions, and invalid input.

Changes: Dimensionless SVG support

Layer / File(s) Summary
Recognize SVG roots
src/svg.ts, test/svg.test.ts
The new isSVGInput helper recognizes lowercase SVG roots after supported XML prolog content. Tests cover valid roots, prologs, and non-SVG input.
Process SVG without dimensions
src/ipx.ts, test/index.test.ts, test/svg.test.ts
When metadata parsing fails, IPX accepts input recognized as SVG and supplies placeholder dimensions. The Sharp metadata probe runs when dimensions are missing and fills available values. Tests cover SVG pass-through, rasterization, input-format rejection, and invalid SVG errors.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant IPX
  participant Meta as image-meta
  participant Recognizer as isSVGInput
  participant Sharp
  Caller->>IPX: Submit image input
  IPX->>Meta: Parse image metadata
  Meta-->>IPX: Metadata parse fails
  IPX->>Recognizer: Check input bytes for an SVG root
  Recognizer-->>IPX: Return SVG recognition result
  IPX->>Sharp: Probe metadata when dimensions are missing
  Sharp-->>IPX: Return available dimensions
  IPX-->>Caller: Return processed SVG or raster output
Loading
Merge Risk: Minimal · up to 98626

This change lets IPX accept valid SVGs whose root element has no width, height or viewBox, such as symbol/use sprites. HTML documents containing an SVG, and other non-SVG input, are still rejected with 400. No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(svg): support valid SVGs without root width/height or viewBox" matches the main change. The PR adds SVG recognition (isSVGInput in src/svg.ts) and processing for SVGs whose root svg lac…
Linked Issues check Passed [#287] The issue requires IPX to accept SVGs whose root <svg> lacks width, height, and viewBox, such as files that put viewBox only on a nested <symbol> used by <use>. Instead of returni…
Out of Scope Changes check Passed All changed areas support the #287 objective. src/svg.ts adds the root-element detector. src/ipx.ts adds the fallback path and the Sharp metadata probe for missing dimensions. The tests cover the …
Full details: Title check

Explanation

The title "fix(svg): support valid SVGs without root width/height or viewBox" matches the main change. The PR adds SVG recognition (isSVGInput in src/svg.ts) and processing for SVGs whose root svg lacks dimensions, and the linked issue #287 asks for the same behavior. The title is specific and concise and follows the conventional commit style. It does not list every detail, such as the Sharp metadata probe, which is acceptable.

Full details: Linked Issues check

Explanation

[#287] The issue requires IPX to accept SVGs whose root &lt;svg&gt; lacks width, height, and viewBox, such as files that put viewBox only on a nested &lt;symbol&gt; used by &lt;use&gt;. Instead of returning 400 IPX_INVALID_IMAGE, IPX should render them. The PR adds exported isSVGInput in src/svg.ts. It skips the BOM, whitespace, processing instructions, comments, and DOCTYPE, then checks that the first root element is &lt;svg. src/ipx.ts (per the summary) uses this check when image-meta fails. It assigns placeholder dimensions, continues through the format checks and SVG or Sharp processing, and runs a Sharp metadata probe to fill missing width or height. The reported tests in test/index.test.ts cover a dimensionless inline SVG that passes through as svg+xml with its &lt;symbol&gt; retained, rasterizes to WebP at width 64, and is rejected by allowedInputFormats. The reported test/svg.test.ts cases cover prolog and root detection. Limitation: I could not read the src/ipx.ts diff or the test files (the diff was unavailable and the file read was truncated). The src/ipx.ts flow is established from the PR summary and the reported tests, not from line-level inspection.

Full details: Out of Scope Changes check

Explanation

All changed areas support the #287 objective. src/svg.ts adds the root-element detector. src/ipx.ts adds the fallback path and the Sharp metadata probe for missing dimensions. The tests cover the new behavior. The PR states that the multiPage height-limit guard is kept, that allowedInputFormats is still checked before SVGO or Sharp, that HTML documents with an embedded &lt;svg&gt; are still rejected with 400 IPX_INVALID_IMAGE, and that SVG passthrough is still sanitized by sanitizeSVGPlugin. The HTML rejection follows from the root-element check, which requires the first root element to be svg. I did not find any change unrelated to #287. Limitation: the line-level src/ipx.ts diff could not be read, so these retained guards are supported by the PR description and the reported tests, not by direct code inspection.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3092db23-02a7-4ba4-b3d0-be3c94f21355
Commits

Reviewing files that changed from the base of the PR and between c573e7f and 9862637.

Files selected for processing: 4

  • src/ipx.ts
  • src/svg.ts
  • test/index.test.ts
  • test/svg.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

SVG files with <symbols>/<use> gives IPX_INVALID_IMAGE error

1 participant