diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 7d9b72d1..edbdb4c9 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -156,9 +156,10 @@ function normalize_expression(expression) { }); 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]; + var userVariablePattern = '(\\$_*[^_ ]+)'; + var variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g"); + expression = expression.replace(variablesReplaceRE, function (match) { + return PREDEFINED_VARS[match] || match; }); return expression.replace(/[ _]+/g, '_'); diff --git a/lib/utils/index.js b/lib/utils/index.js index 13867857..3170346d 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -141,8 +141,9 @@ function normalize_expression(expression) { 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])); + const userVariablePattern = '(\\$_*[^_ ]+)'; + const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g"); + expression = expression.replace(variablesReplaceRE, (match) => (PREDEFINED_VARS[match] || match)); return expression.replace(/[ _]+/g, '_'); } diff --git a/test/unit/cloudinary_spec.js b/test/unit/cloudinary_spec.js index 27ed5703..f44bb58a 100644 --- a/test/unit/cloudinary_spec.js +++ b/test/unit/cloudinary_spec.js @@ -795,6 +795,7 @@ describe("cloudinary", function () { api_key: "1234" }); }); + it("should support preloaded identifier format", function () { var result = cloudinary.utils.url("raw/private/v123456/document.docx"); expect(result).to.eql("http://res.cloudinary.com/test123/raw/private/v123456/document.docx"); @@ -804,6 +805,7 @@ describe("cloudinary", function () { }); expect(result).to.eql("http://res.cloudinary.com/test123/image/private/c_scale,w_1.0/v123456/img.jpg"); }); + it("should add responsive width transformation", function () { var options, result; options = { @@ -861,4 +863,20 @@ describe("cloudinary", function () { result = cloudinary.utils.url("sample.jpg", options); expect(result).to.eql('http://res.cloudinary.com/test123/image/upload/s--2hbrSMPO--/sample.jpg'); }); + + it("should not affect user variable names containing predefined names", function() { + const options = { transformation: [ + { + $mywidth: "100", + $aheight: 300 + }, + { + width: "3 + $mywidth * 3 + 4 / 2 * initialWidth * $mywidth", + height: "3 * initialHeight + $aheight", + crop: 'scale' + } + ]}; + const result = cloudinary.utils.url("sample", options); + expect(result).to.contain("$aheight_300,$mywidth_100/c_scale,h_3_mul_ih_add_$aheight,w_3_add_$mywidth_mul_3_add_4_div_2_mul_iw_mul_$mywidth"); + }); }); diff --git a/test/utils/utils_spec.js b/test/utils/utils_spec.js index d3810d04..6d916839 100644 --- a/test/utils/utils_spec.js +++ b/test/utils/utils_spec.js @@ -1061,6 +1061,7 @@ 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 = { @@ -1076,6 +1077,7 @@ describe("utils", function () { 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",