|
| 1 | +import { transformPastedHTML } from '../TipTapEditor/utils/pasteTransform'; |
| 2 | + |
| 3 | +describe('transformPastedHTML', () => { |
| 4 | + describe('empty inputs', () => { |
| 5 | + it('returns empty string for empty input', () => { |
| 6 | + expect(transformPastedHTML('')).toBe(''); |
| 7 | + }); |
| 8 | + |
| 9 | + it('returns empty string for null', () => { |
| 10 | + expect(transformPastedHTML(null)).toBe(''); |
| 11 | + }); |
| 12 | + |
| 13 | + it('returns empty string for undefined', () => { |
| 14 | + expect(transformPastedHTML(undefined)).toBe(''); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('image stripping', () => { |
| 19 | + it('strips a single remote img', () => { |
| 20 | + const input = '<p>before <img src="https://example.com/x.png"> after</p>'; |
| 21 | + expect(transformPastedHTML(input)).toBe('<p>before after</p>'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('strips a data: URI img', () => { |
| 25 | + const input = '<p><img src="data:image/png;base64,iVBORw0KGgo="></p>'; |
| 26 | + expect(transformPastedHTML(input)).toBe('<p></p>'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('strips img with no src', () => { |
| 30 | + const input = '<p><img></p>'; |
| 31 | + expect(transformPastedHTML(input)).toBe('<p></p>'); |
| 32 | + }); |
| 33 | + |
| 34 | + it.each([ |
| 35 | + ['http', '<img src="http://x.test/a.png">'], |
| 36 | + ['blob', '<img src="blob:https://x.test/abc">'], |
| 37 | + ['file', '<img src="file:///tmp/a.png">'], |
| 38 | + ['relative', '<img src="../a.png">'], |
| 39 | + ])('strips img with %s scheme', (_scheme, imgTag) => { |
| 40 | + expect(transformPastedHTML(`<p>${imgTag}</p>`)).toBe('<p></p>'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('strips multiple imgs in different parents', () => { |
| 44 | + const input = [ |
| 45 | + '<p>top <img src="a"></p>', |
| 46 | + '<img src="b">', |
| 47 | + '<ul><li><img src="c"> item</li></ul>', |
| 48 | + ].join(''); |
| 49 | + const output = transformPastedHTML(input); |
| 50 | + expect(output).not.toContain('<img'); |
| 51 | + expect(output).toContain('<p>top </p>'); |
| 52 | + expect(output).toContain('<ul><li> item</li></ul>'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('preserves surrounding marks when stripping mixed imgs', () => { |
| 56 | + const input = |
| 57 | + '<p><strong>bold</strong> <img src="a"> <em>italic</em> <a href="https://x">link</a></p>'; |
| 58 | + const output = transformPastedHTML(input); |
| 59 | + expect(output).not.toContain('<img'); |
| 60 | + expect(output).toContain('<strong>bold</strong>'); |
| 61 | + expect(output).toContain('<em>italic</em>'); |
| 62 | + expect(output).toContain('<a href="https://x">link</a>'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('Word/Office cleanup', () => { |
| 67 | + it('removes MSO conditional comments', () => { |
| 68 | + const input = '<p>before <!--[if gte mso 9]><xml>junk</xml><![endif]--> after</p>'; |
| 69 | + expect(transformPastedHTML(input)).toBe('<p>before after</p>'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('removes Office-namespaced tags (w:, m:, o:, v:)', () => { |
| 73 | + const input = |
| 74 | + '<p>before<w:hint val="x"></w:hint><o:p></o:p><m:r></m:r><v:rect></v:rect>after</p>'; |
| 75 | + const output = transformPastedHTML(input); |
| 76 | + expect(output).not.toMatch(/<\/?[wmov]:/); |
| 77 | + expect(output).toContain('before'); |
| 78 | + expect(output).toContain('after'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('strips mso-* style declarations while keeping other styles', () => { |
| 82 | + const input = |
| 83 | + '<p style="mso-list:l0 level1; color: red; mso-bidi-font-size: 11pt; font-size: 12pt">x</p>'; |
| 84 | + const output = transformPastedHTML(input); |
| 85 | + expect(output).not.toMatch(/mso-/); |
| 86 | + expect(output).toContain('color: red'); |
| 87 | + expect(output).toContain('font-size: 12pt'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('removes the style attribute entirely when all declarations were mso-*', () => { |
| 91 | + const input = '<p style="mso-list:l0 level1;mso-bidi-font-size: 11pt">x</p>'; |
| 92 | + expect(transformPastedHTML(input)).toBe('<p>x</p>'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('strips Mso* classes (case-insensitive) while keeping other classes', () => { |
| 96 | + const input = '<p class="MsoNormal kept-class MSOPlain">x</p>'; |
| 97 | + const output = transformPastedHTML(input); |
| 98 | + expect(output).toContain('class="kept-class"'); |
| 99 | + expect(output).not.toMatch(/Mso/i); |
| 100 | + }); |
| 101 | + |
| 102 | + it('removes the class attribute entirely when all classes were Mso*', () => { |
| 103 | + const input = '<p class="MsoNormal MsoListParagraph">x</p>'; |
| 104 | + expect(transformPastedHTML(input)).toBe('<p>x</p>'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('hoists nested lists out of strike/s/del wrappers', () => { |
| 108 | + const input = '<s><ul><li>a</li></ul></s>'; |
| 109 | + const output = transformPastedHTML(input); |
| 110 | + expect(output).toContain('<ul><li>a</li></ul>'); |
| 111 | + expect(output.indexOf('</s>')).toBeLessThan(output.indexOf('<ul>')); |
| 112 | + }); |
| 113 | + |
| 114 | + it('re-parents nested lists inside <li> to the end of the <li>', () => { |
| 115 | + const input = '<ul><li>text<ul><li>nested</li></ul>more text</li></ul>'; |
| 116 | + const output = transformPastedHTML(input); |
| 117 | + expect(output).toMatch(/<li>textmore text<ul><li>nested<\/li><\/ul><\/li>/); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('idempotency', () => { |
| 122 | + it.each([ |
| 123 | + ['<p>plain text</p>'], |
| 124 | + ['<p>before <img src="x"> after</p>'], |
| 125 | + ['<p style="mso-bidi-font-size:11pt;color:red">x</p>'], |
| 126 | + ['<p class="MsoNormal kept">x</p>'], |
| 127 | + ['<s><ul><li>a</li></ul></s>'], |
| 128 | + ['<ul><li>text<ul><li>n</li></ul>more</li></ul>'], |
| 129 | + ['<!--[if gte mso 9]>x<![endif]--><p>y</p>'], |
| 130 | + ])('is idempotent: f(f(x)) === f(x) for %s', input => { |
| 131 | + const once = transformPastedHTML(input); |
| 132 | + const twice = transformPastedHTML(once); |
| 133 | + expect(twice).toBe(once); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments