From 037a213cdbf87338b21f7af941692966794059cd Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 4 May 2015 07:22:45 +0100 Subject: [PATCH] Fixes #448 - rebasing no protocol URIs. When rebasing remote imports with URIs given without a protocol the resulting CSS should also use no protocol, e.g: ``` @import "//127.0.0.1/no-protocol.css"; ``` => ``` a{background:url(//127.0.0.1/image.png)} ``` --- History.md | 1 + lib/imports/inliner.js | 3 ++- lib/urls/rewrite.js | 2 +- test/protocol-imports-test.js | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 43093b64..9d488514 100644 --- a/History.md +++ b/History.md @@ -8,6 +8,7 @@ * Moves URL scanner into lib/urls/reduce (was named incorrectly before). * Moves URL rebasing & rewriting into lib/urls. * Fixed issue [#436](https://github.com/jakubpawlowicz/clean-css/issues/436) - refactors URI rewriting. +* Fixed issue [#448](https://github.com/jakubpawlowicz/clean-css/issues/448) - rebasing no protocol URIs. [3.2.7 / 2015-05-03](https://github.com/jakubpawlowicz/clean-css/compare/v3.2.6...v3.2.7) ================== diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index 762ace74..1a77b57c 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -215,6 +215,7 @@ function inlineRemoteResource(importedFile, mediaQuery, context) { var importedUrl = REMOTE_RESOURCE.test(importedFile) ? importedFile : url.resolve(context.relativeTo, importedFile); + var originalUrl = importedUrl; if (importedUrl.indexOf('//') === 0) importedUrl = 'http:' + importedUrl; @@ -263,7 +264,7 @@ function inlineRemoteResource(importedFile, mediaQuery, context) { res.on('end', function() { var importedData = chunks.join(''); if (context.rebase) - importedData = rewriteUrls(importedData, { toBase: importedUrl }, context); + importedData = rewriteUrls(importedData, { toBase: originalUrl }, context); context.sourceReader.trackSource(importedUrl, importedData); importedData = context.sourceTracker.store(importedUrl, importedData); importedData = rebaseMap(importedData, importedUrl); diff --git a/lib/urls/rewrite.js b/lib/urls/rewrite.js index c4c52bda..af575008 100644 --- a/lib/urls/rewrite.js +++ b/lib/urls/rewrite.js @@ -18,7 +18,7 @@ function isEscaped(uri) { } function isRemote(uri) { - return uri.indexOf('http://') === 0 || uri.indexOf('https://') === 0; + return uri.indexOf('http://') === 0 || uri.indexOf('https://') === 0 || uri.indexOf('//') === 0; } function isImport(uri) { diff --git a/test/protocol-imports-test.js b/test/protocol-imports-test.js index 22a38f59..0e0c65e6 100644 --- a/test/protocol-imports-test.js +++ b/test/protocol-imports-test.js @@ -331,6 +331,44 @@ vows.describe('protocol imports').addBatch({ nock.cleanAll(); } }, + 'of a resource without protocol with rebase': { + topic: function() { + this.reqMocks = nock('http://127.0.0.1') + .get('/no-protocol.css') + .reply(200, 'a{background:url(image.png)}'); + + new CleanCSS().minify('@import url(//127.0.0.1/no-protocol.css);', this.callback); + }, + 'should not raise errors': function(errors, minified) { + assert.isNull(errors); + }, + 'should process @import': function(errors, minified) { + assert.equal(minified.styles, 'a{background:url(//127.0.0.1/image.png)}'); + }, + teardown: function() { + assert.isTrue(this.reqMocks.isDone()); + nock.cleanAll(); + } + }, + 'of a resource without protocol with rebase to another domain': { + topic: function() { + this.reqMocks = nock('http://127.0.0.1') + .get('/no-protocol.css') + .reply(200, 'a{background:url(//127.0.0.2/image.png)}'); + + new CleanCSS().minify('@import url(http://127.0.0.1/no-protocol.css);', this.callback); + }, + 'should not raise errors': function(errors, minified) { + assert.isNull(errors); + }, + 'should process @import': function(errors, minified) { + assert.equal(minified.styles, 'a{background:url(//127.0.0.2/image.png)}'); + }, + teardown: function() { + assert.isTrue(this.reqMocks.isDone()); + nock.cleanAll(); + } + }, 'of a resource available via POST only': { topic: function() { this.reqMocks = nock('http://127.0.0.1') -- 2.34.1