From 12d67fda6c6a2b98d86530f3a60a6bd0f91586fd Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 22 Jul 2017 07:37:13 +0300 Subject: [PATCH] fixup 312424ff: process multiple top-level elements in tHTML --- js/localization.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/js/localization.js b/js/localization.js index 643caf3d..0df6086c 100644 --- a/js/localization.js +++ b/js/localization.js @@ -38,11 +38,19 @@ function tHTML(html, tag) { if (tag) { html = `<${tag}>${html}`; } - const node = (new DOMParser()).parseFromString(html, 'text/html').querySelector('body').firstElementChild; + const body = t.DOMParser.parseFromString(html, 'text/html').body; if (html.includes('i18n-')) { - tNodeList(node.getElementsByTagName('*')); + tNodeList(body.getElementsByTagName('*')); } - return node; + // the html string may contain more than one top-level elements + if (body.childElementCount <= 1) { + return body.firstElementChild; + } + const fragment = document.createDocumentFragment(); + while (body.childElementCount) { + fragment.appendChild(body.firstElementChild); + } + return fragment; } return html; } @@ -100,6 +108,7 @@ function tNodeList(nodes) { function tDocLoader() { + t.DOMParser = new DOMParser(); t.cache = tryJSONparse(localStorage.L10N) || {}; // reset L10N cache on UI language change