From cd2c147f9f47bafe0cbe38ef543ea3b3c1a72486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 16 Mar 2025 18:56:28 +0100 Subject: [PATCH] Optimization of the fallback matchclass loop --- tarteaucitron.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tarteaucitron.js b/tarteaucitron.js index 234f9d23..702dafaa 100644 --- a/tarteaucitron.js +++ b/tarteaucitron.js @@ -2284,28 +2284,19 @@ var tarteaucitron = { }, "fallback": function (matchClass, content, noInner) { "use strict"; - var elems = document.getElementsByTagName('*'), - i, - index = 0; - - for (i in elems) { - if (elems[i] !== undefined) { - for (index = 0; index < matchClass.length; index += 1) { - if ((' ' + elems[i].className + ' ') - .indexOf(' ' + matchClass[index] + ' ') > -1) { - if (typeof content === 'function') { - if (noInner === true) { - content(elems[i]); - } else { - elems[i].innerHTML = content(elems[i]); - } - } else { - elems[i].innerHTML = content; - } - } + const selector = matchClass.map(cls => `.${cls}`).join(', '); + const elems = document.querySelectorAll(selector); + elems.forEach((elem) => { + if (typeof content === 'function') { + if (noInner === true) { + content(elem); + } else { + elem.innerHTML = content(elem); } + } else { + elem.innerHTML = content; } - } + }); }, "engage": function (id) { "use strict";