diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index c8be7271..009f9e69 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -137,12 +137,20 @@ function normalize_expression(expression) { if (!isString(expression) || expression.length === 0 || expression.match(/^!.+!$/)) { return expression; } + var operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*"; - var pattern = "((" + operators + ")(?=[ _])|" + Object.keys(PREDEFINED_VARS).join("|") + ")"; - var replaceRE = new RegExp(pattern, "g"); - expression = expression.replace(replaceRE, function (match) { - return CONDITIONAL_OPERATORS[match] || PREDEFINED_VARS[match]; + var operatorsPattern = "((" + operators + ")(?=[ _]))"; + var operatorsReplaceRE = new RegExp(operatorsPattern, "g"); + expression = expression.replace(operatorsReplaceRE, function (match) { + return CONDITIONAL_OPERATORS[match]; + }); + + var predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")"; + var predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g"); + expression = expression.replace(predefinedVarsReplaceRE, function (match, p1, offset) { + return expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match]; }); + return expression.replace(/[ _]+/g, '_'); } diff --git a/lib/utils/index.js b/lib/utils/index.js index be7f0ce1..1752cacc 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -127,12 +127,16 @@ function normalize_expression(expression) { if (!isString(expression) || expression.length === 0 || expression.match(/^!.+!$/)) { return expression; } + const operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*"; - const pattern = "((" + operators + ")(?=[ _])|" + Object.keys(PREDEFINED_VARS).join("|") + ")"; - const replaceRE = new RegExp(pattern, "g"); - expression = expression.replace(replaceRE, function (match) { - return CONDITIONAL_OPERATORS[match] || PREDEFINED_VARS[match]; - }); + const operatorsPattern = "((" + operators + ")(?=[ _]))"; + const operatorsReplaceRE = new RegExp(operatorsPattern, "g"); + expression = expression.replace(operatorsReplaceRE, match => CONDITIONAL_OPERATORS[match]); + + const predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")"; + const predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g"); + expression = expression.replace(predefinedVarsReplaceRE, (match, p1, offset) => (expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match])); + return expression.replace(/[ _]+/g, '_'); } diff --git a/test/utils_spec.js b/test/utils_spec.js index adfc4dff..ca32422e 100644 --- a/test/utils_spec.js +++ b/test/utils_spec.js @@ -1052,6 +1052,21 @@ describe("utils", function () { t = cloudinary.utils.generate_transformation_string(options); expect(t).to.eql("$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end"); }); + it("should not change variable names even if they look like keywords", function () { + var options, t; + options = { + transformation: [ + { + $width: 10, + }, + { + width: "$width + 10 + width", + }, + ], + }; + t = cloudinary.utils.generate_transformation_string(options); + expect(t).to.eql("$width_10/w_$width_add_10_add_w"); + }); it("should support text values", function () { test_cloudinary_url("sample", { effect: "$efname:100",