| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xdescribe, | 
					
						
							|  |  |  |   xit | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | } from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {HtmlParser} from '@angular/compiler/src/html_parser'; | 
					
						
							|  |  |  | import {MessageExtractor, removeDuplicates} from '@angular/compiler/src/i18n/message_extractor'; | 
					
						
							|  |  |  | import {Message} from '@angular/compiler/src/i18n/message'; | 
					
						
							|  |  |  | import {Parser} from '@angular/compiler/src/expression_parser/parser'; | 
					
						
							|  |  |  | import {Lexer} from '@angular/compiler/src/expression_parser/lexer'; | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('MessageExtractor', () => { | 
					
						
							|  |  |  |     let extractor: MessageExtractor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       let htmlParser = new HtmlParser(); | 
					
						
							|  |  |  |       var parser = new Parser(new Lexer()); | 
					
						
							|  |  |  |       extractor = new MessageExtractor(htmlParser, parser); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract from elements with the i18n attr', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract("<div i18n='meaning|desc'>message</div>", "someurl"); | 
					
						
							|  |  |  |       expect(res.messages).toEqual([new Message("message", 'meaning', 'desc')]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract from elements with the i18n attr without a desc', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract("<div i18n='meaning'>message</div>", "someurl"); | 
					
						
							|  |  |  |       expect(res.messages).toEqual([new Message("message", 'meaning', null)]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract from elements with the i18n attr without a meaning', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract("<div i18n>message</div>", "someurl"); | 
					
						
							|  |  |  |       expect(res.messages).toEqual([new Message("message", null, null)]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract from attributes', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |         <div | 
					
						
							|  |  |  |           title1='message1' i18n-title1='meaning1|desc1' | 
					
						
							|  |  |  |           title2='message2' i18n-title2='meaning2|desc2'> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       `,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                   "someurl"); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message("message1", "meaning1", "desc1"), | 
					
						
							|  |  |  |             new Message("message2", "meaning2", "desc2") | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract from partitions', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |          <!-- i18n: meaning1|desc1 -->message1<!-- /i18n --> | 
					
						
							|  |  |  |          <!-- i18n: meaning2|desc2 -->message2<!-- /i18n -->`,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                   "someUrl"); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message("message1", "meaning1", "desc1"), | 
					
						
							|  |  |  |             new Message("message2", "meaning2", "desc2") | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should ignore other comments', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |          <!-- i18n: meaning1|desc1 --><!-- other -->message1<!-- /i18n -->`,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                   "someUrl"); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       expect(res.messages).toEqual([new Message("message1", "meaning1", "desc1")]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should replace interpolation with placeholders (text nodes)', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       let res = extractor.extract("<div i18n>Hi {{one}} and {{two}}</div>", "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual( | 
					
						
							|  |  |  |               [new Message('<ph name="t0">Hi <ph name="0"/> and <ph name="1"/></ph>', null, null)]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should replace interpolation with placeholders (attributes)', () => { | 
					
						
							|  |  |  |       let res = | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |           extractor.extract("<div title='Hi {{one}} and {{two}}' i18n-title></div>", "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([new Message('Hi <ph name="0"/> and <ph name="1"/>', null, null)]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-14 16:16:22 -07:00
										 |  |  |     it('should replace interpolation with named placeholders if provided (text nodes)', () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							|  |  |  |         <div i18n>Hi {{one //i18n(ph="FIRST")}} and {{two //i18n(ph="SECOND")}}</div>`,
 | 
					
						
							|  |  |  |                                   'someurl'); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message('<ph name="t0">Hi <ph name="FIRST"/> and <ph name="SECOND"/></ph>', null, | 
					
						
							|  |  |  |                         null) | 
					
						
							|  |  |  |           ]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should replace interpolation with named placeholders if provided (attributes)', () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							|  |  |  |       <div title='Hi {{one //i18n(ph="FIRST")}} and {{two //i18n(ph="SECOND")}}' | 
					
						
							|  |  |  |         i18n-title></div>`,
 | 
					
						
							|  |  |  |                                   'someurl'); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([new Message('Hi <ph name="FIRST"/> and <ph name="SECOND"/>', null, null)]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should match named placeholders with extra spacing', () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							|  |  |  |       <div title='Hi {{one // i18n ( ph = "FIRST" )}} and {{two // i18n ( ph = "SECOND" )}}' | 
					
						
							|  |  |  |         i18n-title></div>`,
 | 
					
						
							|  |  |  |                                   'someurl'); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([new Message('Hi <ph name="FIRST"/> and <ph name="SECOND"/>', null, null)]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should suffix duplicate placeholder names with numbers', () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							|  |  |  |       <div title='Hi {{one //i18n(ph="FIRST")}} and {{two //i18n(ph="FIRST")}} and {{three //i18n(ph="FIRST")}}' | 
					
						
							|  |  |  |         i18n-title></div>`,
 | 
					
						
							|  |  |  |                                   'someurl'); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message('Hi <ph name="FIRST"/> and <ph name="FIRST_1"/> and <ph name="FIRST_2"/>', | 
					
						
							|  |  |  |                         null, null) | 
					
						
							|  |  |  |           ]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     it("should handle html content", () => { | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |       let res = extractor.extract( | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |           '<div i18n><div attr="value">zero<div>one</div></div><div>two</div></div>', "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message('<ph name="e0">zero<ph name="e2">one</ph></ph><ph name="e4">two</ph>', null, | 
					
						
							|  |  |  |                         null) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     it("should handle html content with interpolation", () => { | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |       let res = | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |           extractor.extract('<div i18n><div>zero{{a}}<div>{{b}}</div></div></div>', "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message( | 
					
						
							|  |  |  |                 '<ph name="e0"><ph name="t1">zero<ph name="0"/></ph><ph name="e2"><ph name="t3"><ph name="0"/></ph></ph></ph>', | 
					
						
							|  |  |  |                 null, null) | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     it("should extract from nested elements", () => { | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |       let res = extractor.extract( | 
					
						
							|  |  |  |           '<div title="message1" i18n-title="meaning1|desc1"><div i18n="meaning2|desc2">message2</div></div>', | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |           "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message("message2", "meaning2", "desc2"), | 
					
						
							|  |  |  |             new Message("message1", "meaning1", "desc1") | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     it("should extract messages from attributes in i18n blocks", () => { | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |       let res = extractor.extract( | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |           '<div i18n><div attr="value" i18n-attr="meaning|desc">message</div></div>', "someurl"); | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message('<ph name="e0">message</ph>', null, null), | 
					
						
							|  |  |  |             new Message('value', "meaning", "desc") | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 11:46:49 -07:00
										 |  |  |     it("should extract messages from special forms", () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							|  |  |  |         <div> | 
					
						
							|  |  |  |           {messages.length, plural, | 
					
						
							|  |  |  |              =0 {You have <b>no</b> messages} | 
					
						
							|  |  |  |              =1 {You have one message} | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       `,
 | 
					
						
							|  |  |  |                                   "someurl"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.messages) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message('You have <ph name="e1">no</ph> messages', "plural_0", null), | 
					
						
							|  |  |  |             new Message('You have one message', "plural_1", null) | 
					
						
							|  |  |  |           ]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     it("should remove duplicate messages", () => { | 
					
						
							|  |  |  |       let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |          <!-- i18n: meaning|desc1 -->message<!-- /i18n --> | 
					
						
							|  |  |  |          <!-- i18n: meaning|desc2 -->message<!-- /i18n -->`,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                   "someUrl"); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       expect(removeDuplicates(res.messages)) | 
					
						
							|  |  |  |           .toEqual([ | 
					
						
							|  |  |  |             new Message("message", "meaning", "desc1"), | 
					
						
							|  |  |  |           ]); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     describe("errors", () => { | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |       it('should error on i18n attributes without matching "real" attributes', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |         let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |         <div | 
					
						
							|  |  |  |           title1='message1' i18n-title1='meaning1|desc1' i18n-title2='meaning2|desc2'> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       `,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                     "someurl"); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(res.errors.length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |         expect(res.errors[0].msg).toEqual("Missing attribute 'title2'."); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should error when cannot find a matching desc', () => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |         let res = extractor.extract(`
 | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |          <!-- i18n: meaning1|desc1 -->message1`,
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                                     "someUrl"); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(res.errors.length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |         expect(res.errors[0].msg).toEqual("Missing closing 'i18n' comment."); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |       it("should return parse errors when the template is invalid", () => { | 
					
						
							|  |  |  |         let res = extractor.extract("<input&#Besfs", "someurl"); | 
					
						
							| 
									
										
										
										
											2016-03-23 13:44:45 -07:00
										 |  |  |         expect(res.errors.length).toEqual(1); | 
					
						
							|  |  |  |         expect(res.errors[0].msg).toEqual('Unexpected character "s"'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-03-14 10:51:23 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |