|
6 | 6 | * |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html'; |
9 | 10 | import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; |
10 | 11 | import {ContentEditable} from '@lexical/react/LexicalContentEditable'; |
11 | 12 | import {LexicalErrorBoundary} from '@lexical/react/LexicalErrorBoundary'; |
@@ -57,6 +58,7 @@ import { |
57 | 58 | } from 'react'; |
58 | 59 | import {createPortal} from 'react-dom'; |
59 | 60 | import {createRoot, Root} from 'react-dom/client'; |
| 61 | +import invariant from 'shared/invariant'; |
60 | 62 | import * as ReactTestUtils from 'shared/react-test-utils'; |
61 | 63 |
|
62 | 64 | import { |
@@ -2777,4 +2779,94 @@ describe('LexicalEditor tests', () => { |
2777 | 2779 | newEditor1.setRootElement(null); |
2778 | 2780 | newEditor2.setRootElement(null); |
2779 | 2781 | }); |
| 2782 | + |
| 2783 | + describe('html config', () => { |
| 2784 | + it('should override export output function', async () => { |
| 2785 | + const onError = jest.fn(); |
| 2786 | + |
| 2787 | + const newEditor = createTestEditor({ |
| 2788 | + html: { |
| 2789 | + export: new Map([ |
| 2790 | + [ |
| 2791 | + TextNode, |
| 2792 | + (_, target) => { |
| 2793 | + invariant($isTextNode(target)); |
| 2794 | + |
| 2795 | + return { |
| 2796 | + element: target.hasFormat('bold') |
| 2797 | + ? document.createElement('bor') |
| 2798 | + : document.createElement('foo'), |
| 2799 | + }; |
| 2800 | + }, |
| 2801 | + ], |
| 2802 | + ]), |
| 2803 | + }, |
| 2804 | + onError: onError, |
| 2805 | + }); |
| 2806 | + |
| 2807 | + newEditor.setRootElement(container); |
| 2808 | + |
| 2809 | + newEditor.update(() => { |
| 2810 | + const root = $getRoot(); |
| 2811 | + const paragraph = $createParagraphNode(); |
| 2812 | + const text = $createTextNode(); |
| 2813 | + root.append(paragraph); |
| 2814 | + paragraph.append(text); |
| 2815 | + |
| 2816 | + const selection = $createNodeSelection(); |
| 2817 | + selection.add(text.getKey()); |
| 2818 | + |
| 2819 | + const htmlFoo = $generateHtmlFromNodes(newEditor, selection); |
| 2820 | + expect(htmlFoo).toBe('<foo></foo>'); |
| 2821 | + |
| 2822 | + text.toggleFormat('bold'); |
| 2823 | + |
| 2824 | + const htmlBold = $generateHtmlFromNodes(newEditor, selection); |
| 2825 | + expect(htmlBold).toBe('<bor></bor>'); |
| 2826 | + }); |
| 2827 | + |
| 2828 | + expect(onError).not.toHaveBeenCalled(); |
| 2829 | + }); |
| 2830 | + |
| 2831 | + it('should override import conversion function', async () => { |
| 2832 | + const onError = jest.fn(); |
| 2833 | + |
| 2834 | + const newEditor = createTestEditor({ |
| 2835 | + html: { |
| 2836 | + import: { |
| 2837 | + figure: () => ({ |
| 2838 | + conversion: () => ({node: $createTextNode('yolo')}), |
| 2839 | + priority: 4, |
| 2840 | + }), |
| 2841 | + }, |
| 2842 | + }, |
| 2843 | + onError: onError, |
| 2844 | + }); |
| 2845 | + |
| 2846 | + newEditor.setRootElement(container); |
| 2847 | + |
| 2848 | + newEditor.update(() => { |
| 2849 | + const html = '<figure></figure>'; |
| 2850 | + |
| 2851 | + const parser = new DOMParser(); |
| 2852 | + const dom = parser.parseFromString(html, 'text/html'); |
| 2853 | + const node = $generateNodesFromDOM(newEditor, dom)[0]; |
| 2854 | + |
| 2855 | + expect(node).toEqual({ |
| 2856 | + __detail: 0, |
| 2857 | + __format: 0, |
| 2858 | + __key: node.getKey(), |
| 2859 | + __mode: 0, |
| 2860 | + __next: null, |
| 2861 | + __parent: null, |
| 2862 | + __prev: null, |
| 2863 | + __style: '', |
| 2864 | + __text: 'yolo', |
| 2865 | + __type: 'text', |
| 2866 | + }); |
| 2867 | + }); |
| 2868 | + |
| 2869 | + expect(onError).not.toHaveBeenCalled(); |
| 2870 | + }); |
| 2871 | + }); |
2780 | 2872 | }); |
0 commit comments