Index: ../commons/src/main/java/org/hippoecm/hst/util/HeadElementUtils.java
===================================================================
--- ../commons/src/main/java/org/hippoecm/hst/util/HeadElementUtils.java	(revision 23654)
+++ ../commons/src/main/java/org/hippoecm/hst/util/HeadElementUtils.java	(revision )
@@ -27,64 +27,92 @@
 
 /**
  * HeadElementUtils
- * 
+ *
  * @version $Id: HeadElementUtils.java 23654 2010-08-10 23:56:34Z wko $
  */
 public class HeadElementUtils
 {
 
     private static final int HEX = 16;
-    
+
     private static final String[] KNOWN_ENTITIES = {"gt", "amp", "lt", "apos", "quot"};
-    
-    private static final Set<String> EXPANDABLE_HEAD_ELEMENT_TAG_NAME_SET = 
+
+    private static final Set<String> EXPANDABLE_HEAD_ELEMENT_TAG_NAME_SET =
         new HashSet<String>(Arrays.asList(new String [] { "SCRIPT", "STYLE", "TITLE" } ));
-    
-    private static final Set<String> PREFORMATTED_HEAD_ELEMENT_TAG_NAME_SET = 
+
+    private static final Set<String> PREFORMATTED_HEAD_ELEMENT_TAG_NAME_SET =
         new HashSet<String>(Arrays.asList(new String [] { "SCRIPT", "STYLE" } ));
-    
+
+    // For backward compatibility
+    private static final boolean IS_CONTENT_TYPE_TEXT_HTML_DEFAULT = false;
+
     private HeadElementUtils()
     {
     }
-    
+
     public static String toHtmlString(final HeadElement headElement)
     {
         String tagName = headElement.getTagName().toUpperCase();
         boolean isExpanedEmptyElements = EXPANDABLE_HEAD_ELEMENT_TAG_NAME_SET.contains(tagName);
         boolean isPreformattedTextContent = PREFORMATTED_HEAD_ELEMENT_TAG_NAME_SET.contains(tagName);
-        return toString(headElement, isExpanedEmptyElements, isPreformattedTextContent, false);
+        return toString(headElement, isExpanedEmptyElements, isPreformattedTextContent, false, IS_CONTENT_TYPE_TEXT_HTML_DEFAULT);
     }
-    
+
     public static String toXhtmlString(final HeadElement headElement)
     {
+        return toXhtmlString(headElement, IS_CONTENT_TYPE_TEXT_HTML_DEFAULT);
+    }
+
+    public static String toXhtmlString(final HeadElement headElement, boolean isContentTypeTextHtml)
+    {
         String tagName = headElement.getTagName().toUpperCase();
         boolean isExpanedEmptyElements = EXPANDABLE_HEAD_ELEMENT_TAG_NAME_SET.contains(tagName);
         boolean isPreformattedTextContent = PREFORMATTED_HEAD_ELEMENT_TAG_NAME_SET.contains(tagName);
-        return toString(headElement, isExpanedEmptyElements, isPreformattedTextContent, true);
+        return toString(headElement, isExpanedEmptyElements, isPreformattedTextContent, true, isContentTypeTextHtml);
     }
-    
+
-    public static String toString(final HeadElement headElement, boolean isExpanedEmptyElements, boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA)
+    public static String toString(final HeadElement headElement, boolean isExpanedEmptyElements,
+            boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA)
     {
+        return toString(headElement, isExpanedEmptyElements, isPreformattedTextContent,
+                isPreformattedTextContentInCDATA, IS_CONTENT_TYPE_TEXT_HTML_DEFAULT);
+    }
+
+    public static String toString(final HeadElement headElement, boolean isExpanedEmptyElements,
+            boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA,
+            boolean isContentTypeTextHtml)
+    {
         StringWriter writer = new StringWriter(80);
-        
+
         try
         {
-            writeHeadElement(writer, headElement, isExpanedEmptyElements, isPreformattedTextContent, isPreformattedTextContentInCDATA);
+            writeHeadElement(writer, headElement, isExpanedEmptyElements, isPreformattedTextContent,
+                    isPreformattedTextContentInCDATA, isContentTypeTextHtml);
         }
         catch (IOException e)
         {
             throw new RuntimeException(e);
         }
-        
+
         return writer.toString();
     }
-    
+
-    public static void writeHeadElement(final Writer writer, final HeadElement headElement, boolean isExpandEmptyElements, boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA) throws IOException
+    public static void writeHeadElement(final Writer writer, final HeadElement headElement,
+            boolean isExpandEmptyElements, boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA)
+            throws IOException
     {
+        writeHeadElement(writer, headElement, isExpandEmptyElements, isPreformattedTextContent,
+                isPreformattedTextContentInCDATA, IS_CONTENT_TYPE_TEXT_HTML_DEFAULT);
+    }
+
+    public static void writeHeadElement(final Writer writer, final HeadElement headElement,
+            boolean isExpandEmptyElements, boolean isPreformattedTextContent, boolean isPreformattedTextContentInCDATA,
+            boolean isContentTypeTextHtml) throws IOException
+    {
         String tagName = headElement.getTagName();
         writer.write('<');
         writer.write(tagName);
-        
+
         for (Map.Entry<String, String> entry : headElement.getAttributeMap().entrySet())
         {
             writer.write(' ');
@@ -93,11 +121,11 @@
             writer.write(encode(entry.getValue()));
             writer.write("\"");
         }
-        
+
         if (!headElement.hasChildHeadElements())
         {
             String textContent = headElement.getTextContent();
-            
+
             if (!isExpandEmptyElements && (textContent == null || "".equals(textContent)))
             {
                 writer.write("/>");
@@ -105,15 +133,24 @@
             else
             {
                 writer.write('>');
-                
+
-                if (textContent != null)
+                // Do not add CDATA tags when the text content is empty
+                if (textContent != null && !textContent.trim().isEmpty())
                 {
                     if (isPreformattedTextContent)
                     {
                         if (isPreformattedTextContentInCDATA)
                         {
+                            // XHTML page served as text/html: //<![CDATA[ ... //]]>
+                            // XHTML page served as XML: <![CDATA[ ... ]]>
+                            if (isContentTypeTextHtml) {
+                                writer.write("//");
+                            }
                             writer.write("<![CDATA[");
                             writer.write(textContent);
+                            if (isContentTypeTextHtml) {
+                                writer.write("//");
+                            }
                             writer.write("]]>");
                         }
                         else
@@ -126,7 +163,7 @@
                         writer.write(encode(textContent));
                     }
                 }
-                
+
                 writer.write("</");
                 writer.write(tagName);
                 writer.write('>');
@@ -135,24 +172,25 @@
         else
         {
             writer.write(">\n");
-            
+
             for (HeadElement childHeadElement : headElement.getChildHeadElements())
             {
-                writeHeadElement(writer, childHeadElement, isPreformattedTextContent, isExpandEmptyElements, isPreformattedTextContentInCDATA);
+                writeHeadElement(writer, childHeadElement, isPreformattedTextContent, isExpandEmptyElements,
+                        isPreformattedTextContentInCDATA, isContentTypeTextHtml);
                 writer.write('\n');
             }
-            
+
             writer.write("</");
             writer.write(tagName);
             writer.write('>');
         }
     }
-    
+
     /**
      * @deprecated Use {@link XmlUtils#encode(String)}.
      */
     public static String encode(String value) {
         return XmlUtils.encode(value);
     }
-    
+
 }
