Skip to content

Commit 0b60791

Browse files
authored
Merge pull request #1231 from Patternslib/fix-loading-modernizr
fix(core feature-detection): Fix loading of modernizr script.
2 parents a3b6f48 + 0e49193 commit 0b60791

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

src/core/feature-detection.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
}
1919

2020
// Get the current script tag's URL.
21-
// See: https://stackoverflow.com/a/984656/1337474
22-
const scripts = document.getElementsByTagName("script");
23-
const script = scripts[scripts.length - 1];
24-
let script_url = script.src;
21+
const script_url = document.currentScript.src;
2522
// Get the base URL of the current script tag's URL.
26-
script_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
23+
let base_url = script_url.substring(0, script_url.lastIndexOf("/")) + "/";
24+
// The modernizr script is located outside the chunks directory.
25+
base_url = base_url.replace("chunks/", "");
2726

2827
// Inject a new one with the modernizr bundle.
2928
const script_tag = document.createElement("script");
30-
script_tag.src = script_url + "modernizr.min.js";
29+
script_tag.src = base_url + "modernizr.min.js";
3130
document.getElementsByTagName("head")[0].appendChild(script_tag);
3231
})();

src/core/logging.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ function ConsoleWriter() {}
4646
ConsoleWriter.prototype = {
4747
output: function (log_name, level, messages) {
4848
if (log_name) messages.unshift(log_name + ":");
49-
if (level <= Level.DEBUG) {
50-
// console.debug exists but is deprecated
51-
messages.unshift("[DEBUG]");
52-
console.log.apply(console, messages);
53-
} else if (level <= Level.INFO) console.info.apply(console, messages);
49+
if (level <= Level.DEBUG) console.debug.apply(console, messages);
50+
else if (level <= Level.INFO) console.info.apply(console, messages);
5451
else if (level <= Level.WARN) console.warn.apply(console, messages);
5552
else console.error.apply(console, messages);
5653
},

src/core/registry.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,22 @@ const registry = {
174174
}
175175
}
176176

177+
// Clean up selectors:
178+
// - Remove whitespace,
179+
// - Remove trailing commas,
180+
// - Join to selecto string.
181+
const selector_string = selectors.map(
182+
(selector) => selector.trim().replace(/,$/, "")
183+
).join(",");
184+
185+
// Exit, if no selector.
186+
if (!selector_string) {
187+
return;
188+
}
189+
177190
let matches = dom.querySelectorAllAndMe(
178191
content,
179-
selectors.map((it) => it.trim().replace(/,$/, "")).join(",")
192+
selector_string
180193
);
181194
matches = matches.filter((el) => {
182195
// Filter out patterns:

src/core/registry.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,16 @@ describe("pat-registry: The registry for patterns", function () {
104104

105105
done();
106106
});
107+
108+
it("Does nothing with Patterns without a trigger.", function () {
109+
registry.register(
110+
{
111+
name: "pattern-without-trigger"
112+
}
113+
)
114+
115+
const el = document.createElement("div");
116+
expect(() => { registry.scan(el) }).not.toThrow(DOMException);
117+
});
118+
107119
});

0 commit comments

Comments
 (0)