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(js-regex): 🐛 Fix Regex Pattern in addScripts Method #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions resources/js/Cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class LaravelCookieConsent {
}

addScripts(data) {
if(!data.scripts) {
if (!data.scripts) {
return;
}

data.scripts.forEach(script => {
const scriptRegex = /<script.*<\/script>/;
const scriptRegex = /<script([^]*)<\/script>/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why a capture group would be necessary and why a negated empty set ([^]*) is better than .*? Thanks :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The capture group is indeed not necessary in this context. However, using a negated set like [^]* has the advantage of matching multi-line scripts. This allows us to send non-minified JavaScript code as a string across multiple lines. The negated set captures everything until it finds a specific character (if defined), which is beneficial when working with multi-line code or text.

if (!scriptRegex.test(script)) {
console.error('Invalid script tag: ' + script);
}
Expand All @@ -56,7 +56,7 @@ class LaravelCookieConsent {
}

addNotice(data) {
if(!data.notice) {
if (!data.notice) {
return;
}

Expand All @@ -68,7 +68,7 @@ class LaravelCookieConsent {

let tags = tmp.querySelectorAll('[data-cookie-consent]');

if (! tags.length) {
if (!tags.length) {
return;
}

Expand All @@ -85,5 +85,5 @@ class LaravelCookieConsent {
}

window.addEventListener('load', () => {
window.LaravelCookieConsent = new LaravelCookieConsent({config:1});
window.LaravelCookieConsent = new LaravelCookieConsent({ config: 1 });
});