minor optimization
This commit is contained in:
@@ -30,8 +30,10 @@ function tokens(source) {
|
|||||||
if (start < 0) break;
|
if (start < 0) break;
|
||||||
const end = tagEnd(source, start);
|
const end = tagEnd(source, start);
|
||||||
if (end < 0) break;
|
if (end < 0) break;
|
||||||
result.push({ tag: false, value: source.slice(cursor, start) });
|
result.push(
|
||||||
result.push({ tag: true, value: source.slice(start, end + 1) });
|
{ tag: false, value: source.slice(cursor, start) },
|
||||||
|
{ tag: true, value: source.slice(start, end + 1) }
|
||||||
|
);
|
||||||
cursor = end + 1;
|
cursor = end + 1;
|
||||||
}
|
}
|
||||||
result.push({ tag: false, value: source.slice(cursor) });
|
result.push({ tag: false, value: source.slice(cursor) });
|
||||||
@@ -96,8 +98,9 @@ function localizeAttributes(tag, fields, translate) {
|
|||||||
export function localizeHtml(source, translate) {
|
export function localizeHtml(source, translate) {
|
||||||
const parts = tokens(source);
|
const parts = tokens(source);
|
||||||
const result = [];
|
const result = [];
|
||||||
for (let index = 0; index < parts.length; index += 1) {
|
let skipText = false;
|
||||||
const part = parts[index];
|
for (const [index, part] of parts.entries()) {
|
||||||
|
if (skipText) { skipText = false; continue; }
|
||||||
if (!part.tag) { result.push(part.value); continue; }
|
if (!part.tag) { result.push(part.value); continue; }
|
||||||
const fields = attributes(part.value);
|
const fields = attributes(part.value);
|
||||||
result.push(localizeAttributes(part.value, fields, translate));
|
result.push(localizeAttributes(part.value, fields, translate));
|
||||||
@@ -106,7 +109,7 @@ export function localizeHtml(source, translate) {
|
|||||||
const closing = parts[index + 2];
|
const closing = parts[index + 2];
|
||||||
if (key && !text?.tag && closing?.value.startsWith('</')) {
|
if (key && !text?.tag && closing?.value.startsWith('</')) {
|
||||||
result.push(escapeText(translate(key)));
|
result.push(escapeText(translate(key)));
|
||||||
index += 1;
|
skipText = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result.join('');
|
return result.join('');
|
||||||
|
|||||||
Reference in New Issue
Block a user