Skip to content

Commit 58b18d7

Browse files
committed
fix(partition): fix partition when <!-- i18n --> is the only child
1 parent 04a50f5 commit 58b18d7

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

modules/@angular/compiler/src/i18n/shared.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@ export function partition(nodes: HtmlAst[], errors: ParseError[], implicitTags:
2020
let parts: Part[] = [];
2121

2222
for (let i = 0; i < nodes.length; ++i) {
23-
let n = nodes[i];
24-
let temp: HtmlAst[] = [];
25-
if (_isOpeningComment(n)) {
26-
let i18n = (<HtmlCommentAst>n).value.replace(/^i18n:?/, '').trim();
27-
i++;
28-
while (!_isClosingComment(nodes[i])) {
29-
temp.push(nodes[i++]);
30-
if (i === nodes.length) {
31-
errors.push(new I18nError(n.sourceSpan, 'Missing closing \'i18n\' comment.'));
32-
break;
33-
}
23+
let node = nodes[i];
24+
let msgNodes: HtmlAst[] = [];
25+
// Nodes between `<!-- i18n -->` and `<!-- /i18n -->`
26+
if (_isOpeningComment(node)) {
27+
let i18n = (<HtmlCommentAst>node).value.replace(/^i18n:?/, '').trim();
28+
29+
while (++i < nodes.length && !_isClosingComment(nodes[i])) {
30+
msgNodes.push(nodes[i]);
31+
}
32+
33+
if (i === nodes.length) {
34+
errors.push(new I18nError(node.sourceSpan, 'Missing closing \'i18n\' comment.'));
35+
break;
3436
}
35-
parts.push(new Part(null, null, temp, i18n, true));
36-
37-
} else if (n instanceof HtmlElementAst) {
38-
let i18n = _findI18nAttr(n);
39-
let hasI18n: boolean = isPresent(i18n) || implicitTags.indexOf(n.name) > -1;
40-
parts.push(new Part(n, null, n.children, isPresent(i18n) ? i18n.value : null, hasI18n));
41-
} else if (n instanceof HtmlTextAst) {
42-
parts.push(new Part(null, n, null, null, false));
37+
38+
parts.push(new Part(null, null, msgNodes, i18n, true));
39+
} else if (node instanceof HtmlElementAst) {
40+
// Node with an `i18n` attribute
41+
let i18n = _findI18nAttr(node);
42+
let hasI18n: boolean = isPresent(i18n) || implicitTags.indexOf(node.name) > -1;
43+
parts.push(new Part(node, null, node.children, isPresent(i18n) ? i18n.value : null, hasI18n));
44+
} else if (node instanceof HtmlTextAst) {
45+
parts.push(new Part(null, node, null, null, false));
4346
}
4447
}
4548

modules/@angular/compiler/test/i18n/message_extractor_spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function main() {
1010
let extractor: MessageExtractor;
1111

1212
beforeEach(() => {
13-
let htmlParser = new HtmlParser();
14-
var parser = new Parser(new Lexer());
13+
const htmlParser = new HtmlParser();
14+
const parser = new Parser(new Lexer());
1515
extractor = new MessageExtractor(htmlParser, parser, ['i18n-tag'], {'i18n-el': ['trans']});
1616
});
1717

@@ -153,7 +153,7 @@ export function main() {
153153
new Message('value', 'meaning', 'desc')
154154
]);
155155
});
156-
156+
157157
it('should remove duplicate messages', () => {
158158
let res = extractor.extract(
159159
`
@@ -203,12 +203,14 @@ export function main() {
203203
expect(res.errors[0].msg).toEqual('Missing attribute \'title2\'.');
204204
});
205205

206-
it('should error when cannot find a matching desc', () => {
207-
let res = extractor.extract(
208-
`
209-
<!-- i18n: meaning1|desc1 -->message1`,
210-
'someUrl');
206+
it('should error when i18n comments are unbalanced', () => {
207+
const res = extractor.extract('<!-- i18n -->message1', 'someUrl');
208+
expect(res.errors.length).toEqual(1);
209+
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
210+
});
211211

212+
it('should error when i18n comments are unbalanced', () => {
213+
const res = extractor.extract('<!-- i18n -->', 'someUrl');
212214
expect(res.errors.length).toEqual(1);
213215
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
214216
});

0 commit comments

Comments
 (0)