Author: zhen
Date: Wed Jun 11 16:21:59 2008
New Revision: 666892
URL: http://svn.apache.org/viewvc?rev=666892&view=rev
Log:
SHINDIG-365
Added support for "-prefix" and "-suffix" operators for the url template.
Fixed regular expressions in the bind() method.
Modified:
incubator/shindig/trunk/features/views/urltemplatetest.js
incubator/shindig/trunk/features/views/views.js
Modified: incubator/shindig/trunk/features/views/urltemplatetest.js
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/views/urltemplatetest.js?rev=666892&r1=666891&r2=666892&view=diff
==============================================================================
--- incubator/shindig/trunk/features/views/urltemplatetest.js (original)
+++ incubator/shindig/trunk/features/views/urltemplatetest.js Wed Jun 11
16:21:59 2008
@@ -60,6 +60,14 @@
UrlTemplateTest.prototype.testVariableSubstitution = function() {
this.batchTest([
[
+ 'http://host/path/{A=65}{66=B}',
+ {
+ 'A': 'a'
+ },
+ 'http://host/path/aB'
+ ],
+
+ [
'http://host/path/{open}{social}{0.8}{d-_-b}',
{
'open': 'O',
@@ -87,13 +95,73 @@
],
[
+ null,
+ null,
+ new Error('Invalid urlTemplate')
+ ],
+
+ [
+ 'http://host/path/{var}',
+ 'string',
+ new Error('Invalid environment')
+ ],
+
+ [
'http://host/path/{invalid definition!!!}',
{
'value': 'defined'
},
new Error('Invalid syntax : {invalid definition!!!}')
+ ],
+
+ [
+ 'http://host/path/{} is also invalid',
+ {
+ 'value': 'defined'
+ },
+ new Error('Invalid syntax : {}')
]
]);
};
+UrlTemplateTest.prototype.testPrefixOperator = function() {
+ this.batchTest([
+ [
+ 'http://host/path/{-prefix|/|foo}{-prefix|/|bar}{-prefix|-|baz}',
+ {
+ 'foo': 'bar',
+ 'baz': ['b', 'a', 'z']
+ },
+ 'http://host/path//bar-b-a-z'
+ ],
+
+ [
+ 'http://host/path/{-prefix|/|foo=bar}',
+ {
+ },
+ 'http://host/path//bar'
+ ]
+ ]);
+};
+
+UrlTemplateTest.prototype.testSuffixOperator = function() {
+ this.batchTest([
+ [
+ 'http://host/path/{-suffix|/|foo}{-suffix|/|bar}{-suffix|-|baz}',
+ {
+ 'foo': 'bar',
+ 'baz': ['b', 'a', 'z']
+ },
+ 'http://host/path/bar/b-a-z-'
+ ],
+
+ [
+ 'http://host/path/{-suffix|/|foo=bar}',
+ {
+ },
+ 'http://host/path/bar/'
+ ]
+ ]);
+};
+
Modified: incubator/shindig/trunk/features/views/views.js
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/views/views.js?rev=666892&r1=666891&r2=666892&view=diff
==============================================================================
--- incubator/shindig/trunk/features/views/views.js (original)
+++ incubator/shindig/trunk/features/views/views.js Wed Jun 11 16:21:59 2008
@@ -102,36 +102,69 @@
environment[varName] : defaultVal;
}
- // TODO Validate environment
- // TODO Validate urlTemplate
+ if (typeof urlTemplate != 'string') {
+ throw new Error('Invalid urlTemplate');
+ }
+
+ if (typeof environment != 'object') {
+ throw new Error('Invalid environment');
+ }
- var varRE = /^([a-zA-Z0-9][a-zA-Z0-9_.-]*)$/,
- expansionRE = /\{([^}]*)\}/g,
+ var varRE =
/^([a-zA-Z0-9][a-zA-Z0-9_\.\-]*)(=([a-zA-Z0-9\-\._~]|(%[0-9a-fA-F]{2}))*)?$/,
+ expansionRE = new RegExp('\\{([^}]*)\\}','g'),
+ opRE = /^-([a-zA-Z]+)\|([^|]*)\|(.+)$/,
result = [],
textStart = 0,
group,
match,
- varName;
+ varName,
+ defaultValue,
+ op,
+ arg,
+ vars,
+ opPrefix;
while (group = expansionRE.exec(urlTemplate)) {
result.push(urlTemplate.substring(textStart, group.index));
textStart = expansionRE.lastIndex;
if (match = group[1].match(varRE)) {
- // TODO Add support for "var=default_value" syntax
varName = match[1];
- result.push(getVar(varName, ''));
+ defaultValue = match[2] ? match[2].substr(1) : '';
+ result.push(getVar(varName, defaultValue));
} else {
- // TODO Add support for "-op|arg|vars" syntax
- // TODO Parse the "-opt" operator
- // TODO Parse the "-neg" operator
- // TODO Parse the "-prefix" operator
- // TODO Parse the "-suffix" operator
- // TODO Parse the "-join" operator
- // TODO Parse the "-list" operator
- throw new Error('Invalid syntax : ' + group[0]);
+ if (match = group[1].match(opRE)) {
+ op = match[1];
+ arg = match[2];
+ vars = match[3];
+ opPrefix = false;
+ switch (op) {
+ case 'prefix':
+ opPrefix = true;
+ case 'suffix':
+ if (match = vars.match(varRE)) {
+ value = getVar(match[1], match[2] && match[2].substr(1));
+ if (typeof value === 'string') {
+ result.push(opPrefix ? arg + value : value + arg);
+ } else if (typeof value === 'object' && typeof value.join ===
'function') {
+ result.push(opPrefix ? arg + value.join(arg) :
value.join(arg) + arg);
+ }
+ } else {
+ throw new Error('Invalid variable : ' + vars);
+ }
+ break;
+ // TODO Parse the "-opt" operator
+ // TODO Parse the "-neg" operator
+ // TODO Parse the "-join" operator
+ // TODO Parse the "-list" operator
+ default:
+ throw new Error('Invalid operator : ' + op);
+ }
+ } else {
+ throw new Error('Invalid syntax : ' + group[0]);
+ }
}
}
- // TODO Throw an exception if no variable is defined at all.
+
result.push(urlTemplate.substr(textStart));
return result.join('');