forked from MadLittleMods/gulp-css-spriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-over-styles-and-transform-background-image-declarations.js
106 lines (80 loc) · 3.83 KB
/
map-over-styles-and-transform-background-image-declarations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var extend = require('extend');
var spriterUtil = require('./spriter-util');
var getMetaInfoForDeclaration = require('./get-meta-info-for-declaration');
var transformMap = require('./transform-map');
function mapOverStylesAndTransformBackgroundImageDeclarations(styles, includeMode, cb) {
// Map over all
return mapOverStylesAndTransformAllBackgroundImageDeclarations(styles, function(declaration) {
// Then filter down to only the proper ones (according to their meta data)
if(shouldIncludeFactoringInMetaData(declaration.meta, includeMode)) {
return cb.apply(null, arguments);
}
});
}
// Boolean function to determine if the meta data permits using this declaration
function shouldIncludeFactoringInMetaData(meta, includeMode) {
var metaIncludeValue = (meta && meta.spritesheet && meta.spritesheet.include);
var shouldIncludeBecauseImplicit = includeMode === 'implicit' && (metaIncludeValue === undefined || metaIncludeValue);
var shouldIncludeBecauseExplicit = includeMode === 'explicit' && metaIncludeValue;
var shouldInclude = shouldIncludeBecauseImplicit || shouldIncludeBecauseExplicit;
// Only return declartions that shouldn't be skipped
return shouldInclude;
}
// Pass in a styles object from `css.parse`
// Loop over all of the styles and transform/modify the background image declarations
// Returns a new styles object that has the transformed declarations
function mapOverStylesAndTransformAllBackgroundImageDeclarations(styles, cb) {
function transformDeclaration(declaration, declarationIndex, declarations) {
// Clone the declartion to keep it immutable
var transformedDeclaration = extend(true, {}, declaration);
transformedDeclaration = attachInfoToDeclaration(declarations, declarationIndex);
// background-image always has a url
if(transformedDeclaration.property === 'background-image') {
return cb(transformedDeclaration, declarationIndex, declarations);
}
// Background is a shorthand property so make sure `url()` is in there
else if(transformedDeclaration.property === 'background') {
var hasImageValue = spriterUtil.backgroundURLRegex.test(transformedDeclaration.value);
if(hasImageValue) {
return cb(transformedDeclaration, declarationIndex, declarations);
}
}
// Wrap in an object so that the declaration doesn't get interpreted
return {
'value': transformedDeclaration
};
}
// Clone the declartion to keep it immutable
var transformedStyles = extend(true, {}, styles);
// Go over each background `url()` declarations
transformedStyles.stylesheet.rules.map(function(rule, ruleIndex) {
if(rule.type === 'rule') {
rule.declarations = transformMap(rule.declarations, transformDeclaration);
}
if(rule.type === 'keyframes') {
// Get keyframe from keyframes
rule.keyframes = transformMap(rule.keyframes, function(keyframe, keyframeIndex, keyframes) {
// Get declarations from keyframe
keyframe.declarations = transformMap(keyframe.declarations, transformDeclaration);
});
}
return rule;
});
return transformedStyles;
}
// We do NOT directly modify the declaration in the rule
// We pass the whole rule and current index so we can properly look at the metaData around each declaration
// and add it to the declaration
function attachInfoToDeclaration(declarations, declarationIndex)
{
if(declarations.length > declarationIndex) {
// Clone the declartion to keep it immutable
var declaration = extend(true, {}, declarations[declarationIndex]);
var declarationMetaInfo = getMetaInfoForDeclaration(declarations, declarationIndex);
// Add the meta into to the declaration
declaration.meta = extend(true, {}, declaration.meta, declarationMetaInfo);
return declaration;
}
return null;
}
module.exports = mapOverStylesAndTransformBackgroundImageDeclarations;