mirror of
https://github.com/facebook/lexical.git
synced 2025-08-06 08:30:33 +08:00
Root element count invariant & test (#6003)
This commit is contained in:
@ -1319,8 +1319,10 @@ export function removeRootElementEvents(rootElement: HTMLElement): void {
|
||||
|
||||
// We only want to have a single global selectionchange event handler, shared
|
||||
// between all editor instances.
|
||||
rootElementsRegistered.set(doc, documentRootElementsCount - 1);
|
||||
if (rootElementsRegistered.get(doc) === 0) {
|
||||
const newCount = documentRootElementsCount - 1;
|
||||
invariant(newCount >= 0, 'Root element count less than 0');
|
||||
rootElementsRegistered.set(doc, newCount);
|
||||
if (newCount === 0) {
|
||||
doc.removeEventListener('selectionchange', onDocumentSelectionChange);
|
||||
}
|
||||
|
||||
|
@ -2215,4 +2215,20 @@ describe('LexicalEditor tests', () => {
|
||||
|
||||
expect(ParagraphNode.importDOM).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('root element count is always positive', () => {
|
||||
const newEditor1 = createTestEditor();
|
||||
const newEditor2 = createTestEditor();
|
||||
|
||||
const container1 = document.createElement('div');
|
||||
const container2 = document.createElement('div');
|
||||
|
||||
newEditor1.setRootElement(container1);
|
||||
newEditor1.setRootElement(null);
|
||||
|
||||
newEditor1.setRootElement(container1);
|
||||
newEditor2.setRootElement(container2);
|
||||
newEditor1.setRootElement(null);
|
||||
newEditor2.setRootElement(null);
|
||||
});
|
||||
});
|
||||
|
@ -162,5 +162,6 @@
|
||||
"160": "Expected TableCellNode parent to be a TableRowNode",
|
||||
"161": "Unexpected dirty selection to be null",
|
||||
"162": "Root element not registered",
|
||||
"163": "node is not a ListNode"
|
||||
"163": "node is not a ListNode",
|
||||
"164": "Root element count less than 0"
|
||||
}
|
||||
|
Reference in New Issue
Block a user