create templates on demand

This commit is contained in:
tophf 2022-09-06 18:34:15 +03:00
parent 9398857d93
commit 7b64deeb37

View File

@ -19,7 +19,10 @@ function t(key, params, strict = true) {
Object.assign(t, { Object.assign(t, {
cache: {}, cache: {},
template: {}, template: new Proxy({}, {
get: (obj, k, _) => obj[k] ||
(_ = $(`template[data-id="${k}"]`)) && (obj[k] = t.createTemplate(_)),
}),
ALLOWED_TAGS: ['a', 'b', 'code', 'i', 'sub', 'sup', 'wbr'], ALLOWED_TAGS: ['a', 'b', 'code', 'i', 'sub', 'sup', 'wbr'],
RX_WORD_BREAK: new RegExp([ RX_WORD_BREAK: new RegExp([
'(', '(',
@ -31,7 +34,7 @@ Object.assign(t, {
')', ')',
/(?!\b|\s|$)/, /(?!\b|\s|$)/,
].map(rx => rx.source || rx).join(''), 'gu'), ].map(rx => rx.source || rx).join(''), 'gu'),
SELECTOR: '[i18n], template', SELECTOR: '[i18n]',
HTML(html) { HTML(html) {
return typeof html !== 'string' return typeof html !== 'string'
@ -47,10 +50,6 @@ Object.assign(t, {
} }
for (const node of nodes) { for (const node of nodes) {
if (!node.localName) continue; if (!node.localName) continue;
if (node.localName === 'template') {
t.createTemplate(node);
continue;
}
const attr = node.getAttribute('i18n'); const attr = node.getAttribute('i18n');
if (!attr) continue; if (!attr) continue;
for (const part of attr.split(',')) { for (const part of attr.split(',')) {