Skip to content

[lexical-playground] Fix: add fallback for dimensionless images to prevent collapse#8186

Open
Sa-Te wants to merge 1 commit intofacebook:mainfrom
Sa-Te:fix/svg-dimensions-fallback
Open

[lexical-playground] Fix: add fallback for dimensionless images to prevent collapse#8186
Sa-Te wants to merge 1 commit intofacebook:mainfrom
Sa-Te:fix/svg-dimensions-fallback

Conversation

@Sa-Te
Copy link
Contributor

@Sa-Te Sa-Te commented Feb 28, 2026

Description

This PR fixes an issue where SVGs inserted without explicit width and height attributes (such as dimensionless SVGs or base64 data URIs) would visually collapse to 0x0 in the editor.

The Problem

When a dimensionless SVG or a base64 SVG string is parsed:

  1. The browser reports naturalWidth and naturalHeight as 0.
  2. The ImageNode constructor defaults missing dimensions to 'inherit'.
  3. In the React layer (ImageComponent), the previous fallback logic relied on a fragile isSVG check that failed for base64 data URIs.
  4. Because the intrinsic dimensions were 0, the component rendered with 0px inline styles, making the node invisible and unselectable.

The Fix

I pivoted from a global fallback to a Surgical SVG Fallback to maintain compatibility with Lexical's existing E2E test suite:

  • Updated the logic to identify SVGs using both file extensions and data:image/svg+xml prefixes.
  • Preserved CSS Inheritance: Standard images (JPG/PNG) continue to return 'inherit' for dimensions. This ensures that existing E2E tests (which rely on specific HTML snapshots for standard images) remain green and global CSS remains in control of responsive sizing.
  • SVG Math Safety Net: For SVGs specifically, if no intrinsic dimensions are found, the logic now falls back to the node's maxWidth. This ensures a visible bounding box is rendered, allowing the user to see, select, and resize the image.

Fixes #7511

Test Plan

  • New Test: Added Images.spec.ts -> "Dimensionless SVG renders with a visible bounding box instead of collapsing". This verifies that a base64 encoded SVG without attributes renders with a width/height > 0.
  • Regressions: Verified that standard image snapshots (e.g., the "Yellow Flower" JPG in Toolbar.spec.mjs) still receive style="width: inherit; height: inherit" to satisfy existing Playwright assertions.
  • Local Verification: Ran npx playwright test packages/lexical-playground/__tests__/e2e/ and confirmed all 600+ tests pass.

@vercel
Copy link

vercel bot commented Feb 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Feb 28, 2026 10:42am
lexical-playground Ready Ready Preview, Comment Feb 28, 2026 10:42am

Request Review

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 28, 2026
Comment on lines +165 to +166
const isActuallySVG =
isSVG(src) || src.toLowerCase().startsWith('data:image/svg+xml');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not change isSVG accordingly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Adding SVG without explicit dimensions fails

2 participants