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'); const div = $('#help-popup');
div.classList.remove('big'); div.classList.remove('big');
$('.contents', div).appendChild(tHTML(body));
const contents = $('.contents', div);
if (text instanceof HTMLElement) {
contents.textContent = '';
contents.appendChild(text);
} else {
contents.innerHTML = text;
}
$('.title', div).textContent = title; $('.title', div).textContent = title;
if (getComputedStyle(div).display === 'none') { if (getComputedStyle(div).display === 'none') {

View File

@ -28,13 +28,23 @@ function tE(id, key, attr, esc) {
} }
function tHTML(html) { function tHTML(html, tag) {
const node = document.createElement('div'); // body is a text node without HTML tags
node.innerHTML = html.replace(/>\s+</g, '><'); // spaces are removed; use &nbsp; for an explicit space 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-')) { if (html.includes('i18n-')) {
tNodeList(node.getElementsByTagName('*')); tNodeList(node.getElementsByTagName('*'));
} }
return node.firstElementChild; return node;
}
return html;
} }