Fixes #966 - remote `@import`s referenced from local ones.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 2 Sep 2017 18:35:26 +0000 (20:35 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sat, 2 Sep 2017 18:46:22 +0000 (20:46 +0200)
This is an edge case when local `@import` was given by hash and
referenced a remote one which was processed synchronously but should
have been asynchronously.

History.md
lib/reader/read-sources.js
test/protocol-imports-test.js

index 67ffe34..643b939 100644 (file)
@@ -12,6 +12,7 @@
 * Fixed issue [#959](https://github.com/jakubpawlowicz/clean-css/issues/959) - regression in shortening long hex values.
 * Fixed issue [#960](https://github.com/jakubpawlowicz/clean-css/issues/960) - better explanation of `efficiency` stat.
 * Fixed issue [#965](https://github.com/jakubpawlowicz/clean-css/issues/965) - edge case in parsing comment endings.
+* Fixed issue [#966](https://github.com/jakubpawlowicz/clean-css/issues/966) - remote `@import`s referenced from local ones.
 
 [4.1.7 / 2017-07-14](https://github.com/jakubpawlowicz/clean-css/compare/v4.1.6...v4.1.7)
 ==================
index c9173ed..1338f6a 100644 (file)
@@ -288,7 +288,6 @@ function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
     path.resolve(inlinerContext.rebaseTo, uri);
   var relativeToCurrentPath = path.relative(currentPath, absoluteUri);
   var importedStyles;
-  var importedTokens;
   var isAllowed = isAllowedResource(uri, false, inlinerContext.inline);
   var normalizedPath = normalizePath(relativeToCurrentPath);
   var isLoaded = normalizedPath in inlinerContext.externalContext.sourcesContent;
@@ -316,10 +315,14 @@ function inlineLocalStylesheet(uri, mediaQuery, metadata, inlinerContext) {
     inlinerContext.externalContext.sourcesContent[normalizedPath] = importedStyles;
     inlinerContext.externalContext.stats.originalSize += importedStyles.length;
 
-    importedTokens = fromStyles(importedStyles, inlinerContext.externalContext, inlinerContext, function (tokens) { return tokens; });
-    importedTokens = wrapInMedia(importedTokens, mediaQuery, metadata);
+    return fromStyles(importedStyles, inlinerContext.externalContext, inlinerContext, function (importedTokens) {
+      importedTokens = wrapInMedia(importedTokens, mediaQuery, metadata);
 
-    inlinerContext.outputTokens = inlinerContext.outputTokens.concat(importedTokens);
+      inlinerContext.outputTokens = inlinerContext.outputTokens.concat(importedTokens);
+      inlinerContext.sourceTokens = inlinerContext.sourceTokens.slice(1);
+
+      return doInlineImports(inlinerContext);
+    });
   }
 
   inlinerContext.sourceTokens = inlinerContext.sourceTokens.slice(1);
index 8144c44..2d315f4 100644 (file)
@@ -472,6 +472,27 @@ vows.describe('protocol imports').addBatch({
       nock.cleanAll();
     }
   },
+  'of a remote resource referenced from local one given via hash': {
+    topic: function () {
+      this.reqMocks = nock('http://127.0.0.1')
+        .get('/remote.css')
+        .reply(200, 'div{padding:0}');
+
+      new CleanCSS({ inline: 'all' }).minify({ 'local.css': { styles: '@import url(http://127.0.0.1/remote.css);' } }, this.callback);
+    },
+    'should not raise errors': function (errors, minified) {
+      assert.isNull(errors);
+    },
+    'should process @import': function (errors, minified) {
+      assert.equal(minified.styles, 'div{padding:0}');
+    },
+    'hits the endpoint': function () {
+      assert.isTrue(this.reqMocks.isDone());
+    },
+    teardown: function () {
+      nock.cleanAll();
+    }
+  },
   'of a remote resource after content and no callback': {
     topic: function () {
       var source = '.one{color:red}@import url(http://127.0.0.1/remote.css);';