From 61971b97c892565787405ab34d402044a374d036 Mon Sep 17 00:00:00 2001 From: Jeremy Schomery Date: Wed, 19 Jul 2017 14:36:02 +0430 Subject: [PATCH] tHTML uses parseFromString instead of innerHTML. showHelp now uses tHTML --- edit/edit.js | 11 ++--------- js/localization.js | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/edit/edit.js b/edit/edit.js index 26f83a08..48634791 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -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') { diff --git a/js/localization.js b/js/localization.js index 1db7577b..643caf3d 100644 --- a/js/localization.js +++ b/js/localization.js @@ -28,13 +28,23 @@ function tE(id, key, attr, esc) { } -function tHTML(html) { - const node = document.createElement('div'); - node.innerHTML = html.replace(/>\s+<'); // spaces are removed; use   for an explicit space - if (html.includes('i18n-')) { - tNodeList(node.getElementsByTagName('*')); +function tHTML(html, tag) { + // body is a text node without HTML tags + if (typeof html === 'string' && /<\w+/.test(html) === false) { + return document.createTextNode(html); } - return node.firstElementChild; + if (typeof html === 'string') { + html = html.replace(/>\s+<'); // spaces are removed; use   for an explicit space + if (tag) { + html = `<${tag}>${html}`; + } + const node = (new DOMParser()).parseFromString(html, 'text/html').querySelector('body').firstElementChild; + if (html.includes('i18n-')) { + tNodeList(node.getElementsByTagName('*')); + } + return node; + } + return html; }