Skip to content

Commit 59be884

Browse files
committed
Comment on each step of the minification process
1 parent 1bc9e69 commit 59be884

File tree

1 file changed

+14
-3
lines changed
  • lib/internal/Magento/Framework/View/Template/Html

1 file changed

+14
-3
lines changed

lib/internal/Magento/Framework/View/Template/Html/Minifier.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ public function minify($file)
119119
$content = $this->readFactory->create($dir)->readFile($fileName);
120120
$heredocs = null;
121121

122+
// Safely minify PHP code and remove single-line PHP comments by using a parser.
122123
if (null !== $content) {
123124
$parser = (new \PhpParser\ParserFactory())->create(\PhpParser\ParserFactory::PREFER_PHP7);
124125

125126
/**
126-
* Prevent problems with deeply nested ASTs if xDebug is enabled.
127+
* Prevent problems with deeply nested ASTs if Xdebug is enabled.
127128
* @see https://github.com/nikic/PHP-Parser/blob/v4.4.0/doc/2_Usage_of_basic_components.markdown#bootstrapping
128129
*/
129130
$nestingLevelConfigValue = ini_get('xdebug.max_nesting_level');
@@ -151,7 +152,7 @@ public function minify($file)
151152
}
152153
}
153154

154-
//Storing Heredocs
155+
// Stash the heredocs now if the template could not be parsed.
155156
if (null === $heredocs) {
156157
$content = preg_replace_callback(
157158
'/<<<([A-z]+).*?\1\s*;/ims',
@@ -164,23 +165,33 @@ function ($match) use (&$heredocs) {
164165
);
165166
}
166167

168+
// Remove insignificant spaces before closing HTML tags
169+
// (preserve one space after ]]>, and all spaces inside <pre> and <textarea> tags).
167170
$content = preg_replace(
168171
'#(?<!]]>)\s+</(?!(?>textarea|pre)\b)#',
169172
'</',
173+
// Remove redundant spaces after PHP tags that do not start with a print or condition statement,
174+
// and that do not contain any "?".
170175
preg_replace(
171176
'#((?:<\?php\s+(?!echo|print|if|elseif|else)[^\?]*)\?>)\s+#',
172177
'$1 ',
178+
// Remove single space in empty non-inline tags.
173179
preg_replace(
174180
'#(?<!' . implode('|', $this->inlineHtmlTags) . ')\> \<#',
175181
'><',
182+
// Remove redundant spaces outside of tags in which they are relevant.
176183
preg_replace(
177184
'#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)'
178185
. '(?:<(?>textarea|pre|script)\b|\z))#',
179186
' ',
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.
180189
preg_replace(
181190
'#(?<!:|\\\\|\\\|\'|"|/)//(?!/)(?!\s*\<\!\[)(?!\s*]]\>)[^\n\r]*'
182191
. '(?!(?:(?:[^<]++|<(?!/?(?:script)\b))*+)(?:<(?>script)\b|\z))#',
183192
'',
193+
// Remove commented single-line PHP tags in <script> tags.
194+
// Do nothing if the "//" part is seemingly part of a URL / RegExp.
184195
preg_replace(
185196
'#(?<!:|\\\)//[^\n\r]*(\<\?(php|=))[^\n\r]*(\s\?\>)[^\n\r]*'
186197
. '(?!(?:(?:[^<]++|<(?!/?(?:script)\b))*+)(?:<(?>script)\b|\z))#',
@@ -193,7 +204,7 @@ function ($match) use (&$heredocs) {
193204
)
194205
);
195206

196-
//Restoring Heredocs
207+
// Restore the stashed heredocs.
197208
$content = preg_replace_callback(
198209
'/__MINIFIED_HEREDOC__(\d+)/ims',
199210
function ($match) use ($heredocs) {

0 commit comments

Comments
 (0)