Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib-es5/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_');
Expand Down
5 changes: 3 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_');
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/cloudinary_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 = {
Expand Down Expand Up @@ -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");
});
});
2 changes: 2 additions & 0 deletions test/utils/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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",
Expand Down