fix(ivy): i18n - handle translated text containing HTML comments (#32475)

Fixes FW-1536

PR Close #32475
This commit is contained in:
Pete Bacon Darwin 2019-09-04 16:27:29 +01:00 committed by Miško Hevery
parent 7cc4225eb9
commit 5d8eb74634
2 changed files with 25 additions and 2 deletions

View File

@ -169,7 +169,7 @@ const verify = (input: string, output: string, extra: any = {}): void => {
}
};
describe('i18n support in the view compiler', () => {
describe('i18n support in the template compiler', () => {
describe('element attributes', () => {
it('should add the meaning and description as JsDoc comments', () => {
@ -943,6 +943,24 @@ describe('i18n support in the view compiler', () => {
verify(input, output, {exceptions});
});
it('should ignore HTML comments within translated text', () => {
const input = `
<div i18n>Some <!-- comments --> text</div>
`;
const output = String.raw `
var $I18N_0$;
if (ngI18nClosureMode) {
const $MSG_EXTERNAL_0$ = goog.getMsg("Some text");
$I18N_0$ = $MSG_EXTERNAL_0$;
}
else {
$I18N_0$ = $localize \`Some text\`;
}
`;
verify(input, output);
});
it('should properly escape quotes in content', () => {
const input = `
<div i18n>Some text 'with single quotes', "with double quotes" and without quotes.</div>

View File

@ -44,7 +44,12 @@ class PlaceholderPiece extends MessagePiece {
*/
class LocalizeSerializerVisitor implements i18n.Visitor {
visitText(text: i18n.Text, context: MessagePiece[]): any {
context.push(new LiteralPiece(text.value));
if (context[context.length - 1] instanceof LiteralPiece) {
// Two literal pieces in a row means that there was some comment node in-between.
context[context.length - 1].text += text.value;
} else {
context.push(new LiteralPiece(text.value));
}
}
visitContainer(container: i18n.Container, context: MessagePiece[]): any {