|
4 | 4 |
|
5 | 5 | class RemoveComments extends PageSpeed |
6 | 6 | { |
7 | | - const REGEX_MATCH_JS_AND_CSS_COMMENTS = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/'; |
| 7 | + const REGEX_MATCH_MULTILINE_COMMENTS = '/\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\//'; |
8 | 8 | const REGEX_MATCH_HTML_COMMENTS = '/<!--[^]><!\[](.*?)[^\]]-->/s'; |
9 | 9 |
|
10 | 10 | public function apply($buffer) |
11 | 11 | { |
12 | | - $buffer = $this->replaceInsideHtmlTags(['script', 'style'], self::REGEX_MATCH_JS_AND_CSS_COMMENTS, '', $buffer); |
| 12 | + // First, remove multi-line comments (/* ... */) |
| 13 | + $buffer = $this->replaceInsideHtmlTags(['script', 'style'], self::REGEX_MATCH_MULTILINE_COMMENTS, '', $buffer); |
| 14 | + |
| 15 | + // Then, remove single-line comments (//) more carefully |
| 16 | + $buffer = $this->removeSingleLineComments($buffer); |
13 | 17 |
|
14 | 18 | $replaceHtmlRules = [ |
15 | 19 | self::REGEX_MATCH_HTML_COMMENTS => '', |
16 | 20 | ]; |
17 | 21 |
|
18 | 22 | return $this->replace($replaceHtmlRules, $buffer); |
19 | 23 | } |
| 24 | + |
| 25 | + /** |
| 26 | + * Remove single-line comments (//) from script tags while preserving them inside strings |
| 27 | + * |
| 28 | + * @param string $buffer |
| 29 | + * @return string |
| 30 | + */ |
| 31 | + protected function removeSingleLineComments($buffer) |
| 32 | + { |
| 33 | + foreach ($this->matchAllHtmlTag(['script', 'style'], $buffer) as $tagMatched) { |
| 34 | + $tagAfterReplace = $this->removeCommentsFromTag($tagMatched); |
| 35 | + $buffer = str_replace($tagMatched, $tagAfterReplace, $buffer); |
| 36 | + } |
| 37 | + |
| 38 | + return $buffer; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Remove // comments from a script/style tag content |
| 43 | + * |
| 44 | + * @param string $tag |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + protected function removeCommentsFromTag($tag) |
| 48 | + { |
| 49 | + // Detect line ending style |
| 50 | + $lineEnding = "\n"; |
| 51 | + if (strpos($tag, "\r\n") !== false) { |
| 52 | + $lineEnding = "\r\n"; |
| 53 | + } elseif (strpos($tag, "\r") !== false) { |
| 54 | + $lineEnding = "\r"; |
| 55 | + } |
| 56 | + |
| 57 | + // Split by lines to process each line |
| 58 | + $lines = preg_split('/\r\n|\r|\n/', $tag); |
| 59 | + $processedLines = []; |
| 60 | + |
| 61 | + foreach ($lines as $line) { |
| 62 | + $processedLines[] = $this->removeSingleLineCommentFromLine($line); |
| 63 | + } |
| 64 | + |
| 65 | + return implode($lineEnding, $processedLines); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Remove // comment from a single line while preserving // inside strings |
| 70 | + * |
| 71 | + * @param string $line |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + protected function removeSingleLineCommentFromLine($line) |
| 75 | + { |
| 76 | + $result = ''; |
| 77 | + $length = strlen($line); |
| 78 | + $inSingleQuote = false; |
| 79 | + $inDoubleQuote = false; |
| 80 | + $inRegex = false; |
| 81 | + $escaped = false; |
| 82 | + |
| 83 | + for ($i = 0; $i < $length; $i++) { |
| 84 | + $char = $line[$i]; |
| 85 | + $nextChar = $i + 1 < $length ? $line[$i + 1] : ''; |
| 86 | + $prevChar = $i > 0 ? $line[$i - 1] : ''; |
| 87 | + |
| 88 | + // Handle escape sequences |
| 89 | + if ($escaped) { |
| 90 | + $result .= $char; |
| 91 | + $escaped = false; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + if ($char === '\\' && ($inSingleQuote || $inDoubleQuote || $inRegex)) { |
| 96 | + $result .= $char; |
| 97 | + $escaped = true; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + // Toggle quote states |
| 102 | + if ($char === '"' && !$inSingleQuote && !$inRegex) { |
| 103 | + $inDoubleQuote = !$inDoubleQuote; |
| 104 | + $result .= $char; |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if ($char === "'" && !$inDoubleQuote && !$inRegex) { |
| 109 | + $inSingleQuote = !$inSingleQuote; |
| 110 | + $result .= $char; |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + // Handle regex literals (basic detection) |
| 115 | + if ($char === '/' && !$inSingleQuote && !$inDoubleQuote) { |
| 116 | + // Check if this might be a regex literal |
| 117 | + // Simple heuristic: regex usually comes after =, (, [, ,, return, or at start |
| 118 | + if ($prevChar === '=' || $prevChar === '(' || $prevChar === '[' || $prevChar === ',' || $prevChar === ' ') { |
| 119 | + // Look ahead to see if this looks like a regex (not a comment) |
| 120 | + if ($nextChar !== '/' && $nextChar !== '*') { |
| 121 | + $inRegex = true; |
| 122 | + $result .= $char; |
| 123 | + continue; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // End of regex literal |
| 128 | + if ($inRegex) { |
| 129 | + $inRegex = false; |
| 130 | + $result .= $char; |
| 131 | + continue; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Check for // comment outside of strings |
| 136 | + if (!$inSingleQuote && !$inDoubleQuote && !$inRegex && $char === '/' && $nextChar === '/') { |
| 137 | + // Check if this is not part of a URL (preceded by :) |
| 138 | + if ($prevChar !== ':') { |
| 139 | + // Found a comment, remove everything from here to end of line |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + $result .= $char; |
| 145 | + } |
| 146 | + |
| 147 | + return $result; |
| 148 | + } |
20 | 149 | } |
0 commit comments