Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add single line border-radius property to swap #3

Merged
merged 4 commits into from

2 participants

@esten

This won't work if you use vertical radius as part of this, but it should handle all other cases.

@mzsanford
Owner

Would it be possible to add some spec tests for border_radius_swap? I can't merge until tests are added and I won't have a chance to add them for a few days.

@esten

Just added tests, thanks!

@mzsanford mzsanford merged commit c30c32b into mzsanford:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 0 deletions.
  1. +24 −0 lib/r2.rb
  2. +14 −0 spec/r2_spec.rb
View
24 lib/r2.rb
@@ -49,6 +49,9 @@ class Swapper
VALUE_PROCS = {
'padding' => lambda {|obj,val| obj.quad_swap(val) },
'margin' => lambda {|obj,val| obj.quad_swap(val) },
+ 'border-radius' => lambda {|obj,val| obj.border_radius_swap(val) },
+ '-moz-border-radius' => lambda {|obj,val| obj.border_radius_swap(val) },
+ '-webkit-border-radius' => lambda {|obj,val| obj.border_radius_swap(val) },
'text-align' => lambda {|obj,val| obj.side_swap(val) },
'float' => lambda {|obj,val| obj.side_swap(val) },
'box-shadow' => lambda {|obj,val| obj.quad_swap(val) },
@@ -140,6 +143,27 @@ def quad_swap(val)
val
end
end
+ # Border radius uses top-left, top-right, bottom-left, bottom-right, so all values need to be swapped. Additionally,
+ # two and three value border-radius declarations need to be swapped as well. Vertical radius, specified with a /,
+ # should be left alone.
+ def border_radius_swap(val)
+ # 1px 2px 3px 4px => 1px 4px 3px 2px
+ points = val.to_s.split(/\s+/)
+
+ if points && points.length > 1 && !val.to_s.include?('/')
+ case points.length
+ when 4
+ [points[1], points[0], points[3], points[2]].join(' ')
+ when 3
+ [points[1], points[0], points[1], points[2]].join(' ')
+ when 2
+ [points[1], points[0]].join(' ')
+ else val
+ end
+ else
+ val
+ end
+ end
end
end
View
14 spec/r2_spec.rb
@@ -97,4 +97,18 @@
@r2.quad_swap("1px 2px").should == "1px 2px"
end
end
+
+ context "border_radius_swap" do
+ it "should swap a valid quad value" do
+ @r2.border_radius_swap("1px 2px 3px 4px").should == "2px 1px 4px 3px"
+ end
+
+ it "should skip a triple value" do
+ @r2.border_radius_swap("1px 2px 3px").should == "2px 1px 2px 3px"
+ end
+
+ it "should skip a pair value" do
+ @r2.border_radius_swap("1px 2px").should == "2px 1px"
+ end
+ end
end
Something went wrong with that request. Please try again.