1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-10 16:14:18 +02:00

Now substring and str_slice methods match exactly their Javascript counterparts behavior.

Minor cleaning/formatting.
Tabs to spaces (Clay seems to use spaces instead of tabs)
This commit is contained in:
tubalmartin
2012-03-16 20:48:40 +01:00
parent ff45c76484
commit 73dd77569e

View File

@@ -13,18 +13,18 @@
*/ */
/*! /*!
* YUI Compressor * YUI Compressor
* http://developer.yahoo.com/yui/compressor/ * http://developer.yahoo.com/yui/compressor/
* Author: Julien Lecomte - http://www.julienlecomte.net/ * Author: Julien Lecomte - http://www.julienlecomte.net/
* Copyright (c) 2011 Yahoo! Inc. All rights reserved. * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed * The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license. * by Yahoo! Inc. under the BSD (revised) open source license.
*/ */
class CSSmin class CSSmin
{ {
private $comments = array(); private $comments;
private $preserved_tokens = array(); private $preserved_tokens;
/** /**
* @param bool $raisePhpSettingsLimits if true, raisePhpSettingLimits() will * @param bool $raisePhpSettingsLimits if true, raisePhpSettingLimits() will
@@ -45,12 +45,6 @@ class CSSmin
*/ */
public function run($css, $linebreak_pos = FALSE) public function run($css, $linebreak_pos = FALSE)
{ {
// Try to increase the memory limit for this script
ini_set('memory_limit', '128M');
// Try to increase the PCRE limits
ini_set('pcre.backtrack_limit', 1000 * 1000);
ini_set('pcre.recursion_limit', 500 * 1000);
$this->comments = array(); $this->comments = array();
$this->preserved_tokens = array(); $this->preserved_tokens = array();
@@ -478,7 +472,7 @@ class CSSmin
*/ */
/** /**
* PHP port of Javascript's "indexOf" function * PHP port of Javascript's "indexOf" function for strings only
* Author: Tubal Martin http://blog.margenn.com * Author: Tubal Martin http://blog.margenn.com
* *
* @param string $haystack * @param string $haystack
@@ -496,21 +490,17 @@ class CSSmin
/** /**
* PHP port of Javascript's "substring" function * PHP port of Javascript's "substring" function
* Author: Tubal Martin http://blog.margenn.com * Author: Tubal Martin http://blog.margenn.com
* Tests: http://margenn.com/tubal/substring/
* *
* @param string $str * @param string $str
* @param int $from index * @param int $from index
* @param int|bool $to index (optional) * @param int|bool $to index (optional)
* @return string * @return string
*/ */
private function substring($str, $from, $to = FALSE) private function substring($str, $from = 0, $to = FALSE)
{ {
if ($from < 0) {
$from = 0;
}
if ($to !== FALSE) { if ($to !== FALSE) {
if ($from == $to || ($from <= 0 && $to < 0)) {
if ($from === $to || $to < 0) {
return ''; return '';
} }
@@ -519,18 +509,19 @@ class CSSmin
$from = $to; $from = $to;
$to = $from_copy; $to = $from_copy;
} }
$substring = substr($str, $from, $to - $from);
return ($substring === FALSE) ? '' : $substring;
} }
$substring = substr($str, $from); if ($from < 0) {
$from = 0;
}
$substring = ($to === FALSE) ? substr($str, $from) : substr($str, $from, $to - $from);
return ($substring === FALSE) ? '' : $substring; return ($substring === FALSE) ? '' : $substring;
} }
/** /**
* PHP port of Javascript's "slice" function * PHP port of Javascript's "slice" function for strings only
* Author: Tubal Martin http://blog.margenn.com * Author: Tubal Martin http://blog.margenn.com
* Tests: http://margenn.com/tubal/str_slice/ * Tests: http://margenn.com/tubal/str_slice/
* *
@@ -539,15 +530,9 @@ class CSSmin
* @param int|bool $end index (optional) * @param int|bool $end index (optional)
* @return string * @return string
*/ */
private function str_slice($str, $start, $end = FALSE) private function str_slice($str, $start = 0, $end = FALSE)
{ {
if ($start < 0 || $end <= 0) { if ($end !== FALSE && ($start < 0 || $end <= 0)) {
if ($end === FALSE) {
$slice = substr($str, $start);
return ($slice === FALSE) ? '' : $slice;
}
$max = strlen($str); $max = strlen($str);
if ($start < 0) { if ($start < 0) {
@@ -567,7 +552,7 @@ class CSSmin
} }
} }
$slice = substr($str, $start, $end - $start); $slice = ($end === FALSE) ? substr($str, $start) : substr($str, $start, $end - $start);
return ($slice === FALSE) ? '' : $slice; return ($slice === FALSE) ? '' : $slice;
} }