Skip to content

Commit

Permalink
Merge pull request #3 from estenh/master
Browse files Browse the repository at this point in the history
Add single line border-radius property to swap.
  • Loading branch information
mzsanford committed Nov 15, 2012
2 parents b91f87e + 6eb0212 commit c30c32b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/r2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/r2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c30c32b

Please sign in to comment.