Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n): escape translations per default #363

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/i18n/translate/translate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { escapeHTML } from '../../util/EscapeUtil';


/**
* A simple translation stub to be used for multi-language support
* in diagrams. Can be easily replaced with a more sophisticated
* solution.
* solution. Escapes HTML per default.
*
* @example
*
Expand All @@ -13,14 +16,22 @@
*
* @param {String} template to interpolate
* @param {Object} [replacements] a map with substitutes
* @param {boolean} [safe] true if should not be escaped
*
* @return {String} the translated string
*/
export default function translate(template, replacements) {
export default function translate(template, replacements, safe) {

if (typeof replacements === 'boolean') {
safe = replacements;
replacements = {};
}

replacements = replacements || {};

return template.replace(/{([^}]+)}/g, function(_, key) {
template = template.replace(/{([^}]+)}/g, function(_, key) {
return replacements[key] || '{' + key + '}';
});

return safe ? template : escapeHTML(template);
}
13 changes: 9 additions & 4 deletions lib/util/EscapeUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ export {
} from 'css.escape';

var HTML_ESCAPE_MAP = {
'<': '&lt',
'>': '&gt'
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;'
};

export function escapeHTML(str) {
return str.replace(/[<>]/g, function(match) {
str = '' + str;
nikku marked this conversation as resolved.
Show resolved Hide resolved

return str && str.replace(/[&<>"']/g, function(match) {
return HTML_ESCAPE_MAP[match];
});
}
}
11 changes: 11 additions & 0 deletions test/spec/i18n/translate/translateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe('i18n - translate', function() {
expect(translate('FOO {bar}!', {})).to.eql('FOO {bar}!');
}));


it('should escape HTML per default', inject(function(translate) {
expect(translate('<b>Bold</b> statement', {})).to.eql('&lt;b&gt;Bold&lt;/b&gt; statement');
}));


it('should not escape HTML for safe=true', inject(function(translate) {
expect(translate('<b>Bold</b> statement', true)).to.eql('<b>Bold</b> statement');
expect(translate('<b>Bold</b> statement', {}, true)).to.eql('<b>Bold</b> statement');
}));

});


Expand Down
8 changes: 5 additions & 3 deletions test/spec/util/EscapeUtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ describe('util/EscapeUtil', function() {
});


it('escapeHTML', function() {
var htmlStr = '<video src=1 onerror=alert(\'hueh\')>';
it('should escape HTML', function() {
var htmlStr = '<video src=1 onerror=alert(\'hueh\')>',
htmlStr2 = '" onfocus=alert(1) "';

expect(escapeHTML(htmlStr)).to.eql('&ltvideo src=1 onerror=alert(\'hueh\')&gt');
expect(escapeHTML(htmlStr)).to.eql('&lt;video src=1 onerror=alert(&#39;hueh&#39;)&gt;');
expect(escapeHTML(htmlStr2)).to.eql('&quot; onfocus=alert(1) &quot;');
});

});