Index: cdn.fallback.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cdn/cdn.fallback.inc,v
retrieving revision 1.3
diff -u -F '^f' -r1.3 cdn.fallback.inc
--- cdn.fallback.inc	15 Oct 2010 08:43:04 -0000	1.3
+++ cdn.fallback.inc	18 Nov 2010 00:03:19 -0000
@@ -8,6 +8,8 @@
  * module's logic (with some adaptations to be able to use the CDN module's
  * logic (in particular: cdn_file_url_alter()) to alter file URLs via the
  * theme layer.
+ * Some of the functions are also used when altering rendered nodes so that
+ * their images are served from the CDN.
  */
 
 
@@ -33,36 +35,73 @@ function cdn_theme_registry_alter(&$them
 
 /**
  * Implementation of template_preprocess_page().
- *
- * @param &$variables
- *   Array containing various page elements.
  */
 function cdn_preprocess_page(&$variables) {
-  global $base_url;
-  $root = preg_quote(base_path()) . '|' . preg_quote($base_url);
+  // CSS.
+  cdn_html_alter_css_urls($variables['styles']);
 
-  // CSS
-  $pattern = "#href=\"(($root)(.*?\.css)(\?.*)?)\"#";
-  _cdn_preprocess_page_helper($variables['styles'], $pattern, 0, 3, 4, 'href="', '"');
-
-  // JS
-  $pattern = "#src=\"(($root)(.*?\.js)(\?.*)?)\"#";
-  _cdn_preprocess_page_helper($variables['scripts'], $pattern, 0, 3, 4, 'src="', '"');
-  _cdn_preprocess_page_helper($variables['closure'], $pattern, 0, 3, 4, 'src="', '"');
+  // JS.
+  cdn_html_alter_js_urls($variables['scripts']);
+  cdn_html_alter_js_urls($variables['closure']);
 
-  // Images
-  $skip_keys = array('styles', 'scripts', 'zebra', 'id', 'directory', 'layout', 'head_title', 'base_path', 'front_page', 'head', 'body_clases');
+  // Images.
+  $skip_keys = array('styles', 'scripts', 'zebra', 'id', 'directory', 'layout', 'head_title', 'base_path', 'front_page', 'head', 'body_classes');
   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="', '"');
+      cdn_html_alter_image_urls($variables[$key]);
     }
   }
 }
 
 /**
- * Helper function for cdn_preprocess_page().
+ * Alter CSS file URLs in a piece of HTML.
+ */
+function cdn_html_alter_css_urls(&$html) {
+  $url_prefix_regex = _cdn_generate_url_prefix_regex();
+  $pattern = "#href=\"(($url_prefix_regex)(.*?\.css)(\?.*)?)\"#";
+  _cdn_html_alter_file_url($html, $pattern, 0, 3, 4, 'href="', '"');  
+}
+
+/**
+ * Alter JS file URLs in a piece of HTML.
+ */
+function cdn_html_alter_js_urls(&$html) {
+  $url_prefix_regex = _cdn_generate_url_prefix_regex();
+  $pattern = "#src=\"(($url_prefix_regex)(.*?\.js)(\?.*)?)\"#";
+  _cdn_html_alter_file_url($html, $pattern, 0, 3, 4, 'src="', '"');
+}
+
+/**
+ * Alter image file URLs in a piece of HTML.
+ */
+function cdn_html_alter_image_urls(&$html) {
+  $url_prefix_regex = _cdn_generate_url_prefix_regex();
+  $pattern = "#((<img\s+|<img\s+[^>]*\s+)src\s*=\s*[\"|\'])($url_prefix_regex)([^\"|^\']*)([\"|\'])#i";
+  _cdn_html_alter_file_url($html, $pattern, 0, 4, 5, 1, '');
+}
+
+/**
+ * Generate the URL prefix regular expression, that supports all possible
+ * types of file URLs: root-relative, protocol-relative and absolute URLs.
+ */
+function _cdn_generate_url_prefix_regex() {
+  global $base_url;
+  static $url_prefix_regex;
+
+  if (!isset($url_prefix_regex)) {
+    $url_prefix_regex = implode('|', array_map('preg_quote', array(
+      base_path(),                                // Root-relative URL.
+      '//' . $_SERVER['HTTP_HOST'] . base_path(), // Protocol-relative URL.
+      $base_url . '/',                            // Absolute URL.
+    )));
+  }
+
+  return $url_prefix_regex;
+}
+
+/**
+ * Alter the file URLs in a piece of HTML given a regexp pattern and some
+ * additional parameters.
  *
  * @param &$html
  *   The HTML in which file URLs will be altered.
@@ -76,11 +115,12 @@ function cdn_preprocess_page(&$variables
  *   The index of (an optional) query string in the array of regexp matches.
  * @param $prefix
  *   $search_index will be replaced by $prefix, plus the altered file URL,
- *   plus the @suffix.
+ *   plus the @suffix. If numeric, then it is assumed to be the index of the
+ *   prefix in the array of regexp matches.
  * @param $suffix
  *   See $prefix.
  */
-function _cdn_preprocess_page_helper(&$html, $pattern, $search_index, $path_index, $querystring_index, $prefix, $suffix) {
+function _cdn_html_alter_file_url(&$html, $pattern, $search_index, $path_index, $querystring_index, $prefix, $suffix) {
   // Find a match against the given pattern.
   preg_match_all($pattern, $html, $matches);
 
@@ -91,6 +131,9 @@ function _cdn_preprocess_page_helper(&$h
     $search = $matches[$search_index][$i];
     $path = $matches[$path_index][$i];
 
+    $prefix_string = (is_numeric($prefix)) ? $matches[$prefix][$i] : $prefix;
+    $suffix_string = (is_numeric($suffix)) ? $matches[$suffix][$i] : $suffix;
+
     // Store the current path as the old 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
@@ -102,7 +145,7 @@ function _cdn_preprocess_page_helper(&$h
     }
 
     $searches[]     = $search;
-    $replacements[] = $prefix . $path . $matches[$querystring_index][$i] . $suffix;
+    $replacements[] = $prefix_string . $path . $matches[$querystring_index][$i] . $suffix_string;
   }
 
   // Apply the generated replacements ton the subject.
