Skip to content

Commit

Permalink
Per the CSS spec, There are 96px in 1in.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Aug 7, 2012
1 parent 02947c9 commit 1bd9149
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
13 changes: 7 additions & 6 deletions lib/sass/script/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,13 @@ def normalize!
end

# A hash of unit names to their index in the conversion table
CONVERTABLE_UNITS = {"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4}
CONVERSION_TABLE = [[ 1, 2.54, 6, 25.4, 72 ], # in
[ nil, 1, 2.36220473, 10, 28.3464567], # cm
[ nil, nil, 1, 4.23333333, 12 ], # pc
[ nil, nil, nil, 1, 2.83464567], # mm
[ nil, nil, nil, nil, 1 ]] # pt
CONVERTABLE_UNITS = {"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4, "px" => 5 }
CONVERSION_TABLE = [[ 1, 2.54, 6, 25.4, 72 , 96 ], # in
[ nil, 1, 2.36220473, 10, 28.3464567, 37.795275591], # cm
[ nil, nil, 1, 4.23333333, 12 , 16 ], # pc
[ nil, nil, nil, 1, 2.83464567, 3.7795275591], # mm
[ nil, nil, nil, nil, 1 , 1.3333333333], # pt
[ nil, nil, nil, nil, nil , 1 ]] # px

def conversion_factor(from_unit, to_unit)
res = CONVERSION_TABLE[CONVERTABLE_UNITS[from_unit]][CONVERTABLE_UNITS[to_unit]]
Expand Down
3 changes: 2 additions & 1 deletion test/sass/functions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,8 @@ def test_unit
assert_equal(%Q{"px"}, evaluate("unit(100px)"))
assert_equal(%Q{"em*px"}, evaluate("unit(10px * 5em)"))
assert_equal(%Q{"em*px"}, evaluate("unit(5em * 10px)"))
assert_equal(%Q{"em*px/cm*rem"}, evaluate("unit(10px * 5em / 30cm / 1rem)"))
assert_equal(%Q{"em/rem"}, evaluate("unit(10px * 5em / 30cm / 1rem)"))
assert_equal(%Q{"em*vh/cm*rem"}, evaluate("unit(10vh * 5em / 30cm / 1rem)"))
assert_equal(%Q{"px"}, evaluate("unit($number: 100px)"))
assert_error_message("#ff0000 is not a number for `unit'", "unit(#f00)")
end
Expand Down
2 changes: 1 addition & 1 deletion test/sass/results/units.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ b {
pt: -72pt;
inches: 2in;
more-inches: 3.5in;
mixed: 6px; }
mixed: 2.042in; }
1 change: 1 addition & 0 deletions test/sass/script_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ def test_functions

def test_operator_unit_conversion
assert_equal "1.1cm", resolve("1cm + 1mm")
assert_equal "2in", resolve("1in + 96px")
assert_equal "true", resolve("2mm < 1cm")
assert_equal "true", resolve("10mm == 1cm")
assert_equal "true", resolve("1 == 1cm")
Expand Down

0 comments on commit 1bd9149

Please sign in to comment.