You are here

protected function JSqueeze::extractClosures in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg_js_minify/jsqueeze.inc \Patchwork\JSqueeze::extractClosures()
  2. 8.2 advagg_js_minify/jsqueeze.inc \Patchwork\JSqueeze::extractClosures()
  3. 7.2 advagg_js_compress/jsqueeze.inc \Patchwork\JSqueeze::extractClosures()
1 call to JSqueeze::extractClosures()
JSqueeze::squeeze in advagg_js_minify/jsqueeze.inc
Squeezes a JavaScript source code.

File

advagg_js_minify/jsqueeze.inc, line 533

Class

JSqueeze

Namespace

Patchwork

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[ (].*?\\{)'", $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], '(', 8);
    }
    $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,
  );
}