-
-
Notifications
You must be signed in to change notification settings - Fork 310
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 #329 Prevent regex from getting stripped as comment #330
base: master
Are you sure you want to change the base?
Conversation
As mentioned in wp-media/wp-rocket#2974, lines with |
85ee174
to
227f190
Compare
This change causes side effects in cases of comments starting with An example:
is minified to:
Notice the preserved comment at the end, and since it's on the same line, the following closing parenthesis/brackets are considered inside the comments, thus breaking the code. |
Interesting... but comments staring with I can't think of a solution that will work on 100% of the cases. Identifying a comment in JS is not a trivial thing. |
You'd be surprised by the number of times it happens, and in popular script like flexslider.js, used in all WooCommerce installations, or popular WP themes like Bridge. In the end, for our use case, this change is more impactful than the original way. I agree identifying comments in JS is complex, especially when RegEx is involved. But the solution should not be to create other side effects while fixing the bug. |
How about this pattern? Could you see any side effects? $js = "
// comment
infowindow.open(map,marker);
//infowindow.open(map,marker);
//.comment
/* comment
*/
test: [/\sedg\//i],
//test: [/\sedg\//i],
if(b)return w.IGNORE;var c=B(fb(a,\"href\"));b=R(fb(a,\"hostname\"));c=c?/^https?:\/\//.test(c)||/^\/\//.test(c):!1;if(c&&!lc(a)){if(la)return w.TRACK;
";
$pattern = <<<'EOD'
~
(?(DEFINE)
(?<squoted> ' [^'\n\\]*+ (?: \\. [^'\n\\]* )*+ ' )
(?<dquoted> " [^"\n\\]*+ (?: \\. [^"\n\\]* )*+ " )
(?<quoted> \g<squoted> | \g<dquoted> )
(?<scomment> // \N* )
(?<mcomment> /\* [^*]*+ (?: \*+ (?!/) [^*]* )*+ \*/ )
(?<comment> \g<scomment> | \g<mcomment> )
(?<pattern> / [^\n/*] [^\n/\\]*+ (?>\\.[^\n/\\]*)* / [gimuy]* )
)
(?=[[(:,=/"'])
(?|
\g<quoted> (*SKIP)(*FAIL)
|
( [[(:,=] \s* ) (*SKIP) (?: \g<comment> \s* )*+ ( \g<pattern> )
|
( \g<pattern> \s* ) (?: \g<comment> \s* )*+
( \. \s* ) (?:\g<comment> \s* )*+ ([A-Za-z_]\w*)
|
\g<comment>
)
~x
EOD;
$js = preg_replace($pattern, '$8$9${10}', $js);
echo $js; |
949e01d
to
51d1aa4
Compare
This a quick fix that will solve some issues but prevent the removal of single line comments starting with "//i". I guess it's better to leave some comments than breaking the code.
Fixes #323
Fixes #329