add strict null check and fix codes
This commit is contained in:
1
index.d.ts
vendored
1
index.d.ts
vendored
@@ -1,4 +1,5 @@
|
|||||||
import { serializedNodeWithId } from './src/types';
|
import { serializedNodeWithId } from './src/types';
|
||||||
|
export * from './src/types';
|
||||||
|
|
||||||
export function snapshot(n: Document): serializedNodeWithId | null;
|
export function snapshot(n: Document): serializedNodeWithId | null;
|
||||||
export function rebuild(n: serializedNodeWithId): Node | null;
|
export function rebuild(n: serializedNodeWithId): Node | null;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import snapshot from './snapshot';
|
import snapshot from './snapshot';
|
||||||
import rebuild from './rebuild';
|
import rebuild from './rebuild';
|
||||||
|
export * from './types';
|
||||||
|
|
||||||
export { snapshot, rebuild };
|
export { snapshot, rebuild };
|
||||||
|
|||||||
@@ -62,7 +62,11 @@ function rebuild(n: serializedNodeWithId): Node | null {
|
|||||||
if (n.type === NodeType.Document || n.type === NodeType.Element) {
|
if (n.type === NodeType.Document || n.type === NodeType.Element) {
|
||||||
for (const childN of n.childNodes) {
|
for (const childN of n.childNodes) {
|
||||||
const childNode = rebuild(childN);
|
const childNode = rebuild(childN);
|
||||||
root.appendChild(childNode);
|
if (!childNode) {
|
||||||
|
console.warn('Failed to rebuild', childN);
|
||||||
|
} else {
|
||||||
|
root.appendChild(childNode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
type: NodeType.Text,
|
type: NodeType.Text,
|
||||||
textContent,
|
textContent: textContent || '',
|
||||||
};
|
};
|
||||||
case n.CDATA_SECTION_NODE:
|
case n.CDATA_SECTION_NODE:
|
||||||
return {
|
return {
|
||||||
@@ -108,7 +108,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
|
|||||||
case n.COMMENT_NODE:
|
case n.COMMENT_NODE:
|
||||||
return {
|
return {
|
||||||
type: NodeType.Comment,
|
type: NodeType.Comment,
|
||||||
textContent: (n as Comment).textContent,
|
textContent: (n as Comment).textContent || '',
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@@ -130,7 +130,10 @@ function _snapshot(n: Node, doc: Document): serializedNodeWithId | null {
|
|||||||
serializedNode.type === NodeType.Element
|
serializedNode.type === NodeType.Element
|
||||||
) {
|
) {
|
||||||
for (const childN of Array.from(n.childNodes)) {
|
for (const childN of Array.from(n.childNodes)) {
|
||||||
serializedNode.childNodes.push(_snapshot(childN, doc));
|
const serializedChildNode = _snapshot(childN, doc);
|
||||||
|
if (serializedChildNode) {
|
||||||
|
serializedNode.childNodes.push(serializedChildNode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return serializedNode;
|
return serializedNode;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
"removeComments": true,
|
"removeComments": true,
|
||||||
"preserveConstEnums": true,
|
"preserveConstEnums": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
|
|||||||
Reference in New Issue
Block a user