Author: evan
Date: Tue Dec 16 13:50:00 2008
New Revision: 727176

URL: http://svn.apache.org/viewvc?rev=727176&view=rev
Log:
SHINDIG-790
Regex fix to ${} detection in Opensocial Templates

Modified:
    incubator/shindig/trunk/features/opensocial-templates/base.js
    incubator/shindig/trunk/features/opensocial-templates/compiler.js
    incubator/shindig/trunk/features/opensocial-templates/compiler_test.js
    incubator/shindig/trunk/features/opensocial-templates/data.js
    incubator/shindig/trunk/features/opensocial-templates/data_test.js
    incubator/shindig/trunk/features/opensocial-templates/template_test.js
    
incubator/shindig/trunk/java/server/src/test/resources/endtoend/opensocial-templates/compiler_test.js

Modified: incubator/shindig/trunk/features/opensocial-templates/base.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/base.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/base.js (original)
+++ incubator/shindig/trunk/features/opensocial-templates/base.js Tue Dec 16 
13:50:00 2008
@@ -90,8 +90,8 @@
  * TODO(levik): Move all regular expressions here.
  */
 os.regExps_ = {
-  onlyWhitespace: /^[ \t\n]*$/,
-  variableSubstitution: /^([^$]*)(\$\{[^\}]*\})([\w\W]*)$/
+  ONLY_WHITESPACE: /^[ \t\n]*$/,
+  VARIABLE_SUBSTITUTION: /^([\w\W]*?)(\$\{[^\}]*\})([\w\W]*)$/
 };
 
 /**
@@ -334,5 +334,3 @@
   node[PROP_jstcache] = null;
   node.removeAttribute(ATT_jstcache);
 };
-
- 

Modified: incubator/shindig/trunk/features/opensocial-templates/compiler.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/compiler.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/compiler.js (original)
+++ incubator/shindig/trunk/features/opensocial-templates/compiler.js Tue Dec 
16 13:50:00 2008
@@ -51,7 +51,7 @@
       nodes.push(os.compileNode_(child));
     } else if (child.nodeType == DOM_TEXT_NODE) {
       if (child != node.firstChild ||
-          !child.nodeValue.match(os.regExps_.onlyWhitespace)) {
+          !child.nodeValue.match(os.regExps_.ONLY_WHITESPACE)) {
         var compiled = os.breakTextNode_(child);
         for (var i = 0; i < compiled.length; i++) {
           nodes.push(compiled[i]);
@@ -626,7 +626,7 @@
  * substitutions were found, or an empty array if none were.
  */
 os.breakTextNode_ = function(textNode) {
-  var substRex = os.regExps_.variableSubstitution;
+  var substRex = os.regExps_.VARIABLE_SUBSTITUTION;
   var text = textNode.data;
   var nodes = [];
   var match = text.match(substRex);
@@ -636,7 +636,7 @@
     }
     var token = match[2].substring(2, match[2].length - 1);
     if (!token) {
-      token = '$this';
+      token = VAR_this;
     }
     var tokenSpan = document.createElement("span");
     tokenSpan.setAttribute(ATT_content, os.transformExpression_(token));
@@ -673,7 +673,7 @@
   if (!value.length) {
     return null;
   }
-  var substRex = os.regExps_.variableSubstitution;
+  var substRex = os.regExps_.VARIABLE_SUBSTITUTION;
   var text = value;
   var parts = [];
   var match = text.match(substRex);
@@ -686,6 +686,9 @@
           os.trimWhitespaceForIE_(match[1], parts.length == 0)));
     }
     var expr = match[2].substring(2, match[2].length - 1);
+    if (!expr) {
+      expr = VAR_this;
+    }
     parts.push('(' + os.transformExpression_(expr) + ')');
     text = match[3];
     match = text.match(substRex);
@@ -740,6 +743,7 @@
 
     ret = [];
     for (var child = node.firstChild; child; child = child.nextSibling) {
+      console.log(child);
       if (allChildren) {
         ret.push(child);
         continue;
@@ -778,7 +782,6 @@
   } else if (ret == "0") {
     ret = 0;
   }
-
   return ret;
 };
 

Modified: incubator/shindig/trunk/features/opensocial-templates/compiler_test.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/compiler_test.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/compiler_test.js 
(original)
+++ incubator/shindig/trunk/features/opensocial-templates/compiler_test.js Tue 
Dec 16 13:50:00 2008
@@ -16,6 +16,24 @@
  * specific language governing permissions and limitations under the License.
  */
 
