|
6 | 6 | *
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html'; |
9 | 10 | import {useLexicalComposerContext} from '@lexical/react/src/LexicalComposerContext';
|
10 | 11 | import {ContentEditable} from '@lexical/react/src/LexicalContentEditable';
|
11 | 12 | import LexicalErrorBoundary from '@lexical/react/src/LexicalErrorBoundary';
|
@@ -2212,4 +2213,105 @@ describe('LexicalEditor tests', () => {
|
2212 | 2213 |
|
2213 | 2214 | expect(ParagraphNode.importDOM).toHaveBeenCalledTimes(1);
|
2214 | 2215 | });
|
| 2216 | + |
| 2217 | + describe('html config', () => { |
| 2218 | + it('should work correctly', async () => { |
| 2219 | + const onError = jest.fn(); |
| 2220 | + |
| 2221 | + const newEditor = createTestEditor({ |
| 2222 | + html: { |
| 2223 | + export: new Map([ |
| 2224 | + [TextNode, () => ({element: document.createElement('figure')})], |
| 2225 | + ]), |
| 2226 | + import: { |
| 2227 | + figure: () => ({ |
| 2228 | + conversion: () => ({node: $createTextNode('yolo')}), |
| 2229 | + priority: 4, |
| 2230 | + }), |
| 2231 | + }, |
| 2232 | + }, |
| 2233 | + onError: onError, |
| 2234 | + }); |
| 2235 | + |
| 2236 | + newEditor.setRootElement(container); |
| 2237 | + |
| 2238 | + newEditor.update(() => { |
| 2239 | + const root = $getRoot(); |
| 2240 | + const paragraph = $createParagraphNode(); |
| 2241 | + const text = $createTextNode(); |
| 2242 | + root.append(paragraph); |
| 2243 | + paragraph.append(text); |
| 2244 | + |
| 2245 | + const selection = $createNodeSelection(); |
| 2246 | + selection.add(text.getKey()); |
| 2247 | + |
| 2248 | + const html = $generateHtmlFromNodes(newEditor, selection); |
| 2249 | + expect(html).toBe('<figure></figure>'); |
| 2250 | + |
| 2251 | + const parser = new DOMParser(); |
| 2252 | + const dom = parser.parseFromString(html, 'text/html'); |
| 2253 | + const node = $generateNodesFromDOM(newEditor, dom)[0]; |
| 2254 | + |
| 2255 | + expect(node).toEqual({ |
| 2256 | + __detail: 0, |
| 2257 | + __format: 0, |
| 2258 | + __key: node.getKey(), |
| 2259 | + __mode: 0, |
| 2260 | + __next: null, |
| 2261 | + __parent: null, |
| 2262 | + __prev: null, |
| 2263 | + __style: '', |
| 2264 | + __text: 'yolo', |
| 2265 | + __type: 'text', |
| 2266 | + }); |
| 2267 | + }); |
| 2268 | + |
| 2269 | + expect(onError).not.toHaveBeenCalled(); |
| 2270 | + }); |
| 2271 | + |
| 2272 | + it('can utilize the methods of the target node in export callback', async () => { |
| 2273 | + const onError = jest.fn(); |
| 2274 | + |
| 2275 | + const newEditor = createTestEditor({ |
| 2276 | + html: { |
| 2277 | + export: new Map([ |
| 2278 | + [ |
| 2279 | + TextNode, |
| 2280 | + (_, target: TextNode) => { |
| 2281 | + if (target.hasFormat('bold')) { |
| 2282 | + return {element: document.createElement('bar')}; |
| 2283 | + } |
| 2284 | + |
| 2285 | + return {element: document.createElement('foo')}; |
| 2286 | + }, |
| 2287 | + ], |
| 2288 | + ]), |
| 2289 | + }, |
| 2290 | + onError: onError, |
| 2291 | + }); |
| 2292 | + |
| 2293 | + newEditor.setRootElement(container); |
| 2294 | + |
| 2295 | + newEditor.update(() => { |
| 2296 | + const root = $getRoot(); |
| 2297 | + const paragraph = $createParagraphNode(); |
| 2298 | + const text = $createTextNode(); |
| 2299 | + root.append(paragraph); |
| 2300 | + paragraph.append(text); |
| 2301 | + |
| 2302 | + const selection = $createNodeSelection(); |
| 2303 | + selection.add(text.getKey()); |
| 2304 | + |
| 2305 | + const htmlFoo = $generateHtmlFromNodes(newEditor, selection); |
| 2306 | + expect(htmlFoo).toBe('<foo></foo>'); |
| 2307 | + |
| 2308 | + text.toggleFormat('bold'); |
| 2309 | + |
| 2310 | + const htmlBar = $generateHtmlFromNodes(newEditor, selection); |
| 2311 | + expect(htmlBar).toBe('<bar></bar>'); |
| 2312 | + }); |
| 2313 | + |
| 2314 | + expect(onError).not.toHaveBeenCalled(); |
| 2315 | + }); |
| 2316 | + }); |
2215 | 2317 | });
|
0 commit comments