From 73b7d99dc495aafe19cd2a091e5114e19e50026a Mon Sep 17 00:00:00 2001 From: yjbanov Date: Tue, 28 Jul 2015 18:38:35 -0700 Subject: [PATCH] fix(style_url_resolver): fix data: url resolution --- modules/angular2/src/facade/lang.ts | 5 ++++- .../render/dom/compiler/style_url_resolver.ts | 10 +++++++-- modules/angular2/test/facade/lang_spec.ts | 8 +++++++ .../dom/compiler/style_url_resolver_spec.ts | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/facade/lang.ts b/modules/angular2/src/facade/lang.ts index 94da79057f..be0a883816 100644 --- a/modules/angular2/src/facade/lang.ts +++ b/modules/angular2/src/facade/lang.ts @@ -263,7 +263,10 @@ export class RegExpWrapper { regExp.lastIndex = 0; return regExp.exec(input); } - static test(regExp: RegExp, input: string): boolean { return regExp.test(input); } + static test(regExp: RegExp, input: string): boolean { + regExp.lastIndex = 0; + return regExp.test(input); + } static matcher(regExp: RegExp, input: string): { re: RegExp; input: string diff --git a/modules/angular2/src/render/dom/compiler/style_url_resolver.ts b/modules/angular2/src/render/dom/compiler/style_url_resolver.ts index c72a41774d..c794a000ad 100644 --- a/modules/angular2/src/render/dom/compiler/style_url_resolver.ts +++ b/modules/angular2/src/render/dom/compiler/style_url_resolver.ts @@ -2,7 +2,7 @@ // https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js import {Injectable} from 'angular2/di'; -import {RegExp, StringWrapper} from 'angular2/src/facade/lang'; +import {RegExp, RegExpWrapper, StringWrapper} from 'angular2/src/facade/lang'; import {UrlResolver} from 'angular2/src/services/url_resolver'; /** @@ -21,7 +21,12 @@ export class StyleUrlResolver { _replaceUrls(cssText: string, re: RegExp, baseUrl: string) { return StringWrapper.replaceAllMapped(cssText, re, (m) => { var pre = m[1]; - var url = StringWrapper.replaceAll(m[2], _quoteRe, ''); + var originalUrl = m[2]; + if (RegExpWrapper.test(_dataUrlRe, originalUrl)) { + // Do not attempt to resolve data: URLs + return m[0]; + } + var url = StringWrapper.replaceAll(originalUrl, _quoteRe, ''); var post = m[3]; var resolvedUrl = this._resolver.resolve(baseUrl, url); @@ -34,3 +39,4 @@ export class StyleUrlResolver { var _cssUrlRe = /(url\()([^)]*)(\))/g; var _cssImportRe = /(@import[\s]+(?!url\())['"]([^'"]*)['"](.*;)/g; var _quoteRe = /['"]/g; +var _dataUrlRe = /^['"]?data:/g; diff --git a/modules/angular2/test/facade/lang_spec.ts b/modules/angular2/test/facade/lang_spec.ts index 746eb049a3..e954165869 100644 --- a/modules/angular2/test/facade/lang_spec.ts +++ b/modules/angular2/test/facade/lang_spec.ts @@ -24,6 +24,14 @@ export function main() { expect(indexes).toEqual([1, 4, 8, 9]); }); + + it('should reset before it is reused', () => { + var re = /^['"]/g; + var str = "'"; + expect(RegExpWrapper.test(re, str)).toEqual(true); + // If not reset, the second attempt to test results in false + expect(RegExpWrapper.test(re, str)).toEqual(true); + }); }); describe('const', () => { diff --git a/modules/angular2/test/render/dom/compiler/style_url_resolver_spec.ts b/modules/angular2/test/render/dom/compiler/style_url_resolver_spec.ts index 1bfcdd5d61..31a0bd7942 100644 --- a/modules/angular2/test/render/dom/compiler/style_url_resolver_spec.ts +++ b/modules/angular2/test/render/dom/compiler/style_url_resolver_spec.ts @@ -70,5 +70,26 @@ export function main() { var resolvedCss = styleUrlResolver.resolveUrls(css, 'http://ng.io'); expect(resolvedCss).toEqual(expectedCss); }); + + it('should not strip quotes from inlined SVG styles', () => { + var css = ` + .selector { + background:rgb(55,71,79) url('data:image/svg+xml;utf8,'); + background:rgb(55,71,79) url("data:image/svg+xml;utf8,"); + background:rgb(55,71,79) url("/some/data:image"); + } + `; + + var expectedCss = ` + .selector { + background:rgb(55,71,79) url('data:image/svg+xml;utf8,'); + background:rgb(55,71,79) url("data:image/svg+xml;utf8,"); + background:rgb(55,71,79) url('http://ng.io/some/data:image'); + } + `; + + var resolvedCss = styleUrlResolver.resolveUrls(css, 'http://ng.io'); + expect(resolvedCss).toEqual(expectedCss); + }); }); }