| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Minify_Lines |
| 4 |
* @package Minify |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* Add line numbers in C-style comments for easier debugging of combined content |
| 9 |
* |
| 10 |
* @package Minify |
| 11 |
* @author Stephen Clay <[email protected]> |
| 12 |
* @author Adam Pedersen (Issue 55 fix) |
| 13 |
*/ |
| 14 |
class Minify_Lines |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* Add line numbers in C-style comments |
| 19 |
* |
| 20 |
* This uses a very basic parser easily fooled by comment tokens inside |
| 21 |
* strings or regexes, but, otherwise, generally clean code will not be |
| 22 |
* mangled. URI rewriting can also be performed. |
| 23 |
* |
| 24 |
* @param string $content |
| 25 |
* |
| 26 |
* @param array $options available options: |
| 27 |
* |
| 28 |
* 'id': (optional) string to identify file. E.g. file name/path |
| 29 |
* |
| 30 |
* 'currentDir': (default null) if given, this is assumed to be the |
| 31 |
* directory of the current CSS file. Using this, minify will rewrite |
| 32 |
* all relative URIs in import/url declarations to correctly point to |
| 33 |
* the desired files, and prepend a comment with debugging information about |
| 34 |
* this process. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public static function minify($content, $options = array()) |
| 39 |
{ |
| 40 |
$id = (isset($options['id']) && $options['id']) ? $options['id'] : ''; |
| 41 |
$content = str_replace("\r\n", "\n", $content); |
| 42 |
|
| 43 |
$lines = explode("\n", $content); |
| 44 |
$numLines = count($lines); |
| 45 |
// determine left padding |
| 46 |
$padTo = strlen((string) $numLines); // e.g. 103 lines = 3 digits |
| 47 |
$inComment = false; |
| 48 |
$i = 0; |
| 49 |
$newLines = array(); |
| 50 |
|
| 51 |
while (null !== ($line = array_shift($lines))) { |
| 52 |
if (('' !== $id) && (0 === $i % 50)) { |
| 53 |
if ($inComment) { |
| 54 |
array_push($newLines, '', "/* {$id} *|", ''); |
| 55 |
} else { |
| 56 |
array_push($newLines, '', "/* {$id} */", ''); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
++$i; |
| 61 |
$newLines[] = self::_addNote($line, $i, $inComment, $padTo); |
| 62 |
$inComment = self::_eolInComment($line, $inComment); |
| 63 |
} |
| 64 |
|
| 65 |
$content = implode("\n", $newLines) . "\n"; |
| 66 |
|
| 67 |
// check for desired URI rewriting |
| 68 |
if (isset($options['currentDir'])) { |
| 69 |
Minify_CSS_UriRewriter::$debugText = ''; |
| 70 |
$docRoot = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']; |
| 71 |
$symlinks = isset($options['symlinks']) ? $options['symlinks'] : array(); |
| 72 |
|
| 73 |
$content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], $docRoot, $symlinks); |
| 74 |
|
| 75 |
$content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" |
| 76 |
. Minify_CSS_UriRewriter::$debugText . "*/\n" |
| 77 |
. $content; |
| 78 |
} |
| 79 |
|
| 80 |
return $content; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Is the parser within a C-style comment at the end of this line? |
| 85 |
* |
| 86 |
* @param string $line current line of code |
| 87 |
* |
| 88 |
* @param bool $inComment was the parser in a C-style comment at the |
| 89 |
* beginning of the previous line? |
| 90 |
* |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
private static function _eolInComment($line, $inComment) |
| 94 |
{ |
| 95 |
while (strlen($line)) { |
| 96 |
if ($inComment) { |
| 97 |
// only "*/" can end the comment |
| 98 |
$index = self::_find($line, '*/'); |
| 99 |
if ($index === false) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
// stop comment and keep walking line |
| 104 |
$inComment = false; |
| 105 |
@$line = (string)substr($line, $index + 2); |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
// look for "//" and "/*" |
| 110 |
$single = self::_find($line, '//'); |
| 111 |
$multi = self::_find($line, '/*'); |
| 112 |
if ($multi === false) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
if ($single === false || $multi < $single) { |
| 117 |
// start comment and keep walking line |
| 118 |
$inComment = true; |
| 119 |
@$line = (string)substr($line, $multi + 2); |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
// a single-line comment preceeded it |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
return $inComment; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Prepend a comment (or note) to the given line |
| 132 |
* |
| 133 |
* @param string $line current line of code |
| 134 |
* |
| 135 |
* @param string $note content of note/comment |
| 136 |
* |
| 137 |
* @param bool $inComment was the parser in a comment at the |
| 138 |
* beginning of the line? |
| 139 |
* |
| 140 |
* @param int $padTo minimum width of comment |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
private static function _addNote($line, $note, $inComment, $padTo) |
| 145 |
{ |
| 146 |
if ($inComment) { |
| 147 |
$line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line; |
| 148 |
} else { |
| 149 |
$line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line; |
| 150 |
} |
| 151 |
|
| 152 |
return rtrim($line); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Find a token trying to avoid false positives |
| 157 |
* |
| 158 |
* @param string $str String containing the token |
| 159 |
* @param string $token Token being checked |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
private static function _find($str, $token) |
| 163 |
{ |
| 164 |
switch ($token) { |
| 165 |
case '//': |
| 166 |
$fakes = array( |
| 167 |
'://' => 1, |
| 168 |
'"//' => 1, |
| 169 |
'\'//' => 1, |
| 170 |
'".//' => 2, |
| 171 |
'\'.//' => 2, |
| 172 |
); |
| 173 |
break; |
| 174 |
case '/*': |
| 175 |
$fakes = array( |
| 176 |
'"/*' => 1, |
| 177 |
'\'/*' => 1, |
| 178 |
'"//*' => 2, |
| 179 |
'\'//*' => 2, |
| 180 |
'".//*' => 3, |
| 181 |
'\'.//*' => 3, |
| 182 |
'*/*' => 1, |
| 183 |
'\\/*' => 1, |
| 184 |
); |
| 185 |
break; |
| 186 |
default: |
| 187 |
$fakes = array(); |
| 188 |
} |
| 189 |
|
| 190 |
$index = strpos($str, $token); |
| 191 |
$offset = 0; |
| 192 |
|
| 193 |
while ($index !== false) { |
| 194 |
foreach ($fakes as $fake => $skip) { |
| 195 |
$check = substr($str, $index - $skip, strlen($fake)); |
| 196 |
if ($check === $fake) { |
| 197 |
// move offset and scan again |
| 198 |
$offset += $index + strlen($token); |
| 199 |
$index = strpos($str, $token, $offset); |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
// legitimate find |
| 204 |
return $index; |
| 205 |
} |
| 206 |
|
| 207 |
return $index; |
| 208 |
} |
| 209 |
} |
| 210 |
|