From 2a50fff366fe1d5eccce368ad0889c29fa3e5593 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Mon, 22 Oct 2018 23:03:10 +0800 Subject: [PATCH] allow skip child when serialize node and rebuild node --- index.d.ts | 2 ++ src/rebuild.ts | 6 +++++- src/snapshot.ts | 6 ++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 21a07000..27eec303 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,10 +10,12 @@ export function serializeNodeWithId( n: Node, doc: Document, map: idNodeMap, + skipChild?: boolean, ): serializedNodeWithId | null; export function resetId(): void; export function buildNodeWithSN( n: serializedNodeWithId, doc: Document, map: idNodeMap, + skipChild?: boolean, ): INode | null; diff --git a/src/rebuild.ts b/src/rebuild.ts index c995b6cc..476ae251 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -166,6 +166,7 @@ export function buildNodeWithSN( n: serializedNodeWithId, doc: Document, map: idNodeMap, + skipChild = false, ): INode | null { let node = buildNode(n, doc); if (!node) { @@ -179,7 +180,10 @@ export function buildNodeWithSN( (node as INode).__sn = n; map[n.id] = node as INode; - if (n.type === NodeType.Document || n.type === NodeType.Element) { + if ( + (n.type === NodeType.Document || n.type === NodeType.Element) && + !skipChild + ) { for (const childN of n.childNodes) { const childNode = buildNodeWithSN(childN, doc, map); if (!childNode) { diff --git a/src/snapshot.ts b/src/snapshot.ts index d141b35b..42ceaf6e 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -175,6 +175,7 @@ export function serializeNodeWithId( n: Node, doc: Document, map: idNodeMap, + skipChild = false, ): serializedNodeWithId | null { const _serializedNode = serializeNode(n, doc); if (!_serializedNode) { @@ -188,8 +189,9 @@ export function serializeNodeWithId( (n as INode).__sn = serializedNode; map[serializedNode.id] = n as INode; if ( - serializedNode.type === NodeType.Document || - serializedNode.type === NodeType.Element + (serializedNode.type === NodeType.Document || + serializedNode.type === NodeType.Element) && + !skipChild ) { for (const childN of Array.from(n.childNodes)) { const serializedChildNode = serializeNodeWithId(childN, doc, map);