@@ -11,7 +11,7 @@ public function apply($buffer)
1111 {
1212 // First, remove multi-line comments (/* ... */)
1313 $ buffer = $ this ->replaceInsideHtmlTags (['script ' , 'style ' ], self ::REGEX_MATCH_MULTILINE_COMMENTS , '' , $ buffer );
14-
14+
1515 // Then, remove single-line comments (//) more carefully
1616 $ buffer = $ this ->removeSingleLineComments ($ buffer );
1717
@@ -21,7 +21,7 @@ public function apply($buffer)
2121
2222 return $ this ->replace ($ replaceHtmlRules , $ buffer );
2323 }
24-
24+
2525 /**
2626 * Remove single-line comments (//) from script tags while preserving them inside strings
2727 *
@@ -34,10 +34,10 @@ protected function removeSingleLineComments($buffer)
3434 $ tagAfterReplace = $ this ->removeCommentsFromTag ($ tagMatched );
3535 $ buffer = str_replace ($ tagMatched , $ tagAfterReplace , $ buffer );
3636 }
37-
37+
3838 return $ buffer ;
3939 }
40-
40+
4141 /**
4242 * Remove // comments from a script/style tag content
4343 *
@@ -53,18 +53,18 @@ protected function removeCommentsFromTag($tag)
5353 } elseif (strpos ($ tag , "\r" ) !== false ) {
5454 $ lineEnding = "\r" ;
5555 }
56-
56+
5757 // Split by lines to process each line
5858 $ lines = preg_split ('/\r\n|\r|\n/ ' , $ tag );
5959 $ processedLines = [];
60-
60+
6161 foreach ($ lines as $ line ) {
6262 $ processedLines [] = $ this ->removeSingleLineCommentFromLine ($ line );
6363 }
64-
64+
6565 return implode ($ lineEnding , $ processedLines );
6666 }
67-
67+
6868 /**
6969 * Remove // comment from a single line while preserving // inside strings
7070 *
@@ -79,38 +79,38 @@ protected function removeSingleLineCommentFromLine($line)
7979 $ inDoubleQuote = false ;
8080 $ inRegex = false ;
8181 $ escaped = false ;
82-
82+
8383 for ($ i = 0 ; $ i < $ length ; $ i ++) {
8484 $ char = $ line [$ i ];
8585 $ nextChar = $ i + 1 < $ length ? $ line [$ i + 1 ] : '' ;
8686 $ prevChar = $ i > 0 ? $ line [$ i - 1 ] : '' ;
87-
87+
8888 // Handle escape sequences
8989 if ($ escaped ) {
9090 $ result .= $ char ;
9191 $ escaped = false ;
9292 continue ;
9393 }
94-
94+
9595 if ($ char === '\\' && ($ inSingleQuote || $ inDoubleQuote || $ inRegex )) {
9696 $ result .= $ char ;
9797 $ escaped = true ;
9898 continue ;
9999 }
100-
100+
101101 // Toggle quote states
102102 if ($ char === '" ' && !$ inSingleQuote && !$ inRegex ) {
103103 $ inDoubleQuote = !$ inDoubleQuote ;
104104 $ result .= $ char ;
105105 continue ;
106106 }
107-
107+
108108 if ($ char === "' " && !$ inDoubleQuote && !$ inRegex ) {
109109 $ inSingleQuote = !$ inSingleQuote ;
110110 $ result .= $ char ;
111111 continue ;
112112 }
113-
113+
114114 // Handle regex literals (basic detection)
115115 if ($ char === '/ ' && !$ inSingleQuote && !$ inDoubleQuote ) {
116116 // Check if this might be a regex literal
@@ -123,15 +123,15 @@ protected function removeSingleLineCommentFromLine($line)
123123 continue ;
124124 }
125125 }
126-
126+
127127 // End of regex literal
128128 if ($ inRegex ) {
129129 $ inRegex = false ;
130130 $ result .= $ char ;
131131 continue ;
132132 }
133133 }
134-
134+
135135 // Check for // comment outside of strings
136136 if (!$ inSingleQuote && !$ inDoubleQuote && !$ inRegex && $ char === '/ ' && $ nextChar === '/ ' ) {
137137 // Check if this is not part of a URL (preceded by :)
@@ -140,10 +140,10 @@ protected function removeSingleLineCommentFromLine($line)
140140 break ;
141141 }
142142 }
143-
143+
144144 $ result .= $ char ;
145145 }
146-
146+
147147 return $ result ;
148148 }
149149}
0 commit comments