? coder.patch
Index: data_uri_sprites.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data_uri_sprites/data_uri_sprites.admin.inc,v
retrieving revision 1.2
diff -u -p -r1.2 data_uri_sprites.admin.inc
--- data_uri_sprites.admin.inc 8 Jul 2010 18:06:35 -0000 1.2
+++ data_uri_sprites.admin.inc 9 Jul 2010 12:59:09 -0000
@@ -1,7 +1,7 @@
array(
'#type' => 'checkbox',
@@ -26,14 +26,15 @@ function data_uri_sprites_admin_form($st
'data_uri_sprites_keep_images' => array(
'#type' => 'checkbox',
'#title' => t('Keep links to images in standard optimized CSS file'),
- '#description' => t('With this option enabled old browsers without Data URI or MHTML support still can use normal images. Disadvantage is CSS file size.') . '
TODO: removing old url(...) images is not implemented yet',
+ '#description' => t('With this option enabled old browsers without Data URI or MHTML support still can use normal images. Disadvantage is CSS file size.') .'
TODO: removing old url(...) images is not implemented yet',
'#default_value' => variable_get('data_uri_sprites_keep_images', 0),
'#disabled' => TRUE,
),
),
);
-
+
$form['#submit'][] = 'drupal_clear_css_cache';
-
+
return system_settings_form($form);
}
+
Index: data_uri_sprites.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data_uri_sprites/data_uri_sprites.info,v
retrieving revision 1.1
diff -u -p -r1.1 data_uri_sprites.info
--- data_uri_sprites.info 8 Jul 2010 17:17:25 -0000 1.1
+++ data_uri_sprites.info 9 Jul 2010 12:59:09 -0000
@@ -2,3 +2,4 @@
core = "6.x"
description = "Convert your themes to Data URI Sprites on the fly. The result is faster page loading and lower server load."
name = "Data URI Sprites Generator"
+
Index: data_uri_sprites.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/data_uri_sprites/data_uri_sprites.install,v
retrieving revision 1.2
diff -u -p -r1.2 data_uri_sprites.install
--- data_uri_sprites.install 8 Jul 2010 18:06:35 -0000 1.2
+++ data_uri_sprites.install 9 Jul 2010 12:59:09 -0000
@@ -1,6 +1,6 @@
*/
-
-function _data_uri_sprites_aggregation_enabled()
-{
+function _data_uri_sprites_aggregation_enabled() {
static $enabled = NULL;
- if ($enabled === NULL)
- {
+ if ($enabled === NULL) {
$preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
- $directory = file_directory_path();
- $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
- $enabled = ($is_writable && $preprocess_css);
+ $directory = file_directory_path();
+ $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
+ $enabled = ($is_writable && $preprocess_css);
}
return $enabled;
}
-function data_uri_sprites_menu()
-{
+function data_uri_sprites_menu() {
$items = array(
'admin/settings/performance/basic' => array(
'title' => 'Basic',
@@ -45,23 +41,22 @@ function data_uri_sprites_menu()
return $items;
}
-function _data_uri_sprites_collapse_path($path)
-{
+function _data_uri_sprites_collapse_path($path) {
$parts = explode('/', $path);
$result = array();
- for ($item = reset($parts); $item !== FALSE; $item = next($parts))
- {
- switch ($item)
- {
+ for ($item = reset($parts); $item !== FALSE; $item = next($parts)) {
+ switch ($item) {
case '..':
- if (count($result)) //if we can go up, go
- array_pop($result);
- else
- $result[] = $item;
+ //if we can go up, go
+ if (count($result))
+ array_pop($result);
+ else $result[] = $item;
break;
+
case '.':
case '':
break;
+
default:
$result[] = $item;
}
@@ -69,35 +64,34 @@ function _data_uri_sprites_collapse_path
return implode('/', $result);
}
-function _data_uri_sprites_get_images()
-{
+function _data_uri_sprites_get_images() {
return _data_uri_sprites_add_image();
}
-function _data_uri_sprites_add_image($selectors = NULL, $path = NULL, $property = NULL, $reset = FALSE)
-{
+function _data_uri_sprites_add_image($selectors = NULL, $path = NULL, $property = NULL, $reset = FALSE) {
static $images = array();
- if ($reset)
+ if ($reset) {
$images = array();
- if ($selectors && $path && $property)
- {
+ }
+ if ($selectors && $path && $property) {
$path = _data_uri_sprites_collapse_path($path);
- if (file_exists($path))
- {
- if (!variable_get('data_uri_sprites_32kb_limit', 1) || filesize($path) < 32*1024)
- {
- if (empty($images[$path]))
+ if (file_exists($path)) {
+ if (!variable_get('data_uri_sprites_32kb_limit', 1) || filesize($path) < 32 * 1024) {
+ if (empty($images[$path])) {
$images[$path] = array();
-
+ }
+
$img_info = getimagesize($path);
$images[$path]['mime'] = $img_info['mime'];
-
- if (empty($images[$path][$property]))
+
+ if (empty($images[$path][$property])) {
+
$images[$path][$property] = array();
-
+
+ }
+
$selectors = explode(',', $selectors);
- foreach ($selectors as $selector)
- {
+ foreach ($selectors as $selector) {
$images[$path][$property][] = trim($selector);
}
}
@@ -106,37 +100,32 @@ function _data_uri_sprites_add_image($se
return $images;
}
-function data_uri_sprites_preprocess_page(&$variables)
-{
- if (variable_get('data_uri_sprites_enable', 1))
- {
+function data_uri_sprites_preprocess_page(&$variables) {
+ if (variable_get('data_uri_sprites_enable', 1)) {
//TODO support for conditional stylesheets module
$css = drupal_add_css();
-
+
$csspath = file_create_path('css');
file_check_directory($csspath, FILE_CREATE_DIRECTORY);
$query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
-
+
$mhtml = variable_get('data_uri_sprites_mhtml', 1);
-
+
$additional_styles = array('sprites' => array(), 'mhtml' => array());
-
- foreach ($css as $media => $types)
- {
+
+ foreach ($css as $media => $types) {
$sprites_regenerated = FALSE;
$filename = md5(serialize($types) . $query_string);
//We build aggregated CSS filename just like in drupal_get_css(...)
- $aggregated_filename = $csspath . '/css_' . $filename . '.css';
- $sprites_filename = $csspath . '/sprites_' . $filename . '.css';
- $mhtml_filename = $mhtml ? $csspath . '/mhtml_' . $filename . '.css' : NULL;
-
- if (!file_exists($sprites_filename) || ($mhtml && !file_exists($mhtml_filename)))
- {
+ $aggregated_filename = $csspath .'/css_'. $filename .'.css';
+ $sprites_filename = $csspath .'/sprites_'. $filename .'.css';
+ $mhtml_filename = $mhtml ? $csspath .'/mhtml_'. $filename .'.css' : NULL;
+
+ if (!file_exists($sprites_filename) || ($mhtml && !file_exists($mhtml_filename))) {
//Required files aren't exist. Rebuild them
$images = _data_uri_sprites_parse_images($types, $aggregated_filename);
- if (!_data_uri_sprites_build_css($images, $sprites_filename, $mhtml_filename))
- {
+ if (!_data_uri_sprites_build_css($images, $sprites_filename, $mhtml_filename)) {
//TODO error
continue;
}
@@ -144,140 +133,121 @@ function data_uri_sprites_preprocess_pag
$sprites_regenerated = TRUE;
}
}
-
+
//If we have mhtml, always include sprites as a separate files
- if ($mhtml)
- {
+ if ($mhtml) {
//TODO What about theme-module override by name and -rtl replacement?
- if (filesize($sprites_filename) > 0)
- {
- $additional_styles['sprites'][] = '';
+ if (filesize($sprites_filename) > 0) {
+ $additional_styles['sprites'][] = '';
}
- if ($mhtml && filesize($mhtml_filename) > 0)
- {
- $additional_styles['mhtml'][] = '';
+ if ($mhtml && filesize($mhtml_filename) > 0) {
+ $additional_styles['mhtml'][] = '';
}
}
- else
- {
+ else {
//Attach CSS as a single file or even append it to an aggregated CSS
- if (_data_uri_sprites_aggregation_enabled() && file_exists($aggregated_filename))
- {
- if ($sprites_regenerated)
- {
+ if (_data_uri_sprites_aggregation_enabled() && file_exists($aggregated_filename)) {
+ if ($sprites_regenerated) {
//Append sprites code only if sprites were regenerated
$sprites_code = file_get_contents($sprites_filename);
$aggregated_f = fopen($aggregated_filename, "ab");
- if ($aggregated_f)
- {
+ if ($aggregated_f) {
fwrite($aggregated_f, $sprites_code);
fclose($aggregated_f);
}
- else
- {
+ else {
//TODO error?
}
}
}
- else
- {
+ else {
//we should have our hook_preprocess_page called latest, so no other modules/themes can be interested in css... remove it?
drupal_add_css($sprites_filename, 'theme', $media, TRUE);
-
- $variables['styles'] .= '' . "\n";
+
+ $variables['styles'] .= '' . "\n";
}
}
}
-
+
$code = '';
- if (!empty($additional_styles['sprites']))
- {
+ if (!empty($additional_styles['sprites'])) {
$code .= "\n";
$code .= implode("\n", $additional_styles['sprites']);
$code .= "\n\n";
}
- if (!empty($additional_styles['mhtml']))
- {
+ if (!empty($additional_styles['mhtml'])) {
$code .= "\n";
}
- if ($code)
- {
+ if ($code) {
$variables['styles'] .= $code;
}
}
}
-function _data_uri_sprites_build_css($images, $sprites_filename, $mhtml_filename)
-{
+function _data_uri_sprites_build_css($images, $sprites_filename, $mhtml_filename) {
$fsprites = fopen($sprites_filename, 'wb');
- if (!$fsprites)
+ if (!$fsprites) {
return FALSE;
+ }
$mhtml = variable_get('data_uri_sprites_mhtml', 1);
- if ($mhtml)
- {
+ if ($mhtml) {
$fmhtml = fopen($mhtml_filename, 'wb');
- if (!$fmhtml)
- {
+ if (!$fmhtml) {
fclose($fsprites);
return FALSE;
}
}
-
- if (empty($images))
- {
+
+ if (empty($images)) {
//If there were no images parsed, just create empty files to skip parsing on next page requests
fclose($fsprites);
- if ($mhtml)
+ if ($mhtml) {
fclose($fmhtml);
+ }
return TRUE;
}
-
- if ($mhtml)
- {
+
+ if ($mhtml) {
fwrite($fmhtml, "/*\nContent-Type: multipart/related; boundary=\"_\"");
$mhtml_absolute_path = url($mhtml_filename, array('absolute' => TRUE));
$mhtml_rules = '';
- $i = 1; //Start numbering mhtml sprites from 1
+ // Start numbering mhtml sprites from 1
+ $i = 1;
}
- foreach ($images as $path => $properties)
- {
+ foreach ($images as $path => $properties) {
$mime = $properties['mime'];
unset($properties['mime']);
$base64 = base64_encode(file_get_contents($path));
- if ($mhtml)
- {
+ if ($mhtml) {
fwrite($fmhtml, "\n\n--_\n");
- fwrite($fmhtml, 'Content-Location:' . $i . "\n");
- fwrite($fmhtml, 'Content-Type:' . $mime . "\n");
+ fwrite($fmhtml, 'Content-Location:'. $i ."\n");
+ fwrite($fmhtml, 'Content-Type:'. $mime ."\n");
fwrite($fmhtml, 'Content-Transfer-Encoding:base64' . "\n\n");
fwrite($fmhtml, $base64);
- $mhtml_url = 'mhtml:' . $mhtml_absolute_path . '!' . $i;
+ $mhtml_url = 'mhtml:'. $mhtml_absolute_path .'!'. $i;
$i++;
}
- foreach ($properties as $property => $selectors)
- {
- switch (strtolower($property))
- {
+ foreach ($properties as $property => $selectors) {
+ switch (strtolower($property)) {
case 'background':
$property = 'background-image';
break;
+
case 'list-style':
$property = 'list-style-image';
break;
}
- $css = implode(',', $selectors) . '{' . $property . ':url(';
+ $css = implode(',', $selectors) .'{'. $property .':url(';
$css2 = ');}';
- if ($mhtml)
- {
- $mhtml_rules .= $css . "'" . $mhtml_url . "'" . $css2;
+ if ($mhtml) {
+ $mhtml_rules .= $css ."'". $mhtml_url ."'". $css2;
}
- fwrite($fsprites, $css . 'data:' . $mime . ';base64,' . $base64 . $css2);
+ fwrite($fsprites, $css .'data:'. $mime .';base64,'. $base64 . $css2);
}
}
- if ($mhtml)
- {
+ if ($mhtml) {
fwrite($fmhtml, "\n\n--_--\n*/\n");
fwrite($fmhtml, $mhtml_rules);
fclose($fmhtml);
@@ -286,55 +256,44 @@ function _data_uri_sprites_build_css($im
return TRUE;
}
-function _data_uri_sprites_parse_css($path, $remove_url = FALSE)
-{
+function _data_uri_sprites_parse_css($path, $remove_url = FALSE) {
static $pattern = '/(?<=^|\})([^\}]+)\{[^\}]*(background|list-style)[^\:]*\:[^\;\}]*url\(\s*(?:\\\'?|\"?)([^\\\'\"\)]+)/is';
-
+
$file = file_get_contents($path);
-
+
//Get rid of comments, @-rules, @import-s and corresponding closing brackets
//TODO take care about line-comments // and don't forget they are used for IE hacks sometimes
$file = preg_replace(array('@\/\*.*?\*\/@s', '/\@[^\{]+\;/', '/\@import[^\{]*\{/', '/(?<=\})[^\{]*?\}/'), '', $file);
-
+
//TODO take into account @media rules, now they are just ignored
//TODO take into account !important rules and make resulting CSS with sprites important too
- if (preg_match_all($pattern, $file, $matches, PREG_SET_ORDER))
- {
- foreach ($matches as $match)
- {
+ if (preg_match_all($pattern, $file, $matches, PREG_SET_ORDER)) {
+ foreach ($matches as $match) {
$img_path = $match[3];
- if ($img_path[0] != '/')
- {
+ if ($img_path[0] != '/') {
//Relative path, adding CSS file location in front of image's path
- $img_path = dirname($path) . '/' . $img_path;
+ $img_path = dirname($path) .'/'. $img_path;
}
_data_uri_sprites_add_image($match[1], $img_path, $match[2]);
}
- if ($remove_url)
- {
+ if ($remove_url) {
//TODO remove url(...) from aggregated CSS and save it
}
}
}
-function _data_uri_sprites_parse_images($types, $aggregated_filename = NULL)
-{
+function _data_uri_sprites_parse_images($types, $aggregated_filename = NULL) {
_data_uri_sprites_add_image(NULL, NULL, NULL, TRUE);
-
- if (_data_uri_sprites_aggregation_enabled() && file_exists($aggregated_filename))
- {
+
+ if (_data_uri_sprites_aggregation_enabled() && file_exists($aggregated_filename)) {
//Parse only aggregated CSS file
_data_uri_sprites_parse_css($aggregated_filename, !variable_get('data_uri_sprites_keep_images', 0));
}
- else
- {
+ else {
//Parse all files one-by-one
- foreach ($types as $type => $files)
- {
- foreach ($files as $path => $preprocess)
- {
- if ($preprocess)
- {
+ foreach ($types as $type => $files) {
+ foreach ($files as $path => $preprocess) {
+ if ($preprocess) {
_data_uri_sprites_parse_css($path);
}
}
@@ -359,3 +318,4 @@ function data_uri_sprites_theme_registry
$theme_registry['page']['preprocess functions'][] = 'data_uri_sprites_preprocess_page';
}
}
+