2016-07-01 20:29:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-10-28 22:53:42 -04:00
|
|
|
import {DEFAULT_INTERPOLATION_CONFIG, HtmlParser} from '@angular/compiler';
|
2017-07-07 19:16:49 -04:00
|
|
|
import {MissingTranslationStrategy} from '@angular/core';
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-10-28 22:53:42 -04:00
|
|
|
import {digest, serializeNodes as serializeI18nNodes} from '../../src/i18n/digest';
|
2016-08-04 20:46:31 -04:00
|
|
|
import {extractMessages, mergeTranslations} from '../../src/i18n/extractor_merger';
|
|
|
|
import * as i18n from '../../src/i18n/i18n_ast';
|
2016-08-01 21:10:34 -04:00
|
|
|
import {TranslationBundle} from '../../src/i18n/translation_bundle';
|
|
|
|
import * as html from '../../src/ml_parser/ast';
|
2018-11-27 15:32:14 -05:00
|
|
|
import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util';
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-08-01 21:10:34 -04:00
|
|
|
describe('Extractor', () => {
|
2016-07-21 16:56:58 -04:00
|
|
|
describe('elements', () => {
|
|
|
|
it('should extract from elements', () => {
|
2016-08-01 14:33:35 -04:00
|
|
|
expect(extract('<div i18n="m|d|e">text<span>nested</span></div>')).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[
|
|
|
|
['text', '<ph tag name="START_TAG_SPAN">nested</ph name="CLOSE_TAG_SPAN">'], 'm', 'd|e',
|
|
|
|
''
|
|
|
|
],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
2016-08-01 16:11:48 -04:00
|
|
|
});
|
|
|
|
|
2016-08-04 20:46:31 -04:00
|
|
|
it('should extract from attributes', () => {
|
2016-08-01 17:43:20 -04:00
|
|
|
expect(
|
|
|
|
extract(
|
2016-08-04 20:46:31 -04:00
|
|
|
'<div i18n="m1|d1"><span i18n-title="m2|d2" title="single child">nested</span></div>'))
|
2016-08-01 17:43:20 -04:00
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['<ph tag name="START_TAG_SPAN">nested</ph name="CLOSE_TAG_SPAN">'], 'm1', 'd1', ''],
|
|
|
|
[['single child'], 'm2', 'd2', ''],
|
2016-08-01 17:43:20 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2016-12-06 09:04:59 -05:00
|
|
|
it('should extract from attributes with id', () => {
|
|
|
|
expect(
|
|
|
|
extract(
|
|
|
|
'<div i18n="m1|d1@@i1"><span i18n-title="m2|d2@@i2" title="single child">nested</span></div>'))
|
|
|
|
.toEqual([
|
|
|
|
[
|
|
|
|
['<ph tag name="START_TAG_SPAN">nested</ph name="CLOSE_TAG_SPAN">'], 'm1', 'd1',
|
|
|
|
'i1'
|
|
|
|
],
|
|
|
|
[['single child'], 'm2', 'd2', 'i2'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-12-03 03:36:56 -05:00
|
|
|
it('should trim whitespace from custom ids (but not meanings)', () => {
|
|
|
|
expect(extract('<div i18n="\n m1|d1@@i1\n ">test</div>')).toEqual([
|
|
|
|
[['test'], '\n m1', 'd1', 'i1'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2016-12-06 09:04:59 -05:00
|
|
|
it('should extract from attributes without meaning and with id', () => {
|
|
|
|
expect(
|
|
|
|
extract(
|
|
|
|
'<div i18n="d1@@i1"><span i18n-title="d2@@i2" title="single child">nested</span></div>'))
|
|
|
|
.toEqual([
|
|
|
|
[['<ph tag name="START_TAG_SPAN">nested</ph name="CLOSE_TAG_SPAN">'], '', 'd1', 'i1'],
|
|
|
|
[['single child'], '', 'd2', 'i2'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract from attributes with id only', () => {
|
|
|
|
expect(
|
|
|
|
extract(
|
|
|
|
'<div i18n="@@i1"><span i18n-title="@@i2" title="single child">nested</span></div>'))
|
|
|
|
.toEqual([
|
|
|
|
[['<ph tag name="START_TAG_SPAN">nested</ph name="CLOSE_TAG_SPAN">'], '', '', 'i1'],
|
|
|
|
[['single child'], '', '', 'i2'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-08-04 20:46:31 -04:00
|
|
|
it('should extract from ICU messages', () => {
|
2016-08-01 16:11:48 -04:00
|
|
|
expect(
|
|
|
|
extract(
|
|
|
|
'<div i18n="m|d">{count, plural, =0 { <p i18n-title i18n-desc title="title" desc="desc"></p>}}</div>'))
|
|
|
|
.toEqual([
|
|
|
|
[
|
|
|
|
[
|
2016-08-04 20:46:31 -04:00
|
|
|
'{count, plural, =0 {[<ph tag name="START_PARAGRAPH"></ph name="CLOSE_PARAGRAPH">]}}'
|
2016-08-01 16:11:48 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
'm', 'd', ''
|
2016-08-01 16:11:48 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
[['title'], '', '', ''],
|
|
|
|
[['desc'], '', '', ''],
|
2016-08-01 16:11:48 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should not create a message for empty elements', () => {
|
|
|
|
expect(extract('<div i18n="m|d"></div>')).toEqual([]);
|
|
|
|
});
|
2016-08-05 17:45:47 -04:00
|
|
|
|
|
|
|
it('should ignore implicit elements in translatable elements', () => {
|
|
|
|
expect(extract('<div i18n="m|d"><p></p></div>', ['p'])).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['<ph tag name="START_PARAGRAPH"></ph name="CLOSE_PARAGRAPH">'], 'm', 'd', '']
|
2016-08-05 17:45:47 -04:00
|
|
|
]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
describe('blocks', () => {
|
|
|
|
it('should extract from blocks', () => {
|
|
|
|
expect(extract(`<!-- i18n: meaning1|desc1 -->message1<!-- /i18n -->
|
2016-08-01 14:33:35 -04:00
|
|
|
<!-- i18n: desc2 -->message2<!-- /i18n -->
|
2016-12-06 09:04:59 -05:00
|
|
|
<!-- i18n -->message3<!-- /i18n -->
|
|
|
|
<!-- i18n: meaning4|desc4@@id4 -->message4<!-- /i18n -->
|
|
|
|
<!-- i18n: @@id5 -->message5<!-- /i18n -->`))
|
2016-07-21 16:56:58 -04:00
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['message1'], 'meaning1', 'desc1', ''], [['message2'], '', 'desc2', ''],
|
|
|
|
[['message3'], '', '', ''], [['message4'], 'meaning4', 'desc4', 'id4'],
|
|
|
|
[['message5'], '', '', 'id5']
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2016-08-05 17:45:47 -04:00
|
|
|
it('should ignore implicit elements in blocks', () => {
|
|
|
|
expect(extract('<!-- i18n:m|d --><p></p><!-- /i18n -->', ['p'])).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['<ph tag name="START_PARAGRAPH"></ph name="CLOSE_PARAGRAPH">'], 'm', 'd', '']
|
2016-08-05 17:45:47 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
it('should extract siblings', () => {
|
|
|
|
expect(
|
|
|
|
extract(
|
|
|
|
`<!-- i18n -->text<p>html<b>nested</b></p>{count, plural, =0 {<span>html</span>}}{{interp}}<!-- /i18n -->`))
|
|
|
|
.toEqual([
|
|
|
|
[
|
|
|
|
[
|
2016-08-04 20:46:31 -04:00
|
|
|
'{count, plural, =0 {[<ph tag name="START_TAG_SPAN">html</ph name="CLOSE_TAG_SPAN">]}}'
|
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
'', '', ''
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2020-04-08 13:14:18 -04:00
|
|
|
'text',
|
|
|
|
'<ph tag name="START_PARAGRAPH">html, <ph tag' +
|
2016-11-02 20:40:15 -04:00
|
|
|
' name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">',
|
|
|
|
'<ph icu name="ICU">{count, plural, =0 {[<ph tag' +
|
|
|
|
' name="START_TAG_SPAN">html</ph name="CLOSE_TAG_SPAN">]}}</ph>',
|
2016-08-04 20:46:31 -04:00
|
|
|
'[<ph name="INTERPOLATION">interp</ph>]'
|
2016-07-21 16:56:58 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
'', '', ''
|
2016-07-21 16:56:58 -04:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
it('should ignore other comments', () => {
|
2016-12-06 09:04:59 -05:00
|
|
|
expect(extract(`<!-- i18n: meaning1|desc1@@id1 --><!-- other -->message1<!-- /i18n -->`))
|
2016-07-21 16:56:58 -04:00
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['message1'], 'meaning1', 'desc1', 'id1'],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should not create a message for empty blocks', () => {
|
|
|
|
expect(extract(`<!-- i18n: meaning1|desc1 --><!-- /i18n -->`)).toEqual([]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ICU messages', () => {
|
|
|
|
it('should extract ICU messages from translatable elements', () => {
|
|
|
|
// single message when ICU is the only children
|
|
|
|
expect(extract('<div i18n="m|d">{count, plural, =0 {text}}</div>')).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['{count, plural, =0 {[text]}}'], 'm', 'd', ''],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
|
2016-08-01 17:43:20 -04:00
|
|
|
// single message when ICU is the only (implicit) children
|
|
|
|
expect(extract('<div>{count, plural, =0 {text}}</div>', ['div'])).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['{count, plural, =0 {[text]}}'], '', '', ''],
|
2016-08-01 17:43:20 -04:00
|
|
|
]);
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
// one message for the element content and one message for the ICU
|
2016-12-06 09:04:59 -05:00
|
|
|
expect(extract('<div i18n="m|d@@i">before{count, plural, =0 {text}}after</div>')).toEqual([
|
|
|
|
[
|
|
|
|
['before', '<ph icu name="ICU">{count, plural, =0 {[text]}}</ph>', 'after'], 'm', 'd',
|
|
|
|
'i'
|
|
|
|
],
|
|
|
|
[['{count, plural, =0 {[text]}}'], '', '', ''],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract ICU messages from translatable block', () => {
|
|
|
|
// single message when ICU is the only children
|
|
|
|
expect(extract('<!-- i18n:m|d -->{count, plural, =0 {text}}<!-- /i18n -->')).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['{count, plural, =0 {[text]}}'], 'm', 'd', ''],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
// one message for the block content and one message for the ICU
|
|
|
|
expect(extract('<!-- i18n:m|d -->before{count, plural, =0 {text}}after<!-- /i18n -->'))
|
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['{count, plural, =0 {[text]}}'], '', '', ''],
|
2016-08-04 20:46:31 -04:00
|
|
|
[
|
|
|
|
['before', '<ph icu name="ICU">{count, plural, =0 {[text]}}</ph>', 'after'], 'm',
|
2016-12-06 09:04:59 -05:00
|
|
|
'd', ''
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should not extract ICU messages outside of i18n sections', () => {
|
|
|
|
expect(extract('{count, plural, =0 {text}}')).toEqual([]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
|
2016-08-05 17:45:47 -04:00
|
|
|
it('should ignore nested ICU messages', () => {
|
2016-11-04 18:10:19 -04:00
|
|
|
expect(extract('<div i18n="m|d">{count, plural, =0 { {sex, select, male {m}} }}</div>'))
|
2016-07-21 16:56:58 -04:00
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['{count, plural, =0 {[{sex, select, male {[m]}}, ]}}'], 'm', 'd', ''],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-08-05 17:45:47 -04:00
|
|
|
|
|
|
|
it('should ignore implicit elements in non translatable ICU messages', () => {
|
2016-12-06 09:04:59 -05:00
|
|
|
expect(extract(
|
|
|
|
'<div i18n="m|d@@i">{count, plural, =0 { {sex, select, male {<p>ignore</p>}}' +
|
|
|
|
' }}</div>',
|
|
|
|
['p']))
|
2016-08-05 17:45:47 -04:00
|
|
|
.toEqual([[
|
|
|
|
[
|
2016-11-04 18:10:19 -04:00
|
|
|
'{count, plural, =0 {[{sex, select, male {[<ph tag name="START_PARAGRAPH">ignore</ph name="CLOSE_PARAGRAPH">]}}, ]}}'
|
2016-08-05 17:45:47 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
'm', 'd', 'i'
|
2016-08-05 17:45:47 -04:00
|
|
|
]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore implicit elements in non translatable ICU messages', () => {
|
2016-11-04 18:10:19 -04:00
|
|
|
expect(extract('{count, plural, =0 { {sex, select, male {<p>ignore</p>}} }}', ['p']))
|
2016-08-05 17:45:47 -04:00
|
|
|
.toEqual([]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
describe('attributes', () => {
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should extract from attributes outside of translatable sections', () => {
|
2016-12-06 09:04:59 -05:00
|
|
|
expect(extract('<div i18n-title="m|d@@i" title="msg"></div>')).toEqual([
|
|
|
|
[['msg'], 'm', 'd', 'i'],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should extract from attributes in translatable elements', () => {
|
2016-12-06 09:04:59 -05:00
|
|
|
expect(extract('<div i18n><p><b i18n-title="m|d@@i" title="msg"></b></p></div>')).toEqual([
|
2016-08-04 20:46:31 -04:00
|
|
|
[
|
2016-11-02 20:40:15 -04:00
|
|
|
['<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph' +
|
|
|
|
' name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'],
|
2016-12-06 09:04:59 -05:00
|
|
|
'', '', ''
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
[['msg'], 'm', 'd', 'i'],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should extract from attributes in translatable blocks', () => {
|
2016-07-21 16:56:58 -04:00
|
|
|
expect(extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->'))
|
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['msg'], 'm', 'd', ''],
|
2016-08-04 20:46:31 -04:00
|
|
|
[
|
2016-11-02 20:40:15 -04:00
|
|
|
['<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph' +
|
|
|
|
' name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'],
|
2016-12-06 09:04:59 -05:00
|
|
|
'', '', ''
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should extract from attributes in translatable ICUs', () => {
|
2017-08-16 12:00:03 -04:00
|
|
|
expect(extract(`<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d@@i"
|
2016-12-06 09:04:59 -05:00
|
|
|
title="msg"></b></p>}}<!-- /i18n -->`))
|
2016-07-21 16:56:58 -04:00
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['msg'], 'm', 'd', 'i'],
|
2016-08-04 20:46:31 -04:00
|
|
|
[
|
|
|
|
[
|
2016-11-02 20:40:15 -04:00
|
|
|
'{count, plural, =0 {[<ph tag name="START_PARAGRAPH"><ph tag' +
|
|
|
|
' name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">]}}'
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
2016-12-06 09:04:59 -05:00
|
|
|
'', '', ''
|
2016-08-04 20:46:31 -04:00
|
|
|
],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should extract from attributes in non translatable ICUs', () => {
|
2016-07-21 16:56:58 -04:00
|
|
|
expect(extract('{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}'))
|
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['msg'], 'm', 'd', ''],
|
2016-07-01 20:29:54 -04:00
|
|
|
]);
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should not create a message for empty attributes', () => {
|
|
|
|
expect(extract('<div i18n-title="m|d" title></div>')).toEqual([]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('implicit elements', () => {
|
|
|
|
it('should extract from implicit elements', () => {
|
|
|
|
expect(extract('<b>bold</b><i>italic</i>', ['b'])).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['bold'], '', '', ''],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
2016-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
it('should allow nested implicit elements', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
let result: any[] = undefined!;
|
2016-08-05 17:14:31 -04:00
|
|
|
|
2016-08-08 15:44:48 -04:00
|
|
|
expect(() => {
|
|
|
|
result = extract('<div>outer<div>inner</div></div>', ['div']);
|
|
|
|
}).not.toThrow();
|
2016-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
expect(result).toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['outer', '<ph tag name="START_TAG_DIV">inner</ph name="CLOSE_TAG_DIV">'], '', '', ''],
|
2016-08-05 17:14:31 -04:00
|
|
|
]);
|
|
|
|
});
|
2016-07-21 16:56:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('implicit attributes', () => {
|
|
|
|
it('should extract implicit attributes', () => {
|
|
|
|
expect(extract('<b title="bb">bold</b><i title="ii">italic</i>', [], {'b': ['title']}))
|
|
|
|
.toEqual([
|
2016-12-06 09:04:59 -05:00
|
|
|
[['bb'], '', '', ''],
|
2016-07-21 16:56:58 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('errors', () => {
|
|
|
|
describe('elements', () => {
|
|
|
|
it('should report nested translatable elements', () => {
|
|
|
|
expect(extractErrors(`<p i18n><b i18n></b></p>`)).toEqual([
|
|
|
|
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
|
|
|
|
]);
|
2016-07-01 20:29:54 -04:00
|
|
|
});
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
it('should report translatable elements in implicit elements', () => {
|
|
|
|
expect(extractErrors(`<p><b i18n></b></p>`, ['p'])).toEqual([
|
|
|
|
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
|
|
|
|
]);
|
2016-07-01 20:29:54 -04:00
|
|
|
});
|
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
it('should report translatable elements in translatable blocks', () => {
|
|
|
|
expect(extractErrors(`<!-- i18n --><b i18n></b><!-- /i18n -->`)).toEqual([
|
|
|
|
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
|
2016-07-21 16:56:58 -04:00
|
|
|
describe('blocks', () => {
|
|
|
|
it('should report nested blocks', () => {
|
|
|
|
expect(extractErrors(`<!-- i18n --><!-- i18n --><!-- /i18n --><!-- /i18n -->`)).toEqual([
|
|
|
|
['Could not start a block inside a translatable section', '<!--'],
|
|
|
|
['Trying to close an unopened block', '<!--'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report unclosed blocks', () => {
|
|
|
|
expect(extractErrors(`<!-- i18n -->`)).toEqual([
|
|
|
|
['Unclosed block', '<!--'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report translatable blocks in translatable elements', () => {
|
|
|
|
expect(extractErrors(`<p i18n><!-- i18n --><!-- /i18n --></p>`)).toEqual([
|
|
|
|
['Could not start a block inside a translatable section', '<!--'],
|
|
|
|
['Trying to close an unopened block', '<!--'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report translatable blocks in implicit elements', () => {
|
|
|
|
expect(extractErrors(`<p><!-- i18n --><!-- /i18n --></p>`, ['p'])).toEqual([
|
|
|
|
['Could not start a block inside a translatable section', '<!--'],
|
|
|
|
['Trying to close an unopened block', '<!--'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report when start and end of a block are not at the same level', () => {
|
|
|
|
expect(extractErrors(`<!-- i18n --><p><!-- /i18n --></p>`)).toEqual([
|
|
|
|
['I18N blocks should not cross element boundaries', '<!--'],
|
|
|
|
['Unclosed block', '<p>'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(extractErrors(`<p><!-- i18n --></p><!-- /i18n -->`)).toEqual([
|
|
|
|
['I18N blocks should not cross element boundaries', '<!--'],
|
|
|
|
['Unclosed block', '<!--'],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-08-01 21:10:34 -04:00
|
|
|
|
|
|
|
describe('Merger', () => {
|
|
|
|
describe('elements', () => {
|
|
|
|
it('should merge elements', () => {
|
|
|
|
const HTML = `<p i18n="m|d">foo</p>`;
|
2016-08-04 20:46:31 -04:00
|
|
|
expect(fakeTranslate(HTML)).toEqual('<p>**foo**</p>');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should merge nested elements', () => {
|
|
|
|
const HTML = `<div>before<p i18n="m|d">foo</p><!-- comment --></div>`;
|
2016-08-04 20:46:31 -04:00
|
|
|
expect(fakeTranslate(HTML)).toEqual('<div>before<p>**foo**</p></div>');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
2017-01-25 20:43:19 -05:00
|
|
|
|
|
|
|
it('should merge empty messages', () => {
|
|
|
|
const HTML = `<div i18n>some element</div>`;
|
|
|
|
const htmlNodes: html.Node[] = parseHtml(HTML);
|
|
|
|
const messages: i18n.Message[] =
|
|
|
|
extractMessages(htmlNodes, DEFAULT_INTERPOLATION_CONFIG, [], {}).messages;
|
|
|
|
|
|
|
|
expect(messages.length).toEqual(1);
|
|
|
|
const i18nMsgMap: {[id: string]: i18n.Node[]} = {};
|
|
|
|
i18nMsgMap[digest(messages[0])] = [];
|
2017-02-03 17:29:28 -05:00
|
|
|
const translations = new TranslationBundle(i18nMsgMap, null, digest);
|
2017-01-25 20:43:19 -05:00
|
|
|
|
|
|
|
const output =
|
|
|
|
mergeTranslations(htmlNodes, translations, DEFAULT_INTERPOLATION_CONFIG, [], {});
|
|
|
|
expect(output.errors).toEqual([]);
|
|
|
|
|
|
|
|
expect(serializeHtmlNodes(output.rootNodes).join('')).toEqual(`<div></div>`);
|
|
|
|
});
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('blocks', () => {
|
2017-09-01 08:50:58 -04:00
|
|
|
it('should console.warn if we use i18n comments', () => {
|
|
|
|
// TODO(ocombe): expect a warning message when we have a proper log service
|
|
|
|
extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->');
|
|
|
|
});
|
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
it('should merge blocks', () => {
|
|
|
|
const HTML = `before<!-- i18n --><p>foo</p><span><i>bar</i></span><!-- /i18n -->after`;
|
2016-08-04 20:46:31 -04:00
|
|
|
expect(fakeTranslate(HTML))
|
|
|
|
.toEqual(
|
2016-11-02 20:40:15 -04:00
|
|
|
'before**[ph tag name="START_PARAGRAPH">foo[/ph name="CLOSE_PARAGRAPH">[ph tag' +
|
|
|
|
' name="START_TAG_SPAN">[ph tag name="START_ITALIC_TEXT">bar[/ph' +
|
|
|
|
' name="CLOSE_ITALIC_TEXT">[/ph name="CLOSE_TAG_SPAN">**after');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should merge nested blocks', () => {
|
|
|
|
const HTML =
|
|
|
|
`<div>before<!-- i18n --><p>foo</p><span><i>bar</i></span><!-- /i18n -->after</div>`;
|
|
|
|
expect(fakeTranslate(HTML))
|
2016-08-04 20:46:31 -04:00
|
|
|
.toEqual(
|
2016-11-02 20:40:15 -04:00
|
|
|
'<div>before**[ph tag name="START_PARAGRAPH">foo[/ph name="CLOSE_PARAGRAPH">[ph' +
|
|
|
|
' tag name="START_TAG_SPAN">[ph tag name="START_ITALIC_TEXT">bar[/ph' +
|
|
|
|
' name="CLOSE_ITALIC_TEXT">[/ph name="CLOSE_TAG_SPAN">**after</div>');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('attributes', () => {
|
|
|
|
it('should merge attributes', () => {
|
|
|
|
const HTML = `<p i18n-title="m|d" title="foo"></p>`;
|
2016-08-04 20:46:31 -04:00
|
|
|
expect(fakeTranslate(HTML)).toEqual('<p title="**foo**"></p>');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
|
2017-03-20 09:52:24 -04:00
|
|
|
it('should merge attributes with ids', () => {
|
|
|
|
const HTML = `<p i18n-title="@@id" title="foo"></p>`;
|
|
|
|
expect(fakeTranslate(HTML)).toEqual('<p title="**foo**"></p>');
|
|
|
|
});
|
|
|
|
|
2016-08-12 00:00:35 -04:00
|
|
|
it('should merge nested attributes', () => {
|
2016-08-01 21:10:34 -04:00
|
|
|
const HTML = `<div>{count, plural, =0 {<p i18n-title title="foo"></p>}}</div>`;
|
2016-08-04 20:46:31 -04:00
|
|
|
expect(fakeTranslate(HTML))
|
|
|
|
.toEqual('<div>{count, plural, =0 {<p title="**foo**"></p>}}</div>');
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
2016-08-12 00:00:35 -04:00
|
|
|
|
|
|
|
it('should merge attributes without values', () => {
|
|
|
|
const HTML = `<p i18n-title="m|d" title=""></p>`;
|
|
|
|
expect(fakeTranslate(HTML)).toEqual('<p title=""></p>');
|
|
|
|
});
|
2017-01-25 20:43:19 -05:00
|
|
|
|
|
|
|
it('should merge empty attributes', () => {
|
|
|
|
const HTML = `<div i18n-title title="some attribute">some element</div>`;
|
|
|
|
const htmlNodes: html.Node[] = parseHtml(HTML);
|
|
|
|
const messages: i18n.Message[] =
|
|
|
|
extractMessages(htmlNodes, DEFAULT_INTERPOLATION_CONFIG, [], {}).messages;
|
|
|
|
|
|
|
|
expect(messages.length).toEqual(1);
|
|
|
|
const i18nMsgMap: {[id: string]: i18n.Node[]} = {};
|
|
|
|
i18nMsgMap[digest(messages[0])] = [];
|
2017-02-03 17:29:28 -05:00
|
|
|
const translations = new TranslationBundle(i18nMsgMap, null, digest);
|
2017-01-25 20:43:19 -05:00
|
|
|
|
|
|
|
const output =
|
|
|
|
mergeTranslations(htmlNodes, translations, DEFAULT_INTERPOLATION_CONFIG, [], {});
|
|
|
|
expect(output.errors).toEqual([]);
|
|
|
|
|
|
|
|
expect(serializeHtmlNodes(output.rootNodes).join(''))
|
|
|
|
.toEqual(`<div title="">some element</div>`);
|
|
|
|
});
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
2017-07-07 19:16:49 -04:00
|
|
|
|
|
|
|
describe('no translations', () => {
|
|
|
|
it('should remove i18n attributes', () => {
|
|
|
|
const HTML = `<p i18n="m|d">foo</p>`;
|
|
|
|
expect(fakeNoTranslate(HTML)).toEqual('<p>foo</p>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove i18n- attributes', () => {
|
|
|
|
const HTML = `<p i18n-title="m|d" title="foo"></p>`;
|
|
|
|
expect(fakeNoTranslate(HTML)).toEqual('<p title="foo"></p>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove i18n comment blocks', () => {
|
|
|
|
const HTML = `before<!-- i18n --><p>foo</p><span><i>bar</i></span><!-- /i18n -->after`;
|
|
|
|
expect(fakeNoTranslate(HTML)).toEqual('before<p>foo</p><span><i>bar</i></span>after');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove nested i18n markup', () => {
|
|
|
|
const HTML =
|
|
|
|
`<!-- i18n --><span someAttr="ok">foo</span><div>{count, plural, =0 {<p i18n-title title="foo"></p>}}</div><!-- /i18n -->`;
|
|
|
|
expect(fakeNoTranslate(HTML))
|
|
|
|
.toEqual(
|
|
|
|
'<span someAttr="ok">foo</span><div>{count, plural, =0 {<p title="foo"></p>}}</div>');
|
|
|
|
});
|
2017-07-27 19:13:16 -04:00
|
|
|
});
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
2016-07-01 20:29:54 -04:00
|
|
|
}
|
2016-07-08 19:46:49 -04:00
|
|
|
|
2016-08-01 21:10:34 -04:00
|
|
|
function parseHtml(html: string): html.Node[] {
|
2016-07-08 19:46:49 -04:00
|
|
|
const htmlParser = new HtmlParser();
|
2019-02-08 17:10:19 -05:00
|
|
|
const parseResult = htmlParser.parse(html, 'extractor spec', {tokenizeExpansionForms: true});
|
2016-07-08 19:46:49 -04:00
|
|
|
if (parseResult.errors.length > 1) {
|
2016-08-05 17:14:31 -04:00
|
|
|
throw new Error(`unexpected parse errors: ${parseResult.errors.join('\n')}`);
|
2016-07-08 19:46:49 -04:00
|
|
|
}
|
2016-08-01 21:10:34 -04:00
|
|
|
return parseResult.rootNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fakeTranslate(
|
|
|
|
content: string, implicitTags: string[] = [],
|
|
|
|
implicitAttrs: {[k: string]: string[]} = {}): string {
|
|
|
|
const htmlNodes: html.Node[] = parseHtml(content);
|
2016-08-04 20:46:31 -04:00
|
|
|
const messages: i18n.Message[] =
|
|
|
|
extractMessages(htmlNodes, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs)
|
|
|
|
.messages;
|
2016-08-01 21:10:34 -04:00
|
|
|
|
2016-11-02 20:40:15 -04:00
|
|
|
const i18nMsgMap: {[id: string]: i18n.Node[]} = {};
|
2016-08-01 21:10:34 -04:00
|
|
|
|
2016-08-05 14:53:37 -04:00
|
|
|
messages.forEach(message => {
|
2016-10-28 22:53:42 -04:00
|
|
|
const id = digest(message);
|
2016-11-02 20:40:15 -04:00
|
|
|
const text = serializeI18nNodes(message.nodes).join('').replace(/</g, '[');
|
2020-04-08 13:14:18 -04:00
|
|
|
i18nMsgMap[id] = [new i18n.Text(`**${text}**`, null!)];
|
2016-08-01 21:10:34 -04:00
|
|
|
});
|
|
|
|
|
2017-07-07 19:16:49 -04:00
|
|
|
const translationBundle = new TranslationBundle(i18nMsgMap, null, digest);
|
|
|
|
const output = mergeTranslations(
|
|
|
|
htmlNodes, translationBundle, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
|
|
|
|
expect(output.errors).toEqual([]);
|
|
|
|
|
|
|
|
return serializeHtmlNodes(output.rootNodes).join('');
|
|
|
|
}
|
2016-08-01 21:10:34 -04:00
|
|
|
|
2017-07-07 19:16:49 -04:00
|
|
|
function fakeNoTranslate(
|
|
|
|
content: string, implicitTags: string[] = [],
|
|
|
|
implicitAttrs: {[k: string]: string[]} = {}): string {
|
|
|
|
const htmlNodes: html.Node[] = parseHtml(content);
|
|
|
|
const translationBundle = new TranslationBundle(
|
|
|
|
{}, null, digest, undefined, MissingTranslationStrategy.Ignore, console);
|
2017-01-25 20:43:19 -05:00
|
|
|
const output = mergeTranslations(
|
2017-07-07 19:16:49 -04:00
|
|
|
htmlNodes, translationBundle, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
|
2017-01-25 20:43:19 -05:00
|
|
|
expect(output.errors).toEqual([]);
|
2016-07-08 19:46:49 -04:00
|
|
|
|
2017-01-25 20:43:19 -05:00
|
|
|
return serializeHtmlNodes(output.rootNodes).join('');
|
2016-07-08 19:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function extract(
|
2016-07-21 16:56:58 -04:00
|
|
|
html: string, implicitTags: string[] = [],
|
2017-10-24 07:54:08 -04:00
|
|
|
implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string, string][] {
|
2016-08-05 17:14:31 -04:00
|
|
|
const result =
|
|
|
|
extractMessages(parseHtml(html), DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
|
|
|
|
|
|
|
|
if (result.errors.length > 0) {
|
|
|
|
throw new Error(`unexpected errors: ${result.errors.join('\n')}`);
|
|
|
|
}
|
2016-07-08 19:46:49 -04:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
// https://github.com/angular/clang-format/issues/35
|
2016-08-05 17:14:31 -04:00
|
|
|
return result.messages.map(
|
2017-10-24 07:54:08 -04:00
|
|
|
message => [serializeI18nNodes(message.nodes), message.meaning, message.description, message.id]) as [string[], string, string, string][];
|
2016-07-08 19:46:49 -04:00
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractErrors(
|
2016-07-21 16:56:58 -04:00
|
|
|
html: string, implicitTags: string[] = [], implicitAttrs: {[k: string]: string[]} = {}): any[] {
|
2016-08-04 20:46:31 -04:00
|
|
|
const errors =
|
|
|
|
extractMessages(parseHtml(html), DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs)
|
|
|
|
.errors;
|
2016-07-08 19:46:49 -04:00
|
|
|
|
|
|
|
return errors.map((e): [string, string] => [e.msg, e.span.toString()]);
|
|
|
|
}
|