Skip to content

Commit a33c4e8

Browse files
authored
Merge pull request #1240 from Patternslib/registry-blacklist
Patterns blacklist
2 parents b49a5b0 + 2d04c6c commit a33c4e8

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ To facilitate debugging you can change the default log level through the URL que
134134
- `http://www.example.com/?loglevel-inject=DEBUG` changes the log level for just the inject pattern to `DEBUG`.
135135
- `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.
136136

137+
### Patternslib global variables
138+
139+
There are some global variables that are available and can be used to make
140+
global settings or access otherwise hidden objects.
141+
142+
| Global variable | Purpose | Default |
143+
| --------------- | ------- | ------ |
144+
| window.__patternslib_import_styles | Whether to import pattern-specific styles | false |
145+
| window.__patternslib_patterns_blacklist | A list of patterns that should not be loaded. | [] |
146+
| window.__patternslib_registry | Global access to the Patternslib registry object. | - |
147+
| window.__patternslib_registry_initialized | True, if the registry has been initialized. | false |
148+
| window.__patternslib_disable_modernizr (Deprecated) | Disable modernizr, but still write the js/no-js classes to the body. | undefined |
137149

138150
### Bundle build analyzation
139151

src/core/registry.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ const registry = {
219219
log.error("Pattern lacks a name.", pattern);
220220
return false;
221221
}
222+
223+
// Do not register blacklisted patterns.
224+
let BLACKLIST = window.__patternslib_patterns_blacklist;
225+
if (!Array.isArray(BLACKLIST)) {
226+
BLACKLIST = [];
227+
}
228+
if (BLACKLIST.includes(name)) {
229+
log.warn(`Pattern name ${name} is blacklisted.`);
230+
return false;
231+
}
232+
222233
if (registry.patterns[name]) {
223234
log.debug(`Already have a pattern called ${name}.`);
224235
return false;

src/core/registry.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,38 @@ describe("pat-registry: The registry for patterns", function () {
116116
expect(() => { registry.scan(el) }).not.toThrow(DOMException);
117117
});
118118

119+
it("Does not initialize the pattern if blacklisted", function () {
120+
window.__patternslib_patterns_blacklist = ["example"];
121+
122+
Base.extend({
123+
name: "example",
124+
trigger: ".pat-example",
125+
init: function () {
126+
this.el.innerHTML = "initialized";
127+
},
128+
});
129+
130+
const tree = document.createElement("div");
131+
tree.setAttribute("class", "pat-example");
132+
registry.scan(tree);
133+
expect(tree.textContent).toBe("");
134+
});
135+
136+
it("but also doesn't break with invalid blacklists", function () {
137+
window.__patternslib_patterns_blacklist = "example"; // not an array
138+
139+
Base.extend({
140+
name: "example",
141+
trigger: ".pat-example",
142+
init: function () {
143+
this.el.innerHTML = "initialized";
144+
},
145+
});
146+
147+
const tree = document.createElement("div");
148+
tree.setAttribute("class", "pat-example");
149+
registry.scan(tree);
150+
expect(tree.textContent).toBe("initialized");
151+
});
152+
119153
});

src/polyfills-loader.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/public_path.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)