Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core feature-detection): Fix loading of modernizr script. #1231

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/core/feature-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();
7 changes: 2 additions & 5 deletions src/core/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
15 changes: 14 additions & 1 deletion src/core/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions src/core/registry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});