You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by aw...@apache.org on 2009/02/19 22:59:41 UTC

svn commit: r746016 - in /incubator/shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/parse/nekohtml/ test/java/org/apache/shindig/gadgets/parse/nekohtml/ test/resources/org/apache/shindig/gadgets/parse/nekohtml/

Author: awiner
Date: Thu Feb 19 21:59:41 2009
New Revision: 746016

URL: http://svn.apache.org/viewvc?rev=746016&view=rev
Log:
Bug fix: with the social markup parser enabled, client-side templating broke, as:
  <script type="text/os-template>
     <img/>
  </script>
... had the <img/> rewritten to <img>, which isn't valid XML.  Now serializing all
<script> and <style> child elements as XML, which in practice only affects social
markup. 

Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/nekohtml/NekoSerializer.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/nekohtml/SocialMarkupHtmlParserTest.java
    incubator/shindig/trunk/java/gadgets/src/test/resources/org/apache/shindig/gadgets/parse/nekohtml/test-socialmarkup.html

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/nekohtml/NekoSerializer.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/nekohtml/NekoSerializer.java?rev=746016&r1=746015&r2=746016&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/nekohtml/NekoSerializer.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/nekohtml/NekoSerializer.java Thu Feb 19 21:59:41 2009
@@ -19,7 +19,6 @@
 package org.apache.shindig.gadgets.parse.nekohtml;
 
 import org.apache.shindig.gadgets.parse.HtmlSerializer;
-
 import org.cyberneko.html.HTMLElements;
 import org.cyberneko.html.HTMLEntities;
 import org.w3c.dom.Attr;
@@ -57,6 +56,11 @@
   }
 
   public static void serialize(Node n, Appendable output) throws IOException {
+    serialize(n, output, false);
+  }
+    
+  private static void serialize(Node n, Appendable output, boolean xmlMode)
+      throws IOException {
     switch (n.getNodeType()) {
       case Node.CDATA_SECTION_NODE: {
         break;
@@ -66,17 +70,23 @@
         break;
       }
       case Node.DOCUMENT_NODE: {
-        serialize(((Document)n).getDocumentElement(), output);
+        serialize(((Document)n).getDocumentElement(), output, xmlMode);
         break;
       }
       case Node.ELEMENT_NODE: {
         Element elem = (Element)n;
         HTMLElements.Element htmlElement =
             HTMLElements.getElement(elem.getNodeName());
+        
         NodeList children = elem.getChildNodes();
-        printStartElement(elem, output);
+        printStartElement(elem, output, xmlMode && htmlElement.isEmpty());
+        
+        // Special HTML elements - <script> in particular - will typically
+        // only have CDATA.  If they do have elements, that'd be data pipelining
+        // or templating kicking in, and we should use XML-format output.
+        boolean childXmlMode = xmlMode || htmlElement.isSpecial();
         for (int i = 0; i < children.getLength(); i++) {
-          serialize(children.item(i), output);
+          serialize(children.item(i), output, childXmlMode);
         }
         if (!htmlElement.isEmpty()) {
           output.append("</").append(elem.getNodeName()).append('>');
@@ -109,7 +119,19 @@
     output.append(">\n");
   }
 
+  /**
+   * Print the start of an HTML element. 
+   */
   public static void printStartElement(Element elem, Appendable output) throws IOException {
+    printStartElement(elem, output, false);
+  }
+  
+  /**
+   * Print the start of an HTML element.  If withXmlClose==true, this is an
+   * empty element that should have its content 
+   */
+  private static void printStartElement(Element elem, Appendable output, boolean withXmlClose)
+      throws IOException {
     output.append("<").append(elem.getTagName());
     NamedNodeMap attributes = elem.getAttributes();
     for (int i = 0; i < attributes.getLength(); i++) {
@@ -122,7 +144,7 @@
         output.append('"');
       }
     }
-    output.append(">");
+    output.append(withXmlClose ? "/>" : ">");
   }
 
   public static void printAttributeValue(String text, Appendable output) throws IOException {

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/nekohtml/SocialMarkupHtmlParserTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/nekohtml/SocialMarkupHtmlParserTest.java?rev=746016&r1=746015&r2=746016&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/nekohtml/SocialMarkupHtmlParserTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/nekohtml/SocialMarkupHtmlParserTest.java Thu Feb 19 21:59:41 2009
@@ -20,6 +20,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.shindig.gadgets.parse.ParseModule;
@@ -81,6 +82,13 @@
   }
 
   @Test
+  public void testSocialTemplateSerialization() {
+    String content = NekoSerializer.serialize(document);
+    assertTrue("Empty elements not preserved as XML inside template",
+        content.indexOf("<img/>") >= 0);
+  }
+
+  @Test
   public void testJavascript() {
     // Verify text content is unmodified in javascript blocks
     List<Element> scripts = getScripts("text/javascript");

Modified: incubator/shindig/trunk/java/gadgets/src/test/resources/org/apache/shindig/gadgets/parse/nekohtml/test-socialmarkup.html
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/resources/org/apache/shindig/gadgets/parse/nekohtml/test-socialmarkup.html?rev=746016&r1=746015&r2=746016&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/resources/org/apache/shindig/gadgets/parse/nekohtml/test-socialmarkup.html (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/resources/org/apache/shindig/gadgets/parse/nekohtml/test-socialmarkup.html Thu Feb 19 21:59:41 2009
@@ -4,6 +4,7 @@
 
 <script type="text/os-template">
   <b>Some ${viewer} content</b>
+  <img/>
 </script>
 
 <script type="text/javascript">