fix(partition): fix partition when <!-- i18n -->
is the only child
This commit is contained in:
parent
04a50f5832
commit
58b18d7fe7
@ -20,26 +20,29 @@ export function partition(nodes: HtmlAst[], errors: ParseError[], implicitTags:
|
|||||||
let parts: Part[] = [];
|
let parts: Part[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < nodes.length; ++i) {
|
for (let i = 0; i < nodes.length; ++i) {
|
||||||
let n = nodes[i];
|
let node = nodes[i];
|
||||||
let temp: HtmlAst[] = [];
|
let msgNodes: HtmlAst[] = [];
|
||||||
if (_isOpeningComment(n)) {
|
// Nodes between `<!-- i18n -->` and `<!-- /i18n -->`
|
||||||
let i18n = (<HtmlCommentAst>n).value.replace(/^i18n:?/, '').trim();
|
if (_isOpeningComment(node)) {
|
||||||
i++;
|
let i18n = (<HtmlCommentAst>node).value.replace(/^i18n:?/, '').trim();
|
||||||
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));
|
|
||||||
|
|
||||||
} else if (n instanceof HtmlElementAst) {
|
while (++i < nodes.length && !_isClosingComment(nodes[i])) {
|
||||||
let i18n = _findI18nAttr(n);
|
msgNodes.push(nodes[i]);
|
||||||
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) {
|
if (i === nodes.length) {
|
||||||
parts.push(new Part(null, n, null, null, false));
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@ export function main() {
|
|||||||
let extractor: MessageExtractor;
|
let extractor: MessageExtractor;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
let htmlParser = new HtmlParser();
|
const htmlParser = new HtmlParser();
|
||||||
var parser = new Parser(new Lexer());
|
const parser = new Parser(new Lexer());
|
||||||
extractor = new MessageExtractor(htmlParser, parser, ['i18n-tag'], {'i18n-el': ['trans']});
|
extractor = new MessageExtractor(htmlParser, parser, ['i18n-tag'], {'i18n-el': ['trans']});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -153,7 +153,7 @@ export function main() {
|
|||||||
new Message('value', 'meaning', 'desc')
|
new Message('value', 'meaning', 'desc')
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should remove duplicate messages', () => {
|
it('should remove duplicate messages', () => {
|
||||||
let res = extractor.extract(
|
let res = extractor.extract(
|
||||||
`
|
`
|
||||||
@ -203,12 +203,14 @@ export function main() {
|
|||||||
expect(res.errors[0].msg).toEqual('Missing attribute \'title2\'.');
|
expect(res.errors[0].msg).toEqual('Missing attribute \'title2\'.');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error when cannot find a matching desc', () => {
|
it('should error when i18n comments are unbalanced', () => {
|
||||||
let res = extractor.extract(
|
const res = extractor.extract('<!-- i18n -->message1', 'someUrl');
|
||||||
`
|
expect(res.errors.length).toEqual(1);
|
||||||
<!-- i18n: meaning1|desc1 -->message1`,
|
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
|
||||||
'someUrl');
|
});
|
||||||
|
|
||||||
|
it('should error when i18n comments are unbalanced', () => {
|
||||||
|
const res = extractor.extract('<!-- i18n -->', 'someUrl');
|
||||||
expect(res.errors.length).toEqual(1);
|
expect(res.errors.length).toEqual(1);
|
||||||
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
|
expect(res.errors[0].msg).toEqual('Missing closing \'i18n\' comment.');
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user