mirror of
https://github.com/mrclay/minify.git
synced 2025-08-21 13:21:59 +02:00
JSMin.php : trimmed output, better exception messages
Removed unnecessary wrapper Minify_Javascript
This commit is contained in:
@@ -119,6 +119,7 @@ class JSMin {
|
|||||||
}
|
}
|
||||||
$this->action($command);
|
$this->action($command);
|
||||||
}
|
}
|
||||||
|
$this->output = trim($this->output);
|
||||||
return $this->output;
|
return $this->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +137,7 @@ class JSMin {
|
|||||||
case self::ACTION_DELETE_A:
|
case self::ACTION_DELETE_A:
|
||||||
$this->a = $this->b;
|
$this->a = $this->b;
|
||||||
if ($this->a === "'" || $this->a === '"') { // string literal
|
if ($this->a === "'" || $this->a === '"') { // string literal
|
||||||
$str = ''; // in case needed for exception
|
$str = $this->a; // in case needed for exception
|
||||||
while (true) {
|
while (true) {
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
$this->a = $this->get();
|
$this->a = $this->get();
|
||||||
@@ -145,7 +146,7 @@ class JSMin {
|
|||||||
}
|
}
|
||||||
if (ord($this->a) <= self::ORD_LF) {
|
if (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMin_UnterminatedStringException(
|
throw new JSMin_UnterminatedStringException(
|
||||||
'Contents: ' . var_export($str, true));
|
'Unterminated String: ' . var_export($str, true));
|
||||||
}
|
}
|
||||||
$str .= $this->a;
|
$str .= $this->a;
|
||||||
if ($this->a === '\\') {
|
if ($this->a === '\\') {
|
||||||
@@ -172,7 +173,7 @@ class JSMin {
|
|||||||
$pattern .= $this->a;
|
$pattern .= $this->a;
|
||||||
} elseif (ord($this->a) <= self::ORD_LF) {
|
} elseif (ord($this->a) <= self::ORD_LF) {
|
||||||
throw new JSMin_UnterminatedRegExpException(
|
throw new JSMin_UnterminatedRegExpException(
|
||||||
'Contents: '. var_export($pattern, true));
|
'Unterminated RegExp: '. var_export($pattern, true));
|
||||||
}
|
}
|
||||||
$this->output .= $this->a;
|
$this->output .= $this->a;
|
||||||
}
|
}
|
||||||
@@ -284,7 +285,7 @@ class JSMin {
|
|||||||
return ' ';
|
return ' ';
|
||||||
}
|
}
|
||||||
} elseif ($get === null) {
|
} elseif ($get === null) {
|
||||||
throw new JSMin_UnterminatedCommentException('Contents: ' . var_export($comment, true));
|
throw new JSMin_UnterminatedCommentException('Unterminated Comment: ' . var_export('/*' . $comment, true));
|
||||||
}
|
}
|
||||||
$comment .= $get;
|
$comment .= $get;
|
||||||
}
|
}
|
||||||
|
@@ -71,7 +71,7 @@ abstract class Minify_Controller_Base {
|
|||||||
* @return array minifier callbacks for common types
|
* @return array minifier callbacks for common types
|
||||||
*/
|
*/
|
||||||
public function getDefaultMinifers() {
|
public function getDefaultMinifers() {
|
||||||
$ret[Minify::TYPE_JS] = array('Minify_Javascript', 'minify');
|
$ret[Minify::TYPE_JS] = array('JSMin', 'minify');
|
||||||
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
|
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
|
||||||
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
|
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
|
||||||
return $ret;
|
return $ret;
|
||||||
|
@@ -52,7 +52,7 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
|||||||
// this will be the 2nd argument passed to Minify_HTML::minify()
|
// this will be the 2nd argument passed to Minify_HTML::minify()
|
||||||
$sourceSpec['minifyOptions'] = array(
|
$sourceSpec['minifyOptions'] = array(
|
||||||
'cssMinifier' => array('Minify_CSS', 'minify')
|
'cssMinifier' => array('Minify_CSS', 'minify')
|
||||||
,'jsMinifier' => array('Minify_Javascript', 'minify')
|
,'jsMinifier' => array('JSMin', 'minify')
|
||||||
);
|
);
|
||||||
$this->_loadCssJsMinifiers = true;
|
$this->_loadCssJsMinifiers = true;
|
||||||
unset($options['minifyAll']);
|
unset($options['minifyAll']);
|
||||||
@@ -77,7 +77,7 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
|||||||
// Minify will not call for these so we must manually load
|
// Minify will not call for these so we must manually load
|
||||||
// them when Minify/HTML.php is called for.
|
// them when Minify/HTML.php is called for.
|
||||||
require 'Minify/CSS.php';
|
require 'Minify/CSS.php';
|
||||||
require 'Minify/Javascript.php';
|
require 'JSMin.php';
|
||||||
}
|
}
|
||||||
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
|
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
|
||||||
}
|
}
|
||||||
|
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Class Minify_Javascript
|
|
||||||
* @package Minify
|
|
||||||
*/
|
|
||||||
|
|
||||||
require 'JSMin.php';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compress Javascript using Ryan Grove's JSMin class
|
|
||||||
*
|
|
||||||
* @package Minify
|
|
||||||
* @author Stephen Clay <steve@mrclay.org>
|
|
||||||
*/
|
|
||||||
class Minify_Javascript {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minify a Javascript string
|
|
||||||
*
|
|
||||||
* @param string $js
|
|
||||||
*
|
|
||||||
* @param array $options available options (none currently)
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function minify($js, $options = array())
|
|
||||||
{
|
|
||||||
return trim(JSMin::minify($js));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@@ -7,9 +7,9 @@
|
|||||||
* include_path).
|
* include_path).
|
||||||
* @link http://joliclic.free.fr/php/javascript-packer/en/
|
* @link http://joliclic.free.fr/php/javascript-packer/en/
|
||||||
*
|
*
|
||||||
* Be aware that, as long as HTTP encoding is used, scripts minified
|
* Be aware that, as long as HTTP encoding is used, scripts minified with JSMin
|
||||||
* with Minify_Javascript (JSMin) will provide better client-side
|
* will provide better client-side performance, as they need not be unpacked in
|
||||||
* performance, as they need not be unpacked in client-side code.
|
* client-side code.
|
||||||
*
|
*
|
||||||
* @package Minify
|
* @package Minify
|
||||||
*/
|
*/
|
||||||
|
@@ -8,7 +8,7 @@ if (isset($_FILES['subject']['name'])
|
|||||||
// easier to just require them all
|
// easier to just require them all
|
||||||
require 'Minify/HTML.php';
|
require 'Minify/HTML.php';
|
||||||
require 'Minify/CSS.php';
|
require 'Minify/CSS.php';
|
||||||
require 'Minify/Javascript.php';
|
require 'JSMin.php';
|
||||||
|
|
||||||
$arg2 = null;
|
$arg2 = null;
|
||||||
switch ($m[1]) {
|
switch ($m[1]) {
|
||||||
@@ -24,7 +24,7 @@ if (isset($_FILES['subject']['name'])
|
|||||||
$type = 'HTML';
|
$type = 'HTML';
|
||||||
$arg2 = array(
|
$arg2 = array(
|
||||||
'cssMinifier' => array('Minify_CSS', 'minify')
|
'cssMinifier' => array('Minify_CSS', 'minify')
|
||||||
,'jsMinifier' => array('Minify_Javascript', 'minify')
|
,'jsMinifier' => array('JSMin', 'minify')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$func = array('Minify_' . $type, 'minify');
|
$func = array('Minify_' . $type, 'minify');
|
||||||
|
63
min_unit_tests/test_JSMin.php
Normal file
63
min_unit_tests/test_JSMin.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
require_once '_inc.php';
|
||||||
|
|
||||||
|
require_once 'JSMin.php';
|
||||||
|
|
||||||
|
function test_JSMin()
|
||||||
|
{
|
||||||
|
global $thisDir;
|
||||||
|
|
||||||
|
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
||||||
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
|
||||||
|
$minOutput = JSMin::minify($src);
|
||||||
|
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'JSMin : Overall');
|
||||||
|
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$src = file_get_contents($thisDir . '/_test_files/js/issue74.js');
|
||||||
|
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js');
|
||||||
|
$minOutput = JSMin::minify($src);
|
||||||
|
|
||||||
|
$passed = assertTrue($minExpected == $minOutput, 'JSMin : Quotes in RegExp literals (Issue 74)');
|
||||||
|
|
||||||
|
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
||||||
|
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
||||||
|
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
||||||
|
|
||||||
|
test_JSMin_exception('"Hello'
|
||||||
|
,'Unterminated String'
|
||||||
|
,'JSMin_UnterminatedStringException'
|
||||||
|
,"Unterminated String: '\"Hello'");
|
||||||
|
test_JSMin_exception("return /regexp\n}"
|
||||||
|
,'Unterminated RegExp'
|
||||||
|
,'JSMin_UnterminatedRegExpException'
|
||||||
|
,"Unterminated RegExp: '/regexp\n'");
|
||||||
|
test_JSMin_exception("/* Comment "
|
||||||
|
,'Unterminated Comment'
|
||||||
|
,'JSMin_UnterminatedCommentException'
|
||||||
|
,"Unterminated Comment: '/* Comment '");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_JSMin_exception($js, $label, $expClass, $expMessage) {
|
||||||
|
$eClass = $eMsg = '';
|
||||||
|
try {
|
||||||
|
JSMin::minify($js);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$eClass = get_class($e);
|
||||||
|
$eMsg = $e->getMessage();
|
||||||
|
}
|
||||||
|
$passed = assertTrue($eClass === $expClass && $eMsg === $expMessage,
|
||||||
|
'JSMin : throw on ' . $label);
|
||||||
|
if (! $passed && __FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||||
|
echo "\n ---" , $e, "\n\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test_JSMin();
|
@@ -63,7 +63,7 @@ function test_Minify()
|
|||||||
$expected = array(
|
$expected = array(
|
||||||
'success' => true
|
'success' => true
|
||||||
,'statusCode' => 200
|
,'statusCode' => 200
|
||||||
// Minify_Javascript always converts to \n line endings
|
// JSMin always converts to \n line endings
|
||||||
,'content' => $content
|
,'content' => $content
|
||||||
,'headers' => array (
|
,'headers' => array (
|
||||||
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
|
||||||
|
@@ -3,7 +3,7 @@ require_once '_inc.php';
|
|||||||
|
|
||||||
require_once 'Minify/HTML.php';
|
require_once 'Minify/HTML.php';
|
||||||
require_once 'Minify/CSS.php';
|
require_once 'Minify/CSS.php';
|
||||||
require_once 'Minify/Javascript.php';
|
require_once 'JSMin.php';
|
||||||
|
|
||||||
function test_HTML()
|
function test_HTML()
|
||||||
{
|
{
|
||||||
@@ -15,7 +15,7 @@ function test_HTML()
|
|||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
$minOutput = Minify_HTML::minify($src, array(
|
$minOutput = Minify_HTML::minify($src, array(
|
||||||
'cssMinifier' => array('Minify_CSS', 'minify')
|
'cssMinifier' => array('Minify_CSS', 'minify')
|
||||||
,'jsMinifier' => array('Minify_Javascript', 'minify')
|
,'jsMinifier' => array('JSMin', 'minify')
|
||||||
));
|
));
|
||||||
$time = microtime(true) - $time;
|
$time = microtime(true) - $time;
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ function test_HTML()
|
|||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
$minOutput = Minify_HTML::minify($src, array(
|
$minOutput = Minify_HTML::minify($src, array(
|
||||||
'cssMinifier' => array('Minify_CSS', 'minify')
|
'cssMinifier' => array('Minify_CSS', 'minify')
|
||||||
,'jsMinifier' => array('Minify_Javascript', 'minify')
|
,'jsMinifier' => array('JSMin', 'minify')
|
||||||
));
|
));
|
||||||
$time = microtime(true) - $time;
|
$time = microtime(true) - $time;
|
||||||
|
|
||||||
|
@@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once '_inc.php';
|
|
||||||
|
|
||||||
require_once 'Minify/Javascript.php';
|
|
||||||
|
|
||||||
function test_Javascript()
|
|
||||||
{
|
|
||||||
global $thisDir;
|
|
||||||
|
|
||||||
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
|
||||||
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
|
|
||||||
$minOutput = Minify_Javascript::minify($src);
|
|
||||||
|
|
||||||
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
|
|
||||||
|
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
|
||||||
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
|
||||||
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
|
||||||
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
$src = file_get_contents($thisDir . '/_test_files/js/issue74.js');
|
|
||||||
$minExpected = file_get_contents($thisDir . '/_test_files/js/issue74.min.js');
|
|
||||||
$minOutput = Minify_Javascript::minify($src);
|
|
||||||
|
|
||||||
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript : Quotes in RegExp literals (Issue 74)');
|
|
||||||
|
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
|
||||||
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
|
||||||
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
|
||||||
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test_Javascript();
|
|
@@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once '_inc.php';
|
|
||||||
|
|
||||||
require_once 'MyMin.php';
|
|
||||||
|
|
||||||
function test_MyMin()
|
|
||||||
{
|
|
||||||
global $thisDir;
|
|
||||||
|
|
||||||
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
|
|
||||||
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
|
|
||||||
$minOutput = MyMin::parse($src);
|
|
||||||
|
|
||||||
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
|
|
||||||
|
|
||||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
|
||||||
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
|
|
||||||
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
|
|
||||||
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test_MyMin();
|
|
@@ -10,8 +10,8 @@ require 'test_Minify_CSS_UriRewriter.php';
|
|||||||
require 'test_Minify_CommentPreserver.php';
|
require 'test_Minify_CommentPreserver.php';
|
||||||
require 'test_Minify_HTML.php';
|
require 'test_Minify_HTML.php';
|
||||||
require 'test_Minify_ImportProcessor.php';
|
require 'test_Minify_ImportProcessor.php';
|
||||||
require 'test_Minify_Javascript.php';
|
|
||||||
require 'test_Minify_Lines.php';
|
require 'test_Minify_Lines.php';
|
||||||
require 'test_HTTP_Encoder.php';
|
require 'test_HTTP_Encoder.php';
|
||||||
require 'test_HTTP_ConditionalGet.php';
|
require 'test_HTTP_ConditionalGet.php';
|
||||||
|
require 'test_JSMin.php';
|
||||||
require 'test_environment.php';
|
require 'test_environment.php';
|
||||||
|
Reference in New Issue
Block a user