Module: SilkLayout::CSS::Values

Defined in:
lib/silk_layout/css/values.rb

Defined Under Namespace

Classes: Length, Term

Constant Summary collapse

NUMBER =
/[-+]?(?:\d+(?:\.\d+)?|\.\d+)/
LENGTH =
/\A(?<number>#{NUMBER.source})(?<unit>px|%)?\z/i
CALC =
/\Acalc\((?<expression>.*)\)\z/i
NAMED_LENGTHS =
{
  "thin" => 1,
  "medium" => 3,
  "thick" => 5
}.freeze

Class Method Summary collapse

Class Method Details

.expanded_edges(value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/silk_layout/css/values.rb', line 151

def expanded_edges(value)
  tokens = split_tokens(value)

  case tokens.length
  when 0
    {top: nil, right: nil, bottom: nil, left: nil}
  when 1
    {top: tokens[0], right: tokens[0], bottom: tokens[0], left: tokens[0]}
  when 2
    {top: tokens[0], right: tokens[1], bottom: tokens[0], left: tokens[1]}
  when 3
    {top: tokens[0], right: tokens[1], bottom: tokens[2], left: tokens[1]}
  else
    {top: tokens[0], right: tokens[1], bottom: tokens[2], left: tokens[3]}
  end
end

.length(value) ⇒ Object



110
111
112
# File 'lib/silk_layout/css/values.rb', line 110

def length(value)
  value.is_a?(Length) ? value : Length.parse(value)
end

.reference_relative?(value) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/silk_layout/css/values.rb', line 118

def reference_relative?(value)
  length(value).reference_relative?
end

.resolve_length(value, reference: nil, default: 0) ⇒ Object



114
115
116
# File 'lib/silk_layout/css/values.rb', line 114

def resolve_length(value, reference: nil, default: 0)
  length(value).resolve(reference: reference, default: default)
end

.split_tokens(value) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/silk_layout/css/values.rb', line 122

def split_tokens(value)
  tokens = []
  current = +""
  depth = 0

  value.to_s.each_char do |char|
    case char
    when "("
      depth += 1
      current << char
    when ")"
      depth -= 1 if depth.positive?
      current << char
    when /\s/
      if depth.zero?
        tokens << current unless current.empty?
        current = +""
      else
        current << char
      end
    else
      current << char
    end
  end

  tokens << current unless current.empty?
  tokens
end