2018-06-18 10:55:43 -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
* /
2019-03-14 11:26:01 -04:00
import { NgForOfContext } from '@angular/common' ;
2019-04-04 14:41:52 -04:00
2018-11-13 03:36:30 -05:00
import { noop } from '../../../compiler/src/render3/view/util' ;
2018-10-30 20:03:01 -04:00
import { Component as _Component } from '../../src/core' ;
2019-04-11 16:46:47 -04:00
import { ɵ ɵ defineComponent , ɵ ɵ defineDirective } from '../../src/render3/definition' ;
import { getTranslationForTemplate , ɵ ɵ i18n , ɵ ɵ i18nApply , ɵ ɵ i18nAttributes , ɵ ɵ i18nEnd , ɵ ɵ i18nExp , ɵ ɵ i18nPostprocess , ɵ ɵ i18nStart } from '../../src/render3/i18n' ;
import { ɵ ɵ allocHostVars , ɵ ɵ bind , ɵ ɵ element , ɵ ɵ elementContainerEnd , ɵ ɵ elementContainerStart , ɵ ɵ elementEnd , ɵ ɵ elementProperty , ɵ ɵ elementStart , ɵ ɵ nextContext , ɵ ɵ projection , ɵ ɵ projectionDef , ɵ ɵ template , ɵ ɵ text , ɵ ɵ textBinding } from '../../src/render3/instructions/all' ;
2018-06-18 10:55:43 -04:00
import { RenderFlags } from '../../src/render3/interfaces/definition' ;
2019-04-04 14:41:52 -04:00
import { COMMENT_MARKER , ELEMENT_MARKER , I18nMutateOpCode , I18nUpdateOpCode , I18nUpdateOpCodes , IcuType , TI18n } from '../../src/render3/interfaces/i18n' ;
2019-01-29 09:28:31 -05:00
import { AttributeMarker } from '../../src/render3/interfaces/node' ;
2019-04-04 14:41:52 -04:00
import { HEADER_OFFSET , LView , TVIEW } from '../../src/render3/interfaces/view' ;
2019-02-20 17:21:20 -05:00
import { getNativeByIndex , getTNode } from '../../src/render3/util/view_utils' ;
2019-04-04 14:41:52 -04:00
2019-03-14 11:26:01 -04:00
import { NgForOf , NgIf } from './common_with_def' ;
2018-06-18 10:55:43 -04:00
import { ComponentFixture , TemplateFixture } from './render_util' ;
2018-10-30 20:03:01 -04:00
const Component : typeof _Component = function ( . . . args : any [ ] ) : any {
// In test we use @Component for documentation only so it's safe to mock out the implementation.
return ( ) = > undefined ;
} as any ;
2018-06-18 10:55:43 -04:00
describe ( 'Runtime i18n' , ( ) = > {
2018-11-13 03:36:30 -05:00
describe ( 'getTranslationForTemplate' , ( ) = > {
it ( 'should crop messages for the selected template' , ( ) = > {
let message = ` simple text ` ;
expect ( getTranslationForTemplate ( message ) ) . toEqual ( message ) ;
message = ` Hello <20> 0<EFBFBD> ! ` ;
expect ( getTranslationForTemplate ( message ) ) . toEqual ( message ) ;
message = ` Hello <20> #2<> <32> 0<EFBFBD> <30> /#2<> ! ` ;
expect ( getTranslationForTemplate ( message ) ) . toEqual ( message ) ;
// Embedded sub-templates
message = ` <EFBFBD> 0<EFBFBD> is rendered as: <20> *2:1<> before<72> *1:2<> middle<6C> /*1:2<> after<65> /*2:1<> !` ;
expect ( getTranslationForTemplate ( message ) ) . toEqual ( '<27> 0<EFBFBD> is rendered as: <20> *2:1<> <31> /*2:1<> !' ) ;
expect ( getTranslationForTemplate ( message , 1 ) ) . toEqual ( 'before<72> *1:2<> <32> /*1:2<> after' ) ;
expect ( getTranslationForTemplate ( message , 2 ) ) . toEqual ( 'middle' ) ;
// Embedded & sibling sub-templates
message =
` <EFBFBD> 0<EFBFBD> is rendered as: <20> *2:1<> before<72> *1:2<> middle<6C> /*1:2<> after<65> /*2:1<> and also <20> *4:3<> before<72> *1:4<> middle<6C> /*1:4<> after<65> /*4:3<> !` ;
expect ( getTranslationForTemplate ( message ) )
. toEqual ( '<27> 0<EFBFBD> is rendered as: <20> *2:1<> <31> /*2:1<> and also <20> *4:3<> <33> /*4:3<> !' ) ;
expect ( getTranslationForTemplate ( message , 1 ) ) . toEqual ( 'before<72> *1:2<> <32> /*1:2<> after' ) ;
expect ( getTranslationForTemplate ( message , 2 ) ) . toEqual ( 'middle' ) ;
expect ( getTranslationForTemplate ( message , 3 ) ) . toEqual ( 'before<72> *1:4<> <34> /*1:4<> after' ) ;
expect ( getTranslationForTemplate ( message , 4 ) ) . toEqual ( 'middle' ) ;
} ) ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'should throw if the template is malformed' , ( ) = > {
const message = ` <EFBFBD> *2:1<> message!` ;
expect ( ( ) = > getTranslationForTemplate ( message ) ) . toThrowError ( /Tag mismatch/ ) ;
} ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
function prepareFixture (
createTemplate : ( ) = > void , updateTemplate : ( ( ) = > void ) | null , nbConsts = 0 ,
nbVars = 0 ) : TemplateFixture {
return new TemplateFixture ( createTemplate , updateTemplate || noop , nbConsts , nbVars ) ;
}
function getOpCodes (
createTemplate : ( ) = > void , updateTemplate : ( ( ) = > void ) | null , nbConsts : number ,
index : number ) : TI18n | I18nUpdateOpCodes {
const fixture = prepareFixture ( createTemplate , updateTemplate , nbConsts ) ;
const tView = fixture . hostView [ TVIEW ] ;
return tView . data [ index + HEADER_OFFSET ] as TI18n ;
}
describe ( 'i18nStart' , ( ) = > {
it ( 'for text' , ( ) = > {
const MSG_DIV = ` simple text ` ;
const nbConsts = 1 ;
const index = 0 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
2019-03-15 18:04:34 -04:00
// Check debug
const debugOps = ( opCodes as any ) . create . debug ! . operations ;
expect ( debugOps [ 0 ] . __raw_opCode ) . toBe ( 'simple text' ) ;
expect ( debugOps [ 0 ] . type ) . toBe ( 'Create Text Node' ) ;
expect ( debugOps [ 0 ] . nodeIndex ) . toBe ( 1 ) ;
expect ( debugOps [ 0 ] . text ) . toBe ( 'simple text' ) ;
expect ( debugOps [ 1 ] . __raw_opCode ) . toBe ( 1 ) ;
expect ( debugOps [ 1 ] . type ) . toBe ( 'AppendChild' ) ;
expect ( debugOps [ 1 ] . nodeIndex ) . toBe ( 0 ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 1 ,
2019-01-13 05:10:04 -05:00
create : [
'simple text' , nbConsts ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
] ,
2018-11-13 03:36:30 -05:00
update : [ ] ,
icus : null
2018-11-13 03:36:30 -05:00
} ) ;
2018-11-13 03:36:30 -05:00
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for elements' , ( ) = > {
const MSG_DIV = ` Hello <20> #2<> world<6C> /#2<> and <20> #3<> universe<73> /#3<> ! ` ;
// Template: `<div>Hello <div>world</div> and <span>universe</span>!`
// 3 consts for the 2 divs and 1 span + 1 const for `i18nStart` = 4 consts
const nbConsts = 4 ;
const index = 1 ;
const elementIndex = 2 ;
const elementIndex2 = 3 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 5 ,
create : [
'Hello ' ,
2019-01-13 05:10:04 -05:00
nbConsts ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
elementIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'world' ,
2019-01-13 05:10:04 -05:00
nbConsts + 1 ,
2018-11-13 03:36:30 -05:00
elementIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
elementIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . ElementEnd ,
' and ' ,
2019-01-13 05:10:04 -05:00
nbConsts + 2 ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
elementIndex2 << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'universe' ,
2019-01-13 05:10:04 -05:00
nbConsts + 3 ,
2018-11-13 03:36:30 -05:00
elementIndex2 << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
elementIndex2 << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . ElementEnd ,
'!' ,
2019-01-13 05:10:04 -05:00
nbConsts + 4 ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
] ,
update : [ ] ,
icus : null
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for simple bindings' , ( ) = > {
const MSG_DIV = ` Hello <20> 0<EFBFBD> ! ` ;
const nbConsts = 2 ;
const index = 1 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-14 13:23:21 -05:00
2019-03-15 18:04:34 -04:00
expect ( ( opCodes as any ) . update . debug . operations ) . toEqual ( [
{ __raw_opCode : 8 , checkBit : 1 , type : 'Text' , nodeIndex : 2 , text : 'Hello <20> 0<EFBFBD> !' }
] ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 1 ,
2019-01-13 05:10:04 -05:00
create :
[ '' , nbConsts , index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ] ,
2018-11-13 03:36:30 -05:00
update : [
0 b1 , // bindings mask
4 , // if no update, skip 4
'Hello ' ,
- 1 , // binding index
'!' , ( index + 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Text
] ,
icus : null
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for multiple bindings' , ( ) = > {
const MSG_DIV = ` Hello <20> 0<EFBFBD> and <20> 1<EFBFBD> , again <20> 0<EFBFBD> ! ` ;
const nbConsts = 2 ;
const index = 1 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
2018-08-18 14:14:50 -04:00
vars : 1 ,
2019-01-13 05:10:04 -05:00
create :
[ '' , nbConsts , index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ] ,
2018-11-13 03:36:30 -05:00
update : [
0 b11 , // bindings mask
8 , // if no update, skip 8
'Hello ' , - 1 , ' and ' , - 2 , ', again ' , - 1 , '!' ,
( index + 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Text
] ,
icus : null
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for sub-templates' , ( ) = > {
// Template:
// <div>
// {{value}} is rendered as:
// <span *ngIf>
// before <b *ngIf>middle</b> after
// </span>
// !
// </div>
const MSG_DIV =
` <EFBFBD> 0<EFBFBD> is rendered as: <20> *2:1<> <31> #1:1<> before<72> *2:2<> <32> #1:2<> middle<6C> /#1:2<> <32> /*2:2<> after<65> /#1:1<> <31> /*2:1<> !` ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
/**** Root template ****/
// <20> 0<EFBFBD> is rendered as: <20> *2:1<> <31> /*2:1<> !
let nbConsts = 3 ;
let index = 1 ;
const firstTextNode = 3 ;
2019-04-11 16:46:47 -04:00
let opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 2 ,
create : [
'' ,
2019-01-13 05:10:04 -05:00
nbConsts ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2 << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'!' ,
2019-01-13 05:10:04 -05:00
nbConsts + 1 ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
] ,
update : [
0 b1 , // bindings mask
3 , // if no update, skip 3
- 1 , // binding index
' is rendered as: ' , firstTextNode << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Text
] ,
icus : null
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
/**** First sub-template ****/
// <20> #1:1<> before<72> *2:2<> middle<6C> /*2:2<> after<65> /#1:1<>
nbConsts = 3 ;
index = 0 ;
const spanElement = 1 ;
const bElementSubTemplate = 2 ;
2019-04-11 16:46:47 -04:00
opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV , 1 ) ; } , null , nbConsts , index ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
2018-08-21 03:03:21 -04:00
vars : 2 ,
2018-11-13 03:36:30 -05:00
create : [
spanElement << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'before' ,
2019-01-13 05:10:04 -05:00
nbConsts ,
2018-11-13 03:36:30 -05:00
spanElement << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
bElementSubTemplate << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
spanElement << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'after' ,
2019-01-13 05:10:04 -05:00
nbConsts + 1 ,
2018-11-13 03:36:30 -05:00
spanElement << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
spanElement << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . ElementEnd ,
] ,
update : [ ] ,
icus : null
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
/**** Second sub-template ****/
// middle
nbConsts = 2 ;
index = 0 ;
const bElement = 1 ;
2019-04-11 16:46:47 -04:00
opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV , 2 ) ; } , null , nbConsts , index ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 1 ,
create : [
bElement << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Select ,
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'middle' ,
2019-01-13 05:10:04 -05:00
nbConsts ,
2018-11-13 03:36:30 -05:00
bElement << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
bElement << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . ElementEnd ,
] ,
update : [ ] ,
icus : null
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2018-11-13 03:36:30 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
} ` ;
const nbConsts = 1 ;
const index = 0 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
const tIcuIndex = 0 ;
const icuCommentNodeIndex = index + 1 ;
2019-01-13 05:10:04 -05:00
const firstTextNodeIndex = index + 2 ;
2018-11-13 03:36:30 -05:00
const bElementNodeIndex = index + 3 ;
const iElementNodeIndex = index + 3 ;
const spanElementNodeIndex = index + 3 ;
const innerTextNode = index + 4 ;
const lastTextNode = index + 5 ;
2019-03-15 18:04:34 -04:00
const debugOps = ( opCodes as any ) . update . debug . operations ;
expect ( debugOps [ 0 ] . __raw_opCode ) . toBe ( 6 ) ;
expect ( debugOps [ 0 ] . checkBit ) . toBe ( 1 ) ;
expect ( debugOps [ 0 ] . type ) . toBe ( 'IcuSwitch' ) ;
expect ( debugOps [ 0 ] . nodeIndex ) . toBe ( 1 ) ;
expect ( debugOps [ 0 ] . tIcuIndex ) . toBe ( 0 ) ;
expect ( debugOps [ 0 ] . mainBinding ) . toBe ( '<27> 0<EFBFBD> ' ) ;
expect ( debugOps [ 1 ] . __raw_opCode ) . toBe ( 7 ) ;
expect ( debugOps [ 1 ] . checkBit ) . toBe ( 3 ) ;
expect ( debugOps [ 1 ] . type ) . toBe ( 'IcuUpdate' ) ;
expect ( debugOps [ 1 ] . nodeIndex ) . toBe ( 1 ) ;
expect ( debugOps [ 1 ] . tIcuIndex ) . toBe ( 0 ) ;
const icuDebugOps = ( opCodes as any ) . icus [ 0 ] . create [ 0 ] . debug . operations ;
let op : any ;
let i = 0 ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 'no ' ) ;
expect ( op . type ) . toBe ( 'Create Text Node' ) ;
expect ( op . nodeIndex ) . toBe ( 2 ) ;
expect ( op . text ) . toBe ( 'no ' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 131073 ) ;
expect ( op . type ) . toBe ( 'AppendChild' ) ;
expect ( op . nodeIndex ) . toBe ( 1 ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toEqual ( { marker : 'element' } ) ;
expect ( op . type ) . toBe ( 'ELEMENT_MARKER' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 'b' ) ;
expect ( op . type ) . toBe ( 'Create Text Node' ) ;
expect ( op . nodeIndex ) . toBe ( 3 ) ;
expect ( op . text ) . toBe ( 'b' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 131073 ) ;
expect ( op . type ) . toBe ( 'AppendChild' ) ;
expect ( op . nodeIndex ) . toBe ( 1 ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 28 ) ;
expect ( op . type ) . toBe ( 'Attr' ) ;
expect ( op . nodeIndex ) . toBe ( 3 ) ;
expect ( op . attrName ) . toBe ( 'title' ) ;
expect ( op . attrValue ) . toBe ( 'none' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 'emails' ) ;
expect ( op . type ) . toBe ( 'Create Text Node' ) ;
expect ( op . nodeIndex ) . toBe ( 4 ) ;
expect ( op . text ) . toBe ( 'emails' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 393217 ) ;
expect ( op . type ) . toBe ( 'AppendChild' ) ;
expect ( op . nodeIndex ) . toBe ( 3 ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( '!' ) ;
expect ( op . type ) . toBe ( 'Create Text Node' ) ;
expect ( op . nodeIndex ) . toBe ( 5 ) ;
expect ( op . text ) . toBe ( '!' ) ;
op = icuDebugOps [ i ++ ] ;
expect ( op . __raw_opCode ) . toBe ( 131073 ) ;
expect ( op . type ) . toBe ( 'AppendChild' ) ;
expect ( op . nodeIndex ) . toBe ( 1 ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( {
vars : 5 ,
create : [
2019-01-13 05:10:04 -05:00
COMMENT_MARKER , 'ICU 1' , icuCommentNodeIndex ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
] ,
update : [
0 b1 , // mask for ICU main binding
3 , // skip 3 if not changed
- 1 , // icu main binding
icuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . IcuSwitch , tIcuIndex ,
0 b11 , // mask for all ICU bindings
2 , // skip 2 if not changed
icuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . IcuUpdate , tIcuIndex
] ,
icus : [ {
type : 1 ,
vars : [ 4 , 3 , 3 ] ,
childIcus : [ [ ] , [ ] , [ ] ] ,
cases : [ '0' , '1' , 'other' ] ,
create : [
[
'no ' ,
2019-01-13 05:10:04 -05:00
firstTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
ELEMENT_MARKER ,
'b' ,
2019-01-13 05:10:04 -05:00
bElementNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
bElementNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Attr ,
'title' ,
'none' ,
'emails' ,
2019-01-13 05:10:04 -05:00
innerTextNode ,
2018-11-13 03:36:30 -05:00
bElementNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
'!' ,
2019-01-13 05:10:04 -05:00
lastTextNode ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
] ,
[
2019-01-13 05:10:04 -05:00
'one ' , firstTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
ELEMENT_MARKER , 'i' , iElementNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
'email' , innerTextNode ,
2018-11-13 03:36:30 -05:00
iElementNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
] ,
[
2019-01-13 05:10:04 -05:00
'' , firstTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
ELEMENT_MARKER , 'span' , spanElementNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
'emails' , innerTextNode ,
2018-11-13 03:36:30 -05:00
spanElementNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
]
] ,
remove : [
[
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
2018-11-13 03:36:30 -05:00
innerTextNode << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
bElementNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
lastTextNode << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
] ,
[
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
2018-11-13 03:36:30 -05:00
innerTextNode << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
iElementNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
] ,
[
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
2018-11-13 03:36:30 -05:00
innerTextNode << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
spanElementNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
]
] ,
update : [
[ ] , [ ] ,
[
0 b1 , // mask for the first binding
3 , // skip 3 if not changed
- 1 , // binding index
' ' , // text string to concatenate to the binding value
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Text ,
2018-11-13 03:36:30 -05:00
0 b10 , // mask for the title attribute binding
4 , // skip 4 if not changed
- 2 , // binding index
bElementNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Attr ,
'title' , // attribute name
null // sanitize function
]
]
} ]
} ) ;
} ) ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for nested ICU expressions' , ( ) = > {
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
2019-02-20 17:21:20 -05:00
= 0 { zero }
other { <EFBFBD> 0 <EFBFBD> { <EFBFBD> 1 <EFBFBD> , select ,
cat { cats }
dog { dogs }
2018-11-13 03:36:30 -05:00
other { animals }
} ! }
} ` ;
const nbConsts = 1 ;
const index = 0 ;
2019-04-11 16:46:47 -04:00
const opCodes = getOpCodes ( ( ) = > { ɵ ɵ i18nStart ( index , MSG_DIV ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
const icuCommentNodeIndex = index + 1 ;
2019-01-13 05:10:04 -05:00
const firstTextNodeIndex = index + 2 ;
2018-11-13 03:36:30 -05:00
const nestedIcuCommentNodeIndex = index + 3 ;
2019-01-13 05:10:04 -05:00
const lastTextNodeIndex = index + 4 ;
const nestedTextNodeIndex = index + 5 ;
2018-11-13 03:36:30 -05:00
const tIcuIndex = 1 ;
const nestedTIcuIndex = 0 ;
expect ( opCodes ) . toEqual ( {
vars : 6 ,
create : [
2019-01-13 05:10:04 -05:00
COMMENT_MARKER , 'ICU 1' , icuCommentNodeIndex ,
2018-11-13 03:36:30 -05:00
index << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
] ,
update : [
0 b1 , // mask for ICU main binding
3 , // skip 3 if not changed
- 1 , // icu main binding
icuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . IcuSwitch , tIcuIndex ,
0 b11 , // mask for all ICU bindings
2 , // skip 2 if not changed
icuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . IcuUpdate , tIcuIndex
] ,
icus : [
{
type : 0 ,
vars : [ 1 , 1 , 1 ] ,
childIcus : [ [ ] , [ ] , [ ] ] ,
cases : [ 'cat' , 'dog' , 'other' ] ,
create : [
[
2019-01-13 05:10:04 -05:00
'cats' , nestedTextNodeIndex , nestedIcuCommentNodeIndex
<< I18nMutateOpCode . SHIFT_PARENT |
2018-11-13 03:36:30 -05:00
I18nMutateOpCode . AppendChild
] ,
[
2019-01-13 05:10:04 -05:00
'dogs' , nestedTextNodeIndex , nestedIcuCommentNodeIndex
<< I18nMutateOpCode . SHIFT_PARENT |
2018-11-13 03:36:30 -05:00
I18nMutateOpCode . AppendChild
] ,
[
2019-01-13 05:10:04 -05:00
'animals' , nestedTextNodeIndex , nestedIcuCommentNodeIndex
<< I18nMutateOpCode . SHIFT_PARENT |
2018-11-13 03:36:30 -05:00
I18nMutateOpCode . AppendChild
]
] ,
remove : [
2019-01-13 05:10:04 -05:00
[ nestedTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ] ,
[ nestedTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ] ,
[ nestedTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ]
2018-11-13 03:36:30 -05:00
] ,
update : [ [ ] , [ ] , [ ] ]
} ,
{
type : 1 ,
vars : [ 1 , 4 ] ,
childIcus : [ [ ] , [ 0 ] ] ,
cases : [ '0' , 'other' ] ,
create : [
[
2019-01-13 05:10:04 -05:00
'zero' , firstTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
] ,
[
2019-01-13 05:10:04 -05:00
'' , firstTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
COMMENT_MARKER , 'nested ICU 0' , nestedIcuCommentNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild ,
2019-01-13 05:10:04 -05:00
'!' , lastTextNodeIndex ,
2018-11-13 03:36:30 -05:00
icuCommentNodeIndex << I18nMutateOpCode . SHIFT_PARENT | I18nMutateOpCode . AppendChild
]
] ,
remove : [
2019-01-13 05:10:04 -05:00
[ firstTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ] ,
2018-11-13 03:36:30 -05:00
[
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
lastTextNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
2018-11-13 03:36:30 -05:00
0 << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . RemoveNestedIcu ,
nestedIcuCommentNodeIndex << I18nMutateOpCode . SHIFT_REF | I18nMutateOpCode . Remove ,
]
] ,
update : [
[ ] ,
[
0 b1 , // mask for ICU main binding
3 , // skip 3 if not changed
- 1 , // binding index
' ' , // text string to concatenate to the binding value
2019-01-13 05:10:04 -05:00
firstTextNodeIndex << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Text ,
2018-11-13 03:36:30 -05:00
0 b10 , // mask for inner ICU main binding
3 , // skip 3 if not changed
- 2 , // inner ICU main binding
nestedIcuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF |
I18nUpdateOpCode . IcuSwitch ,
nestedTIcuIndex ,
0 b10 , // mask for all inner ICU bindings
2 , // skip 2 if not changed
nestedIcuCommentNodeIndex << I18nUpdateOpCode . SHIFT_REF |
I18nUpdateOpCode . IcuUpdate ,
nestedTIcuIndex
]
]
2018-06-18 10:55:43 -04:00
}
2018-11-13 03:36:30 -05:00
]
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
describe ( ` i18nEnd ` , ( ) = > {
it ( 'for text' , ( ) = > {
const MSG_DIV = ` simple text ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( ` <div> ${ MSG_DIV } </div> ` ) ;
} ) ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for bindings' , ( ) = > {
const MSG_DIV = ` Hello <20> 0<EFBFBD> ! ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// But it should have created an empty text node in `viewData`
const textTNode = fixture . hostView [ HEADER_OFFSET + 2 ] as Node ;
expect ( textTNode . nodeType ) . toEqual ( Node . TEXT_NODE ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for elements' , ( ) = > {
const MSG_DIV = ` Hello <20> #3<> world<6C> /#3<> and <20> #2<> universe<73> /#2<> ! ` ;
let fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
ɵ ɵ element ( 2 , 'div' ) ;
ɵ ɵ element ( 3 , 'span' ) ;
ɵ ɵ i18nEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 4 ) ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>Hello <span>world</span> and <div>universe</div>!</div>' ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for translations without top level element' , ( ) = > {
// When it's the first node
let MSG_DIV = ` Hello world ` ;
2019-04-11 16:46:47 -04:00
let fixture = prepareFixture ( ( ) = > { ɵ ɵ i18n ( 0 , MSG_DIV ) ; } , null , 1 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( 'Hello world' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// When the first node is a text node
MSG_DIV = ` world ` ;
fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ text ( 0 , 'Hello' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( 'Hello world' ) ;
2018-11-13 03:36:30 -05:00
2018-11-13 03:36:30 -05:00
// When the first node is an element
fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ text ( 1 , 'Hello' ) ;
ɵ ɵ elementEnd ( ) ;
ɵ ɵ i18n ( 2 , MSG_DIV ) ;
2018-11-13 03:36:30 -05:00
} , null , 3 ) ;
2018-11-13 03:36:30 -05:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>Hello</div> world' ) ;
// When there is a node after
MSG_DIV = ` Hello ` ;
fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18n ( 0 , MSG_DIV ) ;
ɵ ɵ text ( 1 , 'world' ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
expect ( fixture . html ) . toEqual ( 'Hello world' ) ;
} ) ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for deleted placeholders' , ( ) = > {
const MSG_DIV = ` Hello <20> #3<> world<6C> /#3<> ` ;
let fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 2 , 'div' ) ; // Will be removed
ɵ ɵ element ( 3 , 'span' ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementStart ( 4 , 'div' ) ;
{ ɵ ɵ text ( 5 , '!' ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 6 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>Hello <span>world</span></div><div>!</div>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for sub-templates' , ( ) = > {
// Template: `<div>Content: <div>before<span>middle</span>after</div>!</div>`;
const MSG_DIV =
` Content: <20> *2:1<> <31> #1:1<> before<72> *2:2<> <32> #1:2<> middle<6C> /#1:2<> <32> /*2:2<> after<65> /#1:1<> <31> /*2:1<> ! ` ;
function subTemplate_1 ( rf : RenderFlags , ctx : any ) {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 0 , MSG_DIV , 1 ) ;
ɵ ɵ elementStart ( 1 , 'div' ) ;
ɵ ɵ template ( 2 , subTemplate_2 , 2 , 0 , 'span' , [ AttributeMarker . Template , 'ngIf' ] ) ;
ɵ ɵ elementEnd ( ) ;
ɵ ɵ i18nEnd ( ) ;
2018-08-16 21:53:21 -04:00
}
2018-11-13 03:36:30 -05:00
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementProperty ( 2 , 'ngIf' , ɵ ɵ bind ( true ) ) ;
2018-11-13 03:36:30 -05:00
}
}
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
function subTemplate_2 ( rf : RenderFlags , ctx : any ) {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 0 , MSG_DIV , 2 ) ;
ɵ ɵ element ( 1 , 'span' ) ;
ɵ ɵ i18nEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
class MyApp {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-06-18 10:55:43 -04:00
type : MyApp ,
2018-11-14 13:23:21 -05:00
selectors : [ [ 'my-app' ] ] ,
2018-11-13 03:36:30 -05:00
directives : [ NgIf ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 3 ,
2018-08-18 14:14:50 -04:00
vars : 1 ,
2018-11-13 03:36:30 -05:00
template : ( rf : RenderFlags , ctx : MyApp ) = > {
2018-06-18 10:55:43 -04:00
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
ɵ ɵ template ( 2 , subTemplate_1 , 3 , 1 , 'div' , [ AttributeMarker . Template , 'ngIf' ] ) ;
ɵ ɵ i18nEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementProperty ( 2 , 'ngIf' , true ) ;
2018-06-18 10:55:43 -04:00
}
2018-11-13 03:36:30 -05:00
}
2018-06-18 10:55:43 -04:00
} ) ;
}
const fixture = new ComponentFixture ( MyApp ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html )
. toEqual ( '<div>Content: <div>before<span>middle</span>after</div>!</div>' ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2018-11-13 03:36:30 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
} ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div><!--ICU 2--></div>' ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2019-01-13 05:10:04 -05:00
it ( 'for multiple ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-13 05:10:04 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
2019-02-20 17:21:20 -05:00
} - { <EFBFBD> 0 <EFBFBD> , select ,
2019-01-13 05:10:04 -05:00
other { ( <EFBFBD> 0 <EFBFBD> ) }
} ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2019-01-13 05:10:04 -05:00
} , null , 2 ) ;
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div><!--ICU 2--> - <!--ICU 8--></div>' ) ;
} ) ;
it ( 'for multiple ICU expressions inside html' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` <EFBFBD> #2<> {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-13 05:10:04 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
2019-02-20 17:21:20 -05:00
} <EFBFBD> / # 2 <EFBFBD> <EFBFBD> # 3 <EFBFBD> { <EFBFBD> 0 <EFBFBD> , select ,
2019-01-13 05:10:04 -05:00
other { ( <EFBFBD> 0 <EFBFBD> ) }
} <EFBFBD> / # 3 <EFBFBD> ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
ɵ ɵ element ( 2 , 'span' ) ;
ɵ ɵ element ( 3 , 'span' ) ;
ɵ ɵ i18nEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2019-01-13 05:10:04 -05:00
} , null , 4 ) ;
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div><span><!--ICU 4--></span><span><!--ICU 9--></span></div>' ) ;
} ) ;
2019-01-03 14:18:09 -05:00
it ( 'for ICU expressions inside templates' , ( ) = > {
const MSG_DIV = ` <EFBFBD> *2:1<> <31> #1:1<> {<7B> 0:1<> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
other { <EFBFBD> 0 :1 <EFBFBD> < span title = "<22> 1:1<> " > emails < / span > }
} <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 2 :1 <EFBFBD> ` ;
function subTemplate_1 ( rf : RenderFlags , ctx : any ) {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 0 , MSG_DIV , 1 ) ;
ɵ ɵ element ( 1 , 'span' ) ;
ɵ ɵ i18nEnd ( ) ;
2019-01-03 14:18:09 -05:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
const ctx = ɵ ɵ nextContext ( ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 0 ) ;
2019-01-03 14:18:09 -05:00
}
}
class MyApp {
value0 = 0 ;
value1 = 'emails label' ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2019-01-03 14:18:09 -05:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
directives : [ NgIf ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 3 ,
vars : 1 ,
template : ( rf : RenderFlags , ctx : MyApp ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
ɵ ɵ template ( 2 , subTemplate_1 , 2 , 2 , 'span' , [ AttributeMarker . Template , 'ngIf' ] ) ;
ɵ ɵ i18nEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2019-01-03 14:18:09 -05:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementProperty ( 2 , 'ngIf' , true ) ;
2019-01-03 14:18:09 -05:00
}
}
} ) ;
}
const fixture = new ComponentFixture ( MyApp ) ;
expect ( fixture . html )
. toEqual ( '<div><span>no <b title="none">emails</b>!<!--ICU 4--></span></div>' ) ;
// Update the value
fixture . component . value0 = 3 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>3 <span title="emails label">emails</span><!--ICU 4--></span></div>' ) ;
2019-01-04 11:17:50 -05:00
} ) ;
2019-01-04 09:51:33 -05:00
it ( 'for ICU expressions inside <ng-container>' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-04 09:51:33 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
} ` ;
2019-01-04 11:17:50 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2019-01-04 11:17:50 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementContainerStart ( 1 ) ;
{ ɵ ɵ i18n ( 2 , MSG_DIV ) ; }
ɵ ɵ elementContainerEnd ( ) ;
2019-01-04 11:17:50 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2019-01-04 11:17:50 -05:00
} ,
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( 0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( 'more than one' ) ) ;
ɵ ɵ i18nApply ( 2 ) ;
2019-01-04 11:17:50 -05:00
} ,
3 , 2 ) ;
2019-01-04 09:51:33 -05:00
2019-01-04 11:17:50 -05:00
expect ( fixture . html ) . toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 5--></div>' ) ;
2019-01-03 14:18:09 -05:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for nested ICU expressions' , ( ) = > {
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
2019-02-20 17:21:20 -05:00
= 0 { zero }
other { <EFBFBD> 0 <EFBFBD> { <EFBFBD> 1 <EFBFBD> , select ,
cat { cats }
dog { dogs }
2018-11-13 03:36:30 -05:00
other { animals }
} ! }
} ` ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div><!--ICU 2--></div>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
describe ( ` i18nAttribute ` , ( ) = > {
it ( 'for text' , ( ) = > {
const MSG_title = ` Hello world! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
const nbConsts = 2 ;
const index = 1 ;
const fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nAttributes ( index , MSG_div_attr ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} , null , nbConsts , index ) ;
const tView = fixture . hostView [ TVIEW ] ;
const opCodes = tView . data [ index + HEADER_OFFSET ] as I18nUpdateOpCodes ;
expect ( opCodes ) . toEqual ( [ ] ) ;
2018-11-22 00:14:06 -05:00
expect (
( getNativeByIndex ( 0 , fixture . hostView as LView ) as any as Element ) . getAttribute ( 'title' ) )
2018-11-13 03:36:30 -05:00
. toEqual ( MSG_title ) ;
} ) ;
2018-08-16 21:53:21 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for simple bindings' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
const nbConsts = 2 ;
const index = 1 ;
const opCodes =
2019-04-11 16:46:47 -04:00
getOpCodes ( ( ) = > { ɵ ɵ i18nAttributes ( index , MSG_div_attr ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( [
0 b1 , // bindings mask
6 , // if no update, skip 4
'Hello ' ,
- 1 , // binding index
'!' , ( index - 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Attr , 'title' , null
] ) ;
} ) ;
2018-08-16 21:53:21 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for multiple bindings' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> and <20> 1<EFBFBD> , again <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
const nbConsts = 2 ;
const index = 1 ;
const opCodes =
2019-04-11 16:46:47 -04:00
getOpCodes ( ( ) = > { ɵ ɵ i18nAttributes ( index , MSG_div_attr ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( [
0 b11 , // bindings mask
10 , // size
'Hello ' , - 1 , ' and ' , - 2 , ', again ' , - 1 , '!' ,
( index - 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Attr , 'title' , null
] ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for multiple attributes' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title , 'aria-label' , MSG_title ] ;
const nbConsts = 2 ;
const index = 1 ;
const opCodes =
2019-04-11 16:46:47 -04:00
getOpCodes ( ( ) = > { ɵ ɵ i18nAttributes ( index , MSG_div_attr ) ; } , null , nbConsts , index ) ;
2018-11-13 03:36:30 -05:00
expect ( opCodes ) . toEqual ( [
0 b1 , // bindings mask
6 , // if no update, skip 4
'Hello ' ,
- 1 , // binding index
'!' , ( index - 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Attr , 'title' , null ,
0 b1 , // bindings mask
6 , // if no update, skip 4
'Hello ' ,
- 1 , // binding index
'!' , ( index - 1 ) << I18nUpdateOpCode . SHIFT_REF | I18nUpdateOpCode . Attr , 'aria-label' , null
] ) ;
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
describe ( ` i18nExp & i18nApply ` , ( ) = > {
it ( 'for text bindings' , ( ) = > {
const MSG_DIV = ` Hello <20> 0<EFBFBD> ! ` ;
const ctx = { value : 'world' } ;
2018-07-09 14:59:43 -04:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2018-11-13 03:36:30 -05:00
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 1 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
// Template should be empty because there is no update template function
expect ( fixture . html ) . toEqual ( '<div>Hello world!</div>' ) ;
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for attribute bindings' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
const ctx = { value : 'world' } ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nAttributes ( 1 , MSG_div_attr ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 1 ) ;
2018-11-13 03:36:30 -05:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello world!"></div>' ) ;
// Change detection cycle, no model changes
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello world!"></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value = 'universe' ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello universe!"></div>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for attributes with no bindings' , ( ) = > {
const MSG_title = ` Hello world! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
2018-08-16 21:53:21 -04:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nAttributes ( 1 , MSG_div_attr ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2019-04-11 16:46:47 -04:00
( ) = > { ɵ ɵ i18nApply ( 1 ) ; } , 2 , 1 ) ;
2018-08-16 21:53:21 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello world!"></div>' ) ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
// Change detection cycle, no model changes
fixture . update ( ) ;
expect ( fixture . html ) . toEqual ( '<div title="Hello world!"></div>' ) ;
} ) ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
it ( 'for multiple attribute bindings' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> and <20> 1<EFBFBD> , again <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title ] ;
const ctx = { value0 : 'world' , value1 : 'universe' } ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nAttributes ( 1 , MSG_div_attr ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2018-11-13 03:36:30 -05:00
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 2 ) ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello world and universe, again world!"></div>' ) ;
2018-07-20 10:21:05 -04:00
// Change detection cycle, no model changes
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello world and universe, again world!"></div>' ) ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 'earth' ;
2018-07-20 10:21:05 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div title="Hello earth and universe, again earth!"></div>' ) ;
2018-07-20 10:21:05 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 'earthlings' ;
ctx . value1 = 'martians' ;
2018-07-20 10:21:05 -04:00
fixture . update ( ) ;
expect ( fixture . html )
2018-11-13 03:36:30 -05:00
. toEqual ( '<div title="Hello earthlings and martians, again earthlings!"></div>' ) ;
2018-07-20 10:21:05 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for bindings of multiple attributes' , ( ) = > {
const MSG_title = ` Hello <20> 0<EFBFBD> ! ` ;
const MSG_div_attr = [ 'title' , MSG_title , 'aria-label' , MSG_title ] ;
const ctx = { value : 'world' } ;
2018-08-16 21:53:21 -04:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nAttributes ( 1 , MSG_div_attr ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2018-11-13 03:36:30 -05:00
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 1 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div aria-label="Hello world!" title="Hello world!"></div>' ) ;
2018-06-18 10:55:43 -04:00
// Change detection cycle, no model changes
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div aria-label="Hello world!" title="Hello world!"></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value = 'universe' ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
expect ( fixture . html )
2018-11-13 03:36:30 -05:00
. toEqual ( '<div aria-label="Hello universe!" title="Hello universe!"></div>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2018-11-13 03:36:30 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
} ` ;
const ctx = { value0 : 0 , value1 : 'emails label' } ;
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2018-11-13 03:36:30 -05:00
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 2 ) ;
expect ( fixture . html ) . toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-07-09 14:59:43 -04:00
// Change detection cycle, no model changes
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 1 ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>one <i>email</i><!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 10 ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html )
. toEqual ( '<div>10 <span title="emails label">emails</span><!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value1 = '10 emails' ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
expect ( fixture . html )
2018-11-13 03:36:30 -05:00
. toEqual ( '<div>10 <span title="10 emails">emails</span><!--ICU 4--></div>' ) ;
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 0 ;
fixture . update ( ) ;
expect ( fixture . html ) . toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--></div>' ) ;
} ) ;
2018-11-14 13:23:21 -05:00
2019-01-13 05:10:04 -05:00
it ( 'for multiple ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-13 05:10:04 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
2019-02-20 17:21:20 -05:00
} - { <EFBFBD> 0 <EFBFBD> , select ,
2019-01-13 05:10:04 -05:00
other { ( <EFBFBD> 0 <EFBFBD> ) }
} ` ;
const ctx = { value0 : 0 , value1 : 'emails label' } ;
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2019-01-13 05:10:04 -05:00
} ,
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2019-01-13 05:10:04 -05:00
} ,
2 , 2 ) ;
expect ( fixture . html )
. toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--> - (0)<!--ICU 10--></div>' ) ;
// Change detection cycle, no model changes
fixture . update ( ) ;
expect ( fixture . html )
. toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--> - (0)<!--ICU 10--></div>' ) ;
ctx . value0 = 1 ;
fixture . update ( ) ;
expect ( fixture . html ) . toEqual ( '<div>one <i>email</i><!--ICU 4--> - (1)<!--ICU 10--></div>' ) ;
ctx . value0 = 10 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div>10 <span title="emails label">emails</span><!--ICU 4--> - (10)<!--ICU 10--></div>' ) ;
ctx . value1 = '10 emails' ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div>10 <span title="10 emails">emails</span><!--ICU 4--> - (10)<!--ICU 10--></div>' ) ;
ctx . value0 = 0 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual ( '<div>no <b title="none">emails</b>!<!--ICU 4--> - (0)<!--ICU 10--></div>' ) ;
} ) ;
it ( 'for multiple ICU expressions' , ( ) = > {
2019-02-20 17:21:20 -05:00
const MSG_DIV = ` <EFBFBD> #2<> {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-13 05:10:04 -05:00
other { <EFBFBD> 0 <EFBFBD> < span title = "<22> 1<EFBFBD> " > emails < / span > }
2019-02-20 17:21:20 -05:00
} <EFBFBD> / # 2 <EFBFBD> <EFBFBD> # 3 <EFBFBD> { <EFBFBD> 0 <EFBFBD> , select ,
2019-01-13 05:10:04 -05:00
other { ( <EFBFBD> 0 <EFBFBD> ) }
} <EFBFBD> / # 3 <EFBFBD> ` ;
const ctx = { value0 : 0 , value1 : 'emails label' } ;
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
ɵ ɵ element ( 2 , 'span' ) ;
ɵ ɵ element ( 3 , 'span' ) ;
ɵ ɵ i18nEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2019-01-13 05:10:04 -05:00
} ,
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2019-01-13 05:10:04 -05:00
} ,
4 , 2 ) ;
expect ( fixture . html )
. toEqual (
'<div><span>no <b title="none">emails</b>!<!--ICU 6--></span><span>(0)<!--ICU 11--></span></div>' ) ;
// Change detection cycle, no model changes
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>no <b title="none">emails</b>!<!--ICU 6--></span><span>(0)<!--ICU 11--></span></div>' ) ;
ctx . value0 = 1 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>one <i>email</i><!--ICU 6--></span><span>(1)<!--ICU 11--></span></div>' ) ;
ctx . value0 = 10 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>10 <span title="emails label">emails</span><!--ICU 6--></span><span>(10)<!--ICU 11--></span></div>' ) ;
ctx . value1 = '10 emails' ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>10 <span title="10 emails">emails</span><!--ICU 6--></span><span>(10)<!--ICU 11--></span></div>' ) ;
ctx . value0 = 0 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
'<div><span>no <b title="none">emails</b>!<!--ICU 6--></span><span>(0)<!--ICU 11--></span></div>' ) ;
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'for nested ICU expressions' , ( ) = > {
const MSG_DIV = ` {<7B> 0<EFBFBD> , plural,
2019-02-20 17:21:20 -05:00
= 0 { zero }
other { <EFBFBD> 0 <EFBFBD> { <EFBFBD> 1 <EFBFBD> , select ,
cat { cats }
dog { dogs }
2018-11-13 03:36:30 -05:00
other { animals }
} ! }
} ` ;
const ctx = { value0 : 0 , value1 : 'cat' } ;
const fixture = prepareFixture (
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
ɵ ɵ i18n ( 1 , MSG_DIV ) ;
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
} ,
2018-11-13 03:36:30 -05:00
( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value0 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . value1 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
2018-11-13 03:36:30 -05:00
} ,
2 , 2 ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>zero<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
// Change detection cycle, no model changes
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>zero<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 10 ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>10 cats<!--nested ICU 0-->!<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value1 = 'squirrel' ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>10 animals<!--nested ICU 0-->!<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
ctx . value0 = 0 ;
2018-06-18 10:55:43 -04:00
fixture . update ( ) ;
2018-11-13 03:36:30 -05:00
expect ( fixture . html ) . toEqual ( '<div>zero<!--ICU 4--></div>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
} ) ;
2018-11-13 03:36:30 -05:00
describe ( 'integration' , ( ) = > {
it ( 'should support multiple i18n blocks' , ( ) = > {
// Translated template:
// <div>
// <a i18n>
// trad {{exp1}}
// </a>
// hello
// <b i18n i18n-title title="start {{exp2}} middle {{exp1}} end">
// <e></e>
// <c>trad</c>
// </b>
// </div>
const MSG_DIV_1 = ` trad <20> 0<EFBFBD> ` ;
const MSG_DIV_2_ATTR = [ 'title' , ` start <20> 1<EFBFBD> middle <20> 0<EFBFBD> end ` ] ;
const MSG_DIV_2 = ` <EFBFBD> #9<> <39> /#9<> <39> #7<> trad<61> /#7<> ` ;
class MyApp {
exp1 = '1' ;
exp2 = '2' ;
2018-06-18 10:55:43 -04:00
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 10 ,
2018-08-18 14:14:50 -04:00
vars : 2 ,
2018-11-13 03:36:30 -05:00
template : ( rf : RenderFlags , ctx : MyApp ) = > {
2018-06-18 10:55:43 -04:00
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 1 , 'a' ) ;
{ ɵ ɵ i18n ( 2 , MSG_DIV_1 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ text ( 3 , 'hello' ) ;
ɵ ɵ elementStart ( 4 , 'b' ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nAttributes ( 5 , MSG_DIV_2_ATTR ) ;
ɵ ɵ i18nStart ( 6 , MSG_DIV_2 ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 7 , 'c' ) ;
ɵ ɵ element ( 8 , 'd' ) ; // will be removed
ɵ ɵ element ( 9 , 'e' ) ; // will be moved before `c`
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nApply ( 2 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp2 ) ) ;
ɵ ɵ i18nApply ( 5 ) ;
2018-06-18 10:55:43 -04:00
}
}
} ) ;
}
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( MyApp ) ;
2018-06-18 10:55:43 -04:00
expect ( fixture . html )
. toEqual (
2018-11-13 03:36:30 -05:00
` <div><a>trad 1</a>hello<b title="start 2 middle 1 end"><e></e><c>trad</c></b></div> ` ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2019-03-12 11:33:14 -04:00
it ( 'should support multiple sibling i18n blocks' , ( ) = > {
// Translated template:
// <div>
// <div i18n>Section 1</div>
// <div i18n>Section 2</div>
// <div i18n>Section 3</div>
// </div>
const MSG_DIV_1 = ` Section 1 ` ;
const MSG_DIV_2 = ` Section 2 ` ;
const MSG_DIV_3 = ` Section 3 ` ;
class MyApp {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2019-03-12 11:33:14 -04:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 7 ,
vars : 0 ,
template : ( rf : RenderFlags , ctx : MyApp ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2019-03-12 11:33:14 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 1 , 'div' ) ;
{ ɵ ɵ i18n ( 2 , MSG_DIV_1 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementStart ( 3 , 'div' ) ;
{ ɵ ɵ i18n ( 4 , MSG_DIV_2 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementStart ( 5 , 'div' ) ;
{ ɵ ɵ i18n ( 6 , MSG_DIV_3 ) ; }
ɵ ɵ elementEnd ( ) ;
2019-03-12 11:33:14 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2019-03-12 11:33:14 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nApply ( 2 ) ;
ɵ ɵ i18nApply ( 4 ) ;
ɵ ɵ i18nApply ( 6 ) ;
2019-03-12 11:33:14 -04:00
}
}
} ) ;
}
const fixture = new ComponentFixture ( MyApp ) ;
expect ( fixture . html )
. toEqual ( ` <div><div>Section 1</div><div>Section 2</div><div>Section 3</div></div> ` ) ;
} ) ;
2019-03-14 11:26:01 -04:00
it ( 'should support multiple sibling i18n blocks inside of *ngFor' , ( ) = > {
// Translated template:
// <ul *ngFor="let item of [1,2,3]">
// <li i18n>Section 1</li>
// <li i18n>Section 2</li>
// <li i18n>Section 3</li>
// </ul>
const MSG_DIV_1 = ` Section 1 ` ;
const MSG_DIV_2 = ` Section 2 ` ;
const MSG_DIV_3 = ` Section 3 ` ;
function liTemplate ( rf : RenderFlags , ctx : NgForOfContext < string > ) {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'ul' ) ;
ɵ ɵ elementStart ( 1 , 'li' ) ;
{ ɵ ɵ i18n ( 2 , MSG_DIV_1 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementStart ( 3 , 'li' ) ;
{ ɵ ɵ i18n ( 4 , MSG_DIV_2 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementStart ( 5 , 'li' ) ;
{ ɵ ɵ i18n ( 6 , MSG_DIV_3 ) ; }
ɵ ɵ elementEnd ( ) ;
ɵ ɵ elementEnd ( ) ;
2019-03-14 11:26:01 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nApply ( 2 ) ;
ɵ ɵ i18nApply ( 4 ) ;
ɵ ɵ i18nApply ( 6 ) ;
2019-03-14 11:26:01 -04:00
}
}
class MyApp {
items : string [ ] = [ '1' , '2' , '3' ] ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2019-03-14 11:26:01 -04:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 2 ,
vars : 1 ,
template : ( rf : RenderFlags , ctx : MyApp ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2019-03-14 11:26:01 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ template (
2019-04-04 14:41:52 -04:00
1 , liTemplate , 7 , 0 , 'ul' , [ AttributeMarker . Template , 'ngFor' , 'ngForOf' ] ) ;
2019-03-14 11:26:01 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2019-03-14 11:26:01 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementProperty ( 1 , 'ngForOf' , ɵ ɵ bind ( ctx . items ) ) ;
2019-03-14 11:26:01 -04:00
}
} ,
directives : ( ) = > [ NgForOf ]
} ) ;
}
const fixture = new ComponentFixture ( MyApp ) ;
expect ( fixture . html )
. toEqual (
` <div><ul><li>Section 1</li><li>Section 2</li><li>Section 3</li></ul><ul><li>Section 1</li><li>Section 2</li><li>Section 3</li></ul><ul><li>Section 1</li><li>Section 2</li><li>Section 3</li></ul></div> ` ) ;
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'should support attribute translations on removed elements' , ( ) = > {
// Translated template:
// <div i18n i18n-title title="start {{exp2}} middle {{exp1}} end">
// trad {{exp1}}
// </div>
2018-11-13 03:36:30 -05:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_1 = ` trad <20> 0<EFBFBD> ` ;
const MSG_DIV_1_ATTR_1 = [ 'title' , ` start <20> 1<EFBFBD> middle <20> 0<EFBFBD> end ` ] ;
class MyApp {
exp1 = '1' ;
exp2 = '2' ;
2018-06-18 10:55:43 -04:00
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 5 ,
vars : 5 ,
template : ( rf : RenderFlags , ctx : MyApp ) = > {
2018-06-18 10:55:43 -04:00
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nAttributes ( 1 , MSG_DIV_1_ATTR_1 ) ;
ɵ ɵ i18nStart ( 2 , MSG_DIV_1 ) ;
2018-06-18 10:55:43 -04:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 3 , 'b' ) ; // Will be removed
{ ɵ ɵ i18nAttributes ( 4 , MSG_DIV_1_ATTR_1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-06-18 10:55:43 -04:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp2 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nApply ( 2 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp2 ) ) ;
ɵ ɵ i18nApply ( 4 ) ;
2018-06-18 10:55:43 -04:00
}
}
} ) ;
}
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( MyApp ) ;
expect ( fixture . html ) . toEqual ( ` <div title="start 2 middle 1 end">trad 1</div> ` ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2019-01-29 09:28:31 -05:00
it ( 'should work with directives and host bindings' , ( ) = > {
let directiveInstances : Directive [ ] = [ ] ;
class Directive {
// @HostBinding('className')
klass = 'foo' ;
2019-04-11 16:46:47 -04:00
static ngDirectiveDef = ɵ ɵ defineDirective ( {
2019-01-29 09:28:31 -05:00
type : Directive ,
selectors : [ [ '' , 'dir' , '' ] ] ,
factory : ( ) = > {
const instance = new Directive ( ) ;
directiveInstances . push ( instance ) ;
return instance ;
} ,
hostBindings : ( rf : RenderFlags , ctx : any , elementIndex : number ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ allocHostVars ( 1 ) ;
2019-01-29 09:28:31 -05:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementProperty ( elementIndex , 'className' , ɵ ɵ bind ( ctx . klass ) , null , true ) ;
2019-01-29 09:28:31 -05:00
}
}
} ) ;
}
// Translated template:
// <div i18n [test]="false" i18n-title title="start {{exp2}} middle {{exp1}} end">
// trad {<7B> 0<EFBFBD> , plural,
// =0 {no <b title="none">emails</b>!}
// =1 {one <i>email</i>}
// other {<7B> 0<EFBFBD> emails}
// }
// </div>
2019-02-20 17:21:20 -05:00
const MSG_DIV_1 = ` trad {<7B> 0<EFBFBD> , plural,
= 0 { no < b title = "none" > emails < / b > ! }
= 1 { one < i > email < / i > }
2019-01-29 09:28:31 -05:00
other { <EFBFBD> 0 <EFBFBD> emails }
} ` ;
const MSG_DIV_1_ATTR_1 = [ 'title' , ` start <20> 1<EFBFBD> middle <20> 0<EFBFBD> end ` ] ;
class MyApp {
exp1 = 1 ;
exp2 = 2 ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2019-01-29 09:28:31 -05:00
type : MyApp ,
selectors : [ [ 'my-app' ] ] ,
factory : ( ) = > new MyApp ( ) ,
consts : 6 ,
vars : 5 ,
directives : [ Directive ] ,
template : ( rf : RenderFlags , ctx : MyApp ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' , [ AttributeMarker . Bindings , 'dir' ] ) ;
2019-01-29 09:28:31 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nAttributes ( 1 , MSG_DIV_1_ATTR_1 ) ;
ɵ ɵ i18nStart ( 2 , MSG_DIV_1 ) ;
2019-01-29 09:28:31 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 3 , 'b' , [ AttributeMarker . Bindings , 'dir' ] ) ; // Will be removed
{ ɵ ɵ i18nAttributes ( 4 , MSG_DIV_1_ATTR_1 ) ; }
ɵ ɵ elementEnd ( ) ;
2019-01-29 09:28:31 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2019-01-29 09:28:31 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
ɵ ɵ element ( 5 , 'div' , [ AttributeMarker . Bindings , 'dir' ] ) ;
2019-01-29 09:28:31 -05:00
}
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp2 ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nApply ( 2 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp1 ) ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( ctx . exp2 ) ) ;
ɵ ɵ i18nApply ( 4 ) ;
2019-01-29 09:28:31 -05:00
}
}
} ) ;
}
const fixture = new ComponentFixture ( MyApp ) ;
// the "test" attribute should not be reflected in the DOM as it is here only for directive
// matching purposes
expect ( fixture . html )
. toEqual (
` <div class="foo" title="start 2 middle 1 end">trad one <i>email</i><!--ICU 23--></div><div class="foo"></div> ` ) ;
directiveInstances . forEach ( instance = > instance . klass = 'bar' ) ;
fixture . component . exp1 = 2 ;
fixture . component . exp2 = 3 ;
fixture . update ( ) ;
expect ( fixture . html )
. toEqual (
` <div class="bar" title="start 3 middle 2 end">trad 2 emails<!--ICU 23--></div><div class="bar"></div> ` ) ;
} ) ;
2019-02-19 10:02:28 -05:00
it ( 'should fix the links when adding/moving/removing nodes' , ( ) = > {
const MSG_DIV = ` <EFBFBD> #2<> <32> /#2<> <32> #8<> <38> /#8<> <38> #4<> <34> /#4<> <34> #5<> <35> /#5<> Hello World<6C> #3<> <33> /#3<> <33> #7<> <37> /#7<> ` ;
let fixture = prepareFixture ( ( ) = > {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2019-02-19 10:02:28 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV ) ;
2019-02-19 10:02:28 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 2 , 'div2' ) ;
ɵ ɵ element ( 3 , 'div3' ) ;
ɵ ɵ element ( 4 , 'div4' ) ;
ɵ ɵ element ( 5 , 'div5' ) ;
ɵ ɵ element ( 6 , 'div6' ) ;
ɵ ɵ element ( 7 , 'div7' ) ;
ɵ ɵ element ( 8 , 'div8' ) ;
2019-02-19 10:02:28 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2019-02-19 10:02:28 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2019-02-19 10:02:28 -05:00
} , null , 9 ) ;
expect ( fixture . html )
. toEqual (
'<div><div2></div2><div8></div8><div4></div4><div5></div5>Hello World<div3></div3><div7></div7></div>' ) ;
const div0 = getTNode ( 0 , fixture . hostView ) ;
const div2 = getTNode ( 2 , fixture . hostView ) ;
const div3 = getTNode ( 3 , fixture . hostView ) ;
const div4 = getTNode ( 4 , fixture . hostView ) ;
const div5 = getTNode ( 5 , fixture . hostView ) ;
const div7 = getTNode ( 7 , fixture . hostView ) ;
const div8 = getTNode ( 8 , fixture . hostView ) ;
const text = getTNode ( 9 , fixture . hostView ) ;
expect ( div0 . child ) . toEqual ( div2 ) ;
expect ( div0 . next ) . toBeNull ( ) ;
expect ( div2 . next ) . toEqual ( div8 ) ;
expect ( div8 . next ) . toEqual ( div4 ) ;
expect ( div4 . next ) . toEqual ( div5 ) ;
expect ( div5 . next ) . toEqual ( text ) ;
expect ( text . next ) . toEqual ( div3 ) ;
expect ( div3 . next ) . toEqual ( div7 ) ;
expect ( div7 . next ) . toBeNull ( ) ;
} ) ;
2018-11-13 03:36:30 -05:00
describe ( 'projection' , ( ) = > {
it ( 'should project the translations' , ( ) = > {
@Component ( { selector : 'child' , template : '<p><ng-content></ng-content></p>' } )
class Child {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Child ,
selectors : [ [ 'child' ] ] ,
factory : ( ) = > new Child ( ) ,
consts : 2 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'p' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_SECTION_1 = ` <EFBFBD> #2<> Je suis projeté depuis <20> #3<> <33> 0<EFBFBD> <30> /#3<> <33> /#2<> ` ;
const MSG_ATTR_1 = [ 'title' , ` Enfant de <20> 0<EFBFBD> ` ] ;
@Component ( {
selector : 'parent' ,
template : `
< div i18n >
< child >
I am projected from
< b i18n - title title = "Child of {{name}}" > { { name } }
< remove - me - 1 > < / r e m o v e - m e - 1 >
< / b >
< remove - me - 2 > < / r e m o v e - m e - 2 >
< / child >
< remove - me - 3 > < / r e m o v e - m e - 3 >
< / div > `
// Translated to:
// <div i18n>
// <child>
// <p>
// Je suis projeté depuis <b i18n-title title="Enfant de {{name}}">{{name}}</b>
// </p>
// </child>
// </div>
} )
class Parent {
name : string = 'Parent' ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Parent ,
selectors : [ [ 'parent' ] ] ,
directives : [ Child ] ,
factory : ( ) = > new Parent ( ) ,
consts : 8 ,
vars : 2 ,
template : ( rf : RenderFlags , cmp : Parent ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV_SECTION_1 ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 2 , 'child' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 3 , 'b' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nAttributes ( 4 , MSG_ATTR_1 ) ;
ɵ ɵ element ( 5 , 'remove-me-1' ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
ɵ ɵ element ( 6 , 'remove-me-2' ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
ɵ ɵ element ( 7 , 'remove-me-3' ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-07-09 14:59:43 -04:00
}
2018-11-13 03:36:30 -05:00
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( cmp . name ) ) ;
ɵ ɵ i18nApply ( 1 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( cmp . name ) ) ;
ɵ ɵ i18nApply ( 4 ) ;
2018-06-18 10:55:43 -04:00
}
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( Parent ) ;
expect ( fixture . html )
. toEqual (
'<div><child><p>Je suis projeté depuis <b title="Enfant de Parent">Parent</b></p></child></div>' ) ;
// <div><child><p><b title="Enfant de Parent">Parent</b></p></child></div>
// <div><child><p>Je suis projeté depuis <b title="Enfant de
// Parent">Parent</b></p></child></div>
} ) ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
it ( 'should project a translated i18n block' , ( ) = > {
@Component ( { selector : 'child' , template : '<p><ng-content></ng-content></p>' } )
class Child {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Child ,
selectors : [ [ 'child' ] ] ,
factory : ( ) = > new Child ( ) ,
consts : 2 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'p' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-06-18 10:55:43 -04:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_SECTION_1 = ` Je suis projeté depuis <20> 0<EFBFBD> ` ;
const MSG_ATTR_1 = [ 'title' , ` Enfant de <20> 0<EFBFBD> ` ] ;
2018-06-18 10:55:43 -04:00
2018-11-13 03:36:30 -05:00
@Component ( {
selector : 'parent' ,
template : `
< div >
< child >
< any > < / any >
< b i18n i18n - title title = "Child of {{name}}" > I am projected from { { name } } < / b >
< any > < / any >
2018-06-18 10:55:43 -04:00
< / child >
2018-11-13 03:36:30 -05:00
< / div > `
// Translated to:
// <div>
// <child>
// <any></any>
// <b i18n i18n-title title="Enfant de {{name}}">Je suis projeté depuis {{name}}</b>
// <any></any>
// </child>
// </div>
} )
class Parent {
name : string = 'Parent' ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Parent ,
selectors : [ [ 'parent' ] ] ,
directives : [ Child ] ,
factory : ( ) = > new Parent ( ) ,
consts : 7 ,
vars : 2 ,
template : ( rf : RenderFlags , cmp : Parent ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'div' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 1 , 'child' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 2 , 'any' ) ;
ɵ ɵ elementStart ( 3 , 'b' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nAttributes ( 4 , MSG_ATTR_1 ) ;
ɵ ɵ i18n ( 5 , MSG_DIV_SECTION_1 ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
ɵ ɵ element ( 6 , 'any' ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-07-09 14:59:43 -04:00
}
2018-11-13 03:36:30 -05:00
if ( rf & RenderFlags . Update ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nExp ( ɵ ɵ bind ( cmp . name ) ) ;
ɵ ɵ i18nApply ( 4 ) ;
ɵ ɵ i18nExp ( ɵ ɵ bind ( cmp . name ) ) ;
ɵ ɵ i18nApply ( 5 ) ;
2018-06-18 10:55:43 -04:00
}
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( Parent ) ;
expect ( fixture . html )
. toEqual (
'<div><child><p><any></any><b title="Enfant de Parent">Je suis projeté depuis Parent</b><any></any></p></child></div>' ) ;
2019-01-03 10:07:00 -05:00
// it should be able to render a new component with the same template code
const fixture2 = new ComponentFixture ( Parent ) ;
expect ( fixture2 . html ) . toEqual ( fixture . html ) ;
// Updating the fixture should work
fixture2 . component . name = 'Parent 2' ;
fixture . update ( ) ;
fixture2 . update ( ) ;
expect ( fixture2 . html )
. toEqual (
'<div><child><p><any></any><b title="Enfant de Parent 2">Je suis projeté depuis Parent 2</b><any></any></p></child></div>' ) ;
// The first fixture should not have changed
expect ( fixture . html )
. toEqual (
'<div><child><p><any></any><b title="Enfant de Parent">Je suis projeté depuis Parent</b><any></any></p></child></div>' ) ;
2018-11-13 03:36:30 -05:00
} ) ;
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
it ( 'should re-project translations when multiple projections' , ( ) = > {
@Component ( { selector : 'grand-child' , template : '<div><ng-content></ng-content></div>' } )
class GrandChild {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : GrandChild ,
selectors : [ [ 'grand-child' ] ] ,
factory : ( ) = > new GrandChild ( ) ,
consts : 2 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'div' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
@Component (
{ selector : 'child' , template : '<grand-child><ng-content></ng-content></grand-child>' } )
class Child {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Child ,
selectors : [ [ 'child' ] ] ,
directives : [ GrandChild ] ,
factory : ( ) = > new Child ( ) ,
consts : 2 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'grand-child' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_SECTION_1 = ` <EFBFBD> #2<> Bonjour<75> /#2<> Monde!` ;
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
@Component ( {
selector : 'parent' ,
template : ` <child i18n><b>Hello</b> World!</child> `
2018-07-19 05:02:22 -04:00
// Translated to:
2018-11-13 03:36:30 -05:00
// <child i18n><grand-child><div><b>Bonjour</b> Monde!</div></grand-child></child>
} )
class Parent {
name : string = 'Parent' ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Parent ,
selectors : [ [ 'parent' ] ] ,
directives : [ Child ] ,
factory : ( ) = > new Parent ( ) ,
consts : 3 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Parent ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'child' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV_SECTION_1 ) ;
{ ɵ ɵ element ( 2 , 'b' ) ; }
ɵ ɵ i18nEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( Parent ) ;
expect ( fixture . html )
. toEqual ( '<child><grand-child><div><b>Bonjour</b> Monde!</div></grand-child></child>' ) ;
// <child><grand-child><div><b>Bonjour</b></div></grand-child></child>
// <child><grand-child><div><b>Bonjour</b> Monde!</div></grand-child></child>
} ) ;
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
xit ( 'should re-project translations when removed placeholders' , ( ) = > {
@Component ( { selector : 'grand-child' , template : '<div><ng-content></ng-content></div>' } )
class GrandChild {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : GrandChild ,
selectors : [ [ 'grand-child' ] ] ,
factory : ( ) = > new GrandChild ( ) ,
consts : 3 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'div' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
@Component (
{ selector : 'child' , template : '<grand-child><ng-content></ng-content></grand-child>' } )
class Child {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Child ,
selectors : [ [ 'child' ] ] ,
directives : [ GrandChild ] ,
factory : ( ) = > new Child ( ) ,
consts : 2 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( ) ;
ɵ ɵ elementStart ( 0 , 'grand-child' ) ;
{ ɵ ɵ projection ( 1 ) ; }
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-11-14 13:23:21 -05:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_SECTION_1 = ` Bonjour Monde! ` ;
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
@Component ( {
selector : 'parent' ,
template : ` <child i18n><b>Hello</b> World!</child> `
2018-07-19 05:02:22 -04:00
// Translated to:
2018-11-13 03:36:30 -05:00
// <child i18n><grand-child><div>Bonjour Monde!</div></grand-child></child>
} )
class Parent {
name : string = 'Parent' ;
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Parent ,
selectors : [ [ 'parent' ] ] ,
directives : [ Child ] ,
factory : ( ) = > new Parent ( ) ,
2019-02-07 10:57:37 -05:00
consts : 3 ,
2018-11-13 03:36:30 -05:00
vars : 0 ,
template : ( rf : RenderFlags , cmp : Parent ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'child' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV_SECTION_1 ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 2 , 'b' ) ; // will be removed
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( Parent ) ;
expect ( fixture . html )
. toEqual ( '<child><grand-child><div>Bonjour Monde!</div></grand-child></child>' ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-11-13 03:36:30 -05:00
it ( 'should project translations with selectors' , ( ) = > {
@Component ( {
selector : 'child' ,
template : `
< ng - content select = "span" > < / n g - c o n t e n t >
`
} )
class Child {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Child ,
selectors : [ [ 'child' ] ] ,
factory : ( ) = > new Child ( ) ,
consts : 1 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Child ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ projectionDef ( [ [ [ 'span' ] ] ] ) ;
ɵ ɵ projection ( 0 , 1 ) ;
2018-11-13 03:36:30 -05:00
}
}
} ) ;
}
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
const MSG_DIV_SECTION_1 = ` <EFBFBD> #2<> Contenu<6E> /#2<> ` ;
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
@Component ( {
selector : 'parent' ,
template : `
< child i18n >
< span title = "keepMe" > < / span >
< span title = "deleteMe" > < / span >
< / child >
`
2018-07-19 05:02:22 -04:00
// Translated to:
2018-11-13 03:36:30 -05:00
// <child i18n><span title="keepMe">Contenu</span></child>
} )
class Parent {
2019-04-11 16:46:47 -04:00
static ngComponentDef = ɵ ɵ defineComponent ( {
2018-11-13 03:36:30 -05:00
type : Parent ,
selectors : [ [ 'parent' ] ] ,
directives : [ Child ] ,
factory : ( ) = > new Parent ( ) ,
consts : 4 ,
vars : 0 ,
template : ( rf : RenderFlags , cmp : Parent ) = > {
if ( rf & RenderFlags . Create ) {
2019-04-11 16:46:47 -04:00
ɵ ɵ elementStart ( 0 , 'child' ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nStart ( 1 , MSG_DIV_SECTION_1 ) ;
2018-11-13 03:36:30 -05:00
{
2019-04-11 16:46:47 -04:00
ɵ ɵ element ( 2 , 'span' , [ 'title' , 'keepMe' ] ) ;
ɵ ɵ element ( 3 , 'span' , [ 'title' , 'deleteMe' ] ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ i18nEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2019-04-11 16:46:47 -04:00
ɵ ɵ elementEnd ( ) ;
2018-11-13 03:36:30 -05:00
}
2018-11-14 13:23:21 -05:00
}
2018-11-13 03:36:30 -05:00
} ) ;
}
2018-07-19 05:02:22 -04:00
2018-11-13 03:36:30 -05:00
const fixture = new ComponentFixture ( Parent ) ;
expect ( fixture . html ) . toEqual ( '<child><span title="keepMe">Contenu</span></child>' ) ;
} ) ;
2018-07-19 05:02:22 -04:00
} ) ;
2018-06-18 10:55:43 -04:00
} ) ;
2018-10-18 13:08:51 -04:00
describe ( 'i18nPostprocess' , ( ) = > {
it ( 'should handle valid cases' , ( ) = > {
2019-01-13 19:56:00 -05:00
const arr = [ '<27> *1:1<> <31> #2:1<> ' , '<27> #4:1<> ' , '<27> 6:1<> ' , '<27> /#2:1<> <31> /*1:1<> ' ] ;
2018-10-18 13:08:51 -04:00
const str = ` [ ${ arr . join ( '|' ) } ] ` ;
const cases = [
// empty string
[ '' , { } , '' ] ,
// string without any special cases
[ 'Foo [1,2,3] Bar - no ICU here' , { } , 'Foo [1,2,3] Bar - no ICU here' ] ,
// multi-value cases
[
` Start: ${ str } , ${ str } and ${ str } , ${ str } end. ` , { } ,
` Start: ${ arr [ 0 ] } , ${ arr [ 1 ] } and ${ arr [ 2 ] } , ${ arr [ 3 ] } end. `
] ,
// replace VAR_SELECT
[
'My ICU: {VAR_SELECT, select, =1 {one} other {other}}' , { VAR_SELECT : '<27> 1:2<> ' } ,
'My ICU: {<7B> 1:2<> , select, =1 {one} other {other}}'
] ,
[
'My ICU: {\n\n\tVAR_SELECT_1 \n\n, select, =1 {one} other {other}}' ,
{ VAR_SELECT_1 : '<27> 1:2<> ' } , 'My ICU: {\n\n\t<> 1:2<> \n\n, select, =1 {one} other {other}}'
] ,
// replace VAR_PLURAL
[
'My ICU: {VAR_PLURAL, plural, one {1} other {other}}' , { VAR_PLURAL : '<27> 1:2<> ' } ,
'My ICU: {<7B> 1:2<> , plural, one {1} other {other}}'
] ,
[
'My ICU: {\n\n\tVAR_PLURAL_1 \n\n, select, =1 {one} other {other}}' ,
{ VAR_PLURAL_1 : '<27> 1:2<> ' } , 'My ICU: {\n\n\t<> 1:2<> \n\n, select, =1 {one} other {other}}'
] ,
// do not replace VAR_* anywhere else in a string (only in ICU)
[
'My ICU: {VAR_PLURAL, plural, one {1} other {other}} VAR_PLURAL and VAR_SELECT' ,
{ VAR_PLURAL : '<27> 1:2<> ' } ,
'My ICU: {<7B> 1:2<> , plural, one {1} other {other}} VAR_PLURAL and VAR_SELECT'
] ,
// replace VAR_*'s in nested ICUs
[
'My ICU: {VAR_PLURAL, plural, one {1 - {VAR_SELECT, age, 50 {fifty} other {other}}} other {other}}' ,
{ VAR_PLURAL : '<27> 1:2<> ' , VAR_SELECT : '<27> 5<EFBFBD> ' } ,
'My ICU: {<7B> 1:2<> , plural, one {1 - {<7B> 5<EFBFBD> , age, 50 {fifty} other {other}}} other {other}}'
] ,
[
'My ICU: {VAR_PLURAL, plural, one {1 - {VAR_PLURAL_1, age, 50 {fifty} other {other}}} other {other}}' ,
{ VAR_PLURAL : '<27> 1:2<> ' , VAR_PLURAL_1 : '<27> 5<EFBFBD> ' } ,
'My ICU: {<7B> 1:2<> , plural, one {1 - {<7B> 5<EFBFBD> , age, 50 {fifty} other {other}}} other {other}}'
] ,
// ICU replacement
[
'My ICU #1: <20> I18N_EXP_ICU<43> , My ICU #2: <20> I18N_EXP_ICU<43> ' ,
{ ICU : [ 'ICU_VALUE_1' , 'ICU_VALUE_2' ] } , 'My ICU #1: ICU_VALUE_1, My ICU #2: ICU_VALUE_2'
] ,
// mixed case
[
` Start: ${ str } , ${ str } . ICU: {VAR_SELECT, count, 10 {ten} other {other}}.
Another ICU : <EFBFBD> I18N_EXP_ICU <EFBFBD> and $ { str } , $ { str } and one more ICU : <EFBFBD> I18N_EXP_ICU <EFBFBD> and end . ` ,
{ VAR_SELECT : '<27> 1:2<> ' , ICU : [ 'ICU_VALUE_1' , 'ICU_VALUE_2' ] } ,
` Start: ${ arr [ 0 ] } , ${ arr [ 1 ] } . ICU: {<7B> 1:2<> , count, 10 {ten} other {other}}.
Another ICU : ICU_VALUE_1 and $ { arr [ 2 ] } , $ { arr [ 3 ] } and one more ICU : ICU_VALUE_2 and end . ` ,
] ,
] ;
cases . forEach ( ( [ input , replacements , output ] ) = > {
2019-04-11 16:46:47 -04:00
expect ( ɵ ɵ i18nPostprocess ( input as string , replacements as any ) ) . toEqual ( output as string ) ;
2018-10-18 13:08:51 -04:00
} ) ;
} ) ;
2019-01-13 19:56:00 -05:00
it ( 'should handle nested template represented by multi-value placeholders' , ( ) = > {
/ * *
* < div i18n >
* < span >
* Hello - 1
* < / span >
* < span * ngIf = "visible" >
* Hello - 2
* < span * ngIf = "visible" >
* Hello - 3
* < span * ngIf = "visible" >
* Hello - 4
* < / span >
* < / span >
* < / span >
* < span >
* Hello - 5
* < / span >
* < / div >
* /
const generated = `
[ <EFBFBD> # 2 <EFBFBD> | <EFBFBD> # 4 <EFBFBD> ] Bonjour - 1 [ <EFBFBD> / # 2 <EFBFBD> | <EFBFBD> / # 1 :3 <EFBFBD> <EFBFBD> / * 2 :3 <EFBFBD> | <EFBFBD> / # 1 :2 <EFBFBD> <EFBFBD> / * 2 :2 <EFBFBD> | <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 3 :1 <EFBFBD> | <EFBFBD> / # 4 <EFBFBD> ]
[ <EFBFBD> * 3 :1 <EFBFBD> <EFBFBD> # 1 :1 <EFBFBD> | <EFBFBD> * 2 :2 <EFBFBD> <EFBFBD> # 1 :2 <EFBFBD> | <EFBFBD> * 2 :3 <EFBFBD> <EFBFBD> # 1 :3 <EFBFBD> ]
Bonjour - 2
[ <EFBFBD> * 3 :1 <EFBFBD> <EFBFBD> # 1 :1 <EFBFBD> | <EFBFBD> * 2 :2 <EFBFBD> <EFBFBD> # 1 :2 <EFBFBD> | <EFBFBD> * 2 :3 <EFBFBD> <EFBFBD> # 1 :3 <EFBFBD> ]
Bonjour - 3
[ <EFBFBD> * 3 :1 <EFBFBD> <EFBFBD> # 1 :1 <EFBFBD> | <EFBFBD> * 2 :2 <EFBFBD> <EFBFBD> # 1 :2 <EFBFBD> | <EFBFBD> * 2 :3 <EFBFBD> <EFBFBD> # 1 :3 <EFBFBD> ] Bonjour - 4 [ <EFBFBD> / # 2 <EFBFBD> | <EFBFBD> / # 1 :3 <EFBFBD> <EFBFBD> / * 2 :3 <EFBFBD> | <EFBFBD> / # 1 :2 <EFBFBD> <EFBFBD> / * 2 :2 <EFBFBD> | <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 3 :1 <EFBFBD> | <EFBFBD> / # 4 <EFBFBD> ]
[ <EFBFBD> / # 2 <EFBFBD> | <EFBFBD> / # 1 :3 <EFBFBD> <EFBFBD> / * 2 :3 <EFBFBD> | <EFBFBD> / # 1 :2 <EFBFBD> <EFBFBD> / * 2 :2 <EFBFBD> | <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 3 :1 <EFBFBD> | <EFBFBD> / # 4 <EFBFBD> ]
[ <EFBFBD> / # 2 <EFBFBD> | <EFBFBD> / # 1 :3 <EFBFBD> <EFBFBD> / * 2 :3 <EFBFBD> | <EFBFBD> / # 1 :2 <EFBFBD> <EFBFBD> / * 2 :2 <EFBFBD> | <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 3 :1 <EFBFBD> | <EFBFBD> / # 4 <EFBFBD> ]
[ <EFBFBD> # 2 <EFBFBD> | <EFBFBD> # 4 <EFBFBD> ] Bonjour - 5 [ <EFBFBD> / # 2 <EFBFBD> | <EFBFBD> / # 1 :3 <EFBFBD> <EFBFBD> / * 2 :3 <EFBFBD> | <EFBFBD> / # 1 :2 <EFBFBD> <EFBFBD> / * 2 :2 <EFBFBD> | <EFBFBD> / # 1 :1 <EFBFBD> <EFBFBD> / * 3 :1 <EFBFBD> | <EFBFBD> / # 4 <EFBFBD> ]
` ;
const final = `
<EFBFBD> # 2 <EFBFBD> Bonjour - 1 <EFBFBD> / # 2 <EFBFBD>
<EFBFBD> * 3 :1 <EFBFBD>
<EFBFBD> # 1 :1 <EFBFBD>
Bonjour - 2
<EFBFBD> * 2 :2 <EFBFBD>
<EFBFBD> # 1 :2 <EFBFBD>
Bonjour - 3
<EFBFBD> * 2 :3 <EFBFBD>
<EFBFBD> # 1 :3 <EFBFBD> Bonjour - 4 <EFBFBD> / # 1 :3 <EFBFBD>
<EFBFBD> / * 2 :3 <EFBFBD>
<EFBFBD> / # 1 :2 <EFBFBD>
<EFBFBD> / * 2 :2 <EFBFBD>
<EFBFBD> / # 1 :1 <EFBFBD>
<EFBFBD> / * 3 :1 <EFBFBD>
<EFBFBD> # 4 <EFBFBD> Bonjour - 5 <EFBFBD> / # 4 <EFBFBD>
` ;
2019-04-11 16:46:47 -04:00
expect ( ɵ ɵ i18nPostprocess ( generated . replace ( /\s+/g , '' ) ) ) . toEqual ( final . replace ( /\s+/g , '' ) ) ;
2019-01-13 19:56:00 -05:00
} ) ;
2018-10-18 13:08:51 -04:00
it ( 'should throw in case we have invalid string' , ( ) = > {
const arr = [ '<27> *1:1<> <31> #2:1<> ' , '<27> #4:2<> ' , '<27> 6:4<> ' , '<27> /#2:1<> <31> /*1:1<> ' ] ;
const str = ` [ ${ arr . join ( '|' ) } ] ` ;
const cases = [
// less placeholders than we have
[ ` Start: ${ str } , ${ str } and ${ str } end. ` , { } ] ,
// more placeholders than we have
[ ` Start: ${ str } , ${ str } and ${ str } , ${ str } ${ str } end. ` , { } ] ,
// not enough ICU replacements
[ 'My ICU #1: <20> I18N_EXP_ICU<43> , My ICU #2: <20> I18N_EXP_ICU<43> ' , { ICU : [ 'ICU_VALUE_1' ] } ]
] ;
cases . forEach ( ( [ input , replacements , output ] ) = > {
2019-04-11 16:46:47 -04:00
expect ( ( ) = > ɵ ɵ i18nPostprocess ( input as string , replacements as any ) ) . toThrowError ( ) ;
2018-10-18 13:08:51 -04:00
} ) ;
} ) ;
} ) ;
2018-06-18 10:55:43 -04:00
} ) ;