tHTML uses parseFromString instead of innerHTML. showHelp now uses tHTML

This commit is contained in:
Jeremy Schomery 2017-07-19 14:36:02 +04:30 committed by tophf
parent 5d46dcc33e
commit 61971b97c8
2 changed files with 18 additions and 15 deletions

View File

@ -1936,17 +1936,10 @@ function showRegExpTester(event, section = getSectionForChild(this)) {
});
}
function showHelp(title, text) {
function showHelp(title, body) {
const div = $('#help-popup');
div.classList.remove('big');
const contents = $('.contents', div);
if (text instanceof HTMLElement) {
contents.textContent = '';
contents.appendChild(text);
} else {
contents.innerHTML = text;
}
$('.contents', div).appendChild(tHTML(body));
$('.title', div).textContent = title;
if (getComputedStyle(div).display === 'none') {

View File

@ -28,13 +28,23 @@ function tE(id, key, attr, esc) {
}
function tHTML(html) {
const node = document.createElement('div');
node.innerHTML = html.replace(/>\s+</g, '><'); // spaces are removed; use &nbsp; for an explicit space
function tHTML(html, tag) {
// body is a text node without HTML tags
if (typeof html === 'string' && /<\w+/.test(html) === false) {
return document.createTextNode(html);
}
if (typeof html === 'string') {
html = html.replace(/>\s+</g, '><'); // spaces are removed; use &nbsp; for an explicit space
if (tag) {
html = `<${tag}>${html}</${tag}>`;
}
const node = (new DOMParser()).parseFromString(html, 'text/html').querySelector('body').firstElementChild;
if (html.includes('i18n-')) {
tNodeList(node.getElementsByTagName('*'));
}
return node.firstElementChild;
return node;
}
return html;
}