? cdn972344.patch
Index: cdn.fallback.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cdn/cdn.fallback.inc,v
retrieving revision 1.3
diff -u -p -r1.3 cdn.fallback.inc
--- cdn.fallback.inc	15 Oct 2010 08:43:04 -0000	1.3
+++ cdn.fallback.inc	15 Nov 2010 22:15:46 -0000
@@ -10,7 +10,7 @@
  * theme layer.
  */
 
-
+ 
 /**
  * Implementation of hook_theme_registry_alter().
  *
@@ -54,9 +54,8 @@ function cdn_preprocess_page(&$variables
   $skip_keys = array('styles', 'scripts', 'zebra', 'id', 'directory', 'layout', 'head_title', 'base_path', 'front_page', 'head', 'body_clases');
   foreach ($variables as $key => $value) {
     if (!in_array($key, $skip_keys) && is_string($value) && !empty($value)) {
-      $matches = array();
-      $pattern = "#(<img\s+|<img\s+[^>]*\s+)src\s*=\s*\"(($root)([^\"]*)(\?.*)?)\"#i";
-      _cdn_preprocess_page_helper($variables[$key], $pattern, 0, 4, 5, '<img src="', '"');
+      $pattern = "#((<img\s+|<img\s+[^>]*\s+)src\s*=\s*[\"|\'](($root/)([^\"|^\']*)(\?.*)?)[\"|\'](.*?)>)#i";
+      $variables[$key] = preg_replace_callback($pattern, '_cdn_process_img_helper', $variables[$key]);
     }
   }
 }
@@ -64,6 +63,31 @@ function cdn_preprocess_page(&$variables
 /**
  * Helper function for cdn_preprocess_page().
  *
+ * @param $matches
+ *   Array containing the captures from our regular expression.
+ * @return
+ *   The new, complete img tag with all attributes.
+ */
+function _cdn_process_img_helper($matches) {
+
+  $path = $matches[5];
+
+  // $matches[5] contains our actual path, so store that in $path, then let 
+  // cdn_file_url_alter() do its magic by invoking all file_url_alter hooks. 
+  // When the path hasn't changed and is not already root-relative or protocol-relative,
+  // then generate a file URL as Drupal core would: prepend the base path.
+  
+  drupal_alter('file_url', $path);
+  if ($path == $matches[5] && drupal_substr($path, 0, 1) != '/' && drupal_substr($path, 0, 2) != '//') {
+    $path = base_path() . $path;
+  }
+
+  return $matches[2] . 'src="' . $path . '"' . $matches[7] . '>';
+}
+
+/**
+ * Helper function for cdn_preprocess_page().
+ *
  * @param &$html
  *   The HTML in which file URLs will be altered.
  * @param $pattern
