From 21b2ba572b16dd33f514d49e2b453ddb7665c645 Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 18 Aug 2017 17:00:06 +0300 Subject: [PATCH] tHTML: handle mixed text/element nodes in top level --- js/localization.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/js/localization.js b/js/localization.js index f7f572cf..0065d22c 100644 --- a/js/localization.js +++ b/js/localization.js @@ -23,7 +23,8 @@ function tHTML(html, tag) { return document.createTextNode(html); } if (typeof html === 'string') { - html = html.replace(/>\s+<'); // spaces are removed; use   for an explicit space + // spaces are removed; use   for an explicit space + html = html.replace(/>\s+<').trim(); if (tag) { html = `<${tag}>${html}`; } @@ -31,13 +32,13 @@ function tHTML(html, tag) { if (html.includes('i18n-')) { tNodeList(body.getElementsByTagName('*')); } - // the html string may contain more than one top-level elements - if (body.childElementCount <= 1) { - return body.firstElementChild; + // the html string may contain more than one top-level node + if (!body.childNodes[1]) { + return body.firstChild; } const fragment = document.createDocumentFragment(); - while (body.childElementCount) { - fragment.appendChild(body.firstElementChild); + while (body.firstChild) { + fragment.appendChild(body.firstChild); } return fragment; }