You are here

private function JavaScriptPacker::_analyze in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_js_minify/jspacker.inc \JavaScriptPacker::_analyze()
  2. 8.3 advagg_js_minify/jspacker.inc \JavaScriptPacker::_analyze()
  3. 6 advagg_js_compress/jspacker.inc \JavaScriptPacker::_analyze()
  4. 7.2 advagg_js_compress/jspacker.inc \JavaScriptPacker::_analyze()
  5. 7 advagg_js_compress/jspacker.inc \JavaScriptPacker::_analyze()
2 calls to JavaScriptPacker::_analyze()
JavaScriptPacker::_encodeKeywords in advagg_js_minify/jspacker.inc
JavaScriptPacker::_encodeSpecialChars in advagg_js_minify/jspacker.inc

File

advagg_js_minify/jspacker.inc, line 225

Class

JavaScriptPacker

Code

private function _analyze($script, $regexp, $encode) {

  // analyse
  // retrieve all words in the script
  $all = array();
  preg_match_all($regexp, $script, $all);
  $_sorted = array();

  // list of words sorted by frequency
  $_encoded = array();

  // dictionary of word->encoding
  $_protected = array();

  // instances of "protected" words
  $all = $all[0];

  // simulate the javascript comportement of global match
  if (!empty($all)) {
    $unsorted = array();

    // same list, not sorted
    $protected = array();

    // "protected" words (dictionary of word->"word")
    $value = array();

    // dictionary of charCode->encoding (eg. 256->ff)
    $this->_count = array();

    // word->count
    $i = count($all);
    $j = 0;

    //$word = null;

    // count the occurrences - used for sorting later
    do {
      --$i;
      $word = '$' . $all[$i];
      if (!isset($this->_count[$word])) {
        $this->_count[$word] = 0;
        $unsorted[$j] = $word;

        // make a dictionary of all of the protected words in this script
        //  these are words that might be mistaken for encoding

        //if (is_string($encode) && method_exists($this, $encode))
        $values[$j] = call_user_func(array(
          &$this,
          $encode,
        ), $j);
        $protected['$' . $values[$j]] = $j++;
      }

      // increment the word counter
      $this->_count[$word]++;
    } while ($i > 0);

    // prepare to sort the word list, first we must protect
    //  words that are also used as codes. we assign them a code
    //  equivalent to the word itself.
    // e.g. if "do" falls within our encoding range
    //      then we store keywords["do"] = "do";
    // this avoids problems when decoding
    $i = count($unsorted);
    do {
      $word = $unsorted[--$i];
      if (isset($protected[$word])) {
        $_sorted[$protected[$word]] = substr($word, 1);
        $_protected[$protected[$word]] = true;
        $this->_count[$word] = 0;
      }
    } while ($i);

    // sort the words by frequency
    // Note: the javascript and php version of sort can be different :
    // in php manual, usort :
    // " If two members compare as equal,
    // their order in the sorted array is undefined."
    // so the final packed script is different of the Dean's javascript version
    // but equivalent.
    // the ECMAscript standard does not guarantee this behaviour,
    // and thus not all browsers (e.g. Mozilla versions dating back to at
    // least 2003) respect this.
    usort($unsorted, array(
      &$this,
      '_sortWords',
    ));
    $j = 0;

    // because there are "protected" words in the list
    //  we must add the sorted words around them
    do {
      if (!isset($_sorted[$i])) {
        $_sorted[$i] = substr($unsorted[$j++], 1);
      }
      $_encoded[$_sorted[$i]] = $values[$i];
    } while (++$i < count($unsorted));
  }
  return array(
    'sorted' => $_sorted,
    'encoded' => $_encoded,
    'protected' => $_protected,
  );
}