Skip to content

Commit 06cf09d

Browse files
Prevent preview:duration from being normalized (#513)
1 parent 50791ac commit 06cf09d

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

lib-es5/uploader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ exports.create_slideshow = function create_slideshow(options, callback) {
226226
// Generate a transformation from the manifest_transformation key, which should be a valid transformation
227227
var manifest_transformation = utils.generate_transformation_string(extend({}, options.manifest_transformation));
228228

229-
// Try to use all the options to generate a transformation (Example: options.width and options.height)
229+
// Try to use {options.transformation} to generate a transformation (Example: options.transformation.width, options.transformation.height)
230230
var transformation = utils.generate_transformation_string(extend({}, ensureOption(options, 'transformation', {})));
231231

232232
return [{

lib-es5/utils/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,14 @@ function normalize_expression(expression) {
159159
return CONDITIONAL_OPERATORS[match];
160160
});
161161

162-
var predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
162+
// Duplicate PREDEFINED_VARS to also include :{var_name} as well as {var_name}
163+
// Example:
164+
// -- PREDEFINED_VARS = ['foo']
165+
// -- predefinedVarsPattern = ':foo|foo'
166+
// It is done like this because node 6 does not support regex lookbehind
167+
var predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).map(function (v) {
168+
return `:${v}|${v}`;
169+
}).join("|") + ")";
163170
var userVariablePattern = '(\\$_*[^_ ]+)';
164171
var variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g");
165172
expression = expression.replace(variablesReplaceRE, function (match) {
@@ -1643,6 +1650,7 @@ exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;
16431650
exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES;
16441651

16451652
Object.assign(module.exports, {
1653+
normalize_expression,
16461654
at,
16471655
clone,
16481656
extend,

lib/utils/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ function normalize_expression(expression) {
144144
const operatorsReplaceRE = new RegExp(operatorsPattern, "g");
145145
expression = expression.replace(operatorsReplaceRE, match => CONDITIONAL_OPERATORS[match]);
146146

147-
const predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
147+
// Duplicate PREDEFINED_VARS to also include :{var_name} as well as {var_name}
148+
// Example:
149+
// -- PREDEFINED_VARS = ['foo']
150+
// -- predefinedVarsPattern = ':foo|foo'
151+
// It is done like this because node 6 does not support regex lookbehind
152+
const predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).map(v => `:${v}|${v}`).join("|") + ")";
148153
const userVariablePattern = '(\\$_*[^_ ]+)';
149154
const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g");
150155
expression = expression.replace(variablesReplaceRE, (match) => (PREDEFINED_VARS[match] || match));
@@ -1509,6 +1514,7 @@ exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;
15091514
exports.DEFAULT_VIDEO_SOURCE_TYPES = DEFAULT_VIDEO_SOURCE_TYPES;
15101515

15111516
Object.assign(module.exports, {
1517+
normalize_expression,
15121518
at,
15131519
clone,
15141520
extend,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const cloudinary = require("../../cloudinary");
2+
const createTestConfig = require('../testUtils/createTestConfig');
3+
4+
describe("normalize_expression tests", function () {
5+
beforeEach(function () {
6+
cloudinary.config(createTestConfig({
7+
cloud_name: "test123",
8+
api_key: 'a',
9+
api_secret: 'b'
10+
}));
11+
});
12+
13+
it("Expression normalization", function() {
14+
const cases = {
15+
'null is not affected': [null, null],
16+
'None is not affected': ['None', 'None'],
17+
'empty string is not affected': ['', ''],
18+
'single space is replaced with a single underscore': [' ', '_'],
19+
'blank string is replaced with a single underscore': [' ', '_'],
20+
'underscore is not affected': ['_', '_'],
21+
'sequence of underscores and spaces is replaced with a single underscore': [' _ __ _', '_'],
22+
'arbitrary text is not affected': ['foobar', 'foobar'],
23+
'double ampersand replaced with and operator': ['foo && bar', 'foo_and_bar'],
24+
'double ampersand with no space at the end is not affected': ['foo&&bar', 'foo&&bar'],
25+
'width recognized as variable and replaced with w': ['width', 'w'],
26+
'initial aspect ratio recognized as variable and replaced with iar': ['initial_aspect_ratio', 'iar'],
27+
'duration is recognized as a variable and replaced with du': ['duration', 'du'],
28+
'duration after : is not a variable and is not affected': ['preview:duration_2', 'preview:duration_2'],
29+
'$width recognized as user variable and not affected': ['$width', '$width'],
30+
'$initial_aspect_ratio recognized as user variable followed by aspect_ratio variable': [
31+
'$initial_aspect_ratio',
32+
'$initial_ar'
33+
],
34+
'$mywidth recognized as user variable and not affected': ['$mywidth', '$mywidth'],
35+
'$widthwidth recognized as user variable and not affected': ['$widthwidth', '$widthwidth'],
36+
'$_width recognized as user variable and not affected': ['$_width', '$_width'],
37+
'$__width recognized as user variable and not affected': ['$__width', '$_width'],
38+
'$$width recognized as user variable and not affected': ['$$width', '$$width'],
39+
'$height recognized as user variable and not affected': ['$height_100', '$height_100'],
40+
'$heightt_100 recognized as user variable and not affected': ['$heightt_100', '$heightt_100'],
41+
'$$height_100 recognized as user variable and not affected': ['$$height_100', '$$height_100'],
42+
'$heightmy_100 recognized as user variable and not affected': ['$heightmy_100', '$heightmy_100'],
43+
'$myheight_100 recognized as user variable and not affected': ['$myheight_100', '$myheight_100'],
44+
'$heightheight_100 recognized as user variable and not affected': [
45+
'$heightheight_100',
46+
'$heightheight_100'
47+
],
48+
'$theheight_100 recognized as user variable and not affected': ['$theheight_100', '$theheight_100'],
49+
'$__height_100 recognized as user variable and not affected': ['$__height_100', '$_height_100']
50+
};
51+
52+
Object.keys(cases).forEach(function (testDescription) {
53+
const [input, expected] = cases[testDescription];
54+
expect(cloudinary.utils.normalize_expression(input)).to.equal(expected);
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)