@tiptap/extension-image 3.31.3 → 3.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/image.test.ts +556 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiptap/extension-image",
3
- "version": "3.31.3",
3
+ "version": "3.32.0",
4
4
  "description": "image extension for tiptap",
5
5
  "keywords": [
6
6
  "tiptap",
@@ -39,10 +39,10 @@
39
39
  }
40
40
  },
41
41
  "devDependencies": {
42
- "@tiptap/core": "^3.31.3"
42
+ "@tiptap/core": "^3.32.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "@tiptap/core": "3.31.3"
45
+ "@tiptap/core": "3.32.0"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "vp pack"
@@ -0,0 +1,556 @@
1
+ import { Editor, Extension } from '@tiptap/core'
2
+ import Document from '@tiptap/extension-document'
3
+ import Paragraph from '@tiptap/extension-paragraph'
4
+ import Text from '@tiptap/extension-text'
5
+ import { Markdown } from '@tiptap/markdown'
6
+ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
7
+
8
+ import { Image } from './image.js'
9
+
10
+ describe('extension-image', () => {
11
+ const editorElClass = 'tiptap'
12
+ let editor: Editor | null = null
13
+
14
+ const createEditorEl = () => {
15
+ const editorEl = document.createElement('div')
16
+
17
+ editorEl.classList.add(editorElClass)
18
+ document.body.appendChild(editorEl)
19
+ return editorEl
20
+ }
21
+
22
+ const getEditorEl = () => document.querySelector(`.${editorElClass}`)
23
+
24
+ const destroyEditor = () => {
25
+ editor?.destroy()
26
+ editor = null
27
+ getEditorEl()?.remove()
28
+ }
29
+
30
+ afterEach(() => {
31
+ destroyEditor()
32
+ })
33
+
34
+ const imgSrc = 'https://example.com/image.jpg'
35
+
36
+ it('should apply HTMLAttributes via renderHTML when resize is disabled', () => {
37
+ editor = new Editor({
38
+ element: createEditorEl(),
39
+ extensions: [
40
+ Document,
41
+ Paragraph,
42
+ Text,
43
+ Image.configure({
44
+ HTMLAttributes: {
45
+ class: 'my-classname',
46
+ },
47
+ }),
48
+ ],
49
+ content: {
50
+ type: 'doc',
51
+ content: [
52
+ {
53
+ type: 'image',
54
+ attrs: { src: imgSrc, alt: 'test-alt' },
55
+ },
56
+ ],
57
+ },
58
+ })
59
+
60
+ expect(editor.getHTML()).toContain('class="my-classname"')
61
+ })
62
+
63
+ it('should apply HTMLAttributes in the editor DOM when resize is disabled', () => {
64
+ editor = new Editor({
65
+ element: createEditorEl(),
66
+ extensions: [
67
+ Document,
68
+ Paragraph,
69
+ Text,
70
+ Image.configure({
71
+ HTMLAttributes: {
72
+ class: 'my-classname',
73
+ },
74
+ }),
75
+ ],
76
+ content: {
77
+ type: 'doc',
78
+ content: [
79
+ {
80
+ type: 'image',
81
+ attrs: { src: imgSrc, alt: 'test-alt' },
82
+ },
83
+ ],
84
+ },
85
+ })
86
+
87
+ const img = editor.view.dom.querySelector('img')
88
+ expect(img).not.toBeNull()
89
+ // ProseMirror may add additional classes like 'ProseMirror-selectednode'
90
+ expect(img?.getAttribute('class')).toContain('my-classname')
91
+ })
92
+
93
+ it('should apply HTMLAttributes via renderHTML when resize is enabled', () => {
94
+ editor = new Editor({
95
+ element: createEditorEl(),
96
+ extensions: [
97
+ Document,
98
+ Paragraph,
99
+ Text,
100
+ Image.configure({
101
+ HTMLAttributes: {
102
+ class: 'my-classname',
103
+ },
104
+ resize: {
105
+ enabled: true,
106
+ },
107
+ }),
108
+ ],
109
+ content: {
110
+ type: 'doc',
111
+ content: [
112
+ {
113
+ type: 'image',
114
+ attrs: { src: imgSrc, alt: 'test-alt' },
115
+ },
116
+ ],
117
+ },
118
+ })
119
+
120
+ // getHTML() goes through renderHTML which already correctly merges HTMLAttributes
121
+ expect(editor.getHTML()).toContain('class="my-classname"')
122
+ })
123
+
124
+ it('should apply HTMLAttributes in the editor DOM when resize is enabled', () => {
125
+ editor = new Editor({
126
+ element: createEditorEl(),
127
+ extensions: [
128
+ Document,
129
+ Paragraph,
130
+ Text,
131
+ Image.configure({
132
+ HTMLAttributes: {
133
+ class: 'my-classname',
134
+ 'data-test': 'value',
135
+ },
136
+ resize: {
137
+ enabled: true,
138
+ },
139
+ }),
140
+ ],
141
+ content: {
142
+ type: 'doc',
143
+ content: [
144
+ {
145
+ type: 'image',
146
+ attrs: { src: imgSrc, alt: 'test-alt' },
147
+ },
148
+ ],
149
+ },
150
+ })
151
+
152
+ const img = editor.view.dom.querySelector('img')
153
+ expect(img).not.toBeNull()
154
+ expect(img?.getAttribute('class')).toBe('my-classname')
155
+ expect(img?.getAttribute('data-test')).toBe('value')
156
+ expect(img?.getAttribute('src')).toBe(imgSrc)
157
+ })
158
+
159
+ it('should apply HTMLAttributes in the editor DOM when resize is enabled and content is markdown', () => {
160
+ editor = new Editor({
161
+ element: createEditorEl(),
162
+ extensions: [
163
+ Document,
164
+ Paragraph,
165
+ Text,
166
+ Markdown,
167
+ Image.configure({
168
+ HTMLAttributes: {
169
+ class: 'my-classname',
170
+ },
171
+ resize: {
172
+ enabled: true,
173
+ },
174
+ }),
175
+ ],
176
+ content: '![Image](https://example.com/image.jpg)',
177
+ contentType: 'markdown',
178
+ })
179
+
180
+ const img = editor.view.dom.querySelector('img')
181
+ expect(img).not.toBeNull()
182
+ expect(img?.getAttribute('class')).toBe('my-classname')
183
+ expect(img?.getAttribute('src')).toBe('https://example.com/image.jpg')
184
+ })
185
+
186
+ it('should apply HTMLAttributes via renderHTML when resize is enabled and content is markdown', () => {
187
+ editor = new Editor({
188
+ element: createEditorEl(),
189
+ extensions: [
190
+ Document,
191
+ Paragraph,
192
+ Text,
193
+ Markdown,
194
+ Image.configure({
195
+ HTMLAttributes: {
196
+ class: 'my-classname',
197
+ },
198
+ resize: {
199
+ enabled: true,
200
+ },
201
+ }),
202
+ ],
203
+ content: '![Image](https://example.com/image.jpg)',
204
+ contentType: 'markdown',
205
+ })
206
+
207
+ // getHTML() goes through renderHTML which already correctly merges HTMLAttributes
208
+ expect(editor.getHTML()).toContain('class="my-classname"')
209
+ })
210
+
211
+ /**
212
+ * Minimal extension that adds a global `customId` attribute (rendered as `data-custom-id`)
213
+ * to specified node types. This simulates what UniqueID does but without the complex
214
+ * plugin machinery, making it reliable in test environments.
215
+ */
216
+ const CustomIdAttribute = Extension.create({
217
+ name: 'customIdAttribute',
218
+
219
+ addGlobalAttributes() {
220
+ return [
221
+ {
222
+ types: ['image'],
223
+ attributes: {
224
+ customId: {
225
+ default: null,
226
+ parseHTML: (element: HTMLElement) => element.getAttribute('data-custom-id'),
227
+ renderHTML: (attributes: Record<string, any>) => {
228
+ if (!attributes.customId) {
229
+ return {}
230
+ }
231
+
232
+ return { 'data-custom-id': attributes.customId }
233
+ },
234
+ },
235
+ },
236
+ },
237
+ ]
238
+ },
239
+ })
240
+
241
+ describe('resizable image attribute updates', () => {
242
+ it('should render global attributes on the resizable image element', () => {
243
+ editor = new Editor({
244
+ element: createEditorEl(),
245
+ extensions: [
246
+ Document,
247
+ Text,
248
+ Paragraph,
249
+ Image.configure({
250
+ resize: { enabled: true },
251
+ }),
252
+ CustomIdAttribute,
253
+ ],
254
+ content: {
255
+ type: 'doc',
256
+ content: [
257
+ {
258
+ type: 'image',
259
+ attrs: {
260
+ src: 'https://tiptap.dev/placeholder.png',
261
+ customId: 'my-id-123',
262
+ },
263
+ },
264
+ ],
265
+ },
266
+ })
267
+
268
+ const container = editor.view.dom.querySelector('[data-resize-container]')
269
+
270
+ expect(container).not.toBeNull()
271
+
272
+ const img = container?.querySelector('img')
273
+
274
+ expect(img).not.toBeNull()
275
+ expect(img?.getAttribute('src')).toBe('https://tiptap.dev/placeholder.png')
276
+ expect(img?.getAttribute('data-custom-id')).toBe('my-id-123')
277
+ })
278
+
279
+ it('should update global attributes on the img element when node attributes change', () => {
280
+ editor = new Editor({
281
+ element: createEditorEl(),
282
+ extensions: [
283
+ Document,
284
+ Text,
285
+ Paragraph,
286
+ Image.configure({
287
+ resize: { enabled: true },
288
+ }),
289
+ CustomIdAttribute,
290
+ ],
291
+ content: {
292
+ type: 'doc',
293
+ content: [
294
+ {
295
+ type: 'image',
296
+ attrs: {
297
+ src: 'https://tiptap.dev/placeholder.png',
298
+ customId: 'original-id',
299
+ },
300
+ },
301
+ ],
302
+ },
303
+ })
304
+
305
+ const container = editor.view.dom.querySelector('[data-resize-container]')
306
+ const img = container?.querySelector('img')
307
+
308
+ expect(img?.getAttribute('data-custom-id')).toBe('original-id')
309
+
310
+ // Update the customId so the img element reflects the new value.
311
+ editor.commands.updateAttributes('image', { customId: 'updated-id' })
312
+
313
+ expect(img?.getAttribute('data-custom-id')).toBe('updated-id')
314
+ })
315
+
316
+ it('should preserve global attributes when other attributes change', () => {
317
+ editor = new Editor({
318
+ element: createEditorEl(),
319
+ extensions: [
320
+ Document,
321
+ Text,
322
+ Paragraph,
323
+ Image.configure({
324
+ resize: { enabled: true },
325
+ }),
326
+ CustomIdAttribute,
327
+ ],
328
+ content: {
329
+ type: 'doc',
330
+ content: [
331
+ {
332
+ type: 'image',
333
+ attrs: {
334
+ src: 'https://tiptap.dev/placeholder.png',
335
+ customId: 'persistent-id',
336
+ },
337
+ },
338
+ ],
339
+ },
340
+ })
341
+
342
+ const container = editor.view.dom.querySelector('[data-resize-container]')
343
+ const img = container?.querySelector('img')
344
+
345
+ expect(img?.getAttribute('data-custom-id')).toBe('persistent-id')
346
+
347
+ // Update alt text while retaining data-custom-id.
348
+ editor.commands.updateAttributes('image', { alt: 'Updated alt text' })
349
+
350
+ expect(img?.getAttribute('alt')).toBe('Updated alt text')
351
+ expect(img?.getAttribute('data-custom-id')).toBe('persistent-id')
352
+ })
353
+
354
+ it('should update src on the resizable image when src attribute changes', () => {
355
+ editor = new Editor({
356
+ element: createEditorEl(),
357
+ extensions: [
358
+ Document,
359
+ Text,
360
+ Paragraph,
361
+ Image.configure({
362
+ resize: { enabled: true },
363
+ }),
364
+ ],
365
+ content: {
366
+ type: 'doc',
367
+ content: [
368
+ {
369
+ type: 'image',
370
+ attrs: {
371
+ src: 'https://tiptap.dev/placeholder.png',
372
+ },
373
+ },
374
+ ],
375
+ },
376
+ })
377
+
378
+ const container = editor.view.dom.querySelector('[data-resize-container]')
379
+ const img = container?.querySelector('img')
380
+
381
+ expect(img?.getAttribute('src')).toBe('https://tiptap.dev/placeholder.png')
382
+
383
+ editor.commands.updateAttributes('image', { src: 'https://tiptap.dev/new-image.png' })
384
+
385
+ expect(img?.getAttribute('src')).toBe('https://tiptap.dev/new-image.png')
386
+ })
387
+
388
+ it('should clear src on the resizable image when src is set to null', () => {
389
+ editor = new Editor({
390
+ element: createEditorEl(),
391
+ extensions: [
392
+ Document,
393
+ Text,
394
+ Paragraph,
395
+ Image.configure({
396
+ resize: { enabled: true },
397
+ }),
398
+ ],
399
+ content: {
400
+ type: 'doc',
401
+ content: [
402
+ {
403
+ type: 'image',
404
+ attrs: {
405
+ src: 'https://tiptap.dev/placeholder.png',
406
+ },
407
+ },
408
+ ],
409
+ },
410
+ })
411
+
412
+ const container = editor.view.dom.querySelector('[data-resize-container]')
413
+ const img = container?.querySelector('img')
414
+
415
+ expect(img?.getAttribute('src')).toBe('https://tiptap.dev/placeholder.png')
416
+
417
+ editor.commands.updateAttributes('image', { src: null })
418
+
419
+ expect(img?.hasAttribute('src')).toBe(false)
420
+ expect(img?.getAttribute('src')).toBeNull()
421
+ expect(img?.src).toBe('')
422
+ })
423
+
424
+ it('should clear src on the resizable image when src is set to an empty string', () => {
425
+ editor = new Editor({
426
+ element: createEditorEl(),
427
+ extensions: [
428
+ Document,
429
+ Text,
430
+ Paragraph,
431
+ Image.configure({
432
+ resize: { enabled: true },
433
+ }),
434
+ ],
435
+ content: {
436
+ type: 'doc',
437
+ content: [
438
+ {
439
+ type: 'image',
440
+ attrs: {
441
+ src: 'https://tiptap.dev/placeholder.png',
442
+ },
443
+ },
444
+ ],
445
+ },
446
+ })
447
+
448
+ const container = editor.view.dom.querySelector('[data-resize-container]')
449
+ const img = container?.querySelector('img')
450
+
451
+ expect(img?.getAttribute('src')).toBe('https://tiptap.dev/placeholder.png')
452
+
453
+ editor.commands.updateAttributes('image', { src: '' })
454
+
455
+ expect(img?.hasAttribute('src')).toBe(false)
456
+ expect(img?.getAttribute('src')).toBeNull()
457
+ expect(img?.src).toBe('')
458
+ })
459
+
460
+ it('should remove attributes from the resizable image when set to null', () => {
461
+ editor = new Editor({
462
+ element: createEditorEl(),
463
+ extensions: [
464
+ Document,
465
+ Text,
466
+ Paragraph,
467
+ Image.configure({
468
+ resize: { enabled: true },
469
+ }),
470
+ CustomIdAttribute,
471
+ ],
472
+ content: {
473
+ type: 'doc',
474
+ content: [
475
+ {
476
+ type: 'image',
477
+ attrs: {
478
+ src: 'https://tiptap.dev/placeholder.png',
479
+ alt: 'Some alt text',
480
+ title: 'Some title',
481
+ customId: 'to-be-removed',
482
+ },
483
+ },
484
+ ],
485
+ },
486
+ })
487
+
488
+ const container = editor.view.dom.querySelector('[data-resize-container]')
489
+ const img = container?.querySelector('img')
490
+
491
+ expect(img?.getAttribute('alt')).toBe('Some alt text')
492
+ expect(img?.getAttribute('title')).toBe('Some title')
493
+ expect(img?.getAttribute('data-custom-id')).toBe('to-be-removed')
494
+
495
+ editor.commands.updateAttributes('image', { alt: null, title: null, customId: null })
496
+
497
+ expect(img?.hasAttribute('alt')).toBe(false)
498
+ expect(img?.hasAttribute('title')).toBe(false)
499
+ expect(img?.hasAttribute('data-custom-id')).toBe(false)
500
+ })
501
+ })
502
+
503
+ describe('resizable image visibility', () => {
504
+ const setImageLoaded = (loaded: boolean) => {
505
+ vi.spyOn(HTMLImageElement.prototype, 'complete', 'get').mockReturnValue(loaded)
506
+ vi.spyOn(HTMLImageElement.prototype, 'naturalWidth', 'get').mockReturnValue(loaded ? 100 : 0)
507
+ }
508
+
509
+ const mountResizableImage = () => {
510
+ editor = new Editor({
511
+ element: createEditorEl(),
512
+ extensions: [Document, Paragraph, Text, Image.configure({ resize: { enabled: true } })],
513
+ content: {
514
+ type: 'doc',
515
+ content: [{ type: 'image', attrs: { src: imgSrc } }],
516
+ },
517
+ })
518
+
519
+ return editor.view.dom.querySelector('[data-resize-container]') as HTMLElement
520
+ }
521
+
522
+ afterEach(() => {
523
+ vi.restoreAllMocks()
524
+ })
525
+
526
+ it('should show the node view when the image is already loaded on mount', () => {
527
+ // A cached image is already complete before the node view mounts, so
528
+ // `load` never fires again and the image would stay hidden forever.
529
+ setImageLoaded(true)
530
+
531
+ expect(mountResizableImage().style.visibility).toBe('')
532
+ })
533
+
534
+ it('should hide the node view until a not-yet-loaded image fires load', () => {
535
+ setImageLoaded(false)
536
+
537
+ const container = mountResizableImage()
538
+
539
+ expect(container.style.visibility).toBe('hidden')
540
+
541
+ container.querySelector('img')?.dispatchEvent(new Event('load'))
542
+
543
+ expect(container.style.visibility).toBe('')
544
+ })
545
+
546
+ it('should show the node view when the image fails to load', () => {
547
+ setImageLoaded(false)
548
+
549
+ const container = mountResizableImage()
550
+
551
+ container.querySelector('img')?.dispatchEvent(new Event('error'))
552
+
553
+ expect(container.style.visibility).toBe('')
554
+ })
555
+ })
556
+ })