You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by bh...@apache.org on 2010/12/03 10:29:48 UTC

svn commit: r1041745 - in /shindig/trunk: features/src/main/javascript/features/opensocial-templates/ features/src/test/javascript/features/opensocial-templates/ php/src/gadgets/templates/ php/test/gadgets/

Author: bhofmann
Date: Fri Dec  3 09:29:47 2010
New Revision: 1041745

URL: http://svn.apache.org/viewvc?rev=1041745&view=rev
Log:
SHINDIG-1478: Added os:Var support in PHP and JavaScript

Added:
    shindig/trunk/php/test/gadgets/TemplateParserTest.php
Modified:
    shindig/trunk/features/src/main/javascript/features/opensocial-templates/os.js
    shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html
    shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js
    shindig/trunk/php/src/gadgets/templates/TemplateParser.php

Modified: shindig/trunk/features/src/main/javascript/features/opensocial-templates/os.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/opensocial-templates/os.js?rev=1041745&r1=1041744&r2=1041745&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/opensocial-templates/os.js (original)
+++ shindig/trunk/features/src/main/javascript/features/opensocial-templates/os.js Fri Dec  3 09:29:47 2010
@@ -29,6 +29,26 @@ os.defineBuiltinTags = function() {
       os.createNamespace('os', 'http://ns.opensocial.org/2008/markup');
 
   /**
+   * <os:Var> custom tag for variable assignment
+   */
+  osn.Var = function(node, data, context) {
+    var value = os.getValueFromNode_(node,'value');
+
+    if (! value && node.innerHTML) {
+      value = node.innerHTML;
+    }
+    
+    var parsedValue = gadgets.json.parse(value)
+     
+    if (parsedValue) {
+      value = parsedValue;
+    }
+
+    context['vars_'][VAR_top][node.getAttribute('key')] = value;
+    return '';
+  };
+
+  /**
    * <os:Render> custom tag renders the specified child nodes of the current
    * context.
    */

Modified: shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html?rev=1041745&r1=1041744&r2=1041745&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html (original)
+++ shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html Fri Dec  3 09:29:47 2010
@@ -22,6 +22,7 @@ under the License.
     <script src="../../lib/JsUtil.js"></script>
     <script src="../../lib/JsUnit.js"></script>
     <script>
+        var gadgets = {};
       // TODO: These adapters are needed because the tests were originally
       // created with a different version of JSUnit in mind. Refactor this file
       // and the various test files to use the TestCase class infrastructure.
@@ -46,7 +47,10 @@ under the License.
     <script src="../../../../main/javascript/features/opensocial-templates/jsTemplate/jstemplate.js"></script>
     
     <script src="../../../../main/javascript/features/opensocial-data-context/datacontext.js"></script>
+    <script src="../../../../main/javascript/features/jsondom/jsondom.js"></script>
     <script src="../../../../main/javascript/features/xmlutil/xmlutil.js"></script>
+    <script src="../../../../main/javascript/features/core.json/json.js"></script>
+    <script src="../../../../main/javascript/features/core.util/util.js"></script>
     <script src="../../../../main/javascript/features/opensocial-data/data.js"></script>
     <script src="../../../../main/javascript/features/opensocial-templates/base.js"></script>
     <script src="../../../../main/javascript/features/opensocial-templates/namespaces.js"></script>

Modified: shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js
URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js?rev=1041745&r1=1041744&r2=1041745&view=diff
==============================================================================
--- shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js (original)
+++ shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js Fri Dec  3 09:29:47 2010
@@ -765,3 +765,26 @@ function testOsIf() {
   var output = os.compileTemplateString('<os:Repeat expression="${list}"><os:If condition="${name != \'b\'}"><b>${name}</b> <b>${name}</b> </os:If></os:Repeat>').render(data);
   assertEquals("a a c c", domutil.getVisibleTextTrim(output));
 };
+
+function testOsVar() {
+
+  assertTemplateOutput(
+  '<div><os:Var key="counter" value="1" />${counter}</div>',
+  '<div>1</div>',
+  {});
+
+  assertTemplateOutput(
+  '<div><os:Var key="counter" value="1" /><os:Var key="counter" value="${counter + 1}" />${counter}</div>',
+  '<div>2</div>',
+  {});
+
+  assertTemplateOutput(
+  '<div><os:Var key="counter" value="[1,3,5,7]" />${counter[1]}</div>',
+  '<div>3</div>',
+  {});
+
+  assertTemplateOutput(
+  '<div><os:Var key="counter">{"key" : "value"}</os:Var>${counter.key}</div>',
+  '<div>value</div>',
+  {});
+}
\ No newline at end of file

Modified: shindig/trunk/php/src/gadgets/templates/TemplateParser.php
URL: http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/templates/TemplateParser.php?rev=1041745&r1=1041744&r2=1041745&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/templates/TemplateParser.php (original)
+++ shindig/trunk/php/src/gadgets/templates/TemplateParser.php Fri Dec  3 09:29:47 2010
@@ -71,6 +71,15 @@ class TemplateParser {
     $this->dataContext['Context'] = array('UniqueId' => uniqid());
   }
 
+  /**
+   * returns the current datacontext, used mainly for testing purposes
+   * 
+   * @return array
+   */
+  public function getDataContext() {
+    return $this->dataContext;
+  }
+
   public function parseNode(DOMNode &$node) {
     $removeNode = false;
     if ($node instanceof DOMText) {
@@ -496,7 +505,35 @@ class TemplateParser {
         $scriptBlock->appendChild($scriptCodeNode);
         return $node;
         break;
+      case 'os:var':
+        // handle expressions
+        $this->parseNodeAttributes($node);
 
+        if (! ($key = $node->getAttribute('key'))) {
+          throw new ExpressionException("os:Var missing attribute: key");
+        }
+
+        // either get value from attribute
+        if (! ($value = $node->getAttribute('value'))) {
+          $value = '';
+        }
+
+        // or from inner text of node
+        if (! $value && $node->textContent) {
+          $value = $node->textContent;
+        }
+
+        // try to decode if the value is a valid json object
+        $parsedValue = json_decode($value, true);
+
+        if ($parsedValue) {
+          $value = $parsedValue;
+        }
+
+        $this->dataContext['Top'][$key] = $value;
+
+        return $node;
+        break;
       case 'osx:navigatetoapp':
         break;
 

Added: shindig/trunk/php/test/gadgets/TemplateParserTest.php
URL: http://svn.apache.org/viewvc/shindig/trunk/php/test/gadgets/TemplateParserTest.php?rev=1041745&view=auto
==============================================================================
--- shindig/trunk/php/test/gadgets/TemplateParserTest.php (added)
+++ shindig/trunk/php/test/gadgets/TemplateParserTest.php Fri Dec  3 09:29:47 2010
@@ -0,0 +1,52 @@
+<?php
+/**
+ * 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.
+ */
+
+class TemplateParserTest extends PHPUnit_Framework_TestCase {
+  public function testOsVar() {
+    $viewNode = '<?xml version="1.0" encoding="UTF-8" ?>
+        <script xmlns:os="http://ns.opensocial.org/2008/markup" type="text/os-template">
+          <os:Var key="counter" value="1" />
+          <os:Var key="counter2" value="${counter + 1}" />
+          <os:Var key="array" value="[1,3,5,7]" />
+          <os:Var key="object">
+            {"key" : "value"}
+          </os:Var>
+        </script>';
+
+    $dataContext = array();
+    $doc = new DomDocument();
+    $doc->loadXml($viewNode);
+    $contentBlocks = $doc->getElementsByTagName('script');
+    $library = new TemplateLibrary(null);
+    $parser = new TemplateParser();
+    $tags = array();
+    foreach ($contentBlocks as $content) {
+      $tags[] = $parser->process($content, $dataContext, $library);
+    }
+    $this->assertEquals(1, count($tags));
+
+    $dataContext = $parser->getDataContext();
+
+    $this->assertEquals(1, $dataContext['Top']['counter']);
+    $this->assertEquals(2, $dataContext['Top']['counter2']);
+    $this->assertEquals(array(1,3,5,7), $dataContext['Top']['array']);
+    $this->assertEquals(array('key' => 'value'), $dataContext['Top']['object']);
+  }
+}
\ No newline at end of file