From 20dd150319f45b4153830119cc874212e6584e3f Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Fri, 4 Apr 2025 23:59:00 +0200 Subject: [PATCH 1/4] maint: Remove the public path helper script. The public path helper script did set the webpack public path to find the path where the bundle was located and to correctly find the chunk directory. The logic was based on finding the URL of the last loaded script. This was not even always correct. It was also possible to override it via `window.__patternslib_public_path__`. Since quite some thime, this is not necessary anymore as webpack handles these path issues on it's own. This dead code is now removed. --- src/public_path.js | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 src/public_path.js diff --git a/src/public_path.js b/src/public_path.js deleted file mode 100644 index 201d2f2a7..000000000 --- a/src/public_path.js +++ /dev/null @@ -1,15 +0,0 @@ -// DEPRECATED. Webpack handles the public path by iteself. -// TODO: REMOVE with next major release -// NOTE: Import this file before any other files -// Overwrite path to load resources or use default one. -__webpack_public_path__ = window.__patternslib_public_path__; // eslint-disable-line no-undef -// eslint-disable-next-line no-undef -if (!__webpack_public_path__) { - // Get chunks path from current script. - let src = document.currentScript?.src; - if (src) { - src = src.split("/"); - src.pop(); - __webpack_public_path__ = src.join("/") + "/"; // eslint-disable-line no-undef - } -} From 6fee70943aee3d2d963c7b5765ca5de5684282dc Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sat, 5 Apr 2025 00:13:50 +0200 Subject: [PATCH 2/4] maint: Remove the polyfills-loader script. The few necessary polyfills are now loaded by Patternslib itself. This script was empty and is now removed. --- src/polyfills-loader.js | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 src/polyfills-loader.js diff --git a/src/polyfills-loader.js b/src/polyfills-loader.js deleted file mode 100644 index c4969ba20..000000000 --- a/src/polyfills-loader.js +++ /dev/null @@ -1,2 +0,0 @@ -// TODO: REMOVE with next major release -// BBB backwards compatibility From 25dab598352c9bba6fd3f416938925b740ce0e83 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sat, 5 Apr 2025 00:18:17 +0200 Subject: [PATCH 3/4] maint: Document patternslib global variables. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 641f58408..43cd30bcb 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,18 @@ To facilitate debugging you can change the default log level through the URL que - `http://www.example.com/?loglevel-inject=DEBUG` changes the log level for just the inject pattern to `DEBUG`. - `http://www.example.com/?loglevel=ERROR&loglevel-inject=INFO` changes the standard log level error, but enables messages at the `INFO` level for the inject pattern. +### Patternslib global variables + +There are some global variables that are available and can be used to make +global settings or access otherwise hidden objects. + +| Global variable | Purpose | Default | +| --------------- | ------- | ------ | +| window.__patternslib_import_styles | Whether to import pattern-specific styles | false | +| window.__patternslib_registry | Global access to the Patternslib registry object. | - | +| window.__patternslib_registry_initialized | True, if the registry has been initialized. | false | +| window.__patternslib_disable_modernizr (Deprecated) | Disable modernizr, but still write the js/no-js classes to the body. | undefined | + ### Bundle build analyzation From 2d04c6cc3850a46026bcd3eae5a6b32a12678870 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 3 Jan 2024 02:03:38 +0100 Subject: [PATCH 4/4] feat(core registry): Allow to disable individual patterns via a global window.__patternslib_patterns_blacklist array. --- README.md | 2 +- src/core/registry.js | 11 +++++++++++ src/core/registry.test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 43cd30bcb..f0c4ca67f 100644 --- a/README.md +++ b/README.md @@ -142,11 +142,11 @@ global settings or access otherwise hidden objects. | Global variable | Purpose | Default | | --------------- | ------- | ------ | | window.__patternslib_import_styles | Whether to import pattern-specific styles | false | +| window.__patternslib_patterns_blacklist | A list of patterns that should not be loaded. | [] | | window.__patternslib_registry | Global access to the Patternslib registry object. | - | | window.__patternslib_registry_initialized | True, if the registry has been initialized. | false | | window.__patternslib_disable_modernizr (Deprecated) | Disable modernizr, but still write the js/no-js classes to the body. | undefined | - ### Bundle build analyzation https://survivejs.com/webpack/optimizing/build-analysis/ diff --git a/src/core/registry.js b/src/core/registry.js index 0def250fe..aef82d554 100644 --- a/src/core/registry.js +++ b/src/core/registry.js @@ -219,6 +219,17 @@ const registry = { log.error("Pattern lacks a name.", pattern); return false; } + + // Do not register blacklisted patterns. + let BLACKLIST = window.__patternslib_patterns_blacklist; + if (!Array.isArray(BLACKLIST)) { + BLACKLIST = []; + } + if (BLACKLIST.includes(name)) { + log.warn(`Pattern name ${name} is blacklisted.`); + return false; + } + if (registry.patterns[name]) { log.debug(`Already have a pattern called ${name}.`); return false; diff --git a/src/core/registry.test.js b/src/core/registry.test.js index 6b211a4b4..7e578ae16 100644 --- a/src/core/registry.test.js +++ b/src/core/registry.test.js @@ -116,4 +116,38 @@ describe("pat-registry: The registry for patterns", function () { expect(() => { registry.scan(el) }).not.toThrow(DOMException); }); + it("Does not initialize the pattern if blacklisted", function () { + window.__patternslib_patterns_blacklist = ["example"]; + + Base.extend({ + name: "example", + trigger: ".pat-example", + init: function () { + this.el.innerHTML = "initialized"; + }, + }); + + const tree = document.createElement("div"); + tree.setAttribute("class", "pat-example"); + registry.scan(tree); + expect(tree.textContent).toBe(""); + }); + + it("but also doesn't break with invalid blacklists", function () { + window.__patternslib_patterns_blacklist = "example"; // not an array + + Base.extend({ + name: "example", + trigger: ".pat-example", + init: function () { + this.el.innerHTML = "initialized"; + }, + }); + + const tree = document.createElement("div"); + tree.setAttribute("class", "pat-example"); + registry.scan(tree); + expect(tree.textContent).toBe("initialized"); + }); + });