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, n: Node,
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
skipChild?: boolean,
): serializedNodeWithId | null; ): serializedNodeWithId | null;
export function resetId(): void; export function resetId(): void;
export function buildNodeWithSN( export function buildNodeWithSN(
n: serializedNodeWithId, n: serializedNodeWithId,
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
skipChild?: boolean,
): INode | null; ): INode | null;

View File

@@ -166,6 +166,7 @@ export function buildNodeWithSN(
n: serializedNodeWithId, n: serializedNodeWithId,
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
skipChild = false,
): INode | null { ): INode | null {
let node = buildNode(n, doc); let node = buildNode(n, doc);
if (!node) { if (!node) {
@@ -179,7 +180,10 @@ export function buildNodeWithSN(
(node as INode).__sn = n; (node as INode).__sn = n;
map[n.id] = node as INode; 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) { for (const childN of n.childNodes) {
const childNode = buildNodeWithSN(childN, doc, map); const childNode = buildNodeWithSN(childN, doc, map);
if (!childNode) { if (!childNode) {

View File

@@ -175,6 +175,7 @@ export function serializeNodeWithId(
n: Node, n: Node,
doc: Document, doc: Document,
map: idNodeMap, map: idNodeMap,
skipChild = false,
): serializedNodeWithId | null { ): serializedNodeWithId | null {
const _serializedNode = serializeNode(n, doc); const _serializedNode = serializeNode(n, doc);
if (!_serializedNode) { if (!_serializedNode) {
@@ -188,8 +189,9 @@ export function serializeNodeWithId(
(n as INode).__sn = serializedNode; (n as INode).__sn = serializedNode;
map[serializedNode.id] = n as INode; map[serializedNode.id] = n as INode;
if ( if (
serializedNode.type === NodeType.Document || (serializedNode.type === NodeType.Document ||
serializedNode.type === NodeType.Element serializedNode.type === NodeType.Element) &&
!skipChild
) { ) {
for (const childN of Array.from(n.childNodes)) { for (const childN of Array.from(n.childNodes)) {
const serializedChildNode = serializeNodeWithId(childN, doc, map); const serializedChildNode = serializeNodeWithId(childN, doc, map);