From: Jakub Pawlowicz Date: Tue, 9 Dec 2014 22:06:27 +0000 (+0000) Subject: Fixes #352 - honors `rebase: false` / `--skip-rebase` in imported stylesheets. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=260e0737f44085f8c4288a5f049853c9f6c2554d;p=clean-css.git Fixes #352 - honors `rebase: false` / `--skip-rebase` in imported stylesheets. --- diff --git a/History.md b/History.md index 119afcaf..8fb2b5d2 100644 --- a/History.md +++ b/History.md @@ -18,6 +18,7 @@ * Speeds up advanced processing by shortening optimize loop. * Fixed issue [#125](https://github.com/GoalSmashers/clean-css/issues/125) - source maps! * Fixed issue [#344](https://github.com/GoalSmashers/clean-css/issues/344) - merging background-size into shorthand. +* Fixed issue [#352](https://github.com/GoalSmashers/clean-css/issues/352) - honors rebasing in imported stylesheets. * Fixed issue [#360](https://github.com/GoalSmashers/clean-css/issues/360) - adds 7 extra CSS colors. * Fixed issue [#363](https://github.com/GoalSmashers/clean-css/issues/363) - `rem` units overriding `px`. * Fixed issue [#395](https://github.com/GoalSmashers/clean-css/issues/395) - unescaped brackets in data URIs. diff --git a/lib/clean.js b/lib/clean.js index 4374f574..dbb02aeb 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -65,7 +65,7 @@ CleanCSS.prototype.minify = function(data, callback) { function (callback) { return callback(); }; return runner(function () { - return new ImportInliner(self.context, options.inliner).process(data, { + return new ImportInliner(self.context, options.inliner, options.rebase).process(data, { localOnly: !callback, root: options.root || process.cwd(), relativeTo: options.relativeTo, diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index fc1a3cc2..ddf84f94 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -34,7 +34,7 @@ function wrap(data, source) { '__ESCAPED_SOURCE_END_CLEAN_CSS__'; } -module.exports = function Inliner(context, options) { +module.exports = function Inliner(context, options, rebase) { var defaultOptions = { timeout: 5000, request: {} @@ -60,6 +60,7 @@ module.exports = function Inliner(context, options) { var isComment = commentScanner(data); var afterContent = contentScanner(data); + options.rebase = rebase; options.relativeTo = options.relativeTo || options.root; options._baseRelativeTo = options._baseRelativeTo || options.relativeTo; options.visited = options.visited || []; @@ -263,7 +264,8 @@ module.exports = function Inliner(context, options) { }); res.on('end', function() { var importedData = chunks.join(''); - importedData = UrlRewriter.process(importedData, { toBase: importedUrl }); + if (options.rebase) + importedData = UrlRewriter.process(importedData, { toBase: importedUrl }); importedData = rebaseMap(wrap(importedData, importedUrl), importedUrl); if (mediaQuery.length > 0) @@ -309,13 +311,15 @@ module.exports = function Inliner(context, options) { options.visited.push(fullPath); - var importedData = fs.readFileSync(fullPath, 'utf8'); var importRelativeTo = path.dirname(fullPath); - importedData = UrlRewriter.process(importedData, { - relative: true, - fromBase: importRelativeTo, - toBase: options._baseRelativeTo - }); + var importedData = fs.readFileSync(fullPath, 'utf8'); + if (options.rebase) { + importedData = UrlRewriter.process(importedData, { + relative: true, + fromBase: importRelativeTo, + toBase: options._baseRelativeTo + }); + } importedData = wrap(importedData, path.resolve(options.relativeTo, fullPath)); if (mediaQuery.length > 0) diff --git a/test/binary-test.js b/test/binary-test.js index c580cbb6..9c3aeb11 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -235,9 +235,9 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({ absolute: binaryContext('-r ./test/data/129-assets --skip-rebase ./test/data/129-assets/assets/ui.css', { 'should rebase urls correctly': function(error, stdout) { assert.equal(error, null); - assert.include(stdout, 'url(../components/bootstrap/images/glyphs.gif)'); - assert.include(stdout, 'url(../components/jquery-ui/images/prev.gif)'); - assert.include(stdout, 'url(../components/jquery-ui/images/next.gif)'); + assert.include(stdout, 'url(../images/glyphs.gif)'); + assert.include(stdout, 'url(../images/prev.gif)'); + assert.include(stdout, 'url(../images/next.gif)'); } }) }, diff --git a/test/integration-test.js b/test/integration-test.js index f565f626..2900936a 100644 --- a/test/integration-test.js +++ b/test/integration-test.js @@ -1021,6 +1021,16 @@ path")}', target: path.join(process.cwd(), 'test.css'), relativeTo: path.join('test', 'data', 'partials-relative') }), + 'urls rewriting - rebase off': cssContext({ + 'keeps urls the same': [ + '@import url(base.css);', + 'a{background:url(../partials/extra/down.gif) no-repeat}' + ], + }, { + target: path.join(process.cwd(), 'test.css'), + relativeTo: path.join('test', 'data', 'partials-relative'), + rebase: false + }), 'fonts': cssContext({ 'keep format quotation': "@font-face{font-family:PublicVintage;src:url(/PublicVintage.otf) format('opentype')}", 'remove font family quotation': [ diff --git a/test/protocol-imports-test.js b/test/protocol-imports-test.js index ad25a53e..4e951c5c 100644 --- a/test/protocol-imports-test.js +++ b/test/protocol-imports-test.js @@ -215,6 +215,27 @@ vows.describe('protocol imports').addBatch({ nock.cleanAll(); } }, + 'of an existing file with relative URLs and rebase turned off': { + topic: function() { + this.reqMocks = nock('http://127.0.0.1') + .get('/base.css') + .reply(200, '@import url(deeply/nested/urls.css);') + .get('/deeply/nested/urls.css') + .reply(200, 'a{background:url(../images/test.png)}'); + + new CleanCSS({ rebase: false }).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(../images/test.png)}'); + }, + teardown: function() { + assert.equal(this.reqMocks.isDone(), true); + nock.cleanAll(); + } + }, 'of a non-resolvable domain': { topic: function() { new CleanCSS().minify('@import url(http://notdefined.127.0.0.1/custom.css);a{color:red}', this.callback);