diff --git a/modules/filter/filter.module b/modules/filter/filter.module
index 9e14812..af1fe8a 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -1065,17 +1065,11 @@ function filter_dom_load($text) {
  *   A valid (X)HTML snippet, as a string.
  */
 function filter_dom_serialize($dom_document) {
+  filter_dom_serialize_escape_cdata_element($dom_document, 'script');
+  filter_dom_serialize_escape_cdata_element($dom_document, 'style');
   $body_node = $dom_document->getElementsByTagName('body')->item(0);
   $body_content = '';
 
-  foreach ($body_node->getElementsByTagName('script') as $node) {
-    filter_dom_serialize_escape_cdata_element($dom_document, $node);
-  }
-
-  foreach ($body_node->getElementsByTagName('style') as $node) {
-    filter_dom_serialize_escape_cdata_element($dom_document, $node, '/*', '*/');
-  }
-
   foreach ($body_node->childNodes as $child_node) {
     $body_content .= $dom_document->saveXML($child_node);
   }
@@ -1094,23 +1088,20 @@ function filter_dom_serialize($dom_document) {
  *
  * @param $dom_document
  *   The DOMDocument containing the $dom_element.
- * @param $dom_element
+ * @param $dom_element$list = $dom->getElementsByTagName('script');
  *   The element potentially containing a CDATA node.
  * @param $comment_start
  *   String to use as a comment start marker to escape the CDATA declaration.
  * @param $comment_end
  *   String to use as a comment end marker to escape the CDATA declaration.
  */
-function filter_dom_serialize_escape_cdata_element($dom_document, $dom_element, $comment_start = '//', $comment_end = '') {
-  foreach ($dom_element->childNodes as $node) {
-    if (get_class($node) == 'DOMCdataSection') {
-      // See drupal_get_js().  This code is more or less duplicated there.
-      $embed_prefix = "\n<!--{$comment_start}--><![CDATA[{$comment_start} ><!--{$comment_end}\n";
-      $embed_suffix = "\n{$comment_start}--><!]]>{$comment_end}\n";
-      $fragment = $dom_document->createDocumentFragment();
-      $fragment->appendXML($embed_prefix . $node->data . $embed_suffix);
-      $dom_element->appendChild($fragment);
-      $dom_element->removeChild($node);
+function filter_dom_serialize_escape_cdata_element($dom_document, $tagname) {
+  $list = $dom_document->getElementsByTagName($tagname);
+  foreach ($list as $script) {
+    if ($script->childNodes->length && $script->firstChild->nodeType == 4) {
+      $cdata = $script->removeChild($script->firstChild);
+      $text = $dom_document->createTextNode($cdata->nodeValue);
+      $script->appendChild($text);
     }
   }
 }
