Skip to content

Commit 7d51484

Browse files
committedFeb 9, 2018
simplify toHTML by relying on node.parent
1 parent fc2ecce commit 7d51484

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed
 

‎src/generators/nodes/Element.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default class Element extends Node {
232232
);
233233
} else {
234234
block.builders.create.addLine(
235-
`${name}.innerHTML = ${stringify(this.children.map(node => toHTML(node, false)).join(''))};`
235+
`${name}.innerHTML = ${stringify(this.children.map(toHTML).join(''))};`
236236
);
237237
}
238238
} else {
@@ -414,9 +414,13 @@ export default class Element extends Node {
414414
);
415415
}
416416

417-
function toHTML(node: Element | Text, dontEscapeText: Boolean) {
417+
function toHTML(node: Element | Text) {
418418
if (node.type === 'Text') {
419-
return dontEscapeText ? node.data : escapeHTML(node.data);
419+
return node.parent &&
420+
node.parent.type === 'Element' &&
421+
(node.parent.name === 'script' || node.parent.name === 'style')
422+
? node.data
423+
: escapeHTML(node.data);
420424
}
421425

422426
let open = `<${node.name}`;
@@ -435,9 +439,7 @@ export default class Element extends Node {
435439

436440
if (isVoidElementName(node.name)) return open + '>';
437441

438-
const shouldNotEscapeText = node.name === 'script' || node.name === 'style';
439-
440-
return `${open}>${node.children.map(node => toHTML(node, shouldNotEscapeText)).join('')}</${node.name}>`;
442+
return `${open}>${node.children.map(toHTML).join('')}</${node.name}>`;
441443
}
442444
}
443445

0 commit comments

Comments
 (0)
Please sign in to comment.