forked from matthiasmullie/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS.php
More file actions
244 lines (205 loc) · 10.6 KB
/
Copy pathJS.php
File metadata and controls
244 lines (205 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace MatthiasMullie\Minify;
/**
* MinifyJS class
*
* This source file can be used to minify Javascript files.
*
* The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to minify@mullie.eu.
* If you report a bug, make sure you give me enough information (include your code).
*
* License
* Copyright (c) 2012, Matthias Mullie. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
*
* This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
*
* @author Matthias Mullie <minify@mullie.eu>
* @author Tijs Verkoyen <minify@verkoyen.eu>
* @version 1.1.0
*
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved.
* @license MIT License
*/
class JS extends Minify
{
const STRIP_COMMENTS = 1;
const STRIP_WHITESPACE = 2;
/**
* Extract comments & strings from source code (and replace them with a placeholder)
* This fancy parsing is neccessary because comments can contain string demarcators and vice versa, and both can
* contain content that is very similar to the rest of the code.
*
* @param string $content The file/content to extract comments & strings for.
* @return array An array containing the (manipulated) content, the strings & the comments.
*/
protected function extract($content)
{
// load the content
$content = $this->load($content);
// initialize array that will contain all strings found in the code
$strings = array();
$comments = array();
// loop all characters
for ($i = 0; $i < strlen($content); $i++) {
$character = $content[$i];
switch ($content[$i]) {
// string demarcation: ' or "
case '\'':
case '"':
$stringOpener = $character;
// process through content until we find the end of the string
for ($j = $i + 1; $j < strlen($content); $j++) {
$character = $content[$j];
$previousCharacter = isset($content[$j - 1]) ? $content[$j - 1] : '';
/*
* Find end of string:
* - string started with double quotes ends in double quotes, likewise for single quotes.
* - unterminated string ends at newline (bad code), unless newline is escaped (though nobody
* knows this.)
*/
if (($stringOpener == $character && $previousCharacter != '\\') || (in_array($character, array("\r", "\n")) && $previousCharacter != '\\')) {
// save string
$replacement = '[MINIFY-STRING-' . count($strings) . ']';
$strings[$replacement] = substr($content, $i, $j - $i + 1);
// replace string by stub
$content = substr_replace($content, $replacement, $i, $j - $i + 1);
// reset pointer to the end of this string
$i += strlen($replacement);
break;
}
}
break;
// comment demarcation: // or /*
case '/':
$commentOpener = $character . (isset($content[$i + 1]) ? $content[$i + 1] : '');
/*
* Both comment opening tags are 2 characters, so grab the next character and verify we're really
* opening a comment here.
*/
if (in_array($commentOpener, array('//', '/*'))) {
// process through content until we find the end of the comment
for ($j = $i + 1; $j < strlen($content); $j++) {
$character = $content[$j];
$previousCharacter = isset($content[$j - 1]) ? $content[$j - 1] : '';
/*
* Find end of comment:
* - // single line comments end at newline.
* - /* multiline comments and at their respective closing tag, which I can't use here or
* it'd end this very comment.
*/
if (($commentOpener == '//' && in_array($character, array("\r", "\n"))) || ($commentOpener == '/*' && $previousCharacter . $character == '*/')) {
// save comment
$replacement = '[MINIFY-COMMENT-' . count($comments) . ']';
$comments[$replacement] = substr($content, $i, $j - $i + 1);
// replace comment by stub
$content = substr_replace($content, $replacement, $i, $j - $i + 1);
// reset pointer to the end of this string
$i += strlen($replacement);
break;
}
}
}
break;
}
}
return array($content, $strings, $comments);
}
/**
* Minify the data.
* Perform JS optimizations.
*
* @param string[optional] $path The path the data should be written to.
* @param int[optional] $options The minify options to be applied.
* @return string The minified data.
*/
public function minify($path = false, $options = self::ALL)
{
$content = '';
// loop files
foreach ($this->data as $source => $js) {
// combine js
$content .= $js;
}
// extract comments & strings from content
list($content, $strings, $comments) = $this->extract($content);
if($options & static::STRIP_COMMENTS) $content = $this->stripComments($content, $comments);
if($options & static::STRIP_WHITESPACE) $content = $this->stripWhitespace($content, $strings, $comments);
// reset strings
$content = str_replace(array_keys($strings), array_values($strings), $content);
// save to path
if($path !== false) $this->save($content, $path);
return $content;
}
/**
* Strip comments from source code.
*
* @param string $content The file/content to strip the comments for.
* @return string
*/
protected function stripComments($content)
{
// little "hack" for internal use
$comments = @func_get_arg(1);
// load the content
$content = $this->load($content);
// content has not been parsed before, do so now
if ($comments === false) {
// extract strings & comments
list($content, $strings, $comments) = $this->extract($content);
// reset strings
$content = str_replace(array_keys($strings), array_values($strings), $content);
}
// strip comments (if any)
if ($comments) $content = str_replace(array_keys($comments), array_fill(0, count($comments), ''), $content);
return $content;
}
/**
* Strip whitespace.
*
* @param string $content The file/content to strip the whitespace for.
* @return string
*/
protected function stripWhitespace($content)
{
// little "hack" for internal use
$strings = @func_get_arg(1);
$comments = @func_get_arg(2);
// load the content
$content = $this->load($content);
// content has not been parsed before, do so now
if ($strings === false || $comments === false) {
// extract strings & comments
list($content, $strings, $comments) = $this->extract($content);
}
// newlines > linefeed
$content = str_replace(array("\r\n", "\r", "\n"), "\n", $content);
// empty lines > collapse
$content = preg_replace('/^[ \t]*|[ \t]*$/m', '', $content);
$content = preg_replace('/\n+/m', "\n", $content);
$content = trim($content);
// redundant whitespace > remove
$content = preg_replace('/(?<=[{}\[\]\(\)=><&\|;:,\?!\+-])[ \t]*|[ \t]*(?=[{}\[\]\(\)=><&\|;:,\?!\+-])/i', '', $content);
$content = preg_replace('/[ \t]+/', ' ', $content);
// redundant semicolons (followed by another semicolon or closing curly bracket) > remove
$content = preg_replace('/;\s*(?=[;}])/s', '', $content);
/*
* @todo: we could remove all line feeds, but then we have to be certain that all statements are properly
* terminated with a semi-colon. So we'd first have to parse the statements to see which require a semi-colon,
* add it if it's not present, and then remove the line feeds. The semi-colon just before a closing curly
* bracket can then also be omitted.
*/
// reset data if this function has not been called upon through internal methods
if (@func_get_arg(1) === false || @func_get_arg(2) === false) {
// reset strings & comments
$content = str_replace(array_keys($strings), array_values($strings), $content);
$content = str_replace(array_keys($comments), array_values($comments), $content);
}
return $content;
}
}