Skip to content

Commit 9ec984d

Browse files
committed
Handle vendor prefixed expressions
1 parent 028d310 commit 9ec984d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/calc.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ module.exports = function (style) {
3636
* Constants
3737
*/
3838
var DEFAULT_UNIT = 'px',
39-
EXPRESSION_METHOD_NAME = 'calc',
40-
EXPRESSION_REGEXP = '\\b' + EXPRESSION_METHOD_NAME + '\\(';
39+
EXPRESSION_METHOD_NAME = 'calc',
40+
EXPRESSION_OPT_VENDOR_PREFIX = '(\\-[a-z]+\\-)?',
41+
EXPRESSION_METHOD_REGEXP = EXPRESSION_OPT_VENDOR_PREFIX + EXPRESSION_METHOD_NAME,
42+
EXPRESSION_REGEXP = '\\b' + EXPRESSION_METHOD_REGEXP + '\\(';
4143

4244
/**
4345
* Visit all rules
@@ -123,7 +125,11 @@ function evaluateAndApplyExpressions(expressions, declaration) {
123125
if (!result) return;
124126

125127
// Insert the evaluated value:
126-
declaration.value = declaration.value.replace(EXPRESSION_METHOD_NAME + '(' + expression + ')', result);
128+
var expRegexp = new RegExp(
129+
EXPRESSION_METHOD_REGEXP + '\\(' +
130+
escapeExp(expression) + '\\)'
131+
);
132+
declaration.value = declaration.value.replace(expRegexp, result);
127133
});
128134
}
129135

@@ -201,3 +207,14 @@ function getUnitsInExpression(expression) {
201207

202208
return uniqueUnits;
203209
}
210+
211+
/**
212+
* Escape string for inclusion in a regex pattern without conflicts
213+
*
214+
* @param {String} str String to escape
215+
* @return {String} Escaped string
216+
* @api private
217+
*/
218+
function escapeExp(str) {
219+
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
220+
}

0 commit comments

Comments
 (0)