| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 11:35:16 -08:00
										 |  |  | import {digest, serializeNodes} from '@angular/compiler/src/i18n/digest'; | 
					
						
							| 
									
										
										
										
											2016-08-04 17:46:31 -07:00
										 |  |  | import {extractMessages} from '@angular/compiler/src/i18n/extractor_merger'; | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | import {Message} from '@angular/compiler/src/i18n/i18n_ast'; | 
					
						
							| 
									
										
										
										
											2017-02-08 11:35:16 -08:00
										 |  |  | import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser'; | 
					
						
							|  |  |  | import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config'; | 
					
						
							| 
									
										
										
										
											2016-07-21 11:41:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   describe('I18nParser', () => { | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |     describe('elements', () => { | 
					
						
							|  |  |  |       it('should extract from elements', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">text</div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['text'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract from nested elements', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">text<span><b>nested</b></span></div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               'text', | 
					
						
							|  |  |  |               '<ph tag name="START_TAG_SPAN"><ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_TAG_SPAN">' | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           ], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |       it('should not create a message for empty elements', () => { | 
					
						
							|  |  |  |         expect(_humanizeMessages('<div i18n="m|d"></div>')).toEqual([]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |       it('should not create a message for plain elements', () => { | 
					
						
							|  |  |  |         expect(_humanizeMessages('<div></div>')).toEqual([]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 12:42:04 +01:00
										 |  |  |       it('should support void elements', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d"><p><br></p></div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               '<ph tag name="START_PARAGRAPH"><ph tag name="LINE_BREAK"/></ph name="CLOSE_PARAGRAPH">' | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           ], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should trim whitespace from custom ids (but not meanings)', () => { | 
					
						
							|  |  |  |         expect(_humanizeMessages('<div i18n="\n   m|d@@id\n   ">text</div>')).toEqual([ | 
					
						
							|  |  |  |           [['text'], '\n   m', 'd', 'id'], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('attributes', () => { | 
					
						
							|  |  |  |       it('should extract from attributes outside of translatable section', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n-title="m|d" title="msg"></div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['msg'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract from attributes in translatable element', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n><p><b i18n-title="m|d" title="msg"></b></p></div>')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                   '<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">' | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |                 '', '', '' | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |               ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['msg'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract from attributes in translatable block', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages( | 
					
						
							|  |  |  |                    '<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->')) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['msg'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |               [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                   '<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">' | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |                 '', '', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |               ], | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract from attributes in translatable ICU', () => { | 
					
						
							|  |  |  |         expect( | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |             _humanizeMessages( | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |                 '<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}<!-- /i18n -->')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['msg'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |               [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                   '{count, plural, =0 {[<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">]}}' | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |                 '', '', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |               ], | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract from attributes in non translatable ICU', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect( | 
					
						
							|  |  |  |             _humanizeMessages('{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}')) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['msg'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 10:14:18 -07:00
										 |  |  |       it('should not create a message for empty attributes', () => { | 
					
						
							|  |  |  |         expect(_humanizeMessages('<div i18n-title="m|d" title></div>')).toEqual([]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('interpolation', () => { | 
					
						
							|  |  |  |       it('should replace interpolation with placeholder', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">before{{ exp }}after</div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['[before, <ph name="INTERPOLATION"> exp </ph>, after]'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should support named interpolation', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">before{{ exp //i18n(ph="teSt") }}after</div>')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['[before, <ph name="TEST"> exp //i18n(ph="teSt") </ph>, after]'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2017-03-24 23:10:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect( | 
					
						
							|  |  |  |             _humanizeMessages('<div i18n=\'m|d\'>before{{ exp //i18n(ph=\'teSt\') }}after</div>')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [[`[before, <ph name="TEST"> exp //i18n(ph='teSt') </ph>, after]`], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2017-03-24 23:10:41 +01:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('blocks', () => { | 
					
						
							|  |  |  |       it('should extract from blocks', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages(`<!-- i18n: meaning1|desc1 -->message1<!-- /i18n -->
 | 
					
						
							| 
									
										
										
										
											2016-08-01 11:33:35 -07:00
										 |  |  |          <!-- i18n: desc2 -->message2<!-- /i18n --> | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |          <!-- i18n -->message3<!-- /i18n -->`))
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['message1'], 'meaning1', 'desc1', ''], | 
					
						
							|  |  |  |               [['message2'], '', 'desc2', ''], | 
					
						
							|  |  |  |               [['message3'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract all siblings', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages(`<!-- i18n -->text<p>html<b>nested</b></p><!-- /i18n -->`)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               'text', | 
					
						
							|  |  |  |               '<ph tag name="START_PARAGRAPH">html, <ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">' | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             '', '', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           ], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('ICU messages', () => { | 
					
						
							|  |  |  |       it('should extract as ICU when single child of an element', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">{count, plural, =0 {zero}}</div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['{count, plural, =0 {[zero]}}'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract as ICU + ph when not single child of an element', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<div i18n="m|d">b{count, plural, =0 {zero}}a</div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd', ''], | 
					
						
							|  |  |  |           [['{count, plural, =0 {[zero]}}'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 12:42:04 +01:00
										 |  |  |       it('should extract as ICU + ph when wrapped in whitespace in an element', () => { | 
					
						
							|  |  |  |         expect(_humanizeMessages('<div i18n="m|d"> {count, plural, =0 {zero}} </div>')).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [[' ', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', ' '], 'm', 'd', ''], | 
					
						
							|  |  |  |           [['{count, plural, =0 {[zero]}}'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2019-08-02 12:42:04 +01:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       it('should extract as ICU when single child of a block', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<!-- i18n:m|d -->{count, plural, =0 {zero}}<!-- /i18n -->')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['{count, plural, =0 {[zero]}}'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should extract as ICU + ph when not single child of a block', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<!-- i18n:m|d -->b{count, plural, =0 {zero}}a<!-- /i18n -->')) | 
					
						
							|  |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['{count, plural, =0 {[zero]}}'], '', '', ''], | 
					
						
							|  |  |  |               [['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd', ''], | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not extract nested ICU messages', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages( | 
					
						
							| 
									
										
										
										
											2016-11-04 15:10:19 -07:00
										 |  |  |                    '<div i18n="m|d">b{count, plural, =0 {{sex, select, male {m}}}}a</div>')) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             .toEqual([ | 
					
						
							|  |  |  |               [ | 
					
						
							|  |  |  |                 [ | 
					
						
							| 
									
										
										
										
											2016-11-04 15:10:19 -07:00
										 |  |  |                   'b', '<ph icu name="ICU">{count, plural, =0 {[{sex, select, male {[m]}}]}}</ph>', | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |                   'a' | 
					
						
							|  |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |                 'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |               ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['{count, plural, =0 {[{sex, select, male {[m]}}]}}'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('implicit elements', () => { | 
					
						
							|  |  |  |       it('should extract from implicit elements', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages('<b>bold</b><i>italic</i>', ['b'])).toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['bold'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('implicit attributes', () => { | 
					
						
							|  |  |  |       it('should extract implicit attributes', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(_humanizeMessages( | 
					
						
							|  |  |  |                    '<b title="bb">bold</b><i title="ii">italic</i>', [], {'b': ['title']})) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             .toEqual([ | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |               [['bb'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |     describe('placeholders', () => { | 
					
						
							|  |  |  |       it('should reuse the same placeholder name for tags', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         const html = '<div i18n="m|d"><p>one</p><p>two</p><p other>three</p></div>'; | 
					
						
							|  |  |  |         expect(_humanizeMessages(html)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               '<ph tag name="START_PARAGRAPH">one</ph name="CLOSE_PARAGRAPH">', | 
					
						
							|  |  |  |               '<ph tag name="START_PARAGRAPH">two</ph name="CLOSE_PARAGRAPH">', | 
					
						
							|  |  |  |               '<ph tag name="START_PARAGRAPH_1">three</ph name="CLOSE_PARAGRAPH">', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           ], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(_humanizePlaceholders(html)).toEqual([ | 
					
						
							|  |  |  |           'START_PARAGRAPH=<p>, CLOSE_PARAGRAPH=</p>, START_PARAGRAPH_1=<p other>', | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |       it('should reuse the same placeholder name for interpolations', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         const html = '<div i18n="m|d">{{ a }}{{ a }}{{ b }}</div>'; | 
					
						
							|  |  |  |         expect(_humanizeMessages(html)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               '[<ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION_1"> b </ph>]' | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |           ], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(_humanizePlaceholders(html)).toEqual([ | 
					
						
							|  |  |  |           'INTERPOLATION={{ a }}, INTERPOLATION_1={{ b }}', | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |       it('should reuse the same placeholder name for icu messages', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         const html = | 
					
						
							|  |  |  |             '<div i18n="m|d">{count, plural, =0 {0}}{count, plural, =0 {0}}{count, plural, =1 {1}}</div>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(_humanizeMessages(html)).toEqual([ | 
					
						
							|  |  |  |           [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               '<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>', | 
					
						
							|  |  |  |               '<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>', | 
					
						
							|  |  |  |               '<ph icu name="ICU_1">{count, plural, =1 {[1]}}</ph>', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |             'm', 'd', '' | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |           ], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |           [['{count, plural, =0 {[0]}}'], '', '', ''], | 
					
						
							|  |  |  |           [['{count, plural, =0 {[0]}}'], '', '', ''], | 
					
						
							|  |  |  |           [['{count, plural, =1 {[1]}}'], '', '', ''], | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 17:40:15 -07:00
										 |  |  |         expect(_humanizePlaceholders(html)).toEqual([ | 
					
						
							|  |  |  |           '', | 
					
						
							|  |  |  |           'VAR_PLURAL=count', | 
					
						
							|  |  |  |           'VAR_PLURAL=count', | 
					
						
							|  |  |  |           'VAR_PLURAL=count', | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-08-09 21:05:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  |         expect(_humanizePlaceholdersToMessage(html)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-08-09 21:05:04 -07:00
										 |  |  |           'ICU=f0f76923009914f1b05f41042a5c7231b9496504, ICU_1=73693d1f78d0fc882f0bcbce4cb31a0aa1995cfe', | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |           '', | 
					
						
							|  |  |  |           '', | 
					
						
							|  |  |  |           '', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 11:58:45 -08:00
										 |  |  | export function _humanizeMessages( | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |     html: string, implicitTags: string[] = [], | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |     implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string, string][] { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   // clang-format off
 | 
					
						
							|  |  |  |   // https://github.com/angular/clang-format/issues/35
 | 
					
						
							|  |  |  |   return _extractMessages(html, implicitTags, implicitAttrs).map( | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:56 +00:00
										 |  |  |     message => [serializeNodes(message.nodes), message.meaning, message.description, message.id]) as [string[], string, string, string][]; | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   // 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( | 
					
						
							| 
									
										
										
										
											2020-10-01 00:17:21 +02:00
										 |  |  |     msg => Object.keys(msg.placeholders).map((name) => `${name}=${msg.placeholders[name].text}`).join(', ')); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   // clang-format on
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  | function _humanizePlaceholdersToMessage( | 
					
						
							| 
									
										
										
										
											2016-08-09 21:05:04 -07:00
										 |  |  |     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( | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  |     msg => Object.keys(msg.placeholderToMessage).map(k => `${k}=${digest(msg.placeholderToMessage[k])}`).join(', ')); | 
					
						
							| 
									
										
										
										
											2016-08-09 21:05:04 -07:00
										 |  |  |   // clang-format on
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 11:58:45 -08:00
										 |  |  | export function _extractMessages( | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     html: string, implicitTags: string[] = [], | 
					
						
							|  |  |  |     implicitAttrs: {[k: string]: string[]} = {}): Message[] { | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |   const htmlParser = new HtmlParser(); | 
					
						
							| 
									
										
										
										
											2019-02-08 22:10:19 +00:00
										 |  |  |   const parseResult = htmlParser.parse(html, 'extractor spec', {tokenizeExpansionForms: true}); | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  |   if (parseResult.errors.length > 1) { | 
					
						
							|  |  |  |     throw Error(`unexpected parse errors: ${parseResult.errors.join('\n')}`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 17:46:31 -07:00
										 |  |  |   return extractMessages( | 
					
						
							|  |  |  |              parseResult.rootNodes, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs) | 
					
						
							|  |  |  |       .messages; | 
					
						
							| 
									
										
										
										
											2016-07-08 16:46:49 -07:00
										 |  |  | } |