@@ -119,11 +119,12 @@ public function minify($file)
119
119
$ content = $ this ->readFactory ->create ($ dir )->readFile ($ fileName );
120
120
$ heredocs = null ;
121
121
122
+ // Safely minify PHP code and remove single-line PHP comments by using a parser.
122
123
if (null !== $ content ) {
123
124
$ parser = (new \PhpParser \ParserFactory ())->create (\PhpParser \ParserFactory::PREFER_PHP7 );
124
125
125
126
/**
126
- * Prevent problems with deeply nested ASTs if xDebug is enabled.
127
+ * Prevent problems with deeply nested ASTs if Xdebug is enabled.
127
128
* @see https://github.com/nikic/PHP-Parser/blob/v4.4.0/doc/2_Usage_of_basic_components.markdown#bootstrapping
128
129
*/
129
130
$ nestingLevelConfigValue = ini_get ('xdebug.max_nesting_level ' );
@@ -151,7 +152,7 @@ public function minify($file)
151
152
}
152
153
}
153
154
154
- //Storing Heredocs
155
+ // Stash the heredocs now if the template could not be parsed.
155
156
if (null === $ heredocs ) {
156
157
$ content = preg_replace_callback (
157
158
'/<<<([A-z]+).*?\1\s*;/ims ' ,
@@ -164,23 +165,33 @@ function ($match) use (&$heredocs) {
164
165
);
165
166
}
166
167
168
+ // Remove insignificant spaces before closing HTML tags
169
+ // (preserve one space after ]]>, and all spaces inside <pre> and <textarea> tags).
167
170
$ content = preg_replace (
168
171
'#(?<!]]>)\s+</(?!(?>textarea|pre)\b)# ' ,
169
172
'</ ' ,
173
+ // Remove redundant spaces after PHP tags that do not start with a print or condition statement,
174
+ // and that do not contain any "?".
170
175
preg_replace (
171
176
'#((?:<\?php\s+(?!echo|print|if|elseif|else)[^\?]*)\?>)\s+# ' ,
172
177
'$1 ' ,
178
+ // Remove single space in empty non-inline tags.
173
179
preg_replace (
174
180
'#(?<! ' . implode ('| ' , $ this ->inlineHtmlTags ) . ')\> \<# ' ,
175
181
'>< ' ,
182
+ // Remove redundant spaces outside of tags in which they are relevant.
176
183
preg_replace (
177
184
'#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+) '
178
185
. '(?:<(?>textarea|pre|script)\b|\z))# ' ,
179
186
' ' ,
187
+ // Remove single-line comments in <script> tags, except for <![CDATA[ and ]]>.
188
+ // Do nothing if the "//" part is seemingly part of a string / URL / RegExp.
180
189
preg_replace (
181
190
'#(?<!:| \\\\| \\\| \'|"|/)//(?!/)(?!\s*\<\!\[)(?!\s*]]\>)[^\n\r]* '
182
191
. '(?!(?:(?:[^<]++|<(?!/?(?:script)\b))*+)(?:<(?>script)\b|\z))# ' ,
183
192
'' ,
193
+ // Remove commented single-line PHP tags in <script> tags.
194
+ // Do nothing if the "//" part is seemingly part of a URL / RegExp.
184
195
preg_replace (
185
196
'#(?<!:| \\\)//[^\n\r]*(\<\?(php|=))[^\n\r]*(\s\?\>)[^\n\r]* '
186
197
. '(?!(?:(?:[^<]++|<(?!/?(?:script)\b))*+)(?:<(?>script)\b|\z))# ' ,
@@ -193,7 +204,7 @@ function ($match) use (&$heredocs) {
193
204
)
194
205
);
195
206
196
- //Restoring Heredocs
207
+ // Restore the stashed heredocs.
197
208
$ content = preg_replace_callback (
198
209
'/__MINIFIED_HEREDOC__(\d+)/ims ' ,
199
210
function ($ match ) use ($ heredocs ) {
0 commit comments