Skip to content

Corrected overcorrections which caused ampersands to be html-encoded #352

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, ge
$rootScope.$broadcast('gettextLanguageChanged');
}

function rollBackOvercorrections(originalKey, key) {
var originalKeyParts = originalKey.split('&');
var re = /&/gi;//replacement match regex
var nth = 0;
var i = 0;

function replaceNth(match) {
nth++;
if (nth === i + 1) {
return '&';
}
return match;
}

for (i = 0; i < originalKeyParts.length; i++) {
//get the next part, which will start with "amp;" if this was an occurrence of an encoded &
var nextPart = (originalKeyParts.length - 1 >= i + 1) ? originalKeyParts[i + 1] : '';

if (nextPart.length < 4 || nextPart.substring(0, 5) !== 'amp;') { //unencoded & in original, needs to be rolled back in key
key = key.replace(re, replaceNth);
}
}

return key;
}

catalog = {
/**
* @ngdoc property
Expand Down Expand Up @@ -152,6 +178,8 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, ge
return this.currentLanguage;
},

rollBackOvercorrections: rollBackOvercorrections,

/**
* @ngdoc method
* @name gettextCatalog#setStrings
Expand All @@ -170,8 +198,11 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, ge
var val = strings[key];

if (isHTMLModified) {
//save the original before we change it
var originalKey = key;
// Use the DOM engine to render any HTML in the key (#131).
key = angular.element('<span>' + key + '</span>').html();
key = rollBackOvercorrections(originalKey, key);
}

if (angular.isString(val) || angular.isArray(val)) {
Expand Down
2 changes: 2 additions & 0 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ angular.module('gettext').directive('translate', function (gettextCatalog, $pars
gettextUtil.assert(!attrs.translateN || attrs.translatePlural, 'translate-plural', 'translate-n');

var msgid = gettextUtil.trim(element.html());
var originalText = element.context && element.context.innerText ? element.context.innerText : element.html();
msgid = gettextCatalog.rollBackOvercorrections(originalText, msgid);
var translatePlural = attrs.translatePlural;
var translateContext = attrs.translateContext;

Expand Down