Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Escape interpolations by default
Browse files Browse the repository at this point in the history
This is in order to get the expected handlebars-like mustache
escaping behavior. Unescaped interpolation made possible via the
common tripple curlies mustache.

Conflicts:
	lib/i18n.js
	spec/spec_support.js
	spec/tSpec.js

Conflicts:
	lib/i18n.js
  • Loading branch information
opichals authored and jamesarosen committed Dec 24, 2014
1 parent 89e4fa8 commit 0fce0a0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
}
}

var escapeExpression = EmHandlebars.Utils.escapeExpression;
var compileImplementation;

function compileTemplate(template) {
Expand All @@ -75,9 +76,13 @@
if (flag === true) {
return function compileWithoutHandlebars(template) {
return function (data) {
return template.replace(/\{\{(.*?)\}\}/g, function(i, match) {
return data[match];
});
return template
.replace(/\{\{\{(.*?)\}\}\}/g, function(i, match) {
// tripple curlies -> no-escaping
return get(data, match);
}).replace(/\{\{(.*?)\}\}/g, function(i, match) {
return escapeExpression( get(data, match) );
});
};
};
}
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

Ember.I18n.translations = {
'foo.bar': 'A Foobar',
'foo.bar.named': 'A Foobar named {{name}}',
'foo.bar.named': 'A Foobar named <span>{{name}}</span>',
'foo.bar.named.noEscape': 'A Foobar named <span>{{{link}}}</span>',
'foo.bar.named.structured': 'A Foobar named {{contact.name}}',
'foo.save.disabled': 'Saving Foo...',
'foos.zero': 'No Foos',
'foos.one': 'One Foo',
Expand Down
14 changes: 13 additions & 1 deletion spec/tSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ describe('Ember.I18n.t', function() {

it('interpolates', function() {
expect(Ember.I18n.t('foo.bar.named', {
name: 'Sue'
name: '<Sue>'
})).to.equal('A Foobar named <span>&lt;Sue&gt;</span>');
});

it('interpolates escaped', function() {
expect(Ember.I18n.t('foo.bar.named.noEscape', {
link: '<a href="#">Sue</a>'
})).to.equal('A Foobar named <span><a href="#">Sue</a></span>');
});

it('interpolates structures correctly', function() {
expect(Ember.I18n.t('foo.bar.named.structured', {
contact: { name: 'Sue' }
})).to.equal('A Foobar named Sue');
});

Expand Down

0 comments on commit 0fce0a0

Please sign in to comment.