FIX: more efficient matching

This commit is contained in:
Joffrey JAFFEUX 2019-06-26 23:17:38 +02:00
parent 49b501ca20
commit 1e8edaddfa
1 changed files with 17 additions and 16 deletions

View File

@ -101,25 +101,26 @@
function processPlaceholders(placeholders, $cooked, mappings) { function processPlaceholders(placeholders, $cooked, mappings) {
mappings.length = 0; mappings.length = 0;
Object.keys(placeholders).map(placeholderKey => { const keys = Object.keys(placeholders);
const placeholder = placeholders[placeholderKey]; const pattern = keys.map(key => {
const pattern = `(${placeholder.delimiter}(?:${placeholderKey})${placeholder.delimiter})`; const placeholder = placeholders[key];
const regex = new RegExp(pattern, "g"); return `(${placeholder.delimiter}${key}${placeholder.delimiter})`;
}).join("|");
const regex = new RegExp(pattern, "g");
$cooked.find(VALID_TAGS).each((index, elem) => { $cooked.find(VALID_TAGS).each((index, elem) => {
const innerHTML = elem.innerHTML; const innerHTML = elem.innerHTML;
let match; let match;
mappings[index] = mappings[index] || []; mappings[index] = mappings[index] || [];
while ((match = regex.exec(innerHTML)) != null) { while ((match = regex.exec(innerHTML)) != null) {
mappings[index].push({ mappings[index].push({
pattern: match[1], pattern: match[0],
position: match.index, position: match.index,
length: match[1].length length: match[0].length
}); });
} }
});
}); });
} }