Skip to content

Commit 4aa9f3e

Browse files
committed
Merge pull request #55 from michaelmoussa/abs-url-cssPath
Use url.resolve(...) instead of path.join(...) to support abs URLs
2 parents 147f2a2 + 60a01dc commit 4aa9f3e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/css-sprite.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var layout = require('layout');
1111
var replaceExtension = require('./replace-extension');
1212
var lwip = require('lwip');
1313
var Color = require('color');
14+
var url = require('url');
1415

1516
// json2css template
1617
json2css.addTemplate('sprite', require(path.join(__dirname, 'templates/sprite.js')));
@@ -110,7 +111,7 @@ module.exports = function (opt) {
110111
sprites.unshift({
111112
name: retinaSprite.relative,
112113
type: 'retina',
113-
image: (!opt.base64) ? path.join(opt.cssPath, retinaSprite.relative).replace(/\\/g, '/') : 'data:' + imageinfo(retinaSprite.buffer).mimeType + ';base64,' + retinaSprite.buffer.toString('base64'),
114+
image: (!opt.base64) ? url.resolve(opt.cssPath.replace(/\\/g, '/'), retinaSprite.relative) : 'data:' + imageinfo(retinaSprite.buffer).mimeType + ';base64,' + retinaSprite.buffer.toString('base64'),
114115
total_width: sprite.canvas.width(),
115116
total_height: sprite.canvas.height()
116117
});
@@ -126,7 +127,7 @@ module.exports = function (opt) {
126127
sprites.unshift({
127128
name: sprite.relative,
128129
type: 'sprite',
129-
image: (!opt.base64) ? path.join(opt.cssPath, sprite.relative).replace(/\\/g, '/') : 'data:' + imageinfo(sprite.buffer).mimeType + ';base64,' + sprite.buffer.toString('base64'),
130+
image: (!opt.base64) ? url.resolve(opt.cssPath.replace(/\\/g, '/'), sprite.relative) : 'data:' + imageinfo(sprite.buffer).mimeType + ';base64,' + sprite.buffer.toString('base64'),
130131
total_width: sprite.canvas.width,
131132
total_height: sprite.canvas.height
132133
});

test/css-sprite.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,30 @@ describe('css-sprite (lib/css-sprite.js)', function () {
213213
done();
214214
});
215215
});
216+
it('should return a css file with an absolute background img URL, if applicable', function (done) {
217+
var css;
218+
vfs.src('./test/fixtures/**')
219+
.pipe(sprite({
220+
out: './dist/img',
221+
name: 'sprites',
222+
style: './dist/css/sprites.css',
223+
cssPath: 'http://foo.bar'
224+
}))
225+
.pipe(through2.obj(function (file, enc, cb) {
226+
if (file.relative.indexOf('css') > -1) {
227+
css = file;
228+
}
229+
cb();
230+
}))
231+
.on('data', noop)
232+
.on('end', function () {
233+
css.should.be.ok;
234+
css.path.should.equal('./dist/css/sprites.css');
235+
css.relative.should.equal('sprites.css');
236+
css.contents.toString('utf-8').should.containEql('background-image: url(\'http://foo.bar/sprites.png\')');
237+
done();
238+
});
239+
});
216240
it('should return a object stream with a sprite and a scss file', function (done) {
217241
var png, css;
218242
vfs.src('./test/fixtures/**')

0 commit comments

Comments
 (0)