summaryrefslogtreecommitdiff
path: root/ext/mixed/js/html-template-collection.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-11-24 19:06:29 -0500
committerGitHub <noreply@github.com>2020-11-24 19:06:29 -0500
commit02d9f7c736b55e22631c7ad29fa60df729a0f05b (patch)
treea3d13c29d01c5c6596e1590b7f196eae3cd1f18d /ext/mixed/js/html-template-collection.js
parent0b00de3c0f0cab64529f18a477120ab9362581fc (diff)
Improve html templates (#1061)
* Add template pre-processing * Remove whitespace * Add labels * Reuse tag template for search tags * Add space
Diffstat (limited to 'ext/mixed/js/html-template-collection.js')
-rw-r--r--ext/mixed/js/html-template-collection.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/mixed/js/html-template-collection.js b/ext/mixed/js/html-template-collection.js
index 56da68b8..d3e2e6fd 100644
--- a/ext/mixed/js/html-template-collection.js
+++ b/ext/mixed/js/html-template-collection.js
@@ -29,6 +29,7 @@ class HtmlTemplateCollection {
for (const template of sourceNode.querySelectorAll('template')) {
const match = pattern.exec(template.id);
if (match === null) { continue; }
+ this._prepareTemplate(template);
this._templates.set(match[1], template);
}
}
@@ -42,4 +43,32 @@ class HtmlTemplateCollection {
const template = this._templates.get(name);
return document.importNode(template.content, true);
}
+
+ // Private
+
+ _prepareTemplate(template) {
+ if (template.dataset.removeWhitespaceText === 'true') {
+ this._removeWhitespaceText(template);
+ }
+ }
+
+ _removeWhitespaceText(template) {
+ const {content} = template;
+ const {TEXT_NODE} = Node;
+ const iterator = document.createNodeIterator(content, NodeFilter.SHOW_TEXT);
+ const removeNodes = [];
+ while (true) {
+ const node = iterator.nextNode();
+ if (node === null) { break; }
+ if (node.nodeType === TEXT_NODE && node.nodeValue.trim().length === 0) {
+ removeNodes.push(node);
+ }
+ }
+ for (const node of removeNodes) {
+ const {parentNode} = node;
+ if (parentNode !== null) {
+ parentNode.removeChild(node);
+ }
+ }
+ }
}