feat(i18n): xmb serializer
This commit is contained in:
91
modules/@angular/compiler/test/i18n/catalog_spec.ts
Normal file
91
modules/@angular/compiler/test/i18n/catalog_spec.ts
Normal file
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* @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 {Catalog, strHash} from '@angular/compiler/src/i18n/catalog';
|
||||
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import Serializable = webdriver.Serializable;
|
||||
import {Serializer} from '@angular/compiler/src/i18n/serializers/serializer';
|
||||
import {serializeAst} from '@angular/compiler/src/i18n/catalog';
|
||||
import * as i18nAst from '@angular/compiler/src/i18n/i18n_ast';
|
||||
|
||||
export function main(): void {
|
||||
ddescribe('Catalog', () => {
|
||||
|
||||
describe('write', () => {
|
||||
let catalog: Catalog;
|
||||
|
||||
beforeEach(() => { catalog = new Catalog(new HtmlParser, [], {}); });
|
||||
|
||||
it('should extract the message to the catalog', () => {
|
||||
catalog.updateFromTemplate(
|
||||
'<p i18n="m|d">Translate Me</p>', 'url', DEFAULT_INTERPOLATION_CONFIG);
|
||||
expect(humanizeCatalog(catalog)).toEqual([
|
||||
'a486901=Translate Me',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract the same message with different meaning in different entries', () => {
|
||||
catalog.updateFromTemplate(
|
||||
'<p i18n="m|d">Translate Me</p><p i18n>Translate Me</p>', 'url',
|
||||
DEFAULT_INTERPOLATION_CONFIG);
|
||||
expect(humanizeCatalog(catalog)).toEqual([
|
||||
'a486901=Translate Me',
|
||||
'8475f2cc=Translate Me',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe(
|
||||
'load', () => {
|
||||
// TODO
|
||||
});
|
||||
|
||||
describe('strHash', () => {
|
||||
it('should return a hash value', () => {
|
||||
// https://github.com/google/closure-library/blob/1fb19a857b96b74e6523f3e9d33080baf25be046/closure/goog/string/string_test.js#L1115
|
||||
expectHash('', 0);
|
||||
expectHash('foo', 101574);
|
||||
expectHash('\uAAAAfoo', 1301670364);
|
||||
expectHash('a', 92567585, 5);
|
||||
expectHash('a', 2869595232, 6);
|
||||
expectHash('a', 3058106369, 7);
|
||||
expectHash('a', 312017024, 8);
|
||||
expectHash('a', 2929737728, 1024);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class _TestSerializer implements Serializer {
|
||||
write(messageMap: {[k: string]: i18nAst.Message}): string {
|
||||
return Object.keys(messageMap)
|
||||
.map(id => `${id}=${serializeAst(messageMap[id].nodes)}`)
|
||||
.join('//');
|
||||
}
|
||||
|
||||
load(content: string): {[k: string]: i18nAst.Node[]} { return null; }
|
||||
}
|
||||
|
||||
function humanizeCatalog(catalog: Catalog): string[] {
|
||||
return catalog.write(new _TestSerializer()).split('//');
|
||||
}
|
||||
|
||||
function expectHash(text: string, decimal: number, repeat: number = 1) {
|
||||
let acc = text;
|
||||
for (let i = 1; i < repeat; i++) {
|
||||
acc += text;
|
||||
}
|
||||
|
||||
const hash = strHash(acc);
|
||||
expect(typeof(hash)).toEqual('string');
|
||||
expect(hash.length > 0).toBe(true);
|
||||
expect(parseInt(hash, 16)).toEqual(decimal);
|
||||
}
|
@ -263,8 +263,8 @@ export function main() {
|
||||
}
|
||||
|
||||
function getExtractionResult(
|
||||
html: string, implicitTags: string[],
|
||||
implicitAttrs: {[k: string]: string[]}): ExtractionResult {
|
||||
html: string, implicitTags: string[], implicitAttrs:
|
||||
{[k: string]: string[]}): ExtractionResult {
|
||||
const htmlParser = new HtmlParser();
|
||||
const parseResult = htmlParser.parse(html, 'extractor spec', true);
|
||||
if (parseResult.errors.length > 1) {
|
||||
@ -275,8 +275,8 @@ function getExtractionResult(
|
||||
}
|
||||
|
||||
function extract(
|
||||
html: string, implicitTags: string[] = [],
|
||||
implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string][] {
|
||||
html: string, implicitTags: string[] = [], implicitAttrs:
|
||||
{[k: string]: string[]} = {}): [string[], string, string][] {
|
||||
const messages = getExtractionResult(html, implicitTags, implicitAttrs).messages;
|
||||
|
||||
// clang-format off
|
||||
@ -287,8 +287,8 @@ function extract(
|
||||
}
|
||||
|
||||
function extractErrors(
|
||||
html: string, implicitTags: string[] = [],
|
||||
implicitAttrs: {[k: string]: string[]} = {}): any[] {
|
||||
html: string, implicitTags: string[] = [], implicitAttrs:
|
||||
{[k: string]: string[]} = {}): any[] {
|
||||
const errors = getExtractionResult(html, implicitTags, implicitAttrs).errors;
|
||||
|
||||
return errors.map((e): [string, string] => [e.msg, e.span.toString()]);
|
||||
|
@ -6,10 +6,12 @@
|
||||
* 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";
|
||||
import {HtmlParser} from '@angular/compiler/src/html_parser';
|
||||
import {serializeAst} from '@angular/compiler/src/i18n/catalog';
|
||||
import {extractI18nMessages} from '@angular/compiler/src/i18n/i18n_parser';
|
||||
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||
import {ddescribe, describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||
|
||||
export function main() {
|
||||
ddescribe('I18nParser', () => {
|
||||
|
||||
@ -22,15 +24,32 @@ export function main() {
|
||||
|
||||
it('should extract from nested elements', () => {
|
||||
expect(extract('<div i18n="m|d">text<span><b>nested</b></span></div>')).toEqual([
|
||||
[['text', '<ph tag name="span"><ph tag name="b">nested</ph></ph>'], 'm', 'd'],
|
||||
[
|
||||
[
|
||||
'text',
|
||||
'<ph tag name="START_TAG_SPAN"><ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_TAG_SPAN">'
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not create a message for empty elements',
|
||||
() => { expect(extract('<div i18n="m|d"></div>')).toEqual([]); });
|
||||
() => { expect(extract('<div i18n="m|d"></div>')).toEqual([]); });
|
||||
|
||||
it('should not create a message for plain elements',
|
||||
() => { expect(extract('<div></div>')).toEqual([]); });
|
||||
() => { expect(extract('<div></div>')).toEqual([]); });
|
||||
|
||||
it('should suppoprt void elements', () => {
|
||||
expect(extract('<div i18n="m|d"><p><br></p></div>')).toEqual([
|
||||
[
|
||||
[
|
||||
'<ph tag name="START_PARAGRAPH"><ph tag name="LINE_BREAK"/></ph name="CLOSE_PARAGRAPH">'
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('attributes', () => {
|
||||
@ -42,51 +61,65 @@ export function main() {
|
||||
|
||||
it('should extract from attributes in translatable element', () => {
|
||||
expect(extract('<div i18n><p><b i18n-title="m|d" title="msg"></b></p></div>')).toEqual([
|
||||
[['<ph tag name="p"><ph tag name="b"></ph></ph>'], '', ''],
|
||||
[
|
||||
[
|
||||
'<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||
],
|
||||
'', ''
|
||||
],
|
||||
[['msg'], 'm', 'd'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract from attributes in translatable block', () => {
|
||||
expect(
|
||||
extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->'))
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
[['<ph tag name="p"><ph tag name="b"></ph></ph>'], '', ''],
|
||||
]);
|
||||
expect(extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->'))
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
[
|
||||
[
|
||||
'<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||
],
|
||||
'', ''
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract from attributes in translatable ICU', () => {
|
||||
expect(
|
||||
extract(
|
||||
'<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}<!-- /i18n -->'))
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
[['{count, plural, =0 {[<ph tag name="p"><ph tag name="b"></ph></ph>]}}'], '', ''],
|
||||
]);
|
||||
extract(
|
||||
'<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}<!-- /i18n -->'))
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
[
|
||||
[
|
||||
'{count, plural, =0 {[<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">]}}'
|
||||
],
|
||||
'', ''
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract from attributes in non translatable ICU', () => {
|
||||
expect(extract('{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}'))
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
]);
|
||||
.toEqual([
|
||||
[['msg'], 'm', 'd'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not create a message for empty attributes',
|
||||
() => { expect(extract('<div i18n-title="m|d" title></div>')).toEqual([]); });
|
||||
() => { expect(extract('<div i18n-title="m|d" title></div>')).toEqual([]); });
|
||||
});
|
||||
|
||||
describe('interpolation', () => {
|
||||
it('should replace interpolation with placeholder', () => {
|
||||
expect(extract('<div i18n="m|d">before{{ exp }}after</div>')).toEqual([
|
||||
[['[before, <ph name="interpolation"> exp </ph>, after]'], 'm', 'd'],
|
||||
[['[before, <ph name="INTERPOLATION"> exp </ph>, after]'], 'm', 'd'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should support named interpolation', () => {
|
||||
expect(extract('<div i18n="m|d">before{{ exp //i18n(ph="teSt") }}after</div>')).toEqual([
|
||||
[['[before, <ph name="teSt"> exp //i18n(ph="teSt") </ph>, after]'], 'm', 'd'],
|
||||
[['[before, <ph name="TEST"> exp //i18n(ph="teSt") </ph>, after]'], 'm', 'd'],
|
||||
]);
|
||||
})
|
||||
});
|
||||
@ -96,19 +129,23 @@ export function main() {
|
||||
expect(extract(`<!-- i18n: meaning1|desc1 -->message1<!-- /i18n -->
|
||||
<!-- i18n: meaning2 -->message2<!-- /i18n -->
|
||||
<!-- i18n -->message3<!-- /i18n -->`))
|
||||
.toEqual([
|
||||
[['message1'], 'meaning1', 'desc1'],
|
||||
[['message2'], 'meaning2', ''],
|
||||
[['message3'], '', ''],
|
||||
]);
|
||||
.toEqual([
|
||||
[['message1'], 'meaning1', 'desc1'],
|
||||
[['message2'], 'meaning2', ''],
|
||||
[['message3'], '', ''],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract all siblings', () => {
|
||||
expect(
|
||||
extract(`<!-- i18n -->text<p>html<b>nested</b></p><!-- /i18n -->`))
|
||||
.toEqual([
|
||||
[[ 'text', '<ph tag name="p">html, <ph tag name="b">nested</ph></ph>'], '', '' ],
|
||||
]);
|
||||
expect(extract(`<!-- i18n -->text<p>html<b>nested</b></p><!-- /i18n -->`)).toEqual([
|
||||
[
|
||||
[
|
||||
'text',
|
||||
'<ph tag name="START_PARAGRAPH">html, <ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||
],
|
||||
'', ''
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -121,8 +158,8 @@ export function main() {
|
||||
|
||||
it('should extract as ICU + ph when not single child of an element', () => {
|
||||
expect(extract('<div i18n="m|d">b{count, plural, =0 {zero}}a</div>')).toEqual([
|
||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||
[[ '{count, plural, =0 {[zero]}}' ], '', ''],
|
||||
[['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||
[['{count, plural, =0 {[zero]}}'], '', ''],
|
||||
]);
|
||||
});
|
||||
|
||||
@ -134,16 +171,23 @@ export function main() {
|
||||
|
||||
it('should extract as ICU + ph when not single child of a block', () => {
|
||||
expect(extract('<!-- i18n:m|d -->b{count, plural, =0 {zero}}a<!-- /i18n -->')).toEqual([
|
||||
[[ '{count, plural, =0 {[zero]}}' ], '', ''],
|
||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||
[['{count, plural, =0 {[zero]}}'], '', ''],
|
||||
[['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should not extract nested ICU messages', () => {
|
||||
expect(extract('<div i18n="m|d">b{count, plural, =0 {{sex, gender, =m {m}}}}a</div>')).toEqual([
|
||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[{sex, gender, =m {[m]}}]}}</ph>', 'a'], 'm', 'd'],
|
||||
[[ '{count, plural, =0 {[{sex, gender, =m {[m]}}]}}' ], '', ''],
|
||||
]);
|
||||
expect(extract('<div i18n="m|d">b{count, plural, =0 {{sex, gender, =m {m}}}}a</div>'))
|
||||
.toEqual([
|
||||
[
|
||||
[
|
||||
'b', '<ph icu name="ICU">{count, plural, =0 {[{sex, gender, =m {[m]}}]}}</ph>',
|
||||
'a'
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
[['{count, plural, =0 {[{sex, gender, =m {[m]}}]}}'], '', ''],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -158,57 +202,71 @@ export function main() {
|
||||
describe('implicit attributes', () => {
|
||||
it('should extract implicit attributes', () => {
|
||||
expect(extract('<b title="bb">bold</b><i title="ii">italic</i>', [], {'b': ['title']}))
|
||||
.toEqual([
|
||||
[['bb'], '', ''],
|
||||
]);
|
||||
.toEqual([
|
||||
[['bb'], '', ''],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('placeholders', () => {
|
||||
it('should reuse the same placeholder name for tags', () => {
|
||||
expect(extract('<div i18n="m|d"><p>one</p><p>two</p><p other>three</p></div>')).toEqual([
|
||||
[
|
||||
[
|
||||
'<ph tag name="START_PARAGRAPH">one</ph name="CLOSE_PARAGRAPH">',
|
||||
'<ph tag name="START_PARAGRAPH">two</ph name="CLOSE_PARAGRAPH">',
|
||||
'<ph tag name="START_PARAGRAPH_1">three</ph name="CLOSE_PARAGRAPH">',
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should reuse the same placeholder name for interpolations', () => {
|
||||
expect(extract('<div i18n="m|d">{{ a }}{{ a }}{{ b }}</div>')).toEqual([
|
||||
[
|
||||
[
|
||||
'[<ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION_1"> b </ph>]'
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('should reuse the same placeholder name for icu messages', () => {
|
||||
expect(
|
||||
extract(
|
||||
'<div i18n="m|d">{count, plural, =0 {0}}{count, plural, =0 {0}}{count, plural, =1 {1}}</div>'))
|
||||
.toEqual([
|
||||
[
|
||||
[
|
||||
'<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>',
|
||||
'<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>',
|
||||
'<ph icu name="ICU_1">{count, plural, =1 {[1]}}</ph>',
|
||||
],
|
||||
'm', 'd'
|
||||
],
|
||||
[['{count, plural, =0 {[0]}}'], '', ''],
|
||||
[['{count, plural, =0 {[0]}}'], '', ''],
|
||||
[['{count, plural, =1 {[1]}}'], '', ''],
|
||||
]);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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 tag name="${ph.name}">${ph.children.map(child => child.visit(this)).join(', ')}</ph>`;
|
||||
}
|
||||
|
||||
visitPlaceholder(ph:i18nAst.Placeholder, context:any):any {
|
||||
return `<ph name="${ph.name}">${ph.value}</ph>`;
|
||||
}
|
||||
|
||||
visitIcuPlaceholder(ph:i18nAst.IcuPlaceholder, context?:any):any {
|
||||
return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`
|
||||
}
|
||||
}
|
||||
|
||||
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][] {
|
||||
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);
|
||||
const messages = extractI18nMessages(
|
||||
parseResult.rootNodes, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
|
||||
|
||||
// clang-format off
|
||||
// https://github.com/angular/clang-format/issues/35
|
||||
@ -216,5 +274,3 @@ function extract(
|
||||
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
|
||||
|
94
modules/@angular/compiler/test/i18n/serializers/util_spec.ts
Normal file
94
modules/@angular/compiler/test/i18n/serializers/util_spec.ts
Normal file
@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @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 {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {PlaceholderRegistry} from '../../../src/i18n/serializers/util';
|
||||
|
||||
export function main(): void {
|
||||
ddescribe('PlaceholderRegistry', () => {
|
||||
let reg: PlaceholderRegistry;
|
||||
|
||||
beforeEach(() => { reg = new PlaceholderRegistry(); });
|
||||
|
||||
describe('tag placeholder', () => {
|
||||
it('should generate names for well known tags', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||
expect(reg.getCloseTagPlaceholderName('p')).toEqual('CLOSE_PARAGRAPH');
|
||||
});
|
||||
|
||||
it('should generate names for custom tags', () => {
|
||||
expect(reg.getStartTagPlaceholderName('my-cmp', {}, false)).toEqual('START_TAG_MY-CMP');
|
||||
expect(reg.getCloseTagPlaceholderName('my-cmp')).toEqual('CLOSE_TAG_MY-CMP');
|
||||
});
|
||||
|
||||
it('should generate the same name for the same tag', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||
});
|
||||
|
||||
it('should be case insensitive for tag name', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('P', {}, false)).toEqual('START_PARAGRAPH');
|
||||
expect(reg.getCloseTagPlaceholderName('p')).toEqual('CLOSE_PARAGRAPH');
|
||||
expect(reg.getCloseTagPlaceholderName('P')).toEqual('CLOSE_PARAGRAPH');
|
||||
});
|
||||
|
||||
it('should generate the same name for the same tag with the same attributes', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {bar: 'b', foo: 'a'}, false))
|
||||
.toEqual('START_PARAGRAPH');
|
||||
});
|
||||
|
||||
it('should generate different names for the same tag with different attributes', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {foo: 'a'}, false)).toEqual('START_PARAGRAPH_1');
|
||||
});
|
||||
|
||||
it('should be case sensitive for attributes', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {fOo: 'a', bar: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH_1');
|
||||
expect(reg.getStartTagPlaceholderName('p', {fOo: 'a', bAr: 'b'}, false))
|
||||
.toEqual('START_PARAGRAPH_2');
|
||||
});
|
||||
|
||||
it('should support void tags', () => {
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, true)).toEqual('PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {}, true)).toEqual('PARAGRAPH');
|
||||
expect(reg.getStartTagPlaceholderName('p', {other: 'true'}, true)).toEqual('PARAGRAPH_1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('arbitrary placeholders', () => {
|
||||
it('should generate the same name given the same name and content', () => {
|
||||
expect(reg.getPlaceholderName('name', 'content')).toEqual('NAME');
|
||||
expect(reg.getPlaceholderName('name', 'content')).toEqual('NAME');
|
||||
});
|
||||
|
||||
it('should generate a different name given different content', () => {
|
||||
expect(reg.getPlaceholderName('name', 'content1')).toEqual('NAME');
|
||||
expect(reg.getPlaceholderName('name', 'content2')).toEqual('NAME_1');
|
||||
expect(reg.getPlaceholderName('name', 'content3')).toEqual('NAME_2');
|
||||
});
|
||||
|
||||
it('should generate a different name given different names', () => {
|
||||
expect(reg.getPlaceholderName('name1', 'content')).toEqual('NAME1');
|
||||
expect(reg.getPlaceholderName('name2', 'content')).toEqual('NAME2');
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
68
modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts
Normal file
68
modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts
Normal file
@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @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 {Catalog} from '@angular/compiler/src/i18n/catalog';
|
||||
import {XmbSerializer} from '@angular/compiler/src/i18n/serializers/xmb';
|
||||
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
|
||||
export function main(): void {
|
||||
ddescribe('XMB serializer', () => {
|
||||
const HTML = `
|
||||
<p>not translatable</p>
|
||||
<p i18n>translatable element <b>with placeholders</b> {{ interpolation}}</p>
|
||||
<!-- i18n -->{ count, plural, =0 {<p>test</p>}}<!-- /i18n -->
|
||||
<p i18n="m|d">foo</p>
|
||||
<p i18n>{ count, plural, =0 { { sex, gender, other {<p>deeply nested</p>}} }}</p>`;
|
||||
|
||||
const XMB = `<? xml version="1.0" encoding="UTF-8" ?>
|
||||
<messagebundle>
|
||||
<msg id="834fa53b">translatable element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></msg>
|
||||
<msg id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</msg>
|
||||
<msg id="b45e58a5" desc="d" meaning="m">foo</msg>
|
||||
<msg id="18ea85bc">{ count, plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</msg>
|
||||
</messagebundle>`;
|
||||
|
||||
it('should write a valid xmb file', () => { expect(toXmb(HTML)).toEqual(XMB); });
|
||||
|
||||
it('should throw when trying to load an xmb file', () => {
|
||||
expect(() => {
|
||||
const serializer = new XmbSerializer();
|
||||
serializer.load(XMB);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function toXmb(html: string): string {
|
||||
let catalog = new Catalog(new HtmlParser, [], {});
|
||||
const serializer = new XmbSerializer();
|
||||
|
||||
catalog.updateFromTemplate(html, '', DEFAULT_INTERPOLATION_CONFIG);
|
||||
|
||||
return catalog.write(serializer);
|
||||
}
|
||||
|
||||
// <? xml version="1.0" encoding="UTF-8" ?><messagebundle><message id="834fa53b">translatable
|
||||
// element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph
|
||||
// name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></message><message
|
||||
// id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph
|
||||
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</message><message id="b45e58a5" description="d"
|
||||
// meaning="m">foo</message><message id="18ea85bc">{ count, plural, =0 {{ sex, gender, other {<ph
|
||||
// name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph
|
||||
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</message></messagebundle>
|
||||
// <? xml version="1.0" encoding="UTF-8" ?><messagebundle><message id="834fa53b">translatable
|
||||
// element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph
|
||||
// name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></message><message
|
||||
// id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph
|
||||
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</message><message id="18ea85bc">{ count,
|
||||
// plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply
|
||||
// nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</message><message id="b45e58a5"
|
||||
// description="d" meaning="m">foo</message></messagebundle>
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @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 {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import * as xml from '../../../src/i18n/serializers/xml_helper';
|
||||
|
||||
export function main(): void {
|
||||
ddescribe('XML helper', () => {
|
||||
it('should serialize XML declaration', () => {
|
||||
expect(xml.serialize([new xml.Declaration({version: '1.0'})]))
|
||||
.toEqual('<? xml version="1.0" ?>');
|
||||
});
|
||||
|
||||
it('should serialize text node',
|
||||
() => { expect(xml.serialize([new xml.Text('foo bar')])).toEqual('foo bar'); });
|
||||
|
||||
it('should escape text nodes',
|
||||
() => { expect(xml.serialize([new xml.Text('<>')])).toEqual('<>'); });
|
||||
|
||||
it('should serialize xml nodes without children', () => {
|
||||
expect(xml.serialize([new xml.Tag('el', {foo: 'bar'}, [])])).toEqual('<el foo="bar"/>');
|
||||
});
|
||||
|
||||
it('should serialize xml nodes with children', () => {
|
||||
expect(xml.serialize([
|
||||
new xml.Tag('parent', {}, [new xml.Tag('child', {}, [new xml.Text('content')])])
|
||||
])).toEqual('<parent><child>content</child></parent>');
|
||||
});
|
||||
|
||||
it('should serialize node lists', () => {
|
||||
expect(xml.serialize([
|
||||
new xml.Tag('el', {order: '0'}, []),
|
||||
new xml.Tag('el', {order: '1'}, []),
|
||||
])).toEqual('<el order="0"/><el order="1"/>');
|
||||
});
|
||||
|
||||
it('should escape attribute values', () => {
|
||||
expect(xml.serialize([new xml.Tag('el', {foo: '<">'}, [])]))
|
||||
.toEqual('<el foo="<">"/>');
|
||||
});
|
||||
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user