You are here

function _labjs_rewrite_js in LABjs 6

Callback function for labjs_rewrite_js().

When called without parameter, returns an array containing all processed scripts.

1 call to _labjs_rewrite_js()
labjs_preprocess_page in ./labjs.module
Implements hook_preprocess_page().
1 string reference to '_labjs_rewrite_js'
labjs_rewrite_js in ./labjs.module
Rewrites <script> tag to use LABjs.

File

./labjs.module, line 288
LABjs module

Code

function _labjs_rewrite_js($matches = NULL) {

  // We store all processed scripts in the $scripts array.
  static $scripts = array();
  if (!$matches) {
    return $scripts;
  }
  else {
    $exception = strpos($matches[0], 'rpxnow.com') !== FALSE;

    // If this is an inline script, we remove and add the src to $scripts array.
    // If not, we try to wrap it with $LAB.wait()
    if (preg_match('#type="text/javascript" (defer="defer" |)src="(.+?)"#', $matches[0], $match)) {
      if (strpos($matches[0], 'tinymce/jscripts/tiny_mce/tiny_mce.js') == FALSE) {
        $scripts[] = "\"{$match[2]}\"";
        return '';
      }
      else {
        return $matches[0];
      }
    }
    else {
      if (strpos($matches[0], LABJS_EXCLUDE) !== FALSE || $exception) {
        return $matches[0];
      }
      $output = str_replace(array(
        "<!--//--><![CDATA[//><!--",
        "//--><!]]>",
      ), array(
        "<!--//--><![CDATA[//><!--\n\$L = \$L.wait(function() {",
        "});\n//--><!]]>",
      ), $matches[0]);

      // If this JS is not CDATA-escaped, we try to rewrite anyway if demanded.
      if (strpos($matches[0], "<!--//--><![CDATA[//><!--") === FALSE && variable_get('labjs_no_cdata', FALSE)) {
        $output = preg_replace('#(<script.+?>)(.+?)(</script>)\\s*#s', '\\1$L = $L.wait(function() {\\2});\\3', $matches[0]);
      }
      return $output;
    }
  }
}