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