| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {extractStyleUrls, isStyleUrlResolvable} from '@angular/compiler/src/style_url_resolver'; | 
					
						
							|  |  |  | import {UrlResolver} from '@angular/compiler/src/url_resolver'; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  |   describe('extractStyleUrls', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let urlResolver: UrlResolver; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     beforeEach(() => { urlResolver = new UrlResolver(); }); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  |     it('should not resolve "url()" urls', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       .foo { | 
					
						
							|  |  |  |         background-image: url("double.jpg"); | 
					
						
							|  |  |  |         background-image: url('simple.jpg'); | 
					
						
							|  |  |  |         background-image: url(noquote.jpg); | 
					
						
							|  |  |  |       }`;
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const resolvedCss = extractStyleUrls(urlResolver, 'http://ng.io', css).style; | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  |       expect(resolvedCss).toEqual(css); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     it('should extract "@import" urls', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       @import '1.css'; | 
					
						
							|  |  |  |       @import "2.css"; | 
					
						
							|  |  |  |       `;
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual(''); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).toEqual(['http://ng.io/1.css', 'http://ng.io/2.css']); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-21 04:51:02 +03:00
										 |  |  |     it('should ignore "@import" in comments', () => { | 
					
						
							|  |  |  |       const css = `
 | 
					
						
							|  |  |  |       @import '1.css'; | 
					
						
							|  |  |  |       /*@import '2.css';*/ | 
					
						
							|  |  |  |       `;
 | 
					
						
							|  |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							|  |  |  |       expect(styleWithImports.style.trim()).toEqual(''); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).toContain('http://ng.io/1.css'); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).not.toContain('http://ng.io/2.css'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     it('should extract "@import url()" urls', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       @import url('3.css'); | 
					
						
							|  |  |  |       @import url("4.css"); | 
					
						
							|  |  |  |       @import url(5.css); | 
					
						
							|  |  |  |       `;
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual(''); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(styleWithImports.styleUrls).toEqual([ | 
					
						
							|  |  |  |         'http://ng.io/3.css', 'http://ng.io/4.css', 'http://ng.io/5.css' | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     it('should extract "@import urls and keep rules in the same line', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `@import url('some.css');div {color: red};`; | 
					
						
							|  |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual('div {color: red};'); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).toEqual(['http://ng.io/some.css']); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |     it('should extract media query in "@import"', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       @import 'print1.css' print; | 
					
						
							|  |  |  |       @import url(print2.css) print; | 
					
						
							|  |  |  |       `;
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual(''); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(styleWithImports.styleUrls).toEqual([ | 
					
						
							|  |  |  |         'http://ng.io/print1.css', 'http://ng.io/print2.css' | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 13:37:56 -07:00
										 |  |  |     it('should leave absolute non-package @import urls intact', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `@import url('http://server.com/some.css');`; | 
					
						
							|  |  |  |       const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-10-07 13:37:56 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual(`@import url('http://server.com/some.css');`); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).toEqual([]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve package @import urls', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const css = `@import url('package:a/b/some.css');`; | 
					
						
							|  |  |  |       const styleWithImports = extractStyleUrls(new FakeUrlResolver(), 'http://ng.io', css); | 
					
						
							| 
									
										
										
										
											2015-10-07 13:37:56 -07:00
										 |  |  |       expect(styleWithImports.style.trim()).toEqual(``); | 
					
						
							|  |  |  |       expect(styleWithImports.styleUrls).toEqual(['fake_resolved_url']); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('isStyleUrlResolvable', () => { | 
					
						
							|  |  |  |     it('should resolve relative urls', | 
					
						
							|  |  |  |        () => { expect(isStyleUrlResolvable('someUrl.css')).toBe(true); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve package: urls', | 
					
						
							|  |  |  |        () => { expect(isStyleUrlResolvable('package:someUrl.css')).toBe(true); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not resolve empty urls', () => { | 
					
						
							|  |  |  |       expect(isStyleUrlResolvable(null)).toBe(false); | 
					
						
							|  |  |  |       expect(isStyleUrlResolvable('')).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not resolve urls with other schema', | 
					
						
							|  |  |  |        () => { expect(isStyleUrlResolvable('http://otherurl')).toBe(false); }); | 
					
						
							| 
									
										
										
										
											2015-10-15 10:17:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-10 18:23:58 -06:00
										 |  |  |     it('should not resolve urls with absolute paths', () => { | 
					
						
							|  |  |  |       expect(isStyleUrlResolvable('/otherurl')).toBe(false); | 
					
						
							|  |  |  |       expect(isStyleUrlResolvable('//otherurl')).toBe(false); | 
					
						
							| 
									
										
										
										
											2015-10-15 10:17:14 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-07 13:37:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class FakeUrlResolver extends UrlResolver { | 
					
						
							|  |  |  |   constructor() { super(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   resolve(baseUrl: string, url: string): string { return 'fake_resolved_url'; } | 
					
						
							|  |  |  | } |