Module: Emjay::ShorthandParser

Defined in:
lib/emjay/helpers/shorthand_parser.rb

Class Method Summary collapse

Class Method Details

.call(css_value, direction) ⇒ Object

Parses CSS shorthand values (like padding/margin) into per-direction integers. Port of shorthandParser.js



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/emjay/helpers/shorthand_parser.rb', line 7

def self.call(css_value, direction)
  parts = css_value.to_s.strip.gsub(/\s+/, " ").split(" ", 4)

  directions = case parts.length
  when 2
    {top: 0, bottom: 0, left: 1, right: 1}
  when 3
    {top: 0, left: 1, right: 1, bottom: 2}
  when 4
    {top: 0, right: 1, bottom: 2, left: 3}
  else
    return css_value.to_i
  end

  (parts[directions[direction.to_sym]] || "0").to_i
end