Skip to content

Commit

Permalink
Merge pull request #1 from svarzee/template-bug-issue-#43
Browse files Browse the repository at this point in the history
Issue izolate#43 template bug fix
  • Loading branch information
svarzee authored Oct 19, 2020
2 parents 4790da5 + 423b0f8 commit 2f87c38
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum Nodes {
Text = '#text',
Comment = '#comment',
Div = 'div',
Template = 'template',
}

/**
Expand Down Expand Up @@ -81,11 +82,11 @@ class Parser {
}

if (
Array.isArray(treeNode.childNodes) &&
treeNode.childNodes.length &&
Array.isArray(getChildNodes(treeNode)) &&
getChildNodes(treeNode).length &&
!hasOnlyTextChildNode(treeNode)
) {
yield* this.walk(treeNode.childNodes, indentLevel + 1);
yield* this.walk(getChildNodes(treeNode), indentLevel + 1);
}
}
}
Expand Down Expand Up @@ -147,7 +148,7 @@ class Parser {
): PugNode {
let value: string = '';
if (hasOnlyTextChildNode(node)) {
const textNode = node.childNodes[0] as DefaultTreeTextNode;
const textNode = getChildNodes(node)[0] as DefaultTreeTextNode;
value = textNode.value;
}

Expand Down Expand Up @@ -285,14 +286,33 @@ class Parser {
*/
}

/**
* Encapsulates template node.
*/
interface TemplateNode extends DefaultTreeElement {
content: DefaultTreeElement;
}

/**
* Gets child nodes.
*
* @param element
*/
function getChildNodes(element: DefaultTreeElement): DefaultTreeNode[] {
return element.nodeName === Nodes.Template
? (element as TemplateNode).content.childNodes
: element.childNodes;
}


/**
* Checks whether a [node] only has a single text child node.
*
* @param node
*/
function hasOnlyTextChildNode(node: DefaultTreeElement): boolean {
if (Array.isArray(node.childNodes) && node.childNodes.length === 1) {
if (node.childNodes[0].nodeName === Nodes.Text) {
if (Array.isArray(getChildNodes(node)) && getChildNodes(node).length === 1) {
if (getChildNodes(node)[0].nodeName === Nodes.Text) {
return true;
}
}
Expand Down

0 comments on commit 2f87c38

Please sign in to comment.