Add root.append invariant on non-block insertion (#668)

* Add root.append invariant on non-block insertion

* Fix unit test
This commit is contained in:
Dominic Gannaway
2021-09-30 14:48:35 +01:00
committed by acywatson
parent 75377613c2
commit 5b748c6020
3 changed files with 21 additions and 3 deletions

View File

@ -20,6 +20,7 @@ import {
} from '../../core/OutlineUtils';
import {initializeUnitTest} from '../utils';
import {createParagraphNode} from 'outline/ParagraphNode';
describe('OutlineUtils tests', () => {
initializeUnitTest((testEnv) => {
@ -97,8 +98,10 @@ describe('OutlineUtils tests', () => {
let textNode;
await editor.update((view) => {
const root = view.getRoot();
const paragraph = createParagraphNode();
textNode = createTextNode('foo');
root.append(textNode);
paragraph.append(textNode);
root.append(paragraph);
});
const domSelection = window.getSelection();
expect(

View File

@ -10,7 +10,7 @@
import type {OutlineNode} from './OutlineNode';
import type {Selection} from './OutlineSelection';
import {BlockNode} from './OutlineBlockNode';
import {BlockNode, isBlockNode} from './OutlineBlockNode';
import invariant from 'shared/invariant';
export class RootNode extends BlockNode {
@ -52,6 +52,20 @@ export class RootNode extends BlockNode {
updateDOM(prevNode: RootNode, dom: HTMLElement): false {
return false;
}
// Mutate
append(...nodesToAppend: OutlineNode[]): BlockNode {
for (let i = 0; i < nodesToAppend.length; i++) {
if (!isBlockNode(nodesToAppend[i])) {
invariant(
false,
'rootNode.append: Only block nodes can be appended to the root node',
);
}
}
return super.append(...nodesToAppend);
}
}
export function createRootNode(): RootNode {

View File

@ -40,5 +40,6 @@
"38": "getNodesInRange: first node does not have parent",
"39": "getNodesInRange: no common ancestor",
"40": "getNodesInRange: excludeFromCopy node does not have non-excluded parent",
"41": "createOffsetModel: could not find node by key"
"41": "createOffsetModel: could not find node by key",
"42": "rootNode.append: Only block nodes can be appended to the root node"
}