|
| 1 | +/** @jsx jsxt */ |
| 2 | + |
| 3 | +import { getHtmlDocument, jsxt } from '@platejs/test-utils'; |
| 4 | + |
| 5 | +import { createPlateEditor } from '../../../../react'; |
| 6 | +import { deserializeHtml } from './deserializeHtml'; |
| 7 | + |
| 8 | +jsxt; |
| 9 | + |
| 10 | +describe('deserializeHtml - Google Docs', () => { |
| 11 | + it('should create single empty paragraphs from BR tags between paragraphs', () => { |
| 12 | + const editor = createPlateEditor({ plugins: [] }); |
| 13 | + |
| 14 | + // HTML structure from Google Docs with BR tags between paragraphs |
| 15 | + const html = ` |
| 16 | + <p>Hello world</p> |
| 17 | + <br /> |
| 18 | + <p>Hello World</p> |
| 19 | + <br /> |
| 20 | + <p>Hello World</p> |
| 21 | + <br /> |
| 22 | + <p>Hello World</p> |
| 23 | + `; |
| 24 | + |
| 25 | + const element = getHtmlDocument(html).body; |
| 26 | + |
| 27 | + const output = ( |
| 28 | + <editor> |
| 29 | + <hp>Hello world</hp> |
| 30 | + <hp> |
| 31 | + <htext /> |
| 32 | + </hp> |
| 33 | + <hp>Hello World</hp> |
| 34 | + <hp> |
| 35 | + <htext /> |
| 36 | + </hp> |
| 37 | + <hp>Hello World</hp> |
| 38 | + <hp> |
| 39 | + <htext /> |
| 40 | + </hp> |
| 41 | + <hp>Hello World</hp> |
| 42 | + </editor> |
| 43 | + ) as any; |
| 44 | + |
| 45 | + const result = deserializeHtml(editor, { element }); |
| 46 | + |
| 47 | + expect(result).toEqual(output.children); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should preserve BR tags within paragraphs', () => { |
| 51 | + const editor = createPlateEditor({ plugins: [] }); |
| 52 | + |
| 53 | + const html = `<p>Line 1<br />Line 2</p>`; |
| 54 | + const element = getHtmlDocument(html).body; |
| 55 | + |
| 56 | + const output = ( |
| 57 | + <editor> |
| 58 | + <hp>Line 1{'\n'}Line 2</hp> |
| 59 | + </editor> |
| 60 | + ) as any; |
| 61 | + |
| 62 | + const result = deserializeHtml(editor, { element }); |
| 63 | + |
| 64 | + expect(result).toEqual(output.children); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle complex Google Docs HTML', () => { |
| 68 | + const editor = createPlateEditor({ plugins: [] }); |
| 69 | + |
| 70 | + // Actual HTML structure from the issue |
| 71 | + const html = ` |
| 72 | + <b style="font-weight:normal;" id="docs-internal-guid-0753e24d-7fff-d209-84cc-3361f30177bf"> |
| 73 | + <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"> |
| 74 | + <span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello world</span> |
| 75 | + </p> |
| 76 | + <br /> |
| 77 | + <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"> |
| 78 | + <span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello World</span> |
| 79 | + </p> |
| 80 | + <br /> |
| 81 | + <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"> |
| 82 | + <span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello World</span> |
| 83 | + </p> |
| 84 | + <br /> |
| 85 | + <p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"> |
| 86 | + <span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Hello World</span> |
| 87 | + </p> |
| 88 | + </b> |
| 89 | + <br class="Apple-interchange-newline"> |
| 90 | + `; |
| 91 | + |
| 92 | + const element = getHtmlDocument(html).body; |
| 93 | + const result = deserializeHtml(editor, { element }); |
| 94 | + |
| 95 | + // Should have 7 elements: 4 paragraphs with content + 3 empty paragraphs from BR tags |
| 96 | + expect(result).toHaveLength(7); |
| 97 | + expect(result[0].type).toBe('p'); |
| 98 | + expect(result[1].type).toBe('p'); |
| 99 | + expect(result[2].type).toBe('p'); |
| 100 | + expect(result[3].type).toBe('p'); |
| 101 | + expect(result[4].type).toBe('p'); |
| 102 | + expect(result[5].type).toBe('p'); |
| 103 | + expect(result[6].type).toBe('p'); |
| 104 | + |
| 105 | + // Check that empty paragraphs are at the right positions |
| 106 | + expect((result[1] as any).children[0].text).toBe(''); |
| 107 | + expect((result[3] as any).children[0].text).toBe(''); |
| 108 | + expect((result[5] as any).children[0].text).toBe(''); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should preserve BR tags within paragraphs as separate text nodes', () => { |
| 112 | + const editor = createPlateEditor({ plugins: [] }); |
| 113 | + |
| 114 | + const html = `<p><span>Hello</span><br /><span>World</span></p>`; |
| 115 | + const element = getHtmlDocument(html).body; |
| 116 | + |
| 117 | + const result = deserializeHtml(editor, { element }); |
| 118 | + |
| 119 | + // BR tags are converted to newline text nodes |
| 120 | + // Note: Text nodes are not merged during deserialization |
| 121 | + expect(result).toHaveLength(1); |
| 122 | + expect((result[0] as any).type).toBe('p'); |
| 123 | + expect((result[0] as any).children).toHaveLength(3); |
| 124 | + expect((result[0] as any).children[0].text).toBe('Hello'); |
| 125 | + expect((result[0] as any).children[1].text).toBe('\n'); |
| 126 | + expect((result[0] as any).children[2].text).toBe('World'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle two consecutive BR tags between paragraphs', () => { |
| 130 | + const editor = createPlateEditor({ plugins: [] }); |
| 131 | + |
| 132 | + const html = ` |
| 133 | + <p>First paragraph</p> |
| 134 | + <br /> |
| 135 | + <br /> |
| 136 | + <p>Second paragraph</p> |
| 137 | + `; |
| 138 | + |
| 139 | + const element = getHtmlDocument(html).body; |
| 140 | + |
| 141 | + const output = ( |
| 142 | + <editor> |
| 143 | + <hp>First paragraph</hp> |
| 144 | + <hp> |
| 145 | + <htext /> |
| 146 | + </hp> |
| 147 | + <hp> |
| 148 | + <htext /> |
| 149 | + </hp> |
| 150 | + <hp>Second paragraph</hp> |
| 151 | + </editor> |
| 152 | + ) as any; |
| 153 | + |
| 154 | + const result = deserializeHtml(editor, { element }); |
| 155 | + |
| 156 | + expect(result).toEqual(output.children); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should handle three consecutive BR tags between paragraphs', () => { |
| 160 | + const editor = createPlateEditor({ plugins: [] }); |
| 161 | + |
| 162 | + const html = ` |
| 163 | + <p>First paragraph</p> |
| 164 | + <br /> |
| 165 | + <br /> |
| 166 | + <br /> |
| 167 | + <p>Second paragraph</p> |
| 168 | + `; |
| 169 | + |
| 170 | + const element = getHtmlDocument(html).body; |
| 171 | + |
| 172 | + const output = ( |
| 173 | + <editor> |
| 174 | + <hp>First paragraph</hp> |
| 175 | + <hp> |
| 176 | + <htext /> |
| 177 | + </hp> |
| 178 | + <hp> |
| 179 | + <htext /> |
| 180 | + </hp> |
| 181 | + <hp> |
| 182 | + <htext /> |
| 183 | + </hp> |
| 184 | + <hp>Second paragraph</hp> |
| 185 | + </editor> |
| 186 | + ) as any; |
| 187 | + |
| 188 | + const result = deserializeHtml(editor, { element }); |
| 189 | + |
| 190 | + expect(result).toEqual(output.children); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should handle multiple consecutive BR tags in complex Google Docs HTML', () => { |
| 194 | + const editor = createPlateEditor({ plugins: [] }); |
| 195 | + |
| 196 | + const html = ` |
| 197 | + <b style="font-weight:normal;"> |
| 198 | + <p dir="ltr">Content 1</p> |
| 199 | + <br /> |
| 200 | + <br /> |
| 201 | + <p dir="ltr">Content 2</p> |
| 202 | + <br /> |
| 203 | + <br /> |
| 204 | + <br /> |
| 205 | + <p dir="ltr">Content 3</p> |
| 206 | + </b> |
| 207 | + `; |
| 208 | + |
| 209 | + const element = getHtmlDocument(html).body; |
| 210 | + const result = deserializeHtml(editor, { element }); |
| 211 | + |
| 212 | + // Should have 8 elements: 3 paragraphs with content + 5 empty paragraphs from BR tags |
| 213 | + expect(result).toHaveLength(8); |
| 214 | + |
| 215 | + // Check content paragraphs |
| 216 | + expect((result[0] as any).children[0].text).toBe('Content 1'); |
| 217 | + expect((result[3] as any).children[0].text).toBe('Content 2'); |
| 218 | + expect((result[7] as any).children[0].text).toBe('Content 3'); |
| 219 | + |
| 220 | + // Check empty paragraphs from BR tags |
| 221 | + expect((result[1] as any).children[0].text).toBe(''); |
| 222 | + expect((result[2] as any).children[0].text).toBe(''); |
| 223 | + expect((result[4] as any).children[0].text).toBe(''); |
| 224 | + expect((result[5] as any).children[0].text).toBe(''); |
| 225 | + expect((result[6] as any).children[0].text).toBe(''); |
| 226 | + }); |
| 227 | + |
| 228 | + it('should handle three consecutive BR tags not between blocks', () => { |
| 229 | + const editor = createPlateEditor({ plugins: [] }); |
| 230 | + |
| 231 | + // 3 BR tags at the start, not between blocks |
| 232 | + const html = ` |
| 233 | + <br /> |
| 234 | + <br /> |
| 235 | + <br /> |
| 236 | + `; |
| 237 | + |
| 238 | + const element = getHtmlDocument(html).body; |
| 239 | + |
| 240 | + const output = ( |
| 241 | + <editor> |
| 242 | + <hp> |
| 243 | + <htext /> |
| 244 | + </hp> |
| 245 | + <hp> |
| 246 | + <htext /> |
| 247 | + </hp> |
| 248 | + <hp> |
| 249 | + <htext /> |
| 250 | + </hp> |
| 251 | + </editor> |
| 252 | + ) as any; |
| 253 | + |
| 254 | + const result = deserializeHtml(editor, { element }); |
| 255 | + |
| 256 | + expect(result).toEqual(output.children); |
| 257 | + }); |
| 258 | + |
| 259 | + it('should handle BR tags in various contexts within a div', () => { |
| 260 | + const editor = createPlateEditor({ plugins: [] }); |
| 261 | + |
| 262 | + const html = ` |
| 263 | + <div> |
| 264 | + <br /> |
| 265 | + <br /> |
| 266 | + <br /> |
| 267 | + <p>Content</p> |
| 268 | + </div> |
| 269 | + `; |
| 270 | + |
| 271 | + const element = getHtmlDocument(html).body; |
| 272 | + |
| 273 | + const result = deserializeHtml(editor, { element }); |
| 274 | + |
| 275 | + // Should have 4 elements: 3 empty paragraphs from BR tags + 1 paragraph with content |
| 276 | + expect(result).toHaveLength(4); |
| 277 | + expect((result[0] as any).children[0].text).toBe(''); |
| 278 | + expect((result[1] as any).children[0].text).toBe(''); |
| 279 | + expect((result[2] as any).children[0].text).toBe(''); |
| 280 | + expect((result[3] as any).children[0].text).toBe('Content'); |
| 281 | + }); |
| 282 | +}); |
0 commit comments