From 99ffe1990adefac03626ec334395be93ac8a38b4 Mon Sep 17 00:00:00 2001
From: Peter Philipp <peter.philipp@cando-image.com>
Date: Sun, 6 Nov 2011 13:21:33 +0100
Subject: [PATCH] Avoid cdata double escaping: http://drupal.org/node/998590

---
 modules/filter/filter.module |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/modules/filter/filter.module b/modules/filter/filter.module
index 71dc21a..d9cfa2a 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -1107,6 +1107,13 @@ function filter_dom_serialize_escape_cdata_element($dom_document, $dom_element,
       // 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";
+      // Avoid doubling up the cdata escaping as this would throw an error.
+      if (preg_match('/[!--<>\/* ]+\<\!\[CDATA\[/', $node->data)) {
+        $embed_prefix = NULL;
+      }
+      if (preg_match('/[!--<>\/* ]+\]\]\>/', $node->data)) {
+        $embed_suffix = NULL;
+      }
       $fragment = $dom_document->createDocumentFragment();
       $fragment->appendXML($embed_prefix . $node->data . $embed_suffix);
       $dom_element->appendChild($fragment);
-- 
1.7.7.msysgit.1

