Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical] Add tests for HTMLConfig #5507

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DOMConversion,
DOMConversionMap,
DOMExportOutput,
DOMExportOutputMap,
NodeKey,
} from './LexicalNode';

Expand Down Expand Up @@ -170,10 +171,7 @@ export type LexicalNodeReplacement = {
};

export type HTMLConfig = {
export?: Map<
Klass<LexicalNode>,
(editor: LexicalEditor, target: LexicalNode) => DOMExportOutput
>;
export?: DOMExportOutputMap;
import?: DOMConversionMap;
};

Expand Down
5 changes: 5 additions & 0 deletions packages/lexical/src/LexicalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export type DOMConversionOutput = {
node: null | LexicalNode | Array<LexicalNode>;
};

export type DOMExportOutputMap = Map<
Klass<LexicalNode>,
(editor: LexicalEditor, target: LexicalNode) => DOMExportOutput
>;

export type DOMExportOutput = {
after?: (
generatedElement: HTMLElement | Text | null | undefined,
Expand Down
92 changes: 92 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {ContentEditable} from '@lexical/react/LexicalContentEditable';
import {LexicalErrorBoundary} from '@lexical/react/LexicalErrorBoundary';
Expand Down Expand Up @@ -57,6 +58,7 @@ import {
} from 'react';
import {createPortal} from 'react-dom';
import {createRoot, Root} from 'react-dom/client';
import invariant from 'shared/invariant';
import * as ReactTestUtils from 'shared/react-test-utils';

import {
Expand Down Expand Up @@ -2777,4 +2779,94 @@ describe('LexicalEditor tests', () => {
newEditor1.setRootElement(null);
newEditor2.setRootElement(null);
});

describe('html config', () => {
it('should override export output function', async () => {
const onError = jest.fn();

const newEditor = createTestEditor({
html: {
export: new Map([
[
TextNode,
(_, target) => {
invariant($isTextNode(target));

return {
element: target.hasFormat('bold')
? document.createElement('bor')
: document.createElement('foo'),
};
},
],
]),
},
onError: onError,
});

newEditor.setRootElement(container);

newEditor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
const text = $createTextNode();
root.append(paragraph);
paragraph.append(text);

const selection = $createNodeSelection();
selection.add(text.getKey());

const htmlFoo = $generateHtmlFromNodes(newEditor, selection);
expect(htmlFoo).toBe('<foo></foo>');

text.toggleFormat('bold');

const htmlBold = $generateHtmlFromNodes(newEditor, selection);
expect(htmlBold).toBe('<bor></bor>');
});

expect(onError).not.toHaveBeenCalled();
});

it('should override import conversion function', async () => {
const onError = jest.fn();

const newEditor = createTestEditor({
html: {
import: {
figure: () => ({
conversion: () => ({node: $createTextNode('yolo')}),
priority: 4,
}),
},
},
onError: onError,
});

newEditor.setRootElement(container);

newEditor.update(() => {
const html = '<figure></figure>';

const parser = new DOMParser();
const dom = parser.parseFromString(html, 'text/html');
const node = $generateNodesFromDOM(newEditor, dom)[0];

expect(node).toEqual({
__detail: 0,
__format: 0,
__key: node.getKey(),
__mode: 0,
__next: null,
__parent: null,
__prev: null,
__style: '',
__text: 'yolo',
__type: 'text',
});
});

expect(onError).not.toHaveBeenCalled();
});
});
});
7 changes: 6 additions & 1 deletion packages/lexical/src/__tests__/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ import {createRef} from 'react';
import {createRoot} from 'react-dom/client';
import * as ReactTestUtils from 'shared/react-test-utils';

import {CreateEditorArgs, LexicalNodeReplacement} from '../../LexicalEditor';
import {
CreateEditorArgs,
HTMLConfig,
LexicalNodeReplacement,
} from '../../LexicalEditor';
import {resetRandomKey} from '../../LexicalUtils';

const prettierConfig = prettier.resolveConfig.sync(
Expand Down Expand Up @@ -520,6 +524,7 @@ export function createTestEditor(
onError?: (error: Error) => void;
disableEvents?: boolean;
readOnly?: boolean;
html?: HTMLConfig;
} = {},
): LexicalEditor {
const customNodes = config.nodes || [];
Expand Down
Loading