Fixes #679 - wrong rebasing of remote URLs.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 12 Oct 2015 07:28:07 +0000 (08:28 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 14 Oct 2015 13:26:15 +0000 (14:26 +0100)
There was an edge case when a remote stylesheet pointed to a URL
in a different domain. Introduced in #667.

History.md
lib/urls/rewrite.js
test/protocol-imports-test.js

index fd6f0dd..1793283 100644 (file)
@@ -3,6 +3,11 @@
 
 * Requires Node.js 4.0+ to run.
 
+[3.4.6 / 2015-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.5...3.4)
+==================
+
+* Fixed issue [#679](https://github.com/jakubpawlowicz/clean-css/issues/679) - wrong rebasing of remote URLs.
+
 [3.4.5 / 2015-09-28](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.4...v3.4.5)
 ==================
 
index f2f1152..13a57e2 100644 (file)
@@ -25,6 +25,11 @@ function isRemote(uri) {
   return /^[^:]+?:\/\//.test(uri) || uri.indexOf('//') === 0;
 }
 
+function isSameOrigin(uri1, uri2) {
+  return url.parse(uri1).protocol == url.parse(uri2).protocol &&
+    url.parse(uri1).host == url.parse(uri2).host;
+}
+
 function isImport(uri) {
   return uri.lastIndexOf('.css') === uri.length - 4;
 }
@@ -63,6 +68,9 @@ function rebase(uri, options) {
   if (isRemote(uri) && !isRemote(options.toBase))
     return uri;
 
+  if (isRemote(uri) && !isSameOrigin(uri, options.toBase))
+    return uri;
+
   if (!isRemote(uri) && isRemote(options.toBase))
     return url.resolve(options.toBase, uri);
 
index 502a4c5..35fa86a 100644 (file)
@@ -239,6 +239,25 @@ vows.describe('protocol imports').addBatch({
       nock.cleanAll();
     }
   },
+  'of an existing file with absolute URLs in different domain': {
+    topic: function () {
+      this.reqMocks = nock('http://127.0.0.1')
+        .get('/base.css')
+        .reply(200, 'a{background:url(http://example.com/deeply/images/test.png)}');
+
+      new CleanCSS().minify('@import url(http://127.0.0.1/base.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(http://example.com/deeply/images/test.png)}');
+    },
+    teardown: function () {
+      assert.isTrue(this.reqMocks.isDone());
+      nock.cleanAll();
+    }
+  },
   'of an unreachable domain': {
     topic: function () {
       new CleanCSS().minify('@import url(http://0.0.0.0/custom.css);a{color:red}', this.callback);