+function testSubstitution() {
+  var values = [
+    [ "hello world", null ],
+    [ "hello $world", null ],
+    [ "hello ${Cur} world", "'hello '+($this)+' world'" ],
+    [ "${Cur} hello", "($this)+' hello'" ],
+    [ "hello ${Cur}", "'hello '+($this)" ],
+    [ "$ ${Cur}", "'$ '+($this)" ],
+    [ "$${Cur}", "'$'+($this)" ],
+    [ "${Cur} ${Index}", "($this)+' '+($index)"],
+    [ "a ${Cur} b ${Index} c", "'a '+($this)+' b '+($index)+' c'"]
+  ];
+  for (var i = 0; i < values.length; i++) {
+    var compiled = os.parseAttribute_(values[i][0]);
+    assertEquals(values[i][1], compiled);
+  }
+};
+
 /**
  * Unit test for compiler identifier wrapping.
  * TODO(kjin): test all of the following:

Modified: incubator/shindig/trunk/features/opensocial-templates/data.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/data.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/data.js (original)
+++ incubator/shindig/trunk/features/opensocial-templates/data.js Tue Dec 16 
13:50:00 2008
@@ -137,7 +137,7 @@
  * @private
  */
 os.data.RequestDescriptor.prototype.computeNeededKeys_ = function(attribute) {
-  var substRex = os.regExps_.variableSubstitution;
+  var substRex = os.regExps_.VARIABLE_SUBSTITUTION;
   var match = attribute.match(substRex);
   while (match) {
     var token = match[2].substring(2, match[2].length - 1);

Modified: incubator/shindig/trunk/features/opensocial-templates/data_test.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/data_test.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/data_test.js 
(original)
+++ incubator/shindig/trunk/features/opensocial-templates/data_test.js Tue Dec 
16 13:50:00 2008
@@ -41,7 +41,7 @@
   var xmlData =
       '<test:request key="first" data="testData"/>' +
       '<test:request key="second" data="${foo}"/>';
-window.console.log(os.data);
+
   os.data.loadRequests(xmlData);
   assertNotNull(os.data.requests_['first']);
   assertNotNull(os.data.requests_['second']);

Modified: incubator/shindig/trunk/features/opensocial-templates/template_test.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-templates/template_test.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-templates/template_test.js 
(original)
+++ incubator/shindig/trunk/features/opensocial-templates/template_test.js Tue 
Dec 16 13:50:00 2008
@@ -352,7 +352,6 @@
 function testList() {
   os.Container.registerTag('custom:list');
   var output = compileAndRender_('_T_List');
-  document.body.appendChild(output);
   assertEquals('helloworld', domutil.getVisibleText(output));
 }
 

Modified: 
incubator/shindig/trunk/java/server/src/test/resources/endtoend/opensocial-templates/compiler_test.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/test/resources/endtoend/opensocial-templates/compiler_test.js?rev=727176&r1=727175&r2=727176&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/server/src/test/resources/endtoend/opensocial-templates/compiler_test.js
 (original)
+++ 
incubator/shindig/trunk/java/server/src/test/resources/endtoend/opensocial-templates/compiler_test.js
 Tue Dec 16 13:50:00 2008
@@ -16,6 +16,24 @@
  * specific language governing permissions and limitations under the License.
  */
 
+function testSubstitution() {
+  var values = [
+    [ "hello world", null ],
+    [ "hello $world", null ],
+    [ "hello ${Cur} world", "'hello '+($this)+' world'" ],
+    [ "${Cur} hello", "($this)+' hello'" ],
+    [ "hello ${Cur}", "'hello '+($this)" ],
+    [ "$ ${Cur}", "'$ '+($this)" ],
+    [ "$${Cur}", "'$'+($this)" ],
+    [ "${Cur} ${Index}", "($this)+' '+($index)"],
+    [ "a ${Cur} b ${Index} c", "'a '+($this)+' b '+($index)+' c'"]
+  ];
+  for (var i = 0; i < values.length; i++) {
+    var compiled = os.parseAttribute_(values[i][0]);
+    assertEquals(values[i][1], compiled);
+  }
+}
+
 /**
  * Unit test for compiler identifier wrapping.
  *    "'a'",


Reply via email to