From c110df8e09c49cdb69a2da390eae0133b188e81f Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Tue, 28 Jan 2025 12:16:36 +0100 Subject: [PATCH 1/3] fix(core feature-detection): Fix loading of modernizr script. In some situations loading of the modernizr.min.js script failed because the base url could be incorrect. That is fixed with a non-IE compatible method of getting the current's script URL, which is safe to use. The problem surfaced in Plone while loading the Patternslib library and in between the protect.js script was loaded. The base url was calculated for the protect.js script and the modernizr script could not be loaded. --- src/core/feature-detection.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/feature-detection.js b/src/core/feature-detection.js index 664c316dd..76bb4b079 100644 --- a/src/core/feature-detection.js +++ b/src/core/feature-detection.js @@ -18,15 +18,14 @@ } // Get the current script tag's URL. - // See: https://stackoverflow.com/a/984656/1337474 - const scripts = document.getElementsByTagName("script"); - const script = scripts[scripts.length - 1]; - let script_url = script.src; + const script_url = document.currentScript.src; // Get the base URL of the current script tag's URL. - script_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/"; + let base_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/"; + // The modernizr script is located outside the chunks directory. + base_url = base_url.replace("chunks/", ""); // Inject a new one with the modernizr bundle. const script_tag = document.createElement("script"); - script_tag.src = script_url + "modernizr.min.js"; + script_tag.src = base_url + "modernizr.min.js"; document.getElementsByTagName("head")[0].appendChild(script_tag); })(); From 514e7ffad98220c307fdfdf0dbb74a052fd388fa Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Tue, 28 Jan 2025 12:30:36 +0100 Subject: [PATCH 2/3] maint(core log): Console.debug is not deprecated. Just use it. --- src/core/logging.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/logging.js b/src/core/logging.js index e96477f7e..85a728cd1 100644 --- a/src/core/logging.js +++ b/src/core/logging.js @@ -46,11 +46,8 @@ function ConsoleWriter() {} ConsoleWriter.prototype = { output: function (log_name, level, messages) { if (log_name) messages.unshift(log_name + ":"); - if (level <= Level.DEBUG) { - // console.debug exists but is deprecated - messages.unshift("[DEBUG]"); - console.log.apply(console, messages); - } else if (level <= Level.INFO) console.info.apply(console, messages); + if (level <= Level.DEBUG) console.debug.apply(console, messages); + else if (level <= Level.INFO) console.info.apply(console, messages); else if (level <= Level.WARN) console.warn.apply(console, messages); else console.error.apply(console, messages); }, From 0e491938c56a84a46cbd04883bce2027843c0df1 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Tue, 28 Jan 2025 13:08:10 +0100 Subject: [PATCH 3/3] fix(core registry): Do nothing with Patterns without a trigger. Patterns without a trigger broke the registry scan method. Now they don't. --- src/core/registry.js | 15 ++++++++++++++- src/core/registry.test.js | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core/registry.js b/src/core/registry.js index 8c99e3762..0def250fe 100644 --- a/src/core/registry.js +++ b/src/core/registry.js @@ -174,9 +174,22 @@ const registry = { } } + // Clean up selectors: + // - Remove whitespace, + // - Remove trailing commas, + // - Join to selecto string. + const selector_string = selectors.map( + (selector) => selector.trim().replace(/,$/, "") + ).join(","); + + // Exit, if no selector. + if (!selector_string) { + return; + } + let matches = dom.querySelectorAllAndMe( content, - selectors.map((it) => it.trim().replace(/,$/, "")).join(",") + selector_string ); matches = matches.filter((el) => { // Filter out patterns: diff --git a/src/core/registry.test.js b/src/core/registry.test.js index 4c332f47f..6b211a4b4 100644 --- a/src/core/registry.test.js +++ b/src/core/registry.test.js @@ -104,4 +104,16 @@ describe("pat-registry: The registry for patterns", function () { done(); }); + + it("Does nothing with Patterns without a trigger.", function () { + registry.register( + { + name: "pattern-without-trigger" + } + ) + + const el = document.createElement("div"); + expect(() => { registry.scan(el) }).not.toThrow(DOMException); + }); + });