protected function JSqueeze::extractClosures in Minify JS 7
1 call to JSqueeze::extractClosures()
- JSqueeze::squeeze in lib/
JSqueeze.php - Squeezes a JavaScript source code.
File
- lib/
JSqueeze.php, line 533
Class
Code
protected function extractClosures($code) {
$code = ';' . $code;
$this->argFreq[-1] += substr_count($code, '}catch(');
if ($this->argFreq[-1]) {
// Special catch scope handling
// FIXME: this implementation doesn't work with nested catch scopes who need
// access to their parent's caught variable (but who needs that?).
$f = preg_split("@}catch\\(({$this->varRx})@", $code, -1, PREG_SPLIT_DELIM_CAPTURE);
$code = 'catch$scope$var' . mt_rand();
$this->specialVarRx = $this->specialVarRx ? '(?:' . $this->specialVarRx . '|' . preg_quote($code) . ')' : preg_quote($code);
$i = count($f) - 1;
while ($i) {
$c = 1;
$j = 0;
$l = strlen($f[$i]);
while ($c && $j < $l) {
$s = $f[$i][$j++];
$c += '(' == $s ? 1 : (')' == $s ? -1 : 0);
}
if (!$c) {
do {
$s = $f[$i][$j++];
$c += '{' == $s ? 1 : ('}' == $s ? -1 : 0);
} while ($c && $j < $l);
}
$c = preg_quote($f[$i - 1], '#');
$f[$i - 2] .= '}catch(' . preg_replace("#([.,{]?)(?<![a-zA-Z0-9_\$@]){$c}\\b#", '$1' . $code, $f[$i - 1] . substr($f[$i], 0, $j)) . substr($f[$i], $j);
unset($f[$i--], $f[$i--]);
}
$code = $f[0];
}
$f = preg_split("'(?<![a-zA-Z0-9_\$])((?:function[ (]|get |set ).*?\\{)'", $code, -1, PREG_SPLIT_DELIM_CAPTURE);
$i = count($f) - 1;
$closures = array();
while ($i) {
$c = 1;
$j = 0;
$l = strlen($f[$i]);
while ($c && $j < $l) {
$s = $f[$i][$j++];
$c += '{' == $s ? 1 : ('}' == $s ? -1 : 0);
}
switch (substr($f[$i - 2], -1)) {
default:
if (false !== ($c = strpos($f[$i - 1], ' ', 8))) {
break;
}
case false:
case "\n":
case ';':
case '{':
case '}':
case ')':
case ']':
$c = strpos($f[$i - 1], '(', 4);
}
$l = "//''\"\"#{$i}'";
$code = substr($f[$i - 1], $c);
$closures[$l] = $code . substr($f[$i], 0, $j);
$f[$i - 2] .= substr($f[$i - 1], 0, $c) . $l . substr($f[$i], $j);
if ('(){' !== $code) {
$j = substr_count($code, ',');
do {
isset($this->argFreq[$j]) ? ++$this->argFreq[$j] : ($this->argFreq[$j] = 1);
} while ($j--);
}
$i -= 2;
}
return array(
$f[0],
$closures,
);
}