From 3f4fe452777395f737a6568df6782a54ff05f818 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 28 Oct 2020 21:01:31 +0000 Subject: [PATCH] fix(compiler): ensure that i18n message-parts have the correct source-span (#39486) In an i18n message, two placeholders next to each other must have an "empty" message-part to separate them. Previously, the source-span for this message-part was pointing to the wrong original location. This caused problems in the generated source-maps and lead to extracted i18n messages from being rendered incorrectly. PR Close #39486 --- .../src/render3/view/i18n/localize_utils.ts | 2 +- .../compiler/test/render3/view/i18n_spec.ts | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/render3/view/i18n/localize_utils.ts b/packages/compiler/src/render3/view/i18n/localize_utils.ts index 444139ecac..b57593e4d8 100644 --- a/packages/compiler/src/render3/view/i18n/localize_utils.ts +++ b/packages/compiler/src/render3/view/i18n/localize_utils.ts @@ -120,7 +120,7 @@ function processMessagePieces(pieces: o.MessagePiece[]): placeHolders.push(part); if (pieces[i - 1] instanceof o.PlaceholderPiece) { // There were two placeholders in a row, so we need to add an empty message part. - messageParts.push(createEmptyMessagePart(part.sourceSpan.end)); + messageParts.push(createEmptyMessagePart(pieces[i - 1].sourceSpan.end)); } } } diff --git a/packages/compiler/test/render3/view/i18n_spec.ts b/packages/compiler/test/render3/view/i18n_spec.ts index 2ed1dba0f7..2e8ca157b6 100644 --- a/packages/compiler/test/render3/view/i18n_spec.ts +++ b/packages/compiler/test/render3/view/i18n_spec.ts @@ -489,6 +489,26 @@ describe('serializeI18nMessageForLocalize', () => { expect(placeHolders[3].sourceSpan.toString()).toEqual(''); }); + it('should create the correct source-spans when there are two placeholders next to each other', + () => { + const {messageParts, placeHolders} = serialize('{{value}}'); + expect(messageParts[0].text).toEqual(''); + expect(humanizeSourceSpan(messageParts[0].sourceSpan)).toEqual('"" (10-10)'); + expect(messageParts[1].text).toEqual(''); + expect(humanizeSourceSpan(messageParts[1].sourceSpan)).toEqual('"" (13-13)'); + expect(messageParts[2].text).toEqual(''); + expect(humanizeSourceSpan(messageParts[2].sourceSpan)).toEqual('"" (22-22)'); + expect(messageParts[3].text).toEqual(''); + expect(humanizeSourceSpan(messageParts[3].sourceSpan)).toEqual('"" (26-26)'); + + expect(placeHolders[0].text).toEqual('START_BOLD_TEXT'); + expect(humanizeSourceSpan(placeHolders[0].sourceSpan)).toEqual('"" (10-13)'); + expect(placeHolders[1].text).toEqual('INTERPOLATION'); + expect(humanizeSourceSpan(placeHolders[1].sourceSpan)).toEqual('"{{value}}" (13-22)'); + expect(placeHolders[2].text).toEqual('CLOSE_BOLD_TEXT'); + expect(humanizeSourceSpan(placeHolders[2].sourceSpan)).toEqual('"" (22-26)'); + }); + it('should serialize simple ICU for `$localize()`', () => { expect(serialize('{age, plural, 10 {ten} other {other}}')).toEqual({ messageParts: [literal('{VAR_PLURAL, plural, 10 {ten} other {other}}')], @@ -496,7 +516,6 @@ describe('serializeI18nMessageForLocalize', () => { }); }); - it('should serialize nested ICUs for `$localize()`', () => { expect(serialize( '{age, plural, 10 {ten {size, select, 1 {one} 2 {two} other {2+}}} other {other}}')) @@ -509,7 +528,6 @@ describe('serializeI18nMessageForLocalize', () => { }); }); - it('should serialize ICU with embedded HTML for `$localize()`', () => { expect(serialize('{age, plural, 10 {ten} other {
other
}}')).toEqual({ messageParts: [ @@ -594,4 +612,8 @@ function literal(text: string, span: any = jasmine.any(ParseSourceSpan)): o.Lite function placeholder(name: string, span: any = jasmine.any(ParseSourceSpan)): o.PlaceholderPiece { return new o.PlaceholderPiece(name, span); -} \ No newline at end of file +} + +function humanizeSourceSpan(span: ParseSourceSpan): string { + return `"${span.toString()}" (${span.start.offset}-${span.end.offset})`; +}