Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more tests added

  • Loading branch information...
commit a45a0dd31aecd3e57f4c98d0f21fc07a94f00748 1 parent f6e723e
@dvv authored
Showing with 42 additions and 5 deletions.
  1. +10 −5 lib/index.js
  2. +32 −0 tests/smoke.js
View
15 lib/index.js
@@ -49,10 +49,11 @@ Locale.prototype.add = function (id, hash) {
//
// JavaScript micro-templating, similar to John Resig's implementation
//
- var re_interpolate1 = /#\{([\s\S]+?)\}/g;
- var re_interpolate2 = /%\{([\s\S]+?)\}:([\w.$]+)/g;
+ var re_interpolate1 = /#\{([\s\S]*?)\}/g;
+ var re_interpolate2 = /%\{([\s\S]*?)\}:([a-zA-Z_$][\w.$]+)/g;
- var re_inflect = /^([\w.$]+?) +([\s\S]+)/;
+ var re_variable = /^\s*[a-zA-Z_$][\w.$]+\s*$/;
+ var re_inflection = /^\s*([a-zA-Z_$][\w.$]+?) +([\s\S]+)/;
function expand(str) {
str = String(str || '');
@@ -69,7 +70,7 @@ Locale.prototype.add = function (id, hash) {
.replace(re_interpolate1, function (match, inner) {
interpolated = true;
inner = inner.replace(/\\'/, "'");
- var m = re_inflect.exec(inner);
+ var m = re_inflection.exec(inner);
if (m) {
// unescape }} and parse word forms
var forms = m[2].replace(re_unescape, '}').split('|');
@@ -77,8 +78,12 @@ Locale.prototype.add = function (id, hash) {
// #{this.foo.bar} is sugar for this.t('foo.bar', locals)
} else if (inner.indexOf('this.') === 0) {
return '\',this.t("' + inner.substring(5) + '",locals),\'';
- } else {
+ // plain interpolation -- `inner` defines variable to put
+ } else if (re_variable.exec(inner)) {
return '\',' + inner + ',\'';
+ // bad interpolation -- just strip it off of #{}
+ } else {
+ return '\',\'' + inner + '\',\'';
}
})
// %{forms...}:var
View
32 tests/smoke.js
@@ -86,6 +86,36 @@ require('vows').describe('smoke')
equal(L.get('user.family1').call(null, vars), "You've got 2 wives and 5 childre n'");
},
},
+ 'parsing quirky strings': {
+ topic: function () {
+ var L = new Locale();
+ return L.add('', {
+ e00: ' #{ ',
+ e01: ' }\t\n #{',
+ e02: ' }}\t\n #{',
+ e03: ' }}}\t\n #{',
+ e1: '#{}',
+ e2: '#{1}',
+ e3: '#{ }',
+ e4: '#{. (.) . (.).}',
+ // FIXME: look closer
+ e5: '#{. (.) #{} . (.).}',
+ });
+ },
+ 'results in ignoring quirks': function (L) {
+ //console.log(L);
+ equal(L.e00, ' #{ ');
+ equal(L.e01, ' }\t\n #{');
+ equal(L.e02, ' }}\t\n #{');
+ equal(L.e03, ' }}}\t\n #{');
+ equal(L.e1.body, 'var __p=this.p(\'\');with(locals||{}){return [\'\',\'\',\'\'].join(\'\')}');
+ equal(L.e2.body, 'var __p=this.p(\'\');with(locals||{}){return [\'\',\'1\',\'\'].join(\'\')}');
+ equal(L.e3.body, 'var __p=this.p(\'\');with(locals||{}){return [\'\',\' \',\'\'].join(\'\')}');
+ equal(L.e4.body, 'var __p=this.p(\'\');with(locals||{}){return [\'\',\'. (.) . (.).\',\'\'].join(\'\')}');
+ // FIXME: look closer
+ equal(L.e5.body, 'var __p=this.p(\'\');with(locals||{}){return [\'\',\'. (.) #{\',\' . (.).}\'].join(\'\')}');
+ }
+ },
'locale provides helper for templates': {
topic: function () {
var L = new Locale();
@@ -112,6 +142,7 @@ require('vows').describe('smoke')
foo: 'simple foo',
bar: 'complex string: reuses [this.foo] #{this.foo}',
baz: 'more complex string: reuses [this.foo] #{this.foo} and [this.bar] #{this.bar}',
+ hello: 'Hello, #{user.name}! You have got #{messages} #{messages message|messages}'
});
return L;
},
@@ -122,6 +153,7 @@ require('vows').describe('smoke')
equal(L.t('foo'), 'simple foo');
equal(L.t('bar'), 'complex string: reuses [this.foo] simple foo');
equal(L.t('baz'), 'more complex string: reuses [this.foo] simple foo and [this.bar] complex string: reuses [this.foo] simple foo');
+ equal(L.get('hello').body, 'var __p=this.p(\'en\');with(locals||{}){return [\'Hello, \',user.name,\'! You have got \',messages,\' \',__p(messages,["message","messages"]),\'\'].join(\'\')}');
},
},
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.