You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by an...@apache.org on 2006/12/29 15:12:16 UTC

svn commit: r491017 - in /tapestry/tapestry4/trunk: src/site/xdoc/usersguide/ tapestry-framework/src/java/org/apache/tapestry/script/ tapestry-framework/src/test/org/apache/tapestry/junit/script/

Author: andyhot
Date: Fri Dec 29 06:12:15 2006
New Revision: 491017

URL: http://svn.apache.org/viewvc?view=rev&rev=491017
Log:
TAPESTRY-1196: unique in <let> (in .script files) is now safer for javascript identifier generation

Modified:
    tapestry/tapestry4/trunk/src/site/xdoc/usersguide/script.xml
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/script/LetToken.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/TestScript.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/unique-let.script

Modified: tapestry/tapestry4/trunk/src/site/xdoc/usersguide/script.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/src/site/xdoc/usersguide/script.xml?view=diff&rev=491017&r1=491016&r2=491017
==============================================================================
--- tapestry/tapestry4/trunk/src/site/xdoc/usersguide/script.xml (original)
+++ tapestry/tapestry4/trunk/src/site/xdoc/usersguide/script.xml Fri Dec 29 06:12:15 2006
@@ -378,6 +378,15 @@
                     conflicts between different script templates, or successive executions of the
                     same script template.
                 </p>
+               
+                <span class="info">
+                    <strong>Note:</strong>
+                    <p>
+                        Starting with Tapestry 4.1.2, unique also ensures that the returned string can
+                        be safely used as a javascript identifier (by replacing hyphens, colons and periods with
+                        underscores).
+                    </p>
+                </span>
 
                 <table>
 

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/script/LetToken.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/script/LetToken.java?view=diff&rev=491017&r1=491016&r2=491017
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/script/LetToken.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/script/LetToken.java Fri Dec 29 06:12:15 2006
@@ -58,7 +58,12 @@
 
         String value = useBuffer.toString().trim();
 
-        if (_unique) value = session.getUniqueString(value);
+        if (_unique) {
+            value = session.getUniqueString(value);
+            
+            // unique in scripts is mostly used to generate javascript identifiers
+            value = makeValidIdentifier(value);
+        }
 
         symbols.put(_key, value);
 
@@ -67,5 +72,19 @@
 
         _bufferLengthHighwater = Math.max(_bufferLengthHighwater, useBuffer
                 .length());
+    }
+    
+    /**
+     * Replaces hyphens, colons and periods (which are allowed in html 
+     * but not in javascript identifiers) with underscores.
+     */
+    private static String makeValidIdentifier(String id) {
+        char[] chars = id.toCharArray();
+        for (int i=0; i<chars.length; i++) {
+            char c = chars[i];
+            if (c==':' || c=='-' || c=='.')
+                chars[i]='_';
+        }
+        return new String(chars);
     }
 }

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/TestScript.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/TestScript.java?view=diff&rev=491017&r1=491016&r2=491017
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/TestScript.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/TestScript.java Fri Dec 29 06:12:15 2006
@@ -177,6 +177,10 @@
         assertSymbol(symbols, "alpha", "Alpha");
         assertSymbol(symbols, "beta", "Alpha_0");
         assertSymbol(symbols, "gamma", "Alpha_1");
+        
+        assertSymbol(symbols, "id1", "form1_field");
+        assertSymbol(symbols, "id2", "form1_field_0");
+        assertSymbol(symbols, "id3", "form1_field_last_name");
     }
 
     public void testIncludeScript() throws Exception

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/unique-let.script
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/junit/script/unique-let.script?view=diff&rev=491017&r1=491016&r2=491017
==============================================================================
Binary files - no diff available.