allow <code> tag in i18n-html

This commit is contained in:
tophf 2020-11-15 19:29:51 +03:00
parent f4a4b05b6e
commit b0f08a7d6f

View File

@ -110,19 +110,21 @@ function tNodeList(nodes) {
} }
function createHtml(value) { function createHtml(value) {
// <a href=foo>bar</a> are the only recognizable HTML elements // <a> and <code> are the only acceptable HTML elements,
const rx = /(?:<a\s([^>]*)>([^<]*)<\/a>)?([^<]*)/gi; // <a> also allows `href` attribute with an http/https URL
const rx = /(<)(a|code)(\s[^>]*|)>(.*?)<\/\2>/i;
const bin = document.createDocumentFragment(); const bin = document.createDocumentFragment();
for (let m; (m = rx.exec(value)) && m[0];) { for (let parts = value.split(rx), i = 0; i < parts.length; i++) {
const [, linkParams, linkText, nextText] = m; const s = parts[i];
if (linkText) { if (s === '<') {
const href = /\bhref\s*=\s*(\S+)/.exec(linkParams); const tag = parts[++i].toLowerCase();
const a = bin.appendChild(document.createElement('a')); const el = bin.appendChild(document.createElement(tag));
a.href = href && href[1].replace(/^(["'])(.*)\1$/, '$2') || ''; const attrs = parts[++i];
a.appendChild(createText(linkText)); const href = tag === 'a' && /(?:^|\s)href\s*=\s*(["'])?(https?:\/\/\S*?)\1/i.exec(attrs);
} if (href) el.href = href[2];
if (nextText) { el.appendChild(createText(parts[++i]));
bin.appendChild(createText(nextText)); } else {
bin.appendChild(createText(s));
} }
} }
return bin; return bin;