From df44e3e425c36aafcfc2c8cd7701e849d717ca02 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 1 Aug 2016 13:11:48 -0700 Subject: [PATCH] fix(i18n extractor): array manipulation --- modules/@angular/compiler/src/i18n/extractor.ts | 14 +++++++------- .../compiler/test/i18n/extractor_spec.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/modules/@angular/compiler/src/i18n/extractor.ts b/modules/@angular/compiler/src/i18n/extractor.ts index 7add249824..34b6a9a7d6 100644 --- a/modules/@angular/compiler/src/i18n/extractor.ts +++ b/modules/@angular/compiler/src/i18n/extractor.ts @@ -172,17 +172,17 @@ class _ExtractVisitor implements html.Visitor { } private _extractFromAttributes(el: html.Element, messages: Message[]): void { - const explicitAttrNameToValue: Map = new Map(); + const explicitAttrNameToValue: {[k: string]: string} = {}; const implicitAttrNames: string[] = this._implicitAttrs[el.name] || []; el.attrs.filter(attr => attr.name.startsWith(_I18N_ATTR_PREFIX)) .forEach( - attr => explicitAttrNameToValue.set( - attr.name.substring(_I18N_ATTR_PREFIX.length), attr.value)); + attr => explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] = + attr.value); el.attrs.forEach(attr => { - if (explicitAttrNameToValue.has(attr.name)) { - this._addMessage(messages, [attr], explicitAttrNameToValue.get(attr.name)); + if (attr.name in explicitAttrNameToValue) { + this._addMessage(messages, [attr], explicitAttrNameToValue[attr.name]); } else if (implicitAttrNames.some(name => attr.name === name)) { this._addMessage(messages, [attr]); } @@ -250,8 +250,8 @@ class _ExtractVisitor implements html.Visitor { 0); if (significantChildren == 1) { - for (let i = startIndex; i < messages.length; i++) { - let ast = messages[i].nodes; + for (let i = messages.length - 1; i >= startIndex; i--) { + const ast = messages[i].nodes; if (!(ast.length == 1 && ast[0] instanceof html.Attribute)) { messages.splice(i, 1); break; diff --git a/modules/@angular/compiler/test/i18n/extractor_spec.ts b/modules/@angular/compiler/test/i18n/extractor_spec.ts index cf00a86d98..81b08275c9 100644 --- a/modules/@angular/compiler/test/i18n/extractor_spec.ts +++ b/modules/@angular/compiler/test/i18n/extractor_spec.ts @@ -21,6 +21,22 @@ export function main() { ]); }); + it('should extract from elements', () => { + expect( + extract( + '
{count, plural, =0 {

}}
')) + .toEqual([ + [ + [ + '{count, plural, =0 {

}}' + ], + 'm', 'd' + ], + [['title="title"'], '', ''], + [['desc="desc"'], '', ''], + ]); + }); + it('should not create a message for empty elements', () => { expect(extract('
')).toEqual([]); }); });