postcss-custom-properties
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,4 +0,9 @@ | ||
# 0.2.0 - 2014-08-212 | ||
# 0.3.0 - 2014-08-25 | ||
* Add map option | ||
* fallback now are always added by default ([see why](http://www.w3.org/TR/css-variables/#invalid-variables)) | ||
* `map` option renamed to `variables` | ||
# 0.2.0 - 2014-08-22 | ||
* Add `map` option | ||
* GNU style error message | ||
@@ -5,0 +10,0 @@ |
77
index.js
@@ -13,2 +13,3 @@ /** | ||
var VAR_FUNC_IDENTIFIER = "var" | ||
var RE_VAR = /([\w-]+)(?:\s*,\s*)?(.*)?/ // matches `name[, fallback]`, captures "name" and "fallback" | ||
@@ -22,3 +23,3 @@ /** | ||
options = options || {} | ||
var map = options.map || {} | ||
var variables = options.variables || {} | ||
var preserve = (options.preserve === true ? true : false) | ||
@@ -43,3 +44,3 @@ | ||
if (prop && prop.indexOf(VAR_PROP_IDENTIFIER) === 0) { | ||
map[prop] = value | ||
variables[prop] = value | ||
varNameIndices.push(i) | ||
@@ -64,3 +65,2 @@ } | ||
style.eachDecl(function(decl) { | ||
var resolvedValue | ||
var value = decl.value | ||
@@ -73,13 +73,11 @@ | ||
resolvedValue = resolveValue(value, map, decl.source) | ||
resolveValue(value, variables, decl.source).forEach(function(resolvedValue) { | ||
var clone = decl.clone() | ||
clone.value = resolvedValue | ||
decl.parent.insertBefore(decl, clone) | ||
}) | ||
if (!preserve) { | ||
decl.value = resolvedValue | ||
decl.removeSelf() | ||
} | ||
else { | ||
decl.parent.insertBefore(decl, { | ||
prop: decl.prop, | ||
value: resolvedValue | ||
}) | ||
} | ||
}) | ||
@@ -99,3 +97,3 @@ } | ||
* @param {String} value A property value known to contain CSS variable functions | ||
* @param {Object} map A map of variable names and values | ||
* @param {Object} variables A map of variable names and values | ||
* @param {Object} source source object of the declaration containing the rule | ||
@@ -105,39 +103,44 @@ * @return {String} A property value with all CSS variables substituted. | ||
function resolveValue(value, map, source) { | ||
// matches `name[, fallback]`, captures "name" and "fallback" | ||
var RE_VAR = /([\w-]+)(?:\s*,\s*)?(.*)?/ | ||
var balancedParens = balanced("(", ")", value) | ||
var varStartIndex = value.indexOf("var(") | ||
function resolveValue(value, variables, source) { | ||
var results = [] | ||
if (!balancedParens) { | ||
var start = value.indexOf("var(") | ||
if (start === -1) { | ||
return [value] | ||
} | ||
var matches = balanced("(", ")", value.substring(start)) | ||
if (!matches) { | ||
throw new SyntaxError(gnuMessage("missing closing ')' in the value '" + value + "'", source)) | ||
} | ||
var varRef = balanced("(", ")", value.substring(varStartIndex)).body | ||
if (varRef === "") { | ||
if (matches.body === "") { | ||
throw new Error(gnuMessage("var() must contain a non-whitespace string", source)) | ||
} | ||
var varFunc = VAR_FUNC_IDENTIFIER + "(" + varRef + ")" | ||
var varResult = varRef.replace(RE_VAR, function(_, name, fallback) { | ||
var replacement = map[name] | ||
matches.body.replace(RE_VAR, function(_, name, fallback) { | ||
var replacement = variables[name] | ||
if (!replacement && !fallback) { | ||
throw new Error(gnuMessage("variable '" + name + "' is undefined", source)) | ||
throw new Error(gnuMessage("variable '" + name + "' is undefined & don't have any fallback", source)) | ||
} | ||
if (!replacement && fallback) { | ||
return fallback | ||
if (fallback) { | ||
// resolve the end of the expression before the rest | ||
(matches.post ? resolveValue(matches.post, variables, source) : [""]).forEach(function(afterValue) { | ||
// resolve fallback values | ||
resolveValue(fallback, variables, source).forEach(function(fbValue) { | ||
results.push(value.slice(0, start) + fbValue + afterValue) | ||
}) | ||
}) | ||
} | ||
return replacement | ||
if (replacement) { | ||
// resolve the end of the expression | ||
(matches.post ? resolveValue(matches.post, variables, source) : [""]).forEach(function(afterValue) { | ||
results.push(value.slice(0, start) + replacement + afterValue) | ||
}) | ||
} | ||
}) | ||
// resolve the variable | ||
value = value.split(varFunc).join(varResult) | ||
// recursively resolve any remaining variables in the value | ||
if (value.indexOf(VAR_FUNC_IDENTIFIER) !== -1) { | ||
value = resolveValue(value, map) | ||
} | ||
return value | ||
return results | ||
} | ||
@@ -144,0 +147,0 @@ |
{ | ||
"name": "postcss-custom-properties", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "PostCSS plugin to polyfill W3C CSS Custom Properties for cascading variables", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -69,3 +69,3 @@ # postcss-custom-properties [![Build Status](https://travis-ci.org/postcss/postcss-custom-properties.png)](https://travis-ci.org/postcss/postcss-custom-properties) | ||
#### `map` (default: `{}`) | ||
#### `variables` (default: `{}`) | ||
@@ -72,0 +72,0 @@ Allow you to pass an object of variables |
8673
124