Skip to content

Commit dfbaf05

Browse files
Add support for complex variable names (aheight) (#488)
1 parent 11cf664 commit dfbaf05

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

lib-es5/utils/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ function normalize_expression(expression) {
156156
});
157157

158158
var predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
159-
var predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g");
160-
expression = expression.replace(predefinedVarsReplaceRE, function (match, p1, offset) {
161-
return expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match];
159+
var userVariablePattern = '(\\$_*[^_ ]+)';
160+
var variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g");
161+
expression = expression.replace(variablesReplaceRE, function (match) {
162+
return PREDEFINED_VARS[match] || match;
162163
});
163164

164165
return expression.replace(/[ _]+/g, '_');

lib/utils/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ function normalize_expression(expression) {
141141
expression = expression.replace(operatorsReplaceRE, match => CONDITIONAL_OPERATORS[match]);
142142

143143
const predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
144-
const predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g");
145-
expression = expression.replace(predefinedVarsReplaceRE, (match, p1, offset) => (expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match]));
144+
const userVariablePattern = '(\\$_*[^_ ]+)';
145+
const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g");
146+
expression = expression.replace(variablesReplaceRE, (match) => (PREDEFINED_VARS[match] || match));
146147

147148
return expression.replace(/[ _]+/g, '_');
148149
}

test/unit/cloudinary_spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ describe("cloudinary", function () {
795795
api_key: "1234"
796796
});
797797
});
798+
798799
it("should support preloaded identifier format", function () {
799800
var result = cloudinary.utils.url("raw/private/v123456/document.docx");
800801
expect(result).to.eql("http://res.cloudinary.com/test123/raw/private/v123456/document.docx");
@@ -804,6 +805,7 @@ describe("cloudinary", function () {
804805
});
805806
expect(result).to.eql("http://res.cloudinary.com/test123/image/private/c_scale,w_1.0/v123456/img.jpg");
806807
});
808+
807809
it("should add responsive width transformation", function () {
808810
var options, result;
809811
options = {
@@ -861,4 +863,20 @@ describe("cloudinary", function () {
861863
result = cloudinary.utils.url("sample.jpg", options);
862864
expect(result).to.eql('http://res.cloudinary.com/test123/image/upload/s--2hbrSMPO--/sample.jpg');
863865
});
866+
867+
it("should not affect user variable names containing predefined names", function() {
868+
const options = { transformation: [
869+
{
870+
$mywidth: "100",
871+
$aheight: 300
872+
},
873+
{
874+
width: "3 + $mywidth * 3 + 4 / 2 * initialWidth * $mywidth",
875+
height: "3 * initialHeight + $aheight",
876+
crop: 'scale'
877+
}
878+
]};
879+
const result = cloudinary.utils.url("sample", options);
880+
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");
881+
});
864882
});

test/utils/utils_spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ describe("utils", function () {
10611061
t = cloudinary.utils.generate_transformation_string(options);
10621062
expect(t).to.eql("$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end");
10631063
});
1064+
10641065
it("should not change variable names even if they look like keywords", function () {
10651066
var options, t;
10661067
options = {
@@ -1076,6 +1077,7 @@ describe("utils", function () {
10761077
t = cloudinary.utils.generate_transformation_string(options);
10771078
expect(t).to.eql("$width_10/w_$width_add_10_add_w");
10781079
});
1080+
10791081
it("should support text values", function () {
10801082
test_cloudinary_url("sample", {
10811083
effect: "$efname:100",

0 commit comments

Comments
 (0)