#183940 by bjaspan, keith.smith: remove broken JS compression, but leave JS aggregation; update help text to that effect

6.x
Gábor Hojtsy 2007-12-19 17:19:50 +00:00
parent 7f2ca82075
commit 277a15248f
2 changed files with 2 additions and 226 deletions

View File

@ -2216,7 +2216,7 @@ function drupal_build_js_cache($files, $filename) {
foreach ($files as $path => $info) { foreach ($files as $path => $info) {
if ($info['preprocess']) { if ($info['preprocess']) {
// Append a ';' after each JS file to prevent them from running together. // Append a ';' after each JS file to prevent them from running together.
$contents .= _drupal_compress_js(file_get_contents($path) .';'); $contents .= file_get_contents($path) .';';
} }
} }
@ -2227,230 +2227,6 @@ function drupal_build_js_cache($files, $filename) {
return $jspath .'/'. $filename; return $jspath .'/'. $filename;
} }
/**
* Perform basic code compression for JavaScript.
*
* Helper function for drupal_pack_js().
*/
function _drupal_compress_js($script) {
$regexps = array(
// Protect strings.
array('/\'[^\'\\n\\r]*\'/', '$0'),
array('/"[^"\\n\\r]*"/', '$0'),
// Remove comments.
array('/\\/\\/[^\\n\\r]*[\\n\\r]/', ''),
array('/\\/\\*[^*]*\\*+((?:[^\\/][^*]*\\*+)*)\\//', ''),
// Protect regular expressions
array('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', '$1'),
array('/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', '$0'),
// Protect spaces between keywords and variables
array('/(?<=[A-Za-z0-9_$])\\s+(?=[A-Za-z0-9_$])/', ' '),
array('/([+\\-])\\s+([+\\-])/', '$1 $2'),
// Remove all other white-space
array('/\\s+/', ''),
);
$script = _packer_apply($script, $regexps, TRUE);
return $script;
}
/**
* Multi-regexp replacements.
*
* Allows you to perform multiple regular expression replacements at once,
* without overlapping matches.
*
* @param $script
* The text to modify.
* @param $regexps
* An array of replacement instructions, each being a tuple with values:
* - A stand-alone regular expression without modifiers (slash-delimited)
* - A replacement expression, which may include placeholders.
* @param $escape
* Whether to ignore slash-escaped characters for matching. This allows you
* to match e.g. quote-delimited strings with /'[^']+'/ without having to
* worry about \'. Otherwise, you'd have to mess with look-aheads and
* look-behinds to match these.
*/
function _packer_apply($script, $regexps, $escape = FALSE) {
$_regexps = array();
// Process all regexps
foreach ($regexps as $regexp) {
list($expression, $replacement) = $regexp;
// Count the number of matching groups (including the whole).
$length = 1 + preg_match_all('/(?<!\\\\)\((?!\?)/', $expression, $out);
// Treat only strings $replacement
if (is_string($replacement)) {
// Does the pattern deal with sub-expressions?
if (preg_match('/\$\d/', $replacement)) {
if (preg_match('/^\$\d+$/', $replacement)) {
// A simple lookup (e.g. "$2")
// Store the index (used for fast retrieval of matched strings)
$replacement = (int)(substr($replacement, 1));
}
else {
// A complicated lookup (e.g. "Hello $2 $1").
// Build a function to do the lookup.
$replacement = array(
'fn' => 'backreferences',
'data' => array(
'replacement' => $replacement,
'length' => $length,
)
);
}
}
}
// Store the modified expression.
if (!empty($expression)) {
$_regexps[] = array($expression, $replacement, $length);
}
else {
$_regexps[] = array('/^$/', $replacement, $length);
}
}
// Execute the global replacement
// Build one mega-regexp out of the smaller ones.
$regexp = '/';
foreach ($_regexps as $_regexp) {
list($expression) = $_regexp;
$regexp .= '('. substr($expression, 1, -1) .')|';
}
$regexp = substr($regexp, 0, -1) .'/';
// In order to simplify the regexps that look e.g. for quoted strings, we
// remove all escaped characters (such as \' or \") from the data. Then, we
// put them back as they were.
if ($escape) {
// Remove escaped characters
$script = preg_replace_callback(
'/\\\\(.)/',
'_packer_escape_char',
$script
);
$escaped = _packer_escape_char(NULL, TRUE);
}
_packer_replacement(NULL, $_regexps, $escape);
$script = preg_replace_callback(
$regexp,
'_packer_replacement',
$script
);
if ($escape) {
// Restore escaped characters
_packer_unescape_char(NULL, $escaped);
$script = preg_replace_callback(
'/\\\\/',
'_packer_unescape_char',
$script
);
// We only delete portions of data afterwards to ensure the escaped character
// replacements don't go out of sync. We mark all sections to delete with
// ASCII 01 bytes.
$script = preg_replace('/\\x01[^\\x01]*\\x01/', '', $script);
}
return $script;
}
/**
* Helper function for _packer_apply().
*/
function _packer_escape_char($match, $return = FALSE) {
// Build array of escaped characters that were removed.
static $_escaped = array();
if ($return) {
$escaped = $_escaped;
$_escaped = array();
return $escaped;
}
else {
$_escaped[] = $match[1];
return '\\';
}
}
/**
* Helper function for _packer_apply().
*
* Performs replacements for the multi-regexp.
*/
function _packer_replacement($arguments, $regexps = NULL, $escape = NULL) {
// Cache regexps
static $_regexps, $_escape;
if (isset($regexps)) {
$_regexps = $regexps;
}
if (isset($escape)) {
$_escape = $escape;
}
if (empty($arguments)) {
return '';
}
$i = 1; $j = 0;
// Loop through the regexps
while (isset($_regexps[$j])) {
list($expression, $replacement, $length) = $_regexps[$j++];
// Do we have a result?
if (isset($arguments[$i]) && ($arguments[$i] != '')) {
if (is_array($replacement) && isset($replacement['fn'])) {
return call_user_func('_packer_'. $replacement['fn'], $arguments, $i, $replacement['data']);
}
elseif (is_int($replacement)) {
return $arguments[$replacement + $i];
}
else {
$delete = !$escape || strpos($arguments[$i], '\\') === FALSE
? '' : "\x01". $arguments[$i] ."\x01";
return $delete . $replacement;
}
// skip over references to sub-expressions
}
else {
$i += $length;
}
}
}
/**
* Helper function for _packer_apply().
*/
function _packer_unescape_char($match, $escaped = NULL) {
// Store array of escaped characters to insert back.
static $_escaped, $i;
if ($escaped) {
$_escaped = $escaped;
$i = 0;
}
else {
return '\\'. array_shift($_escaped);
}
}
/**
* Helper function for _packer_replacement().
*/
function _packer_backreferences($match, $offset, $data) {
$replacement = $data['replacement'];
$i = $data['length'];
while ($i) {
$replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
}
return $replacement;
}
/** /**
* Delete all cached JS files. * Delete all cached JS files.
*/ */

View File

@ -1305,7 +1305,7 @@ function system_performance_settings() {
$form['bandwidth_optimizations'] = array( $form['bandwidth_optimizations'] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => t('Bandwidth optimizations'), '#title' => t('Bandwidth optimizations'),
'#description' => t('<p>Drupal can automatically aggregate and compress external resources like CSS and JavaScript into a single cached file. This can help reduce both the size and number of requests made to your website, and ultimately, may reduce server load, bandwidth requirements, and page loading times.</p><p>These options are disabled if you have not set up your files directory, or if your download method is set to private.</p>') '#description' => t('<p>Drupal can automatically optimize external resources like CSS and JavaScript, which can reduce both the size and number of requests made to your website. CSS files can be aggregated and compressed into a single file, while JavaScript files are aggregated (but not compressed). These optional optimizations may reduce server load, bandwidth requirements, and page loading times.</p><p>These options are disabled if you have not set up your files directory, or if your download method is set to private.</p>')
); );
$directory = file_directory_path(); $directory = file_directory_path();