Skip to content

Commit 22dc791

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@angular-devkit/build-angular): error when using protocol-relative url
Fixes #12648
1 parent 515553f commit 22dc791

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
5454
const dedupeSlashes = (url: string) => url.replace(/\/\/+/g, '/');
5555

5656
const process = async (inputUrl: string, context: string, resourceCache: Map<string, string>) => {
57-
// If root-relative or absolute, leave as is
58-
if (inputUrl.match(/^(?:\w+:\/\/|data:|chrome:|#)/)) {
57+
// If root-relative, absolute or protocol relative url, leave as is
58+
if (/^((?:\w+:)?\/\/|data:|chrome:|#)/.test(inputUrl)) {
5959
return inputUrl;
6060
}
61+
6162
// If starts with a caret, remove and return remainder
6263
// this supports bypassing asset processing
6364
if (inputUrl.startsWith('^')) {
@@ -74,7 +75,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
7475
inputUrl = inputUrl.substr(1);
7576
}
7677

77-
if (inputUrl.startsWith('/') && !inputUrl.startsWith('//')) {
78+
if (inputUrl.startsWith('/')) {
7879
let outputUrl = '';
7980
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
8081
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.

packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts

+21
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,25 @@ describe('Browser Builder styles', () => {
503503
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
504504
).toPromise().then(done, done.fail);
505505
});
506+
507+
it('supports Protocol-relative Url', (done) => {
508+
host.writeMultipleFiles({
509+
'src/styles.css': `
510+
body {
511+
background-image: url('//cdn.com/classic-bg.jpg');
512+
}
513+
`,
514+
});
515+
516+
const overrides = { extractCss: true, optimization: true };
517+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
518+
tap((buildEvent) => {
519+
expect(buildEvent.success).toBe(true);
520+
521+
const filePath = './dist/styles.css';
522+
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(filePath)));
523+
expect(content).toContain('background-image:url(//cdn.com/classic-bg.jpg)');
524+
}),
525+
).toPromise().then(done, done.fail);
526+
});
506527
});

0 commit comments

Comments
 (0)