Skip to content

Commit ae15a87

Browse files
author
RTLcoil
authored
Fix expression normalization in advanced cases
1 parent 13c6d27 commit ae15a87

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

lib/cloudinary/utils.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,34 @@ class Cloudinary::Utils
3232

3333
PREDEFINED_VARS = {
3434
"aspect_ratio" => "ar",
35+
"aspectRatio" => "ar",
3536
"current_page" => "cp",
37+
"currentPage" => "cp",
3638
"face_count" => "fc",
39+
"faceCount" => "fc",
3740
"height" => "h",
3841
"initial_aspect_ratio" => "iar",
42+
"initialAspectRatio" => "iar",
43+
"trimmed_aspect_ratio" => "tar",
44+
"trimmedAspectRatio" => "tar",
3945
"initial_height" => "ih",
46+
"initialHeight" => "ih",
4047
"initial_width" => "iw",
48+
"initialWidth" => "iw",
4149
"page_count" => "pc",
50+
"pageCount" => "pc",
4251
"page_x" => "px",
52+
"pageX" => "px",
4353
"page_y" => "py",
54+
"pageY" => "py",
4455
"tags" => "tags",
4556
"initial_duration" => "idu",
57+
"initialDuration" => "idu",
4658
"duration" => "du",
47-
"width" => "w"
59+
"width" => "w",
60+
"illustration_score" => "ils",
61+
"illustrationScore" => "ils",
62+
"context" => "ctx"
4863
}
4964

5065
SIMPLE_TRANSFORMATION_PARAMS = {
@@ -317,16 +332,16 @@ def self.process_if(if_value)
317332
"if_" + normalize_expression(if_value) unless if_value.to_s.empty?
318333
end
319334

320-
EXP_REGEXP = Regexp.new('(?<!\$)('+PREDEFINED_VARS.keys.join("|")+')'+'|('+CONDITIONAL_OPERATORS.keys.reverse.map { |k| Regexp.escape(k) }.join('|')+')(?=[ _])')
335+
EXP_REGEXP = Regexp.new('(\$_*[^_ ]+)|(?<!\$)('+PREDEFINED_VARS.keys.join("|")+')'+'|('+CONDITIONAL_OPERATORS.keys.reverse.map { |k| Regexp.escape(k) }.join('|')+')(?=[ _])')
321336
EXP_REPLACEMENT = PREDEFINED_VARS.merge(CONDITIONAL_OPERATORS)
322337

323338
def self.normalize_expression(expression)
324339
if expression.nil?
325-
''
340+
nil
326341
elsif expression.is_a?( String) && expression =~ /^!.+!$/ # quoted string
327342
expression
328343
else
329-
expression.to_s.gsub(EXP_REGEXP,EXP_REPLACEMENT).gsub(/[ _]+/, "_")
344+
expression.to_s.gsub(EXP_REGEXP) { |match| EXP_REPLACEMENT[match] || match }.gsub(/[ _]+/, "_")
330345
end
331346
end
332347

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "spec_helper"
2+
3+
describe "Expression normalization" do
4+
{
5+
'nil is not affected' => [nil, nil],
6+
'number replaced with a string value' => [10, '10'],
7+
'empty string is not affected' => ['', ''],
8+
'single space is replaced with a single underscore' => [' ', '_'],
9+
'blank string is replaced with a single underscore' => [' ', '_'],
10+
'underscore is not affected' => ['_', '_'],
11+
'sequence of underscores and spaces is replaced with a single underscore' => [' _ __ _', '_'],
12+
'arbitrary text is not affected' => ['foobar', 'foobar'],
13+
'double ampersand replaced with and operator' => ['foo && bar', 'foo_and_bar'],
14+
'double ampersand with no space at the end is not affected' => ['foo&&bar', 'foo&&bar'],
15+
'width recognized as variable and replaced with w' => ['width', 'w'],
16+
'initial aspect ratio recognized as variable and replaced with iar' => ['initial_aspect_ratio', 'iar'],
17+
'$width recognized as user variable and not affected' => ['$width', '$width'],
18+
'$initial_aspect_ratio recognized as user variable followed by aspect_ratio variable' => [
19+
'$initial_aspect_ratio',
20+
'$initial_ar',
21+
],
22+
'$mywidth recognized as user variable and not affected' => ['$mywidth', '$mywidth'],
23+
'$widthwidth recognized as user variable and not affected' => ['$widthwidth', '$widthwidth'],
24+
'$_width recognized as user variable and not affected' => ['$_width', '$_width'],
25+
'$__width recognized as user variable and not affected' => ['$__width', '$_width'],
26+
'$$width recognized as user variable and not affected' => ['$$width', '$$width'],
27+
'$height recognized as user variable and not affected' => ['$height_100', '$height_100'],
28+
'$heightt_100 recognized as user variable and not affected' => ['$heightt_100', '$heightt_100'],
29+
'$$height_100 recognized as user variable and not affected' => ['$$height_100', '$$height_100'],
30+
'$heightmy_100 recognized as user variable and not affected' => ['$heightmy_100', '$heightmy_100'],
31+
'$myheight_100 recognized as user variable and not affected' => ['$myheight_100', '$myheight_100'],
32+
'$heightheight_100 recognized as user variable and not affected' => [
33+
'$heightheight_100',
34+
'$heightheight_100',
35+
],
36+
'$theheight_100 recognized as user variable and not affected' => ['$theheight_100', '$theheight_100'],
37+
'$__height_100 recognized as user variable and not affected' => ['$__height_100', '$_height_100']
38+
}.each do |test_description, (input, expected)|
39+
specify test_description do
40+
actual = Cloudinary::Utils.normalize_expression(input)
41+
expect(actual).to eq(expected)
42+
end
43+
end
44+
end

spec/utils_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,18 @@
10771077
t = Cloudinary::Utils.generate_transformation_string options, true
10781078
expect(t).to eq("$width_10/w_$width_add_10_add_w")
10791079
end
1080+
1081+
it "should not affect user variable names containing predefined names" do
1082+
options = {
1083+
:transformation => [
1084+
{ :variables => [ ["$aheight", 300], ["$mywidth", "100"] ] },
1085+
{ :width => "3 + $mywidth * 3 + 4 / 2 * initialWidth * $mywidth", :height => "3 * initialHeight + $aheight" },
1086+
]
1087+
}
1088+
1089+
t = Cloudinary::Utils.generate_transformation_string options, true
1090+
expect(t).to eq("$aheight_300,$mywidth_100/h_3_mul_ih_add_$aheight,w_3_add_$mywidth_mul_3_add_4_div_2_mul_iw_mul_$mywidth")
1091+
end
10801092
end
10811093

10821094
describe "context" do

0 commit comments

Comments
 (0)