/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {HtmlParser} from "@angular/compiler/src/html_parser";
import * as i18nAst from "@angular/compiler/src/i18n/i18n_ast";
import {ddescribe, describe, expect, it} from "@angular/core/testing/testing_internal";
import {extractI18nMessages} from "@angular/compiler/src/i18n/i18n_parser";
export function main() {
ddescribe('I18nParser', () => {
describe('elements', () => {
it('should extract from elements', () => {
expect(extract('
text
')).toEqual([
[['text'], 'm', 'd'],
]);
});
it('should extract from nested elements', () => {
expect(extract('textnested
')).toEqual([
[['text', 'nested'], 'm', 'd'],
]);
});
it('should not create a message for empty elements',
() => { expect(extract('')).toEqual([]); });
it('should not create a message for plain elements',
() => { expect(extract('')).toEqual([]); });
});
describe('attributes', () => {
it('should extract from attributes outside of translatable section', () => {
expect(extract('')).toEqual([
[['msg'], 'm', 'd'],
]);
});
it('should extract from attributes in translatable element', () => {
expect(extract('')).toEqual([
[[''], '', ''],
[['msg'], 'm', 'd'],
]);
});
it('should extract from attributes in translatable block', () => {
expect(
extract('
'))
.toEqual([
[['msg'], 'm', 'd'],
[[''], '', ''],
]);
});
it('should extract from attributes in translatable ICU', () => {
expect(
extract(
'{count, plural, =0 {
}}'))
.toEqual([
[['msg'], 'm', 'd'],
[['{count, plural, =0 {[]}}'], '', ''],
]);
});
it('should extract from attributes in non translatable ICU', () => {
expect(extract('{count, plural, =0 {
}}'))
.toEqual([
[['msg'], 'm', 'd'],
]);
});
it('should not create a message for empty attributes',
() => { expect(extract('')).toEqual([]); });
});
describe('interpolation', () => {
it('should replace interpolation with placeholder', () => {
expect(extract('before{{ exp }}after
')).toEqual([
[['[before, exp , after]'], 'm', 'd'],
]);
});
it('should support named interpolation', () => {
expect(extract('before{{ exp //i18n(ph="teSt") }}after
')).toEqual([
[['[before, exp //i18n(ph="teSt") , after]'], 'm', 'd'],
]);
})
});
describe('blocks', () => {
it('should extract from blocks', () => {
expect(extract(`message1
message2
message3`))
.toEqual([
[['message1'], 'meaning1', 'desc1'],
[['message2'], 'meaning2', ''],
[['message3'], '', ''],
]);
});
it('should extract all siblings', () => {
expect(
extract(`texthtmlnested
`))
.toEqual([
[[ 'text', 'html, nested'], '', '' ],
]);
});
});
describe('ICU messages', () => {
it('should extract as ICU when single child of an element', () => {
expect(extract('{count, plural, =0 {zero}}
')).toEqual([
[['{count, plural, =0 {[zero]}}'], 'm', 'd'],
]);
});
it('should extract as ICU + ph when not single child of an element', () => {
expect(extract('b{count, plural, =0 {zero}}a
')).toEqual([
[[ 'b', '{count, plural, =0 {[zero]}}', 'a'], 'm', 'd'],
[[ '{count, plural, =0 {[zero]}}' ], '', ''],
]);
});
it('should extract as ICU when single child of a block', () => {
expect(extract('{count, plural, =0 {zero}}')).toEqual([
[['{count, plural, =0 {[zero]}}'], 'm', 'd'],
]);
});
it('should extract as ICU + ph when not single child of a block', () => {
expect(extract('b{count, plural, =0 {zero}}a')).toEqual([
[[ '{count, plural, =0 {[zero]}}' ], '', ''],
[[ 'b', '{count, plural, =0 {[zero]}}', 'a'], 'm', 'd'],
]);
});
it('should not extract nested ICU messages', () => {
expect(extract('b{count, plural, =0 {{sex, gender, =m {m}}}}a
')).toEqual([
[[ 'b', '{count, plural, =0 {[{sex, gender, =m {[m]}}]}}', 'a'], 'm', 'd'],
[[ '{count, plural, =0 {[{sex, gender, =m {[m]}}]}}' ], '', ''],
]);
});
});
describe('implicit elements', () => {
it('should extract from implicit elements', () => {
expect(extract('bolditalic', ['b'])).toEqual([
[['bold'], '', ''],
]);
});
});
describe('implicit attributes', () => {
it('should extract implicit attributes', () => {
expect(extract('bolditalic', [], {'b': ['title']}))
.toEqual([
[['bb'], '', ''],
]);
});
});
});
}
class _SerializerVisitor implements i18nAst.Visitor {
visitText(text:i18nAst.Text, context:any):any {
return text.value;
}
visitContainer(container:i18nAst.Container, context:any):any {
return `[${container.children.map(child => child.visit(this)).join(', ')}]`
}
visitIcu(icu:i18nAst.Icu, context:any):any {
let strCases = Object.keys(icu.cases).map((k: string) => `${k} {${icu.cases[k].visit(this)}}`);
return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`
}
visitTagPlaceholder(ph:i18nAst.TagPlaceholder, context:any):any {
return `${ph.children.map(child => child.visit(this)).join(', ')}`;
}
visitPlaceholder(ph:i18nAst.Placeholder, context:any):any {
return `${ph.value}`;
}
visitIcuPlaceholder(ph:i18nAst.IcuPlaceholder, context?:any):any {
return `${ph.value.visit(this)}`
}
}
const serializerVisitor = new _SerializerVisitor();
export function serializeAst(ast: i18nAst.I18nNode[]): string[] {
return ast.map(a => a.visit(serializerVisitor, null));
}
function extract(
html: string, implicitTags: string[] = [],
implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string][] {
const htmlParser = new HtmlParser();
const parseResult = htmlParser.parse(html, 'extractor spec', true);
if (parseResult.errors.length > 1) {
throw Error(`unexpected parse errors: ${parseResult.errors.join('\n')}`);
}
const messages = extractI18nMessages(parseResult.rootNodes, implicitTags, implicitAttrs);
// clang-format off
// https://github.com/angular/clang-format/issues/35
return messages.map(
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
// clang-format on
}