From c169604714aad58bf9bc28eec59a9884400610a0 Mon Sep 17 00:00:00 2001
Message-Id: <c169604714aad58bf9bc28eec59a9884400610a0.1332509315.git.dmitriy.trt@gmail.com>
From: "Dmitriy.trt" <dmitriy.trt@gmail.com>
Date: Fri, 23 Mar 2012 20:28:28 +0700
Subject: [PATCH] Issue #1465432: Optional wrapping with CDATA

---
 .../views_data_export_plugin_style_export_xml.inc  |    9 +++++++++
 theme/views_data_export.theme.inc                  |   20 +++++++++++++-------
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/plugins/views_data_export_plugin_style_export_xml.inc b/plugins/views_data_export_plugin_style_export_xml.inc
index 8e6d790..f4bade7 100644
--- a/plugins/views_data_export_plugin_style_export_xml.inc
+++ b/plugins/views_data_export_plugin_style_export_xml.inc
@@ -28,6 +28,10 @@ class views_data_export_plugin_style_export_xml extends views_data_export_plugin
       'default' => 'dash',
       'translatable' => FALSE,
     );
+    $options['cdata'] = array(
+      'default' => FALSE,
+      'translatable' => FALSE,
+    );
 
     return $options;
   }
@@ -66,5 +70,10 @@ class views_data_export_plugin_style_export_xml extends views_data_export_plugin
         'edit-style-options-transform' => array(TRUE),
       ),
     );
+    $form['cdata'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Wrap fields content with CDATA'),
+      '#default_value' => $this->options['cdata'],
+    );
   }
 }
diff --git a/theme/views_data_export.theme.inc b/theme/views_data_export.theme.inc
index 1a4f7d3..e590b24 100644
--- a/theme/views_data_export.theme.inc
+++ b/theme/views_data_export.theme.inc
@@ -378,13 +378,19 @@ function template_preprocess_views_data_export_xml_body(&$vars) {
 
   foreach ($vars['themed_rows'] as $num => $row) {
     foreach ($row as $field => $content) {
-      // Prevent double encoding of the ampersand. Look for the entities produced by check_plain().
-      $content = preg_replace('/&(?!(amp|quot|#039|lt|gt);)/', '&amp;', $content);
-      // Convert < and > to HTML entities.
-      $content = str_replace(
-        array('<', '>'),
-        array('&lt;', '&gt;'),
-        $content);
+      if (empty($vars['options']['cdata'])) {
+        // Prevent double encoding of the ampersand. Look for the entities produced by check_plain().
+        $content = preg_replace('/&(?!(amp|quot|#039|lt|gt);)/', '&amp;', $content);
+        // Convert < and > to HTML entities.
+        $content = str_replace(
+          array('<', '>'),
+          array('&lt;', '&gt;'),
+          $content);
+      }
+      else {
+        // Escape CDATA end sequence only.
+        $content = '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $content) . ']]>';
+      }
       $vars['themed_rows'][$num][$field] = $content;
     }
   }
-- 
1.7.8.3

