Updated Branches:
  refs/heads/wicket-1.5.x 2ab7df583 -> d80606016

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/d8060601
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/d8060601
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/d8060601

Branch: refs/heads/wicket-1.5.x
Commit: d80606016e1f84da1777cbf66c28edbf4fd66914
Parents: 2ab7df5
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:49:47 2012 +0200

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


http://git-wip-us.apache.org/repos/asf/wicket/blob/d8060601/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 957de64..9010980 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;
@@ -24,6 +25,7 @@ import org.apache.wicket.markup.MarkupElement;
 import org.apache.wicket.markup.RawMarkup;
 import org.apache.wicket.markup.parser.AbstractMarkupFilter;
 import org.apache.wicket.markup.parser.XmlPullParser;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.string.JavaScriptUtils;
 
 
@@ -95,8 +97,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;
@@ -110,6 +111,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/d8060601/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
new file mode 100644
index 0000000..0181ba9
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/parser/filter/StyleAndScriptIdentifierTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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;
+
+/**
+ * @since 1.5.6
+ */
+public class StyleAndScriptIdentifierTest extends WicketTestCase
+{
+       @Test
+       public void showWrapInCdata()
+       {
+               StyleAndScriptIdentifier filter = new 
StyleAndScriptIdentifier(Markup.NO_MARKUP);
+
+               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(Markup.NO_MARKUP);
+               filter.postProcess(markup);
+               
assertEquals("<script>\n/*<![CDATA[*/\nsomeJS()\n/*]]>*/\n</script>", 
markup.toString(true));
+       }
+}

Reply via email to