allow skip child when serialize node and rebuild node

This commit is contained in:
Yanzhen Yu
2018-10-22 23:03:10 +08:00
parent e5dba6a986
commit 2a50fff366
3 changed files with 11 additions and 3 deletions

2
index.d.ts vendored
View File

@@ -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;

View File

@@ -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) {

View File

@@ -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);