postcss-reduce-idents
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -0,1 +1,5 @@ | ||
# 2.1.0 | ||
* Replaced reduce-function-call with postcss-value-parser (thanks to @TrySound). | ||
# 2.0.0 | ||
@@ -2,0 +6,0 @@ |
85
index.js
'use strict'; | ||
var postcss = require('postcss'); | ||
var reduce = require('reduce-function-call'); | ||
var parser = require('postcss-value-parser'); | ||
var encode = require('./lib/encode'); | ||
var list = postcss.list; | ||
function eachValue (value, callback) { | ||
return list.space(value).map(function (val) { | ||
return list.comma(val).map(callback).join(); | ||
}).join(' '); | ||
} | ||
function transformAtRule (css, atRuleRegex, propRegex) { | ||
@@ -30,9 +23,15 @@ var cache = {}; | ||
css.walkDecls(propRegex, function (decl) { | ||
decl.value = eachValue(decl.value, function (value) { | ||
if (value in cache) { | ||
cache[value].count++; | ||
return cache[value].ident; | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'word' && node.value in cache) { | ||
cache[node.value].count++; | ||
node.value = cache[node.value].ident; | ||
} | ||
return value; | ||
}); | ||
if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
if (node.type === 'div') { | ||
node.before = ''; | ||
node.after = ''; | ||
} | ||
}).toString(); | ||
}); | ||
@@ -55,6 +54,6 @@ // Ensure that at rules with no references to them are left unchanged | ||
css.walkDecls(/counter-(reset|increment)/, function (decl) { | ||
decl.value = eachValue(decl.value, function (value) { | ||
if (!/^-?\d*$/.test(value)) { | ||
if (!cache[value]) { | ||
cache[value] = { | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'word' && !/^-?\d*$/.test(node.value)) { | ||
if (!cache[node.value]) { | ||
cache[node.value] = { | ||
ident: encode(Object.keys(cache).length), | ||
@@ -64,5 +63,7 @@ count: 0 | ||
} | ||
return cache[value].ident; | ||
node.value = cache[node.value].ident; | ||
} | ||
return value; | ||
if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
}); | ||
@@ -72,28 +73,34 @@ declCache.push(decl); | ||
css.walkDecls('content', function (decl) { | ||
decl.value = eachValue(decl.value, function (value) { | ||
return reduce(value, /(counters?)\(/, function (body, fn) { | ||
var counters = list.comma(body).map(function (counter) { | ||
if (counter in cache) { | ||
cache[counter].count++; | ||
return cache[counter].ident; | ||
} | ||
return counter; | ||
}).join(); | ||
return fn + '(' + counters + ')'; | ||
}); | ||
}); | ||
decl.value = parser(decl.value).walk(function (node) { | ||
if (node.type === 'function') { | ||
if (node.value === 'counter' || node.value === 'counters') { | ||
node.nodes.forEach(function (node) { | ||
if (node.type === 'word' && node.value in cache) { | ||
cache[node.value].count++; | ||
node.value = cache[node.value].ident; | ||
} | ||
if (node.type === 'div') { | ||
node.before = ''; | ||
node.after = ''; | ||
} | ||
}); | ||
} | ||
return false; | ||
} | ||
if (node.type === 'space') { | ||
node.value = ' '; | ||
} | ||
}).toString(); | ||
}); | ||
declCache.forEach(function (decl) { | ||
decl.value = eachValue(decl.value, function (value) { | ||
if (!/^-?\d*$/.test(value)) { | ||
decl.value = decl.value.walk(function (node) { | ||
if (node.type === 'word' && !/^-?\d*$/.test(node.value)) { | ||
Object.keys(cache).forEach(function (key) { | ||
var k = cache[key]; | ||
if (k.ident === value && !k.count) { | ||
value = key; | ||
if (k.ident === node.value && !k.count) { | ||
node.value = key; | ||
} | ||
}); | ||
return value; | ||
} | ||
return value; | ||
}); | ||
}).toString(); | ||
}); | ||
@@ -100,0 +107,0 @@ // Transform @keyframes, @counter-style |
{ | ||
"name": "postcss-reduce-idents", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Reduce custom identifiers with PostCSS.", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"lib", | ||
"LICENSE-MIT" | ||
], | ||
"scripts": { | ||
@@ -28,4 +33,4 @@ "test": "tape test.js | tap-spec" | ||
"postcss": "^5.0.4", | ||
"reduce-function-call": "^1.0.1" | ||
"postcss-value-parser": "^2.0.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9051
122
6
+ Addedpostcss-value-parser@^2.0.3
+ Addedpostcss-value-parser@2.0.5(transitive)
- Removedreduce-function-call@^1.0.1
- Removedbalanced-match@1.0.2(transitive)
- Removedreduce-function-call@1.0.3(transitive)