You are here

function advagg_does_js_start_with_use_strict in Advanced CSS/JS Aggregation 7.2

Given a js string, see if "use strict"; is the first thing ran.

Parameters

string $filename: String; filename containing path information as well.

Return value

bool True if "use strict"; is the first thing ran.

2 calls to advagg_does_js_start_with_use_strict()
advagg_insert_update_files in ./advagg.inc
Insert/Update data in the advagg_files table.
advagg_update_7213 in ./advagg.install
Populate the use_strict field in the advagg_files table.

File

./advagg.inc, line 326
Advanced CSS/JS aggregation module.

Code

function advagg_does_js_start_with_use_strict($filename) {
  $files =& drupal_static(__FUNCTION__, array());
  if (!isset($files[$filename])) {

    // Make advagg_get_*_aggregate_contents() available.
    module_load_include('inc', 'advagg', 'advagg.missing');
    $aggregate_settings = advagg_current_hooks_hash_array();
    $file_aggregate = array(
      $filename => array(),
    );
    list($contents) = advagg_get_js_aggregate_contents($file_aggregate, $aggregate_settings);

    // See if the js file starts with "use strict";.
    // Trim the JS down to 24kb.
    $length = variable_get('advagg_js_header_length', ADVAGG_JS_HEADER_LENGTH);
    $header = advagg_get_js_header($contents, $length);

    // Look for the string.
    $use_strict = stripos($header, '"use strict";');
    $strict_js = FALSE;
    if ($use_strict === FALSE) {
      $use_strict = stripos($header, "'use strict';");
    }
    if ($use_strict !== FALSE) {
      if ($use_strict == 0) {
        $strict_js = TRUE;
      }
      else {

        // Get all text before "use strict";.
        $substr = substr($header, 0, $use_strict);

        // Check if there are any comments.
        $single_line_comment = strpos($substr, '//');
        $multi_line_comment = strpos($substr, '/*');
        $in_function = strpos($substr, '{');
        if ($single_line_comment !== FALSE || $multi_line_comment !== FALSE) {

          // Remove js comments and try again.
          advagg_remove_js_comments($header);

          // Look for the string.
          $use_strict = stripos($header, '"use strict";');
          if ($use_strict === FALSE) {
            $use_strict = stripos($header, "'use strict';");
          }

          // Get all text before "use strict"; with comments removed.
          $substr = substr($header, 0, $use_strict);

          // Check if there is a function before use strict.
          $in_function = strpos($substr, '{');
        }
        if ($in_function === FALSE) {
          $strict_js = TRUE;
        }
      }
    }
    $files[$filename] = $strict_js;
  }
  return $files[$filename];
}