Fixes #448 - rebasing no protocol URIs.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 4 May 2015 06:22:45 +0000 (07:22 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 4 May 2015 06:26:52 +0000 (07:26 +0100)
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
lib/imports/inliner.js
lib/urls/rewrite.js
test/protocol-imports-test.js

index 43093b6..9d48851 100644 (file)
@@ -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)
 ==================
index 762ace7..1a77b57 100644 (file)
@@ -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);
index c4c52bd..af57500 100644 (file)
@@ -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) {
index 22a38f5..0e0c65e 100644 (file)
@@ -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')