Skip to content

Commit e38b220

Browse files
samourircebulko
andauthored
Defensive coding for Text Node (#31)
* Defensive coding for Text Node * Update src/ast.ts Co-authored-by: Ryan Cebulko <[email protected]> Co-authored-by: Ryan Cebulko <[email protected]>
1 parent 45ef1ab commit e38b220

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ export function fromNode(node: Node): NodeProto {
5858

5959
const termRegex = /[\w-]+/gm;
6060
export function getNumTerms(str: string): number {
61-
return str.match(termRegex)?.length ?? 0;
61+
return str?.match(termRegex)?.length ?? 0;
6262
}

src/dom.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ function fromTreeProtoHelper(nodes: NodeProto[], doc: Document, parent: Node) {
5858
for (let i = 0; i < nodes.length; i++) {
5959
const node = nodes[i];
6060
if (!isElementNode(node)) {
61-
parent.appendChild(doc.createTextNode(node.value));
61+
// Only create the text node if it has contents.
62+
if (node.value) {
63+
parent.appendChild(doc.createTextNode(node.value));
64+
}
6265
continue;
6366
}
6467

0 commit comments

Comments
 (0)