/** * @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 {Message} from '@angular/compiler/src/i18n/i18n_ast'; import {extractI18nMessages} from '@angular/compiler/src/i18n/i18n_parser'; import {ddescribe, describe, expect, iit, it} from '@angular/core/testing/testing_internal'; import {serializeNodes} from '../../src/i18n/message_bundle'; import {HtmlParser} from '../../src/ml_parser/html_parser'; import {DEFAULT_INTERPOLATION_CONFIG} from '../../src/ml_parser/interpolation_config'; export function main() { describe('I18nParser', () => { describe('elements', () => { it('should extract from elements', () => { expect(_humanizeMessages('
text
')).toEqual([ [['text'], 'm', 'd'], ]); }); it('should extract from nested elements', () => { expect(_humanizeMessages('
textnested
')).toEqual([ [ [ 'text', 'nested' ], 'm', 'd' ], ]); }); it('should not create a message for empty elements', () => { expect(_humanizeMessages('
')).toEqual([]); }); it('should not create a message for plain elements', () => { expect(_humanizeMessages('
')).toEqual([]); }); it('should suppoprt void elements', () => { expect(_humanizeMessages('


')).toEqual([ [ [ '' ], 'm', 'd' ], ]); }); }); describe('attributes', () => { it('should extract from attributes outside of translatable section', () => { expect(_humanizeMessages('
')).toEqual([ [['msg'], 'm', 'd'], ]); }); it('should extract from attributes in translatable element', () => { expect(_humanizeMessages('

')) .toEqual([ [ [ '' ], '', '' ], [['msg'], 'm', 'd'], ]); }); it('should extract from attributes in translatable block', () => { expect(_humanizeMessages( '

')) .toEqual([ [['msg'], 'm', 'd'], [ [ '' ], '', '' ], ]); }); it('should extract from attributes in translatable ICU', () => { expect( _humanizeMessages( '{count, plural, =0 {

}}')) .toEqual([ [['msg'], 'm', 'd'], [ [ '{count, plural, =0 {[]}}' ], '', '' ], ]); }); it('should extract from attributes in non translatable ICU', () => { expect( _humanizeMessages('{count, plural, =0 {

}}')) .toEqual([ [['msg'], 'm', 'd'], ]); }); it('should not create a message for empty attributes', () => { expect(_humanizeMessages('
')).toEqual([]); }); }); describe('interpolation', () => { it('should replace interpolation with placeholder', () => { expect(_humanizeMessages('
before{{ exp }}after
')).toEqual([ [['[before, exp , after]'], 'm', 'd'], ]); }); it('should support named interpolation', () => { expect(_humanizeMessages('
before{{ exp //i18n(ph="teSt") }}after
')) .toEqual([ [['[before, exp //i18n(ph="teSt") , after]'], 'm', 'd'], ]); }); }); describe('blocks', () => { it('should extract from blocks', () => { expect(_humanizeMessages(`message1 message2 message3`)) .toEqual([ [['message1'], 'meaning1', 'desc1'], [['message2'], '', 'desc2'], [['message3'], '', ''], ]); }); it('should extract all siblings', () => { expect(_humanizeMessages(`text

htmlnested

`)).toEqual([ [ [ 'text', 'html, nested' ], '', '' ], ]); }); }); describe('ICU messages', () => { it('should extract as ICU when single child of an element', () => { expect(_humanizeMessages('
{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(_humanizeMessages('
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(_humanizeMessages('{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(_humanizeMessages('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(_humanizeMessages( '
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(_humanizeMessages('bolditalic', ['b'])).toEqual([ [['bold'], '', ''], ]); }); }); describe('implicit attributes', () => { it('should extract implicit attributes', () => { expect(_humanizeMessages( 'bolditalic', [], {'b': ['title']})) .toEqual([ [['bb'], '', ''], ]); }); }); describe('placeholders', () => { it('should reuse the same placeholder name for tags', () => { const html = '

one

two

three

'; expect(_humanizeMessages(html)).toEqual([ [ [ 'one', 'two', 'three', ], 'm', 'd' ], ]); expect(_humanizePlaceholders(html)).toEqual([ 'START_PARAGRAPH=

, CLOSE_PARAGRAPH=

, START_PARAGRAPH_1=

', ]); }); it('should reuse the same placeholder name for interpolations', () => { const html = '

{{ a }}{{ a }}{{ b }}
'; expect(_humanizeMessages(html)).toEqual([ [ [ '[ a , a , b ]' ], 'm', 'd' ], ]); expect(_humanizePlaceholders(html)).toEqual([ 'INTERPOLATION={{ a }}, INTERPOLATION_1={{ b }}', ]); }); it('should reuse the same placeholder name for icu messages', () => { const html = '
{count, plural, =0 {0}}{count, plural, =0 {0}}{count, plural, =1 {1}}
'; expect(_humanizeMessages(html)).toEqual([ [ [ '{count, plural, =0 {[0]}}', '{count, plural, =0 {[0]}}', '{count, plural, =1 {[1]}}', ], 'm', 'd' ], [['{count, plural, =0 {[0]}}'], '', ''], [['{count, plural, =0 {[0]}}'], '', ''], [['{count, plural, =1 {[1]}}'], '', ''], ]); expect(_humanizePlaceholders(html)).toEqual([ 'ICU={count, plural, =0 {0}}, ICU_1={count, plural, =1 {1}}', '', '', '', ]); }); }); }); } function _humanizeMessages( html: string, implicitTags: string[] = [], implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string][] { // clang-format off // https://github.com/angular/clang-format/issues/35 return _extractMessages(html, implicitTags, implicitAttrs).map( message => [serializeNodes(message.nodes), message.meaning, message.description, ]) as [string[], string, string][]; // clang-format on } function _humanizePlaceholders( html: string, implicitTags: string[] = [], implicitAttrs: {[k: string]: string[]} = {}): string[] { // clang-format off // https://github.com/angular/clang-format/issues/35 return _extractMessages(html, implicitTags, implicitAttrs).map( msg => Object.keys(msg.placeholders).map((name) => `${name}=${msg.placeholders[name]}`).join(', ')); // clang-format on } function _extractMessages( html: string, implicitTags: string[] = [], implicitAttrs: {[k: string]: string[]} = {}): Message[] { 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')}`); } return extractI18nMessages( parseResult.rootNodes, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs); }