Skip to content

Commit 09b07e8

Browse files
committed
Update minify libs and add script for updating
1 parent 27539b8 commit 09b07e8

3 files changed

Lines changed: 62 additions & 17 deletions

File tree

minify/JS/ClosureCompiler.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
* @todo can use a stream wrapper to unit test this?
1515
*/
1616
class Minify_JS_ClosureCompiler {
17-
const URL = 'http://closure-compiler.appspot.com/compile';
17+
/**
18+
* @var $url URL to compiler server. defaults to google server
19+
*/
20+
protected $url = 'http://closure-compiler.appspot.com/compile';
1821

1922
/**
2023
* Minify Javascript code via HTTP request to the Closure Compiler API
@@ -34,12 +37,17 @@ public static function minify($js, array $options = array())
3437
* @param array $options
3538
*
3639
* fallbackFunc : default array($this, 'fallback');
40+
* compilerUrl : URL to closure compiler server
3741
*/
3842
public function __construct(array $options = array())
3943
{
4044
$this->_fallbackFunc = isset($options['fallbackMinifier'])
4145
? $options['fallbackMinifier']
4246
: array($this, '_fallback');
47+
48+
if (isset($options['compilerUrl'])) {
49+
$this->url = $options['compilerUrl'];
50+
}
4351
}
4452

4553
public function min($js)
@@ -76,7 +84,7 @@ protected function _getResponse($postBody)
7684
{
7785
$allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'));
7886
if ($allowUrlFopen) {
79-
$contents = file_get_contents(self::URL, false, stream_context_create(array(
87+
$contents = file_get_contents($this->url, false, stream_context_create(array(
8088
'http' => array(
8189
'method' => 'POST',
8290
'header' => "Content-type: application/x-www-form-urlencoded\r\nConnection: close\r\n",
@@ -86,7 +94,7 @@ protected function _getResponse($postBody)
8694
)
8795
)));
8896
} elseif (defined('CURLOPT_POST')) {
89-
$ch = curl_init(self::URL);
97+
$ch = curl_init($this->url);
9098
curl_setopt($ch, CURLOPT_POST, true);
9199
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
92100
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));

minify/JS/JSMin.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ protected function action($command)
200200
break;
201201
}
202202
if ($this->isEOF($this->a)) {
203+
$byte = $this->inputIndex - 1;
203204
throw new JSMin_UnterminatedStringException(
204-
"JSMin: Unterminated String at byte {$this->inputIndex}: {$str}");
205+
"JSMin: Unterminated String at byte {$byte}: {$str}");
205206
}
206207
$str .= $this->a;
207208
if ($this->a === '\\') {
@@ -251,8 +252,9 @@ protected function action($command)
251252
$this->a = $this->get();
252253
$pattern .= $this->a;
253254
} elseif ($this->isEOF($this->a)) {
255+
$byte = $this->inputIndex - 1;
254256
throw new JSMin_UnterminatedRegExpException(
255-
"JSMin: Unterminated RegExp at byte {$this->inputIndex}: {$pattern}");
257+
"JSMin: Unterminated RegExp at byte {$byte}: {$pattern}");
256258
}
257259
$this->output .= $this->a;
258260
$this->lastByteOut = $this->a;
@@ -272,23 +274,33 @@ protected function isRegexpLiteral()
272274
// we obviously aren't dividing
273275
return true;
274276
}
275-
if ($this->a === ' ' || $this->a === "\n") {
276-
$length = strlen($this->output);
277-
if ($length < 2) { // weird edge case
278-
return true;
277+
278+
// we have to check for a preceding keyword, and we don't need to pattern
279+
// match over the whole output.
280+
$recentOutput = substr($this->output, -10);
281+
282+
// check if return/typeof directly precede a pattern without a space
283+
foreach (array('return', 'typeof') as $keyword) {
284+
if ($this->a !== substr($keyword, -1)) {
285+
// certainly wasn't keyword
286+
continue;
279287
}
280-
// you can't divide a keyword
281-
if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
282-
if ($this->output === $m[0]) { // odd but could happen
283-
return true;
284-
}
285-
// make sure it's a keyword, not end of an identifier
286-
$charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
287-
if (! $this->isAlphaNum($charBeforeKeyword)) {
288+
if (preg_match("~(^|[\\s\\S])" . substr($keyword, 0, -1) . "$~", $recentOutput, $m)) {
289+
if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
288290
return true;
289291
}
290292
}
291293
}
294+
295+
// check all keywords
296+
if ($this->a === ' ' || $this->a === "\n") {
297+
if (preg_match('~(^|[\\s\\S])(?:case|else|in|return|typeof)$~', $recentOutput, $m)) {
298+
if ($m[1] === '' || !$this->isAlphaNum($m[1])) {
299+
return true;
300+
}
301+
}
302+
}
303+
292304
return false;
293305
}
294306

minify/update

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if ( php_sapi_name() !== 'cli' ) {
5+
print "Error: Only executable in CLI mode.\n";
6+
exit( 1 );
7+
}
8+
9+
chdir( __DIR__ . '/../minify' );
10+
11+
$sources = array(
12+
"JS/ClosureCompiler.php" => "https://raw.github.com/mrclay/minify/master/min/lib/Minify/JS/ClosureCompiler.php",
13+
"JS/JSMin.php" => "https://raw.github.com/mrclay/minify/master/min/lib/JSMin.php",
14+
"JS/JSMinPlus.php" => "https://raw.github.com/mrclay/minify/master/min/lib/JSMinPlus.php",
15+
"CSS/Compressor.php" => "https://raw.github.com/mrclay/minify/master/min/lib/Minify/CSS/Compressor.php",
16+
"CSS/UriRewriter.php" => "https://raw.github.com/mrclay/minify/master/min/lib/Minify/CSS/UriRewriter.php",
17+
"CommentPreserver.php" => "https://raw.github.com/mrclay/minify/master/min/lib/Minify/CommentPreserver.php",
18+
);
19+
20+
foreach( $sources as $path => $url ) {
21+
echo "$path => $url\n";
22+
$contents = file_get_contents( $url );
23+
file_put_contents( $path, $contents ) || exit( 1 );
24+
system( sprintf( 'git add %s', escapeshellarg( $path ) ) );
25+
}

0 commit comments

Comments
 (0)