Skip to content

Commit

Permalink
Fixes #679 - wrong rebasing of remote URLs.
Browse files Browse the repository at this point in the history
There was an edge case when a remote stylesheet pointed to a URL
in a different domain. Introduced in #667.
  • Loading branch information
jakubpawlowicz committed Oct 14, 2015
1 parent e87920f commit 46f66d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions History.md
Expand Up @@ -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)
==================

Expand Down
8 changes: 8 additions & 0 deletions lib/urls/rewrite.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions test/protocol-imports-test.js
Expand Up @@ -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);
Expand Down

0 comments on commit 46f66d1

Please sign in to comment.