--- htmlpurifier.module.orig	2010-06-04 09:44:15.000000000 -0700
+++ htmlpurifier.module	2010-06-06 17:46:27.000000000 -0700
@@ -92,6 +92,58 @@ function htmlpurifier_filter_tips($delta
   }
 }
 
+/**
+ * Implementation of hook_nodeapi().
+ */
+function htmlpurifier_nodeapi(&$node, $op, $a3, $a4) {
+  if ($op == 'view') {
+
+    // Should we load CSS cache data from teaser or body?
+    // @todo: We should probably check for CCK here and deal with its fields?
+    if ($a3 == TRUE) {
+      $purify_fields = array( $node->teaser );
+    }
+    else {
+      $purify_fields = array( $node->body );
+    }
+
+    // Try to iterate through fields we've purified - intended for CCK support
+    foreach ($purify_fields as $purify_field) {
+      $cache_matches = array();
+      $cache_match = preg_match('#<!-- HTML Purifier Cache \#([-\w]*:[\w]*) -->#', $purify_field, $cache_matches);
+
+      // If there's an HTML Purifier Cache #, we need to load CSSTidy blocks
+      if ($cache_match == 1) {
+        $cid = 'css:' . $cache_matches[1];
+        $old = cache_get($cid, 'cache_htmlpurifier');
+
+        // We should always have some cached style blocks to load, but if we don't, just bail
+        if ($old) {
+          $styles = array();
+          foreach($old->data as $i => $style) {
+
+            // Replace Node ID tokens if necessary, otherwise use cached CSSTidy blocks
+            if (strpos($style, '[%HTMLPURIFIER:NID%]') !== FALSE) {
+              $styles[$i] = str_replace('[%HTMLPURIFIER:NID%]', (int) $node->nid, $style);
+            }
+            else {
+              $styles[$i] = $style;
+            }
+
+            // Add any CSSTidy blocks we find to the document head
+            drupal_set_html_head('<style type="text/css"><!-- ' . $styles[$i] .' --></style>');
+          }
+
+          // If we had to update CSSTidy blocks, cache the results
+          if ($old->data != $styles) {
+            cache_set($cid, $styles, 'cache_htmlpurifier', CACHE_PERMANENT);
+          }
+        }
+      }
+    }
+  }
+}
+
 
 
 // -- INTERNAL FUNCTIONS ---------------------------------------------------- //
@@ -121,8 +173,44 @@ function _htmlpurifier_process($text, $f
   
   _htmlpurifier_load();
   $config = _htmlpurifier_get_config($format);
+
+  // If ExtractStyleBlocks is enabled, we'll need to do a bit more for CSSTidy
+  $config_extractstyleblocks = $config->get('Filter.ExtractStyleBlocks');
+
+  // Maybe this works if CSSTidy is at root? CSSTidy could be other places though
+  if ($config_extractstyleblocks == true) {
+    // If CSSTidy module is installed, it should have a copy we can use
+    $csstidy_path = drupal_get_path('module', 'csstidy') .'/csstidy';
+
+    // Some future-proofing for library path
+    if (function_exists('libraries_get_path')) {
+      $csstidy_library = libraries_get_path('csstidy');
+      if (file_exists("$csstidy_library/class.csstidy.php")) {
+        $csstidy_path = $csstidy_library;
+      }
+    }
+
+    // Load CSSTidy if we can find it
+    if (file_exists("$csstidy_path/class.csstidy.php")) {
+      require_once "$csstidy_path/class.csstidy.php";
+    }
+  }
+
   $purifier = new HTMLPurifier($config);
   $ret = $purifier->purify($text);
+
+  // If using Filter.ExtractStyleBlocks we need to handle the CSSTidy output
+  if ($config_extractstyleblocks == true) {
+
+    // We're only going to bother if we're caching! - no caching? no style blocks!
+    if ($cache) {
+
+      // Get style blocks, cache them, and help hook_nodeapi find the cache
+      $styles = $purifier->context->get('StyleBlocks');
+      cache_set('css:' . $cid, $styles, 'cache_htmlpurifier', CACHE_PERMANENT);
+      $ret = '<!-- HTML Purifier Cache #' . $cid . ' -->' . $ret;
+    }
+  }
   
   if ($cache) cache_set($cid, $ret, 'cache_htmlpurifier', CACHE_PERMANENT);
   
