Updated Branches:
  refs/heads/master 8b6efc432 -> 252b45f5f

WICKET-4453 [StyleAndScriptIdentifier] Better matching of CDATA-comments


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/252b45f5
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/252b45f5
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/252b45f5

Branch: refs/heads/master
Commit: 252b45f5f493f19516fdd7c6d17e5d82c2602116
Parents: 8b6efc4
Author: Martin Tzvetanov Grigorov <[email protected]>
Authored: Thu Mar 15 13:49:47 2012 +0200
Committer: Martin Tzvetanov Grigorov <[email protected]>
Committed: Thu Mar 15 13:52:44 2012 +0200

----------------------------------------------------------------------
 .../parser/filter/StyleAndScriptIdentifier.java    |   34 ++++++++-
 .../filter/StyleAndScriptIdentifierTest.java       |   58 +++++++++++++++
 2 files changed, 90 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/252b45f5/wicket-core/src/main/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifier.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifier.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifier.java
index 01c490d..0cb3e02 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifier.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifier.java
@@ -17,6 +17,7 @@
 package org.apache.wicket.markup.parser.filter;
 
 import java.text.ParseException;
+import java.util.regex.Pattern;
 
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.Markup;
@@ -25,6 +26,7 @@ import org.apache.wicket.markup.RawMarkup;
 import org.apache.wicket.markup.parser.AbstractMarkupFilter;
 import org.apache.wicket.markup.parser.XmlPullParser;
 import org.apache.wicket.core.util.string.JavaScriptUtils;
+import org.apache.wicket.util.lang.Args;
 
 
 /**
@@ -93,8 +95,7 @@ public final class StyleAndScriptIdentifier extends 
AbstractMarkupFilter
                                                        if (close.closes(open))
                                                        {
                                                                String text = 
body.toString().trim();
-                                                               if 
(!text.startsWith("<!--") && !text.startsWith("<![CDATA[") &&
-                                                                       
!text.startsWith("/*<![CDATA[*/"))
+                                                               if 
(shouldWrapInCdata(text))
                                                                {
                                                                        text = 
JavaScriptUtils.SCRIPT_CONTENT_PREFIX + body.toString() +
                                                                                
JavaScriptUtils.SCRIPT_CONTENT_SUFFIX;
@@ -108,6 +109,35 @@ public final class StyleAndScriptIdentifier extends 
AbstractMarkupFilter
                }
        }
 
+       // OES == optional empty space
+
+       // OES<!--OES
+       private static final Pattern HTML_START_COMMENT = 
Pattern.compile("^\\s*<!--\\s*.*", Pattern.DOTALL);
+
+       // OES<![CDATA[OES
+       private static final Pattern CDATA_START_COMMENT = 
Pattern.compile("^\\s*<!\\[CDATA\\[\\s*.*", Pattern.DOTALL);
+
+       // OES/*OES<![CDATA[OES*/OES
+       private static final Pattern JS_CDATA_START_COMMENT = 
Pattern.compile("^\\s*\\/\\*\\s*<!\\[CDATA\\[\\s*\\*\\/\\s*.*", Pattern.DOTALL);
+
+       boolean shouldWrapInCdata(final String elementBody)
+       {
+               Args.notNull(elementBody, "elementBody");
+
+               boolean shouldWrap = true;
+
+               if (
+                               
HTML_START_COMMENT.matcher(elementBody).matches() ||
+                               
CDATA_START_COMMENT.matcher(elementBody).matches() ||
+                               
JS_CDATA_START_COMMENT.matcher(elementBody).matches()
+                       )
+               {
+                       shouldWrap = false;
+               }
+
+               return shouldWrap;
+       }
+
        private boolean shouldProcess(ComponentTag openTag)
        {
                // do not wrap in CDATA any <script> which has special MIME 
type. WICKET-4425

http://git-wip-us.apache.org/repos/asf/wicket/blob/252b45f5/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
index 37e63a0..ef32104 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
@@ -17,6 +17,8 @@
 package org.apache.wicket.markup.parser.filter;
 
 import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.Markup;
+import org.apache.wicket.markup.MarkupElement;
 import org.junit.Test;
 
 /**
@@ -39,4 +41,60 @@ public class StyleAndScriptIdentifierTest extends 
WicketTestCase
                executeTest(PageWithScriptTemplate.class, 
"PageWithScriptTemplate_expected.html");
        }
 
+       @Test
+       public void showWrapInCdata()
+       {
+               StyleAndScriptIdentifier filter = new 
StyleAndScriptIdentifier();
+
+               String elementBody = "<!-- someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "\n<!-- someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "  <!-- someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+
+               elementBody = "<![CDATA[ someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "\n<![CDATA[ someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "  <![CDATA[ someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+
+               elementBody = "/*<![CDATA[*/ someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "\n/*<![CDATA[ */ someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+
+               elementBody = "  /* <![CDATA[ */ \n someJS() ";
+               assertFalse(filter.shouldWrapInCdata(elementBody));
+       }
+
+       /**
+        * https://issues.apache.org/jira/browse/WICKET-4453
+        *
+        * This test wraps rawMarkup in 
org.apache.wicket.util.string.JavaScriptUtils#SCRIPT_CONTENT_PREFIX
+        * twice - once in Markup.of() and second in the explicit call to 
StyleAndScriptIdentifier.postProcess().
+        * The second time it realizes that the element body is already wrapped 
and skips it.
+        */
+       @Test
+       public void postProcess()
+       {
+               String rawMarkup = "<script>someJS()</script>";
+               Markup createMarkupElementsMarkup = Markup.of(rawMarkup);
+               Markup markup = new 
Markup(createMarkupElementsMarkup.getMarkupResourceStream());
+               for (MarkupElement markupElement : createMarkupElementsMarkup)
+               {
+                       markup.addMarkupElement(markupElement);
+               }
+               StyleAndScriptIdentifier filter = new 
StyleAndScriptIdentifier();
+               filter.postProcess(markup);
+               
assertEquals("<script>\n/*<![CDATA[*/\nsomeJS()\n/*]]>*/\n</script>", 
markup.toString(true));
+       }
 }

Reply via email to