diff --git a/min/builder/_index.js b/min/builder/_index.js index 90c3afd..cf40a3e 100644 --- a/min/builder/_index.js +++ b/min/builder/_index.js @@ -1,3 +1,6 @@ +/*! + * Minify URI Builder + */ var MUB = { _uid : 0 ,_minRoot : '/min/?' diff --git a/min/builder/bm2.js b/min/builder/bm2.js index 236402e..db92172 100644 --- a/min/builder/bm2.js +++ b/min/builder/bm2.js @@ -3,10 +3,11 @@ javascript:(function(){ ,c = d.cookie ,m = c.match(/\bminDebug=([^; ]+)/) ,v = m ? decodeURIComponent(m[1]) : '' - ,p = prompt('Debug Minify URIs on "' + location.hostname + '" \ncontaining: (empty to disable)', v) + ,p = prompt('Debug Minify URIs on ' + location.hostname + ' which contain:' + + '\n(empty for none, space = OR)', v) ; if (p === null) return; - p = p.replace(/\s+/g, ''); + p = p.replace(/^\s+|\s+$/, ''); v = (p === '') ? 'minDebug=; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/' : 'minDebug=' + encodeURIComponent(p) + '; path=/'; diff --git a/min/index.php b/min/index.php index 352313f..4b295ba 100644 --- a/min/index.php +++ b/min/index.php @@ -36,9 +36,13 @@ foreach ($min_symlinks as $uri => $target) { } if ($min_allowDebugFlag) { - if (! empty($_COOKIE['minDebug']) - && false !== strpos($_SERVER['REQUEST_URI'], $_COOKIE['minDebug'])) { - $min_serveOptions['debug'] = true; + if (! empty($_COOKIE['minDebug'])) { + foreach (preg_split('/\\s+/', $_COOKIE['minDebug']) as $debugUri) { + if (false !== strpos($_SERVER['REQUEST_URI'], $debugUri)) { + $min_serveOptions['debug'] = true; + break; + } + } } // allow GET to override if (isset($_GET['debug'])) { diff --git a/min/lib/HTTP/Encoder.php b/min/lib/HTTP/Encoder.php index 4c02fc9..49bedd7 100644 --- a/min/lib/HTTP/Encoder.php +++ b/min/lib/HTTP/Encoder.php @@ -90,8 +90,13 @@ class HTTP_Encoder { */ public function __construct($spec) { + $this->_useMbStrlen = (function_exists('mb_strlen') + && (ini_get('mbstring.func_overload') !== '') + && ((int)ini_get('mbstring.func_overload') & 2)); $this->_content = $spec['content']; - $this->_headers['Content-Length'] = (string)strlen($this->_content); + $this->_headers['Content-Length'] = $this->_useMbStrlen + ? (string)mb_strlen($this->_content, '8bit') + : (string)strlen($this->_content); if (isset($spec['type'])) { $this->_headers['Content-Type'] = $spec['type']; } @@ -275,7 +280,9 @@ class HTTP_Encoder { if (false === $encoded) { return false; } - $this->_headers['Content-Length'] = strlen($encoded); + $this->_headers['Content-Length'] = $this->_useMbStrlen + ? (string)mb_strlen($encoded, '8bit') + : (string)strlen($encoded); $this->_headers['Content-Encoding'] = $this->_encodeMethod[1]; $this->_content = $encoded; return true; @@ -327,4 +334,5 @@ class HTTP_Encoder { protected $_content = ''; protected $_headers = array(); protected $_encodeMethod = array('', ''); + protected $_useMbStrlen = false; } diff --git a/min/lib/JSMin.php b/min/lib/JSMin.php index 288a0e2..ae4ff92 100644 --- a/min/lib/JSMin.php +++ b/min/lib/JSMin.php @@ -84,16 +84,6 @@ class JSMin { // likely pre-minified and would be broken by JSMin return $js; } - if (function_exists('mb_strlen') - && (ini_get('mbstring.func_overload') !== '') - && ((int)ini_get('mbstring.func_overload') & 2)) { - $enc = mb_internal_encoding(); - mb_internal_encoding('8bit'); - $jsmin = new JSMin($js); - $ret = $jsmin->min(); - mb_internal_encoding($enc); - return $ret; - } $jsmin = new JSMin($js); return $jsmin->min(); } @@ -107,8 +97,7 @@ class JSMin { */ public function __construct($input) { - $this->input = str_replace("\r\n", "\n", $input); - $this->inputLength = strlen($this->input); + $this->input = $input; } /** @@ -119,6 +108,15 @@ class JSMin { if ($this->output !== '') { // min already run return $this->output; } + + $mbIntEnc = null; + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + $mbIntEnc = mb_internal_encoding(); + mb_internal_encoding('8bit'); + } + $this->input = str_replace("\r\n", "\n", $this->input); + $this->inputLength = strlen($this->input); + $this->action(self::ACTION_DELETE_A_B); while ($this->a !== null) { @@ -131,8 +129,11 @@ class JSMin { } elseif ($this->a === "\n") { if ($this->b === ' ') { $command = self::ACTION_DELETE_A_B; - } elseif (false === strpos('{[(+-', $this->b) - && ! $this->isAlphaNum($this->b)) { + // in case of mbstring.func_overload & 2, must check for null b, + // otherwise mb_strpos will give WARNING + } elseif ($this->b === null + || (false === strpos('{[(+-', $this->b) + && ! $this->isAlphaNum($this->b))) { $command = self::ACTION_DELETE_A; } } elseif (! $this->isAlphaNum($this->a)) { @@ -145,6 +146,10 @@ class JSMin { $this->action($command); } $this->output = trim($this->output); + + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return $this->output; } diff --git a/min/lib/Minify.php b/min/lib/Minify.php index 5cb2405..fabde72 100644 --- a/min/lib/Minify.php +++ b/min/lib/Minify.php @@ -29,7 +29,7 @@ require_once 'Minify/Source.php'; */ class Minify { - const VERSION = '2.1.3'; + const VERSION = '2.1.4'; const TYPE_CSS = 'text/css'; const TYPE_HTML = 'text/html'; // there is some debate over the ideal JS Content-Type, but this is the @@ -312,12 +312,9 @@ class Minify { } // add headers - $hasMbOverload = (function_exists('mb_strlen') - && (ini_get('mbstring.func_overload') !== '') - && ((int)ini_get('mbstring.func_overload') & 2)); $headers['Content-Length'] = $cacheIsReady ? $cacheContentLength - : ($hasMbOverload + : ((function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) ? mb_strlen($content, '8bit') : strlen($content) ); diff --git a/min/lib/Minify/Cache/APC.php b/min/lib/Minify/Cache/APC.php index ca84d29..24ab046 100644 --- a/min/lib/Minify/Cache/APC.php +++ b/min/lib/Minify/Cache/APC.php @@ -54,9 +54,12 @@ class Minify_Cache_APC { */ public function getSize($id) { - return $this->_fetch($id) - ? strlen($this->_data) - : false; + if (! $this->_fetch($id)) { + return false; + } + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) + ? mb_strlen($this->_data, '8bit') + : strlen($this->_data); } /** diff --git a/min/lib/Minify/Cache/Memcache.php b/min/lib/Minify/Cache/Memcache.php index 2b81e7a..72bf454 100644 --- a/min/lib/Minify/Cache/Memcache.php +++ b/min/lib/Minify/Cache/Memcache.php @@ -60,9 +60,12 @@ class Minify_Cache_Memcache { */ public function getSize($id) { - return $this->_fetch($id) - ? strlen($this->_data) - : false; + if (! $this->_fetch($id)) { + return false; + } + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) + ? mb_strlen($this->_data, '8bit') + : strlen($this->_data); } /** diff --git a/min/lib/Minify/Controller/Version1.php b/min/lib/Minify/Controller/Version1.php index 1861aab..5279d36 100644 --- a/min/lib/Minify/Controller/Version1.php +++ b/min/lib/Minify/Controller/Version1.php @@ -7,7 +7,7 @@ require_once 'Minify/Controller/Base.php'; /** - * Controller class for emulating version 1 of minify.php + * Controller class for emulating version 1 of minify.php (mostly a proof-of-concept) * * * Minify::serve('Version1'); diff --git a/min_unit_tests/_inc.php b/min_unit_tests/_inc.php index bf416d4..711009a 100644 --- a/min_unit_tests/_inc.php +++ b/min_unit_tests/_inc.php @@ -24,7 +24,7 @@ if ($min_errorLogger && true !== $min_errorLogger) { // custom logger error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1); -header('Content-Type: text/plain'); +header('Content-Type: text/plain;charset=utf-8'); $thisDir = dirname(__FILE__); @@ -47,4 +47,17 @@ function assertTrue($test, $message) return (bool)$test; } +/** + * Get number of bytes in a string regardless of mbstring.func_overload + * + * @param string $str + * @return int + */ +function countBytes($str) +{ + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) + ? mb_strlen($str, '8bit') + : strlen($str); +} + ob_start(); \ No newline at end of file diff --git a/min_unit_tests/_test_files/js/issue132.js b/min_unit_tests/_test_files/js/issue132.js new file mode 100644 index 0000000..4ccfb15 --- /dev/null +++ b/min_unit_tests/_test_files/js/issue132.js @@ -0,0 +1,7 @@ +// from jQuery tablesorter +ts.addParser({ + id: "currency", + is: function(s) { + return /^[£$€?.]/.test(s); + }, +}); diff --git a/min_unit_tests/_test_files/js/issue132.min.js b/min_unit_tests/_test_files/js/issue132.min.js new file mode 100644 index 0000000..7215187 --- /dev/null +++ b/min_unit_tests/_test_files/js/issue132.min.js @@ -0,0 +1 @@ +ts.addParser({id:"currency",is:function(s){return /^[£$€?.]/.test(s);},}); \ No newline at end of file diff --git a/min_unit_tests/_test_files/minify/QueryString.js b/min_unit_tests/_test_files/minify/QueryString.js index e56a244..d926d64 100644 --- a/min_unit_tests/_test_files/minify/QueryString.js +++ b/min_unit_tests/_test_files/minify/QueryString.js @@ -1,4 +1,4 @@ -var MrClay = window.MrClay || {}; +var MrClay = window.MrClay || {}; /** * Simplified access to/manipulation of the query string diff --git a/min_unit_tests/_test_files/minify/email.js b/min_unit_tests/_test_files/minify/email.js index 761062d..b725379 100644 --- a/min_unit_tests/_test_files/minify/email.js +++ b/min_unit_tests/_test_files/minify/email.js @@ -1,4 +1,4 @@ -// http://mrclay.org/ +// http://mrclay.org/ (function(){ var reMailto = /^mailto:my_name_is_(\S+)_and_the_domain_is_(\S+)$/, diff --git a/min_unit_tests/test_HTTP_Encoder.php b/min_unit_tests/test_HTTP_Encoder.php index 106d146..665bdd2 100644 --- a/min_unit_tests/test_HTTP_Encoder.php +++ b/min_unit_tests/test_HTTP_Encoder.php @@ -94,10 +94,11 @@ function test_HTTP_Encoder() } // test compression of varied content (HTML,JS, & CSS) + $variedContent = file_get_contents($thisDir . '/_test_files/html/before.html') . file_get_contents($thisDir . '/_test_files/css/subsilver.css') . file_get_contents($thisDir . '/_test_files/js/jquery-1.2.3.js'); - $variedLength = strlen($variedContent); + $variedLength = countBytes($variedContent); $encodingTests = array( array('method' => 'deflate', 'inv' => 'gzinflate', 'exp' => 32157) @@ -111,7 +112,7 @@ function test_HTTP_Encoder() ,'method' => $test['method'] )); $e->encode(9); - $ret = strlen($e->getContent()); + $ret = countBytes($e->getContent()); // test uncompression $roundTrip = @call_user_func($test['inv'], $e->getContent()); @@ -154,15 +155,31 @@ function _gzdecode($data) // http://www.php.net/manual/en/function.gzdecode.php#82930 function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) { + $mbIntEnc = null; + $hasMbOverload = (function_exists('mb_strlen') + && (ini_get('mbstring.func_overload') !== '') + && ((int)ini_get('mbstring.func_overload') & 2)); + if ($hasMbOverload) { + $mbIntEnc = mb_internal_encoding(); + mb_internal_encoding('8bit'); + } + + $len = strlen($data); if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) { $error = "Not in GZIP format."; + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return null; // Not GZIP format (See RFC 1952) } $method = ord(substr($data,2,1)); // Compression method $flags = ord(substr($data,3,1)); // Flags if ($flags & 31 != $flags) { $error = "Reserved bits not allowed."; + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return null; } // NOTE: $mtime may be negative (PHP integer limitations) @@ -176,11 +193,17 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) if ($flags & 4) { // 2-byte length prefixed EXTRA data in header if ($len - $headerlen - 2 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $extralen = unpack("v",substr($data,8,2)); $extralen = $extralen[1]; if ($len - $headerlen - 2 - $extralen < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $extra = substr($data,10,$extralen); @@ -191,10 +214,16 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) if ($flags & 8) { // C-style string if ($len - $headerlen - 1 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $filenamelen = strpos(substr($data,$headerlen),chr(0)); if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $filename = substr($data,$headerlen,$filenamelen); @@ -205,10 +234,16 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) if ($flags & 16) { // C-style string COMMENT data in header if ($len - $headerlen - 1 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $commentlen = strpos(substr($data,$headerlen),chr(0)); if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // Invalid header format } $comment = substr($data,$headerlen,$commentlen); @@ -218,6 +253,9 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) if ($flags & 2) { // 2-bytes (lowest order) of CRC32 on header present if ($len - $headerlen - 2 < 8) { + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // invalid } $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; @@ -225,6 +263,9 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) $headercrc = $headercrc[1]; if ($headercrc != $calccrc) { $error = "Header checksum failed."; + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; // Bad header CRC } $headerlen += 2; @@ -238,6 +279,9 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) $bodylen = $len-$headerlen-8; if ($bodylen < 1) { // IMPLEMENTATION BUG! + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return null; } $body = substr($data,$headerlen,$bodylen); @@ -250,6 +294,9 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) break; default: $error = "Unknown compression method."; + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } return false; } } // zero-byte body content is allowed @@ -259,7 +306,11 @@ function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null) $lenOK = $isize == strlen($data); if (!$lenOK || !$crcOK) { $error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.'); - return false; + $ret = false; } - return $data; + $ret = $data; + if ($mbIntEnc !== null) { + mb_internal_encoding($mbIntEnc); + } + return $ret; } \ No newline at end of file diff --git a/min_unit_tests/test_JSMin.php b/min_unit_tests/test_JSMin.php index 43a8302..ef3a978 100644 --- a/min_unit_tests/test_JSMin.php +++ b/min_unit_tests/test_JSMin.php @@ -10,45 +10,52 @@ function test_JSMin() $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"; + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; } $src = file_get_contents($thisDir . '/_test_files/js/issue144.js'); $minExpected = file_get_contents($thisDir . '/_test_files/js/issue144.min.js'); $minOutput = JSMin::minify($src); - $passed = assertTrue($minExpected == $minOutput, 'JSMin : Don\'t minify files with + ++ (Issue 144)'); + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + $src = file_get_contents($thisDir . '/_test_files/js/issue132.js'); + $minExpected = file_get_contents($thisDir . '/_test_files/js/issue132.min.js'); + $minOutput = JSMin::minify($src); + $passed = assertTrue($minExpected == $minOutput, 'JSMin : mbstring.func_overload shouldn\'t cause failure (Issue 132)'); + if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($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"; - + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; + + // only test exceptions on this page test_JSMin_exception('"Hello' ,'Unterminated String' ,'JSMin_UnterminatedStringException' - ,"Unterminated String: \"Hello"); + ,"JSMin: Unterminated String at byte 6: \"Hello"); test_JSMin_exception("return /regexp\n}" ,'Unterminated RegExp' ,'JSMin_UnterminatedRegExpException' - ,"Unterminated RegExp: /regexp\n"); + ,"JSMin: Unterminated RegExp at byte 15: /regexp\n"); test_JSMin_exception("/* Comment " ,'Unterminated Comment' ,'JSMin_UnterminatedCommentException' - ,"Unterminated Comment: /* Comment "); + ,"JSMin: Unterminated comment at byte 11: /* Comment "); } } diff --git a/min_unit_tests/test_JSMinPlus.php b/min_unit_tests/test_JSMinPlus.php index eaae8bd..880f9eb 100644 --- a/min_unit_tests/test_JSMinPlus.php +++ b/min_unit_tests/test_JSMinPlus.php @@ -15,9 +15,9 @@ function test_JSMinPlus() $passed = assertTrue($minExpected == $minOutput, 'JSMinPlus : Conditional Comments'); 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"; + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; } return; @@ -30,9 +30,9 @@ function test_JSMinPlus() $passed = assertTrue($minExpected == $minOutput, 'JSMinPlus : 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"; + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; } $src = file_get_contents($thisDir . '/_test_files/js/issue74.js'); @@ -42,39 +42,10 @@ function test_JSMinPlus() $passed = assertTrue($minExpected == $minOutput, 'JSMinPlus : 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 '"); - //*/ + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; } } -/*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_JSMinPlus(); diff --git a/min_unit_tests/test_Minify.php b/min_unit_tests/test_Minify.php index 95e5d64..f45be60 100644 --- a/min_unit_tests/test_Minify.php +++ b/min_unit_tests/test_Minify.php @@ -71,7 +71,7 @@ function test_Minify() 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified), 'ETag' => "\"pub{$lastModified}\"", 'Cache-Control' => 'max-age=86400', - 'Content-Length' => strlen($content), + 'Content-Length' => countBytes($content), 'Content-Type' => 'application/x-javascript; charset=utf-8', ) ); @@ -170,9 +170,17 @@ function test_Minify() } } - // Test minifying CSS and responding with Etag/Last-Modified + // Test Issue 132 + if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { + $output = Minify::serve('Files', array( + 'files' => array(dirname(__FILE__) . '/_test_files/js/issue132.js') + ,'quiet' => true + ,'encodeOutput' => false + )); + $passed = assertTrue($output['headers']['Content-Length'] == 77, 'Minify : Issue 132 : mbstring.func_overload shouldn\'t cause incorrect Content-Length'); + } - Minify::setCache(null); + // Test minifying CSS and responding with Etag/Last-Modified // don't allow conditional headers unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']); @@ -188,7 +196,7 @@ function test_Minify() 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified), 'ETag' => "\"pub{$lastModified}\"", 'Cache-Control' => 'max-age=0', - 'Content-Length' => strlen($expectedContent), + 'Content-Length' => countBytes($expectedContent), 'Content-Type' => 'text/css; charset=utf-8', ) ); diff --git a/min_unit_tests/test_Minify_CSS.php b/min_unit_tests/test_Minify_CSS.php index 96e1875..14f53a4 100644 --- a/min_unit_tests/test_Minify_CSS.php +++ b/min_unit_tests/test_Minify_CSS.php @@ -41,10 +41,10 @@ function test_CSS() $passed = assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { - echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n"; + echo "\n---Output: " .countBytes($minOutput). " bytes\n\n{$minOutput}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n"; - echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n"; + echo "---Expected: " .countBytes($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .countBytes($src). " bytes\n\n{$src}\n\n\n"; } } } diff --git a/min_unit_tests/test_Minify_CSS_UriRewriter.php b/min_unit_tests/test_Minify_CSS_UriRewriter.php index 55f09b0..7217a3d 100644 --- a/min_unit_tests/test_Minify_CSS_UriRewriter.php +++ b/min_unit_tests/test_Minify_CSS_UriRewriter.php @@ -20,9 +20,9 @@ function test_Minify_CSS_UriRewriter() $passed = assertTrue($expected === $actual, 'Minify_CSS_UriRewriter'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; - echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n"; + echo "\n---Output: " .countBytes($actual). " bytes\n\n{$actual}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($expected). " bytes\n\n{$expected}\n\n\n"; + echo "---Expected: " .countBytes($expected). " bytes\n\n{$expected}\n\n\n"; } // show debugging only when test run directly @@ -42,9 +42,9 @@ function test_Minify_CSS_UriRewriter() $passed = assertTrue($exp === $actual, 'Minify_CSS_UriRewriter : Issue 99'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n---Input:\n\n{$in}\n"; - echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n"; + echo "\n---Output: " .countBytes($actual). " bytes\n\n{$actual}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($exp). " bytes\n\n{$exp}\n\n\n"; + echo "---Expected: " .countBytes($exp). " bytes\n\n{$exp}\n\n\n"; } // show debugging only when test run directly diff --git a/min_unit_tests/test_Minify_Cache_APC.php b/min_unit_tests/test_Minify_Cache_APC.php index 74a1b2c..1761add 100644 --- a/min_unit_tests/test_Minify_Cache_APC.php +++ b/min_unit_tests/test_Minify_Cache_APC.php @@ -9,14 +9,14 @@ function test_Minify_Cache_APC() if (! function_exists('apc_store')) { return; } - $data = str_repeat(md5('testing'), 160); + $data = str_repeat(md5(time()) . 'í', 100); // 3400 bytes in UTF-8 $id = 'Minify_test_cache'; $cache = new Minify_Cache_APC(); assertTrue(true === $cache->store($id, $data), $prefix . 'store'); - assertTrue(strlen($data) === $cache->getSize($id), $prefix . 'getSize'); + assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize'); assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid'); diff --git a/min_unit_tests/test_Minify_Cache_File.php b/min_unit_tests/test_Minify_Cache_File.php index 2463e5b..00e2c26 100644 --- a/min_unit_tests/test_Minify_Cache_File.php +++ b/min_unit_tests/test_Minify_Cache_File.php @@ -7,7 +7,7 @@ function test_Minify_Cache_File() { global $minifyCachePath; - $data = str_repeat(md5(time()), 160); + $data = str_repeat(md5(time()) . 'í', 100); // 3400 bytes in UTF-8 $id = 'Minify_test_cache_noLock'; $prefix = 'Minify_Cache_File : '; @@ -16,8 +16,8 @@ function test_Minify_Cache_File() echo "NOTE: Minify_Cache_File : path is set to: '" . $cache->getPath() . "'.\n"; assertTrue(true === $cache->store($id, $data), $prefix . 'store'); - - assertTrue(strlen($data) === $cache->getSize($id), $prefix . 'getSize'); + + assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize'); assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid'); @@ -28,26 +28,26 @@ function test_Minify_Cache_File() assertTrue($data === $displayed, $prefix . 'display'); - assertTrue($data === $cache->fetch($id), $prefix . 'fetch'); - - // test with locks - - $id = 'Minify_test_cache_withLock'; - $cache = new Minify_Cache_File($minifyCachePath, true); - - assertTrue(true === $cache->store($id, $data), $prefix . 'store w/ lock'); - - assertTrue(strlen($data) === $cache->getSize($id), $prefix . 'getSize'); - - assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid'); - - ob_start(); - $cache->display($id); - $displayed = ob_get_contents(); - ob_end_clean(); - - assertTrue($data === $displayed, $prefix . 'display w/ lock'); - + assertTrue($data === $cache->fetch($id), $prefix . 'fetch'); + + // test with locks + + $id = 'Minify_test_cache_withLock'; + $cache = new Minify_Cache_File($minifyCachePath, true); + + assertTrue(true === $cache->store($id, $data), $prefix . 'store w/ lock'); + + assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize'); + + assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid'); + + ob_start(); + $cache->display($id); + $displayed = ob_get_contents(); + ob_end_clean(); + + assertTrue($data === $displayed, $prefix . 'display w/ lock'); + assertTrue($data === $cache->fetch($id), $prefix . 'fetch w/ lock'); } diff --git a/min_unit_tests/test_Minify_Cache_Memcache.php b/min_unit_tests/test_Minify_Cache_Memcache.php index b0223aa..a8979c6 100644 --- a/min_unit_tests/test_Minify_Cache_Memcache.php +++ b/min_unit_tests/test_Minify_Cache_Memcache.php @@ -14,14 +14,14 @@ function test_Minify_Cache_Memcache() return; } - $data = str_repeat(md5('testing'), 160); + $data = str_repeat(md5(time()) . 'í', 100); // 3400 bytes in UTF-8 $id = 'Minify_test_cache'; $cache = new Minify_Cache_Memcache($mc); assertTrue(true === $cache->store($id, $data), $prefix . 'store'); - assertTrue(strlen($data) === $cache->getSize($id), $prefix . 'getSize'); + assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize'); assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid'); diff --git a/min_unit_tests/test_Minify_CommentPreserver.php b/min_unit_tests/test_Minify_CommentPreserver.php index e4e0e9f..537a738 100644 --- a/min_unit_tests/test_Minify_CommentPreserver.php +++ b/min_unit_tests/test_Minify_CommentPreserver.php @@ -19,9 +19,9 @@ function test_Minify_CommentPreserver() $actual = Minify_CommentPreserver::process($in, '_test_MCP_processor'); $passed = assertTrue($expected === $actual, 'Minify_CommentPreserver'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { - echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n"; + echo "\n---Output: " .countBytes($actual). " bytes\n\n{$actual}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($expected). " bytes\n\n{$expected}\n\n\n"; + echo "---Expected: " .countBytes($expected). " bytes\n\n{$expected}\n\n\n"; } } } diff --git a/min_unit_tests/test_Minify_HTML.php b/min_unit_tests/test_Minify_HTML.php index 69aeff5..fbc2a02 100644 --- a/min_unit_tests/test_Minify_HTML.php +++ b/min_unit_tests/test_Minify_HTML.php @@ -23,12 +23,12 @@ function test_HTML() if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { if ($passed) { - echo "\n---Source: ", strlen($src), " bytes\n" - , "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n"; + echo "\n---Source: ", countBytes($src), " bytes\n" + , "---Output: ", countBytes($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n"; } else { - echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n" - , "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n" - , "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n"; + echo "\n---Output: ", countBytes($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n" + , "---Expected: ", countBytes($minExpected), " bytes\n\n{$minExpected}\n\n" + , "---Source: ", countBytes($src), " bytes\n\n{$src}\n\n\n"; } } @@ -46,12 +46,12 @@ function test_HTML() if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { if ($passed) { - echo "\n---Source: ", strlen($src), " bytes\n" - , "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n"; + echo "\n---Source: ", countBytes($src), " bytes\n" + , "---Output: ", countBytes($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n"; } else { - echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n" - , "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n" - , "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n"; + echo "\n---Output: ", countBytes($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n" + , "---Expected: ", countBytes($minExpected), " bytes\n\n{$minExpected}\n\n" + , "---Source: ", countBytes($src), " bytes\n\n{$src}\n\n\n"; } } } diff --git a/min_unit_tests/test_Minify_ImportProcessor.php b/min_unit_tests/test_Minify_ImportProcessor.php index 52b19ff..59abfca 100644 --- a/min_unit_tests/test_Minify_ImportProcessor.php +++ b/min_unit_tests/test_Minify_ImportProcessor.php @@ -27,18 +27,18 @@ function test_Minify_ImportProcessor() $passed = assertTrue($expected === $actual, 'ImportProcessor'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { - echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n"; + echo "\n---Output: " .countBytes($actual). " bytes\n\n{$actual}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($expected). " bytes\n\n{$expected}\n\n\n"; + echo "---Expected: " .countBytes($expected). " bytes\n\n{$expected}\n\n\n"; } } - $expectedIncludes = array ( - realpath($linDir . '/input.css') - ,realpath($linDir . '/adjacent.css') - ,realpath($linDir . '/../css/styles.css') - ,realpath($linDir . '/1/tv.css') - ,realpath($linDir . '/1/adjacent.css') + $expectedIncludes = array ( + realpath($linDir . '/input.css') + ,realpath($linDir . '/adjacent.css') + ,realpath($linDir . '/../css/styles.css') + ,realpath($linDir . '/1/tv.css') + ,realpath($linDir . '/1/adjacent.css') ); $passed = assertTrue($expectedIncludes === Minify_ImportProcessor::$filesIncluded diff --git a/min_unit_tests/test_Minify_Lines.php b/min_unit_tests/test_Minify_Lines.php index c518fce..40d531f 100644 --- a/min_unit_tests/test_Minify_Lines.php +++ b/min_unit_tests/test_Minify_Lines.php @@ -26,9 +26,9 @@ function test_Lines() $passed = assertTrue($exp === $ret['content'], 'Minify_Lines'); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { - echo "\n---Output: " .strlen($ret['content']). " bytes\n\n{$ret['content']}\n\n"; + echo "\n---Output: " .countBytes($ret['content']). " bytes\n\n{$ret['content']}\n\n"; if (!$passed) { - echo "---Expected: " .strlen($exp). " bytes\n\n{$exp}\n\n\n"; + echo "---Expected: " .countBytes($exp). " bytes\n\n{$exp}\n\n\n"; } } }