You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by jo...@apache.org on 2008/08/20 03:37:43 UTC

svn commit: r687219 - in /incubator/shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java

Author: johnh
Date: Tue Aug 19 18:37:43 2008
New Revision: 687219

URL: http://svn.apache.org/viewvc?rev=687219&view=rev
Log:
Adding support for rendering of comments in text nodes.


Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java?rev=687219&r1=687218&r2=687219&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/parse/GadgetHtmlNode.java Tue Aug 19 18:37:43 2008
@@ -300,7 +300,33 @@
    */
   public void render(Writer w) throws IOException {
     if (isText()) {
-      w.append(StringEscapeUtils.escapeHtml(getText()));
+      String rawText = getText();
+      int commentStart = 0;
+      int curPos = 0;
+      while ((commentStart = rawText.indexOf("<!--", curPos)) >= 0) {
+        // Comment found. By definition there must be an end-comment marker
+        // since if there wasn't, the comment would subsume all further text.
+        
+        // First append up to the current point, with proper escaping.
+        w.append(StringEscapeUtils.escapeHtml(rawText.substring(curPos, commentStart)));
+        
+        // Then append the comment verbatim.
+        int commentEnd = rawText.indexOf("-->", commentStart);
+        if (commentEnd == -1) {
+          // Should never happen, per above comment. But we know that the comment
+          // has begun, so just append the rest of the string verbatim to be safe.
+          w.append(rawText.substring(commentStart));
+          return;
+        }
+        int endPos = commentEnd + "-->".length();
+        w.append(rawText.substring(commentStart, endPos));
+        
+        // Then set current position
+        curPos = endPos;
+      }
+      
+      // Append remaining (all, if no comment) text, escaped.
+      w.append(StringEscapeUtils.escapeHtml(rawText.substring(curPos)));
     } else {
       w.append('<').append(tagName);
       for (String attrKey : getAttributeKeys()) {

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java?rev=687219&r1=687218&r2=687219&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/parse/GadgetHtmlNodeTest.java Tue Aug 19 18:37:43 2008
@@ -511,6 +511,25 @@
                  renderNode(parent));
   }
   
+  public void testRenderCommentAlone() {
+    String comment = "<!-- comment -->";
+    GadgetHtmlNode commentNode = new GadgetHtmlNode(comment);
+    assertEquals(comment, renderNode(commentNode));
+  }
+  
+  public void testRenderCommentWithWhitespace() {
+    String comment = "\n   <!--      comment\n  \n -->";
+    GadgetHtmlNode commentNode = new GadgetHtmlNode(comment);
+    assertEquals(comment, renderNode(commentNode));
+  }
+  
+  public void testRenderTextWithCommentAndEscaped() {
+    String text = "\n <!-- comment\n <br> --> <foo&bar>";
+    GadgetHtmlNode textNode = new GadgetHtmlNode(text);
+    assertEquals("\n <!-- comment\n <br> --> &lt;foo&amp;bar&gt;",
+                 renderNode(textNode));
+  }
+  
   private String renderNode(GadgetHtmlNode node) {
     StringWriter sw = new StringWriter();
     try {