From 1e8edaddfa3c45effa7ad750b4fda1e8098dbac2 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 26 Jun 2019 23:17:38 +0200 Subject: [PATCH] FIX: more efficient matching --- common/header.html | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/common/header.html b/common/header.html index a6efe35..fcf6436 100644 --- a/common/header.html +++ b/common/header.html @@ -101,25 +101,26 @@ function processPlaceholders(placeholders, $cooked, mappings) { mappings.length = 0; - Object.keys(placeholders).map(placeholderKey => { - const placeholder = placeholders[placeholderKey]; - const pattern = `(${placeholder.delimiter}(?:${placeholderKey})${placeholder.delimiter})`; - const regex = new RegExp(pattern, "g"); + const keys = Object.keys(placeholders); + const pattern = keys.map(key => { + const placeholder = placeholders[key]; + return `(${placeholder.delimiter}${key}${placeholder.delimiter})`; + }).join("|"); + const regex = new RegExp(pattern, "g"); - $cooked.find(VALID_TAGS).each((index, elem) => { - const innerHTML = elem.innerHTML; - let match; + $cooked.find(VALID_TAGS).each((index, elem) => { + const innerHTML = elem.innerHTML; + let match; - mappings[index] = mappings[index] || []; + mappings[index] = mappings[index] || []; - while ((match = regex.exec(innerHTML)) != null) { - mappings[index].push({ - pattern: match[1], - position: match.index, - length: match[1].length - }); - } - }); + while ((match = regex.exec(innerHTML)) != null) { + mappings[index].push({ + pattern: match[0], + position: match.index, + length: match[0].length + }); + } }); }