incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r602860 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/experimental/ test/java/org/apache/sling/microsling/experimental/ test/java/org/apache/sling/microsling/integration/ test/resources/in...
Date Mon, 10 Dec 2007 10:51:34 GMT
Author: bdelacretaz
Date: Mon Dec 10 02:51:13 2007
New Revision: 602860

URL: http://svn.apache.org/viewvc?rev=602860&view=rev
Log:
SLING-144 - ScriptFilteredCopy added to avoid </script> tags in generated out.write
statements

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
  (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/EctScriptEngine.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
    incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.ect

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/EctScriptEngine.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/EctScriptEngine.java?rev=602860&r1=602859&r2=602860&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/EctScriptEngine.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/EctScriptEngine.java
Mon Dec 10 02:51:13 2007
@@ -51,6 +51,7 @@
     public static final String ECT_SCRIPT_EXTENSION = "ect";
     private final List<String> libraryScripts = new LinkedList<String>();
     private final DefaultHtmlRenderer htmlRenderer;
+    private final ScriptFilteredCopy copier = new ScriptFilteredCopy();
     
     public EctScriptEngine() {
         // TODO hardcoded for now...
@@ -66,7 +67,7 @@
         // client run the script
         final PrintWriter w = (PrintWriter)(props.get(SlingScriptEngine.OUT));
         final EspReader er = new EspReader(script.getScriptReader());
-        er.setOutInitStatement("out=document;");
+        er.setOutInitStatement("out=document;\n");
         
         try {
             // access our data (need a Node)
@@ -110,7 +111,7 @@
             
             // output our parsed script, first in body
             w.println("<div id=\"EctRenderingScript\">\n<script language='javascript'>");
-            copy(er,w);
+            copier.copy(er,w);
             w.println("</script>\n</div>");
             
             // default rendering, turned off automatically from the javascript that 
@@ -146,12 +147,4 @@
         return new String [] { ECT_SCRIPT_EXTENSION };
     }
     
-    private static void copy(Reader r, Writer w) throws IOException {
-        final int bufsize = 16384;
-        final char [] buffer = new char[bufsize];
-        int n = 0;
-        while( (n = r.read(buffer, 0, bufsize)) > 0) {
-            w.write(buffer, 0, n);
-        }
-    }
 }

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java?rev=602860&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
Mon Dec 10 02:51:13 2007
@@ -0,0 +1,67 @@
+/*
+ * 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.sling.microsling.experimental;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+
+/** Copy the ECT script to the output, filtering <script>
+ *  tags on the way (see SLING-114 comment 10/Dec/07 01:00 AM).
+ *  To simplify things, assumes each out.write statement is on
+ *  its own line in input.  
+ */  
+class ScriptFilteredCopy {
+
+    /** Read r line-by-line, process lines that need to be
+     *  modified, and write results to w
+     */
+    void copy(Reader r, Writer w) throws IOException {
+        final BufferedReader br = new BufferedReader(r);
+        String line = null;
+        while( (line = br.readLine()) != null) {
+            final String toWrite = processLine(line); 
+            w.write(toWrite, 0, toWrite.length());
+            w.write('\n');
+        }
+    }
+
+    /** Transform lines that look like
+     *  <pre>
+     *      out.write("something and a <script> tag");
+     *  </pre>
+     *  
+     *  Into
+     *   <pre>
+     *      out.write("something and a <");
+     *      out.write("script> tag");
+     *   </pre>
+     *   
+     *  To work around browsers problems when they 
+     *  see a </script> tag in a String. 
+     */
+    String processLine(String line) {
+        if(line.startsWith("out.write(") && line.endsWith("\");")) {
+            return line.replaceAll("script>","\");\nout.write(\"script>");
+        } else {
+            return line;
+        }
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/experimental/ScriptFilteredCopy.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java?rev=602860&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
Mon Dec 10 02:51:13 2007
@@ -0,0 +1,83 @@
+/*
+ * 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.sling.microsling.experimental;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+/** Test the ScriptFilteredCopy */
+public class ScriptFilteredCopyTest extends TestCase {
+    
+    private final ScriptFilteredCopy sfc = new ScriptFilteredCopy();
+    
+    private void runTest(String input, String expected) throws IOException {
+        final StringWriter sw = new StringWriter();
+        sfc.copy(new StringReader(input), sw);
+        assertEquals(flatten(expected), flatten(sw.toString()));
+    }
+    
+    public void testNoChanges() throws IOException {
+        final String input = "No out.write statements on their own lines";
+        final String expected = input + "\n";
+        runTest(input,expected);
+    }
+    
+    public void testOpenClose() throws IOException {
+        final String input = 
+            "A\n"
+            + "out.write(\"some <script> here </script>\");\n"
+            + "B\n";
+        
+        final String expected = 
+        "A\n"
+        + "out.write(\"some <\");\n"
+        + "out.write(\"script> here </\");\n"
+        + "out.write(\"script>\");\n"
+        + "B\n";
+        
+        runTest(input,expected);
+    }
+    
+    public void testOpenAtStart() throws IOException {
+        final String input = "out.write(\"<script> here\");\n";
+        final String expected = 
+            "out.write(\"<\");\n"
+            + "out.write(\"script> here\");\n"
+        ;
+        
+        runTest(input,expected);
+    }
+
+    public void testOpenAtEnd() throws IOException {
+        final String input = "out.write(\"Here a <script>\");\n";
+        final String expected = 
+            "out.write(\"Here a <\");\n"
+            + "out.write(\"script>\");\n"
+        ;
+        runTest(input,expected);
+    }
+    
+    /** Replace \n with . in strings to make it easier to compare visually for testing */
+    private static String flatten(String str) {
+        return str.replace('\n', '.');
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/experimental/ScriptFilteredCopyTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java?rev=602860&r1=602859&r2=602860&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/SlingResourceTypeRenderingTest.java
Mon Dec 10 02:51:13 2007
@@ -154,6 +154,22 @@
         }
     }
 
+    public void testEctHtmlScriptTag() throws IOException {
+        final String toDelete = uploadTestScript("rendering-test.ect","html.ect");
+        try {
+            final String content = getContent(displayUrl + ".html", CONTENT_TYPE_HTML);
+            assertTrue("Content includes ECT marker",content.contains("ECT template"));
+            assertTrue("Content contains scripted stuff (" + content + ")",
+                    content.contains("something scripted"));
+            assertFalse("Script opening tag must be broken in two in content (" + content
+ ")",
+                    content.contains("<script>something")); 
+            assertFalse("Script closing tag must be broken in two in content (" + content
+ ")",
+                    content.contains("scripted</script>")); 
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
+
     public void testEspHtmlUppercase() throws IOException {
         final String toDelete = uploadTestScript("rendering-test.esp","html.esp");
         try {

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.ect
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.ect?rev=602860&r1=602859&r2=602860&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.ect
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/resources/integration-test/rendering-test.ect
Mon Dec 10 02:51:13 2007
@@ -6,6 +6,12 @@
 <p>
   <%= currentNode.title %> </b>
 </p>
+
 <p>
   <%= currentNode.text %> </em>
-</p> 
+</p>
+
+<!-- verify that script tag is broken in two when rendering -->
+<!-- (SLING-114) -->
+ 
+<script>something scripted</script>
\ No newline at end of file



Mime
View raw message