Skip to content

Commit

Permalink
tHTML: handle mixed text/element nodes in top level
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Aug 18, 2017
1 parent 6625cd4 commit 21b2ba5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions js/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ function tHTML(html, tag) {
return document.createTextNode(html);
}
if (typeof html === 'string') {
html = html.replace(/>\s+</g, '><'); // spaces are removed; use &nbsp; for an explicit space
// spaces are removed; use &nbsp; for an explicit space
html = html.replace(/>\s+</g, '><').trim();
if (tag) {
html = `<${tag}>${html}</${tag}>`;
}
const body = t.DOMParser.parseFromString(html, 'text/html').body;
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;
}
Expand Down

0 comments on commit 21b2ba5

Please sign in to comment.