You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by ev...@apache.org on 2009/04/29 08:57:42 UTC
svn commit: r769686 - in /incubator/shindig/trunk:
features/src/main/javascript/features/opensocial-templates/
features/src/main/javascript/features/opensocial-templates/jsTemplate/
features/src/test/javascript/features/opensocial-templates/ java/serve...
Author: evan
Date: Wed Apr 29 06:57:41 2009
New Revision: 769686
URL: http://svn.apache.org/viewvc?rev=769686&view=rev
Log:
SHINDIG-1036
Patch to bring client-side templates up to latest spec.
Modified:
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/base.js
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/compiler.js
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/jsTemplate/jsevalcontext.js
incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/compiler_test.js
incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html
incubator/shindig/trunk/features/src/test/javascript/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/src/main/javascript/features/opensocial-templates/base.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/base.js?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/base.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/base.js Wed Apr 29 06:57:41 2009
@@ -290,21 +290,25 @@
return;
}
+ var ctx = null;
// Process tag's inner content before processing the tag.
- for (var child = node.firstChild; child; child = child.nextSibling) {
+ for (var child = node.firstChild; child; child = child.nextSibling) {
if (child.nodeType == DOM_ELEMENT_NODE) {
- jstProcess(context, child);
+ if (ctx == null) {
+ var selectInner = node[PROP_jstcache] ? node[PROP_jstcache][ATT_innerselect] : null;
+ if (selectInner) {
+ var data = context.jsexec(selectInner, node);
+ ctx = context.clone(data, 0, 0);
+ } else {
+ ctx = context;
+ }
+ }
+ jstProcess(ctx, child);
os.markNodeToSkip(child);
}
- }
+ }
- var ctx = context;
- var selectInner = node[PROP_jstcache] ? node[PROP_jstcache][ATT_innerselect] : null;
- if (selectInner) {
- var data = context.jsexec(selectInner, node);
- ctx = context.clone(data, 0, 0);
- }
-
+ ctx = context.clone({}, 0, 0);
var result = tagFunction.call(null, node, data, ctx);
if (!result && typeof(result) != "string") {
Modified: incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/compiler.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/compiler.js?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/compiler.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/compiler.js Wed Apr 29 06:57:41 2009
@@ -142,17 +142,10 @@
'Cur': VAR_this,
'$cur': VAR_this,
'Top': VAR_top,
- 'Index': VAR_index,
- 'Count': VAR_count
+ 'Context': VAR_loop
};
/**
- * Regular expression for finding and removing the "Context" variable
- * reference from top-level identifier chains.
- */
-os.regExps_.CONTEXT_VAR_REMOVAL = /(^|[^.$a-zA-Z0-9])Context[.]/g;
-
-/**
* Replace the top level variables
* @param {string} expr The expression
* @return {string} Expression with replacements
@@ -160,11 +153,6 @@
os.replaceTopLevelVars_ = function(text) {
var regex;
-
- // Currently, values specced to be inside the "Context" variable are placed
- // by JSTemplate into the top-level of the data context.
- // To make references like "Context.Index" work, remove the "Context." prefix.
- text = text.replace(os.regExps_.CONTEXT_VAR_REMOVAL, "$1");
regex = os.regExps_.TOP_LEVEL_VAR_REPLACEMENT;
if (!regex) {
@@ -179,7 +167,7 @@
} else {
return whole;
}
- });
+ });
};
/**
@@ -315,9 +303,9 @@
if (name == 'var') {
os.appendJSTAttribute_(to, ATT_vars, from.getAttribute(name) +
': $this');
- } else if (name == 'index') {
+ } else if (name == 'context') {
os.appendJSTAttribute_(to, ATT_vars, from.getAttribute(name) +
- ': $index');
+ ': ' + VAR_loop);
} else if (name.length < 7 || name.substring(0, 6) != 'xmlns:') {
if (os.customAttributes_[name]) {
os.appendJSTAttribute_(to, ATT_eval, "os.doAttribute(this, '" + name +
@@ -419,6 +407,14 @@
if (node.tagName == "os:Repeat") {
output = document.createElement(os.computeContainerTag_(node));
output.setAttribute(ATT_select, os.parseAttribute_(node.getAttribute("expression")));
+ var varAttr = node.getAttribute("var");
+ if (varAttr) {
+ os.appendJSTAttribute_(output, ATT_vars, varAttr + ': $this');
+ }
+ var contextAttr = node.getAttribute("context");
+ if (contextAttr) {
+ os.appendJSTAttribute_(output, ATT_vars, contextAttr + ': ' + VAR_loop);
+ }
os.appendJSTAttribute_(output, ATT_eval, "os.setContextNode_($this, $context)");
} else if (node.tagName == "os:If") {
output = document.createElement(os.computeContainerTag_(node));
@@ -759,8 +755,7 @@
os.identifiersNotToWrap_[VAR_this] = true;
os.identifiersNotToWrap_[VAR_context] = true;
os.identifiersNotToWrap_[VAR_top] = true;
-os.identifiersNotToWrap_[VAR_index] = true;
-os.identifiersNotToWrap_[VAR_count] = true;
+os.identifiersNotToWrap_[VAR_loop] = true;
/**
* Checks if a character can begin a legal JS identifier name.
@@ -807,7 +802,8 @@
* literal string 'null').
*/
os.wrapSingleIdentifier = function(iden, opt_context, opt_default) {
- if (os.identifiersNotToWrap_.hasOwnProperty(iden)) {
+ if (os.identifiersNotToWrap_.hasOwnProperty(iden) &&
+ (!opt_context || opt_context == VAR_context)) {
return iden;
}
return os.VAR_identifierresolver + '(' +
Modified: incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/jsTemplate/jsevalcontext.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/jsTemplate/jsevalcontext.js?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/jsTemplate/jsevalcontext.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/opensocial-templates/jsTemplate/jsevalcontext.js Wed Apr 29 06:57:41 2009
@@ -29,11 +29,12 @@
* context. These can be used in js expression in jstemplate
* attributes.
*/
-var VAR_index = '$index';
-var VAR_count = '$count';
+var VAR_index = 'Index';
+var VAR_count = 'Count';
var VAR_this = '$this';
var VAR_context = '$context';
var VAR_top = '$top';
+var VAR_loop = '$loop';
/**
@@ -257,8 +258,12 @@
*/
JsEvalContext.prototype.clone = function(data, index, count) {
var ret = JsEvalContext.create(data, this);
- ret.setVariable(VAR_index, index);
- ret.setVariable(VAR_count, count);
+ if (typeof(index) == 'number' || typeof(count) == 'number') {
+ var loopContext = {};
+ loopContext[VAR_index] = index;
+ loopContext[VAR_count] = count;
+ ret.setVariable(VAR_loop, loopContext);
+ }
return ret;
};
Modified: incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/compiler_test.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/compiler_test.js?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/compiler_test.js (original)
+++ incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/compiler_test.js Wed Apr 29 06:57:41 2009
@@ -25,8 +25,8 @@
[ "hello ${Cur}", "'hello '+($this)" ],
[ "$ ${Cur}", "'$ '+($this)" ],
[ "$${Cur}", "'$'+($this)" ],
- [ "${Cur} ${Index}", "($this)+' '+($index)"],
- [ "a ${Cur} b ${Index} c", "'a '+($this)+' b '+($index)+' c'"]
+ [ "${Cur} ${Context.Index}", "($this)+' '+($_ir($loop, 'Index'))"],
+ [ "a ${Cur} b ${Context.Index} c", "'a '+($this)+' b '+($_ir($loop, 'Index'))+' c'"]
];
for (var i = 0; i < values.length; i++) {
var compiled = os.parseAttribute_(values[i][0]);
@@ -181,8 +181,8 @@
function testNestedIndex() {
var src, template, output;
- src = '<table><tr repeat="list" var="row" index="x">' +
- '<td repeat="row" index="y">${x},${y}</td></tr></table>';
+ src = '<table><tr repeat="${list}" var="row" context="x">' +
+ '<td repeat="${row}" context="y">${x.Index},${y.Index}</td></tr></table>';
template = os.compileTemplateString(src);
output = template.render({ list: [ ['a', 'b'], ['c', 'd'] ] });
// table / tbody / tr / td
Modified: incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html (original)
+++ incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/index.html Wed Apr 29 06:57:41 2009
@@ -123,14 +123,14 @@
<button id="${id}" style="color: ${color}" a1="value ${A1}">${text}</button>
</xmp>
<xmp id="my:user" style="display: none">
- <a href="${url}">${name}</a> (${$my.foo})
+ <a href="${My.dat.url}">${My.dat.name}</a> (${My.foo})
</xmp>
<xmp id="my:record" style="display: none">
- <b style="color: ${My.color}">${title}</b>: <my:user foo="${My.foo}" cur="user"/>
+ <b style="color: ${My.color}">${My.dat.title}</b>: <my:user foo="${My.foo}" dat="${My.dat.user}"/>
</xmp>
<xmp id="_T_Substitution_nested" style="display: none">
<div repeat="users">
- <my:record color="${color}" foo="${user.id}" cur="Cur"/>
+ <my:record color="${color}" foo="${user.id}" dat="${Cur}"/>
</div>
</xmp>
Modified: incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js?rev=769686&r1=769685&r2=769686&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js (original)
+++ incubator/shindig/trunk/features/src/test/javascript/features/opensocial-templates/template_test.js Wed Apr 29 06:57:41 2009
@@ -743,12 +743,9 @@
tryTemplateContent('<os:msg/>', { Value: 'Right' } );
tryTemplateContent('<os:msg Value="Right"/>', { } );
- tryTemplateContent('<os:msg Value="Wrong" cur="${Top}"/>', { Value: 'Right' } );
tryTemplateContent('<os:msg Value="Right"/>', { Value: 'Wrong' } );
- tryTemplateContent('<os:msg cur="${Inner}"/>', { Value: 'Wrong', Inner: { Value: 'Right' }} );
- tryTemplateContent('<os:msg cur="${Inner}" Value="Wrong"/>', { Value: 'Wrong', Inner: { Value: 'Right' }} );
- tryTemplateContent('<os:msg cur="${Inner}"/>', { Value: 'Right', Inner: { Foo: 'Bar' }} );
- tryTemplateContent('<os:msg cur="${Inner}" Value="Right"/>', { Value: 'Wrong', Inner: { Foo: 'Bar' }} );
+ tryTemplateContent('<os:msg Value="Right" cur="${Top}"/>', { Value: 'Wrong' } );
+ tryTemplateContent('<os:msg Value="${Value}"/>', { Value: 'Right' } );
};
function testOsRepeat() {
@@ -760,7 +757,6 @@
assertEquals(6, output.firstChild.options.length);
output = os.compileTemplateString('<table><os:Repeat expression="${list}"><tr><td>${name}</td></tr><tr><td>${name} again</td></tr></os:Repeat></table>').render(data);
- console.log(output.firstChild);
assertEquals(6, output.firstChild.rows.length);
};
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=769686&r1=769685&r2=769686&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 Wed Apr 29 06:57:41 2009
@@ -25,8 +25,8 @@
[ "hello ${Cur}", "'hello '+($this)" ],
[ "$ ${Cur}", "'$ '+($this)" ],
[ "$${Cur}", "'$'+($this)" ],
- [ "${Cur} ${Index}", "($this)+' '+($index)"],
- [ "a ${Cur} b ${Index} c", "'a '+($this)+' b '+($index)+' c'"]
+ [ "${Cur} ${Context.Index}", "($this)+' '+($_ir($loop, 'Index'))"],
+ [ "a ${Cur} b ${Context.Index} c", "'a '+($this)+' b '+($_ir($loop, 'Index'))+' c'"]
];
for (var i = 0; i < values.length; i++) {
var compiled = os.parseAttribute_(values[i][0]);
@@ -180,8 +180,8 @@
function testNestedIndex() {
var src, template, output;
- src = '<table><tr repeat="list" var="row" index="x">' +
- '<td repeat="row" index="y">${x},${y}</td></tr></table>';
+ src = '<table><tr repeat="list" var="row" context="x">' +
+ '<td repeat="row" context="y">${x.Index},${y.Index}</td></tr></table>';
template = os.compileTemplateString(src);
output = template.render({ list: [ ['a', 'b'], ['c', 'd'] ] });
// table / tbody / tr / td