fix(partition): fix partition when `<!-- i18n -->` is the only child

This commit is contained in:
Victor Berchet 2016-06-17 15:00:26 -07:00
parent 04a50f5832
commit 58b18d7fe7
2 changed files with 32 additions and 27 deletions

View File

@ -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 = (<HtmlCommentAst>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 `<!-- i18n -->` and `<!-- /i18n -->`
if (_isOpeningComment(node)) {
let i18n = (<HtmlCommentAst>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));
}
}

View File

@ -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(
`
<!-- i18n: meaning1|desc1 -->message1`,
'someUrl');
it('should error when i18n comments are unbalanced', () => {
const res = extractor.extract('<!-- i18n -->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('<!-- i18n -->', 'someUrl');
expect(res.errors.length).toEqual(1);
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
});