MCPcopy Create free account
hub / github.com/ampproject/amphtml / getTemplateNonEmptyNodeCount

Function getTemplateNonEmptyNodeCount

extensions/amp-render/1.0/amp-render.js:55–74  ·  view source on GitHub ↗

* Returns the non-empty node count in a template (defined as a `template` * or `script` element). This is required to establish if the template content has * a single wrapper element and if so we need to include it while rendering the * template. For more info, see https://github.com/ampproject/a

(doc, template)

Source from the content-addressed store, hash-verified

53 * @return {number} count of non-empty child nodes
54 */
55function getTemplateNonEmptyNodeCount(doc, template) {
56 let childNodes = [];
57 if (template.tagName === 'SCRIPT') {
58 const div = doc.createElement('div');
59 div./*OK*/ innerHTML = template./*OK*/ innerHTML;
60 childNodes = div.childNodes;
61 } else if (template.tagName === 'TEMPLATE') {
62 childNodes = template.content.childNodes;
63 }
64 return toArray(childNodes).reduce(
65 (count, node) =>
66 count +
67 Number(
68 node.nodeType === Node.TEXT_NODE
69 ? node.textContent.trim().length > 0
70 : node.nodeType !== Node.COMMENT_NODE
71 ),
72 0
73 );
74}
75
76export class AmpRender extends setSuperClass(
77 BaseElement,

Callers 1

Calls 1

toArrayFunction · 0.90

Tested by

no test coverage detected