Skip to content

Commit 3c3be4d

Browse files
author
Gleb Mikheev
committed
Merge pull request #16 from setage/background-color-prop-bug/#8
Background color prop bug/#8
2 parents b055cfc + a26b203 commit 3c3be4d

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

CHANGELOG.md

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#0.1.4
22

3-
- Fixes deprecations warnings (Thanks for [Eugene @setage](http://github.com/setage))
3+
- Fixes deprecations warnings (Thanks for [Eugene Romanovsky @setage](http://github.com/setage))
44

55

66
#0.1.3

index.js

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var GROUP_MASK = '*';
2929
var BACKGROUND = 'background';
3030
var BACKGROUND_IMAGE = 'background-image';
3131

32-
module.exports = postcss.plugin('postcss-easysprites', function(opts) {
32+
module.exports = postcss.plugin('postcss-easysprites', function (opts) {
3333
opts = opts || {};
3434

3535
// opts
@@ -42,29 +42,29 @@ module.exports = postcss.plugin('postcss-easysprites', function(opts) {
4242
opts.spritePath = path.resolve(process.cwd(), opts.spritePath || '');
4343

4444
// Group retina images
45-
opts.groupBy.unshift(function(image) {
45+
opts.groupBy.unshift(function (image) {
4646
if (image.ratio > 1) { return '@' + image.ratio + 'x'; }
4747

4848
return null;
4949
});
5050

51-
return function(css) {
51+
return function (css) {
5252
// if file path
5353

5454
return Q
5555

5656
// prepare part
5757
.all([collectImages(css, opts), opts])
5858
.spread(applyGroupBy)
59-
.spread(function(images, opts) {
59+
.spread(function (images, opts) {
6060
return setTokens(images, opts, css);
6161
})
6262

6363
// compilation part
6464
.spread(runSpriteSmith)
6565
.spread(saveSprites)
6666
.spread(mapSpritesProperties)
67-
.spread(function(images, opts, sprites) {
67+
.spread(function (images, opts, sprites) {
6868
return updateReferences(images, opts, sprites, css);
6969
});
7070
};
@@ -77,7 +77,7 @@ function collectImages(css, opts) {
7777
throw 'Stylesheets path is undefined, please use option stylesheetPath!';
7878
}
7979

80-
css.walkRules(function(rule) {
80+
css.walkRules(function (rule) {
8181
var image = {
8282
path: null,
8383
url: null,
@@ -115,7 +115,7 @@ function collectImages(css, opts) {
115115
log('Easysprites:', gutil.colors.red(image.path), 'file unreachable or not exists');
116116

117117
// remove hash from link
118-
lodash.each(rule.nodes, function(node) {
118+
lodash.each(rule.nodes, function (node) {
119119
node.value = node.value.replace('#' + image.hash, '');
120120
});
121121

@@ -130,11 +130,11 @@ function collectImages(css, opts) {
130130
}
131131

132132
function applyGroupBy(images, opts) {
133-
return Q.Promise(function(resolve, reject) {
134-
async.reduce(opts.groupBy, images, function(images, group, next) {
135-
async.map(images, function(image, done) {
133+
return Q.Promise(function (resolve, reject) {
134+
async.reduce(opts.groupBy, images, function (images, group, next) {
135+
async.map(images, function (image, done) {
136136
new Q(group(image))
137-
.then(function(group) {
137+
.then(function (group) {
138138
if (group) {
139139
image.groups.push(group);
140140
}
@@ -143,7 +143,7 @@ function applyGroupBy(images, opts) {
143143
})
144144
.catch(done);
145145
}, next);
146-
}, function(err, images) {
146+
}, function (err, images) {
147147

148148
if (err) {
149149
return reject(err);
@@ -155,8 +155,8 @@ function applyGroupBy(images, opts) {
155155
}
156156

157157
function setTokens(images, opts, css) {
158-
return Q.Promise(function(resolve) {
159-
css.walkDecls(/^background(-image)?$/, function(decl) {
158+
return Q.Promise(function (resolve) {
159+
css.walkDecls(/^background(-image)?$/, function (decl) {
160160
var rule = decl.parent;
161161
var url, image, color;
162162

@@ -170,8 +170,8 @@ function setTokens(images, opts, css) {
170170
// We remove these declarations since
171171
// our plugin will insert them when
172172
// they are necessary.
173-
rule.walkDecls(/^background-(repeat|size|position)$/, function(decl) {
174-
decl.removeSelf();
173+
rule.walkDecls(/^background-(repeat|size|position)$/, function (decl) {
174+
decl.remove();
175175
});
176176

177177
if (decl.prop === BACKGROUND) {
@@ -181,7 +181,7 @@ function setTokens(images, opts, css) {
181181
if (color && color.length === 1) {
182182
rule.prop = 'background-color';
183183
rule.value = color[0];
184-
rule.raws.before = ' ';
184+
rule.before = ' ';
185185
}
186186
}
187187

@@ -191,7 +191,7 @@ function setTokens(images, opts, css) {
191191
raws: {
192192
before: ' ',
193193
left: '@replace|',
194-
right: ''
194+
right: '',
195195
},
196196
});
197197

@@ -208,18 +208,18 @@ function setTokens(images, opts, css) {
208208
}
209209

210210
function runSpriteSmith(images, opts) {
211-
return Q.Promise(function(resolve, reject) {
211+
return Q.Promise(function (resolve, reject) {
212212
var all = lodash
213213
.chain(images)
214-
.groupBy(function(image) {
214+
.groupBy(function (image) {
215215
var temp;
216216

217217
temp = image.groups.map(mask(true));
218218
temp.unshift('_');
219219

220220
return temp.join(GROUP_DELIMITER);
221221
})
222-
.map(function(images, temp) {
222+
.map(function (images, temp) {
223223
var config = lodash.merge({}, opts, {
224224
src: lodash.pluck(images, 'path'),
225225
});
@@ -242,7 +242,7 @@ function runSpriteSmith(images, opts) {
242242

243243
// collect images datechanged
244244
config.spriteName = temp.replace(/^_./, '').replace(/.@/, '@');
245-
lodash.each(config.src, function(image) {
245+
lodash.each(config.src, function (image) {
246246
checkstring.push(image + '=' + md5(fs.readFileSync(image).toString()));
247247
});
248248

@@ -259,7 +259,7 @@ function runSpriteSmith(images, opts) {
259259
}
260260

261261
return Q.nfcall(spritesmith, config)
262-
.then(function(result) {
262+
.then(function (result) {
263263
temp = temp.split(GROUP_DELIMITER);
264264
temp.shift();
265265

@@ -282,10 +282,10 @@ function runSpriteSmith(images, opts) {
282282
.value();
283283

284284
Q.all(all)
285-
.then(function(results) {
285+
.then(function (results) {
286286
resolve([images, opts, results]);
287287
})
288-
.catch(function(err) {
288+
.catch(function (err) {
289289
if (err) {
290290
reject(err);
291291
}
@@ -294,15 +294,15 @@ function runSpriteSmith(images, opts) {
294294
}
295295

296296
function saveSprites(images, opts, sprites) {
297-
return Q.Promise(function(resolve, reject) {
297+
return Q.Promise(function (resolve, reject) {
298298

299299
if (!fs.existsSync(opts.spritePath)) {
300300
mkdirp.sync(opts.spritePath);
301301
}
302302

303303
var all = lodash
304304
.chain(sprites)
305-
.map(function(sprite) {
305+
.map(function (sprite) {
306306
sprite.path = makeSpritePath(opts, sprite.groups);
307307

308308
// if this file is up to date
@@ -315,17 +315,17 @@ function saveSprites(images, opts, sprites) {
315315

316316
// save new file version
317317
return Q.nfcall(fs.writeFile, sprite.path, new Buffer(sprite.image, 'binary'))
318-
.then(function() {
318+
.then(function () {
319319
log('Easysprites:', gutil.colors.yellow(sprite.path), 'generated.');
320320
return sprite;
321321
});
322322
})
323323
.value();
324324

325325
Q.all(all)
326-
.then(function(sprites) {
326+
.then(function (sprites) {
327327
resolve([images, opts, sprites]); })
328-
.catch(function(err) {
328+
.catch(function (err) {
329329
if (err) {
330330
reject(err);
331331
}
@@ -342,9 +342,9 @@ function saveSprites(images, opts, sprites) {
342342
* @return {Promise}
343343
*/
344344
function mapSpritesProperties(images, opts, sprites) {
345-
return Q.Promise(function(resolve) {
346-
sprites = lodash.map(sprites, function(sprite) {
347-
return lodash.map(sprite.coordinates, function(coordinates, imagePath) {
345+
return Q.Promise(function (resolve) {
346+
sprites = lodash.map(sprites, function (sprite) {
347+
return lodash.map(sprite.coordinates, function (coordinates, imagePath) {
348348

349349
return lodash.merge(lodash.find(images, { path: imagePath }), {
350350
coordinates: coordinates,
@@ -359,8 +359,8 @@ function mapSpritesProperties(images, opts, sprites) {
359359
}
360360

361361
function updateReferences(images, opts, sprites, css) {
362-
return Q.Promise(function(resolve) {
363-
css.walkComments(function(comment) {
362+
return Q.Promise(function (resolve) {
363+
css.walkComments(function (comment) {
364364
var rule, image, backgroundImage, backgroundPosition, backgroundSize;
365365

366366
// Manipulate only token comments
@@ -388,11 +388,16 @@ function updateReferences(images, opts, sprites, css) {
388388
// Output the dimensions
389389
rule = backgroundImage.parent;
390390
if (opts.outputDimensions) {
391-
['height', 'width'].forEach(function(prop) {
392-
rule.insertAfter(backgroundImage, postcss.decl({
393-
prop: prop,
394-
value: (image.ratio > 1 ? image.coordinates[prop] / image.ratio : image.coordinates[prop]) + 'px',
395-
}));
391+
['height', 'width'].forEach(function (prop) {
392+
rule.insertAfter(
393+
backgroundImage,
394+
postcss.decl({
395+
prop: prop,
396+
value: (image.ratio > 1 ?
397+
image.coordinates[prop] / image.ratio :
398+
image.coordinates[prop]) + 'px',
399+
})
400+
);
396401
});
397402
}
398403

@@ -424,7 +429,7 @@ function mask(toggle) {
424429
var input = new RegExp('[' + (toggle ? GROUP_DELIMITER : GROUP_MASK) + ']', 'gi');
425430
var output = toggle ? GROUP_MASK : GROUP_DELIMITER;
426431

427-
return function(value) {
432+
return function (value) {
428433
return value.replace(input, output);
429434
};
430435
}
@@ -473,7 +478,7 @@ function getColor(decl) {
473478
var regexes = ['(#([0-9a-f]{3}){1,2})', 'rgba?\\([^\\)]+\\)'];
474479
var matches = null;
475480

476-
lodash.forEach(regexes, function(regex) {
481+
lodash.forEach(regexes, function (regex) {
477482
regex = new RegExp(regex, 'gi');
478483

479484
if (regex.test(decl.value)) {
@@ -564,7 +569,7 @@ function getRetinaRatio(url) {
564569
* @return {Boolean}
565570
*/
566571
function areAllRetina(images) {
567-
return lodash.every(images, function(image) {
572+
return lodash.every(images, function (image) {
568573
return image.ratio > 1;
569574
});
570575
}

0 commit comments

Comments
 (0)