You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/12/20 11:32:11 UTC

svn commit: r605876 - in /incubator/sling/trunk/scripting/javascript/src: main/java/org/apache/sling/scripting/javascript/helper/EspReader.java test/java/org/apache/sling/scripting/javascript/EspReaderTest.java

Author: fmeschbe
Date: Thu Dec 20 02:32:09 2007
New Revision: 605876

URL: http://svn.apache.org/viewvc?rev=605876&view=rev
Log:
SLING-142 Apply patch to support compact ECMA expressions in HTML tag attributes

Modified:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/EspReader.java
    incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/EspReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/EspReader.java?rev=605876&r1=605875&r2=605876&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/EspReader.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/EspReader.java Thu Dec 20 02:32:09 2007
@@ -122,6 +122,23 @@
      * comment is read (and completely returned).
      */
     private static final byte PARSE_STATE_ECMA_COMMENTL = 10;
+    
+    /**
+     * Attribute value (single quote) reading state. This is needed for identifying the compact
+     * syntax which is only allowed in attributes.
+     */
+    private static final byte PARSE_STATE_ATTRIBUTE_SINGLE = 11;
+    
+    /**
+     * Attribute value (double quote) reading state. This is needed for identifying the compact
+     * syntax which is only allowed in attributes.
+     */
+    private static final byte PARSE_STATE_ATTRIBUTE_DOUBLE = 12;
+    
+    /**
+     * Compact JSP expression syntax to be used in attributes.
+     */
+    private static final byte PARSE_STATE_COMPACT_ATTRIBUTE = 13;
 
     /**
      * To work with lookahead and character insertion, we use a PushbackReader.
@@ -416,14 +433,26 @@
 
                 // Template text state - text is wrapped in out.write()
                 case PARSE_STATE_ESP:
-
-                    // might start ECMA code/expr, ESP comment or JSP comment
-                    if (c == '<') {
+                    if (c == '=') { //might start HTML attribute definition
+                    	int c2 = input.read();
+                    	if (c2 == '"') {
+                    		pushState(PARSE_STATE_ATTRIBUTE_DOUBLE);
+                    		//input.unread(c2);
+                    		doVerbatim("=\\\"");
+                    		continue;
+                    	} else if (c2 == '\'') {
+                    		pushState(PARSE_STATE_ATTRIBUTE_SINGLE);
+                    		doVerbatim("='");
+                    		continue;
+                    	} else {
+                    		input.unread(c2);
+                    		continue;
+                    	}
+                    } else  if (c == '<') { // might start ECMA code/expr, ESP comment or JSP comment
                         int c2 = input.read();
                         int c3 = input.read();
 
                         if (c2 == '%') {
-
                             // ECMA or JSP comment
 
                             if (c3 == '=') {
@@ -662,6 +691,64 @@
                     }
 
                     break;
+                    
+                case PARSE_STATE_ATTRIBUTE_DOUBLE:
+                	if (c == '"') {
+                		input.unread(c);
+                		popState();
+                		continue;
+                	} else if (c == '$') {
+                		int c2 = input.read();
+                		if (c2 == '{') {
+	                		// ECMA expression attribute="bla${1+1}"
+	                        pushState(PARSE_STATE_COMPACT_ATTRIBUTE);
+	                        startWrite(null);
+	                        if (!lineStart) {
+	                            doVerbatim("\");");
+	                        }
+	                        continue;
+                		} else {
+                			//false alarm
+                			input.unread(c2);
+                		}
+                	}
+                	break;
+                	
+                case PARSE_STATE_ATTRIBUTE_SINGLE:
+                	if (c == '\'') {
+                		input.unread(c);
+                		popState();
+                		continue;
+                	} else if (c == '$') {
+                		int c2 = input.read();
+                		if (c2 == '{') {
+	                		// ECMA expression attribute="bla${1+1}"
+	                        pushState(PARSE_STATE_COMPACT_ATTRIBUTE);
+	                        startWrite(null);
+	                        if (!lineStart) {
+	                            doVerbatim("\");");
+	                        }
+	                        continue;
+                		} else {
+                			//false alarm
+                			input.unread(c2);
+                		}
+                	}
+                	break;
+            	
+                case PARSE_STATE_COMPACT_ATTRIBUTE:
+                	if (c == '}') { //might be the end of a compact expression
+                        // An expression is wrapped in out.write()
+            			popState();
+                        doVerbatim(");");
+
+                        // next ESP needs out.write(
+                        lineStart = true;
+
+                        continue;
+
+                    }
+                	break;
 
                 // What ???!!!
                 default:

Modified: incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java?rev=605876&r1=605875&r2=605876&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/EspReaderTest.java Thu Dec 20 02:32:09 2007
@@ -125,6 +125,34 @@
         assertEquals("", parse("<%-- test(); --%>"));
     }
     
+    public void testCompactExpressionsDouble() throws IOException {
+    	final String input = "<html version=\"${1+1}\">\n";
+    	final String expected = "out=response.writer;out.write(\"<html version=\\\"\");out.write(1+1);out.write(\"\\\">\\n\");\n";
+    	final String actual = parse(input);
+        assertEquals(flatten(expected), flatten(actual));
+    }
+    
+    public void testCompactExpressionsDoubleNegative() throws IOException {
+    	final String input = "<html version=\"{1+1}\">\n";
+    	final String expected = "out=response.writer;out.write(\"<html version=\\\"{1+1}\\\">\\n\");\n";
+    	final String actual = parse(input);
+        assertEquals(flatten(expected), flatten(actual));
+    }
+    
+    public void testCompactExpressionsSingle() throws IOException {
+    	final String input = "<html version='${1+1}'>\n";
+    	final String expected = "out=response.writer;out.write(\"<html version='\");out.write(1+1);out.write(\"'>\\n\");\n";
+    	final String actual = parse(input);
+        assertEquals(flatten(expected), flatten(actual));
+    }
+    
+    public void testCompactExpressionsSingleNegative() throws IOException {
+    	final String input = "<html version='{1+1}'>\n";
+    	final String expected = "out=response.writer;out.write(\"<html version='{1+1}'>\\n\");\n";
+    	final String actual = parse(input);
+        assertEquals(flatten(expected), flatten(actual));
+    }
+    
     /** Test a complete template, using all features */
     public void testCompleteTemplate() throws IOException {
         final String input =