Skip to content

Commit fbd76b7

Browse files
authored
🐛 fix: clamp rendered image max width to the container (#232)
1 parent e7cf47f commit fbd76b7

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/renderer/__tests__/LexicalRenderer.test.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,38 @@ describe('LexicalRenderer', () => {
297297
expect(html).toContain('alt="test image"');
298298
});
299299

300+
it('clamps image max width so it never overflows the container', () => {
301+
const html = toHTML(
302+
makeEditorState([
303+
{
304+
altText: 'wide chart',
305+
maxWidth: 4200,
306+
src: 'https://example.com/wide.png',
307+
type: 'image',
308+
version: 1,
309+
},
310+
]),
311+
);
312+
313+
expect(html).toContain('max-width:min(4200px, 100%)');
314+
});
315+
316+
it('clamps block image max width so it never overflows the container', () => {
317+
const html = toHTML(
318+
makeEditorState([
319+
{
320+
altText: 'wide chart',
321+
maxWidth: 4200,
322+
src: 'https://example.com/wide.png',
323+
type: 'block-image',
324+
version: 1,
325+
},
326+
]),
327+
);
328+
329+
expect(html).toContain('max-width:min(4200px, 100%)');
330+
});
331+
300332
it('renders mention with class', () => {
301333
const value = makeEditorState([{ label: 'Alice', type: 'mention', version: 1 }]);
302334

‎src/renderer/renderers/image.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { getBlockImageClassName, getImageClassName } from '../style';
55
export function renderImage(node: Record<string, any>, key: string): ReactNode {
66
const { src, altText, width, height, maxWidth } = node;
77
const style: CSSProperties = {};
8-
if (maxWidth) style.maxWidth = maxWidth;
8+
// Keep the persisted width, but never let it exceed the container (mobile/share view).
9+
if (maxWidth) style.maxWidth = `min(${maxWidth}px, 100%)`;
910
if (width && width !== 'inherit') style.width = width;
1011
if (height && height !== 'inherit') style.height = height;
1112

@@ -19,7 +20,8 @@ export function renderImage(node: Record<string, any>, key: string): ReactNode {
1920
export function renderBlockImage(node: Record<string, any>, key: string): ReactNode {
2021
const { src, altText, width, height, maxWidth } = node;
2122
const style: CSSProperties = {};
22-
if (maxWidth) style.maxWidth = maxWidth;
23+
// Keep the persisted width, but never let it exceed the container (mobile/share view).
24+
if (maxWidth) style.maxWidth = `min(${maxWidth}px, 100%)`;
2325
if (width && width !== 'inherit') style.width = width;
2426
if (height && height !== 'inherit') style.height = height;
2527

0 commit comments

Comments
 (0)