diff --git a/modules/@angular/compiler/src/i18n/shared.ts b/modules/@angular/compiler/src/i18n/shared.ts index dd85f8502b..6df28474c9 100644 --- a/modules/@angular/compiler/src/i18n/shared.ts +++ b/modules/@angular/compiler/src/i18n/shared.ts @@ -20,26 +20,29 @@ export function partition(nodes: HtmlAst[], errors: ParseError[], implicitTags: let parts: Part[] = []; for (let i = 0; i < nodes.length; ++i) { - let n = nodes[i]; - let temp: HtmlAst[] = []; - if (_isOpeningComment(n)) { - let i18n = (n).value.replace(/^i18n:?/, '').trim(); - i++; - while (!_isClosingComment(nodes[i])) { - temp.push(nodes[i++]); - if (i === nodes.length) { - errors.push(new I18nError(n.sourceSpan, 'Missing closing \'i18n\' comment.')); - break; - } - } - parts.push(new Part(null, null, temp, i18n, true)); + let node = nodes[i]; + let msgNodes: HtmlAst[] = []; + // Nodes between `` and `` + if (_isOpeningComment(node)) { + let i18n = (node).value.replace(/^i18n:?/, '').trim(); - } else if (n instanceof HtmlElementAst) { - let i18n = _findI18nAttr(n); - let hasI18n: boolean = isPresent(i18n) || implicitTags.indexOf(n.name) > -1; - parts.push(new Part(n, null, n.children, isPresent(i18n) ? i18n.value : null, hasI18n)); - } else if (n instanceof HtmlTextAst) { - parts.push(new Part(null, n, null, null, false)); + while (++i < nodes.length && !_isClosingComment(nodes[i])) { + msgNodes.push(nodes[i]); + } + + if (i === nodes.length) { + errors.push(new I18nError(node.sourceSpan, 'Missing closing \'i18n\' comment.')); + break; + } + + parts.push(new Part(null, null, msgNodes, i18n, true)); + } else if (node instanceof HtmlElementAst) { + // Node with an `i18n` attribute + let i18n = _findI18nAttr(node); + let hasI18n: boolean = isPresent(i18n) || implicitTags.indexOf(node.name) > -1; + parts.push(new Part(node, null, node.children, isPresent(i18n) ? i18n.value : null, hasI18n)); + } else if (node instanceof HtmlTextAst) { + parts.push(new Part(null, node, null, null, false)); } } diff --git a/modules/@angular/compiler/test/i18n/message_extractor_spec.ts b/modules/@angular/compiler/test/i18n/message_extractor_spec.ts index 5643eeb996..c26dd723b6 100644 --- a/modules/@angular/compiler/test/i18n/message_extractor_spec.ts +++ b/modules/@angular/compiler/test/i18n/message_extractor_spec.ts @@ -10,8 +10,8 @@ export function main() { let extractor: MessageExtractor; beforeEach(() => { - let htmlParser = new HtmlParser(); - var parser = new Parser(new Lexer()); + const htmlParser = new HtmlParser(); + const parser = new Parser(new Lexer()); extractor = new MessageExtractor(htmlParser, parser, ['i18n-tag'], {'i18n-el': ['trans']}); }); @@ -153,7 +153,7 @@ export function main() { new Message('value', 'meaning', 'desc') ]); }); - + it('should remove duplicate messages', () => { let res = extractor.extract( ` @@ -203,12 +203,14 @@ export function main() { expect(res.errors[0].msg).toEqual('Missing attribute \'title2\'.'); }); - it('should error when cannot find a matching desc', () => { - let res = extractor.extract( - ` - message1`, - 'someUrl'); + it('should error when i18n comments are unbalanced', () => { + const res = extractor.extract('message1', 'someUrl'); + expect(res.errors.length).toEqual(1); + expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.'); + }); + it('should error when i18n comments are unbalanced', () => { + const res = extractor.extract('', 'someUrl'); expect(res.errors.length).toEqual(1); expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.'); });