| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | // Some of the code comes from WebComponents.JS
 | 
					
						
							|  |  |  | // https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {UrlResolver} from './url_resolver'; | 
					
						
							| 
									
										
										
										
											2015-08-25 15:36:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class StyleWithImports { | 
					
						
							|  |  |  |   constructor(public style: string, public styleUrls: string[]) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  | export function isStyleUrlResolvable(url: string): boolean { | 
					
						
							| 
									
										
										
										
											2017-02-10 18:23:58 -06:00
										 |  |  |   if (url == null || url.length === 0 || url[0] == '/') return false; | 
					
						
							| 
									
										
										
										
											2016-12-21 04:51:02 +03:00
										 |  |  |   const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP); | 
					
						
							| 
									
										
										
										
											2016-08-05 09:50:49 -07:00
										 |  |  |   return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset'; | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Rewrites stylesheets by resolving and removing the @import urls that | 
					
						
							|  |  |  |  * are either relative or don't have a `package:` scheme | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | export function extractStyleUrls( | 
					
						
							|  |  |  |     resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const foundUrls: string[] = []; | 
					
						
							| 
									
										
										
										
											2016-12-21 04:51:02 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 10:14:25 +08:00
										 |  |  |   const modifiedCssText = cssText.replace(CSS_STRIPPABLE_COMMENT_REGEXP, '') | 
					
						
							|  |  |  |                               .replace(CSS_IMPORT_REGEXP, (...m: string[]) => { | 
					
						
							|  |  |  |                                 const url = m[1] || m[2]; | 
					
						
							|  |  |  |                                 if (!isStyleUrlResolvable(url)) { | 
					
						
							|  |  |  |                                   // Do not attempt to resolve non-package absolute URLs with URI
 | 
					
						
							|  |  |  |                                   // scheme
 | 
					
						
							|  |  |  |                                   return m[0]; | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                                 foundUrls.push(resolver.resolve(baseUrl, url)); | 
					
						
							|  |  |  |                                 return ''; | 
					
						
							|  |  |  |                               }); | 
					
						
							| 
									
										
										
										
											2015-10-14 09:39:40 -07:00
										 |  |  |   return new StyleWithImports(modifiedCssText, foundUrls); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:07:31 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-21 04:51:02 +03:00
										 |  |  | const CSS_IMPORT_REGEXP = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g; | 
					
						
							| 
									
										
										
										
											2017-08-12 10:14:25 +08:00
										 |  |  | const CSS_STRIPPABLE_COMMENT_REGEXP = /\/\*(?!#\s*(?:sourceURL|sourceMappingURL)=)[\s\S]+?\*\//g; | 
					
						
							| 
									
										
										
										
											2016-12-21 04:51:02 +03:00
										 |  |  | const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/; |