Module: SilkLayout::CSS::Properties
- Defined in:
- lib/silk_layout/css/properties.rb
Constant Summary collapse
- EDGES =
%w[ top right bottom left ].freeze
- BORDER_PARTS =
%w[ width style color ].freeze
- CSS_WIDE_KEYWORDS =
%w[ inherit initial unset ].freeze
- INHERITED =
%w[ color font-size font-family font-weight font-style line-height ].freeze
- SPACING_INITIALS =
%w[ margin padding ].flat_map do |property| EDGES.map { |side| ["#{property}-#{side}", "0"] } end.to_h.freeze
- BORDER_INITIALS =
EDGES.flat_map do |side| [ ["border-#{side}-width", "medium"], ["border-#{side}-style", "none"], ["border-#{side}-color", "black"] ] end.to_h.freeze
- INITIAL_VALUES =
{ "color" => "black", "font-size" => "16px", "font-family" => "Helvetica", "font-weight" => "normal", "font-style" => "normal", "line-height" => "normal", "display" => "inline", "width" => "auto", "height" => "auto", "box-sizing" => "content-box", "min-width" => "0", "max-width" => "none", "background-color" => "transparent", "flex-direction" => "row", "flex-wrap" => "nowrap", "justify-content" => "flex-start", "align-items" => "stretch", "flex-grow" => "0", "flex-shrink" => "1", "flex-basis" => "auto", "row-gap" => "0", "column-gap" => "0" }.merge(SPACING_INITIALS).merge(BORDER_INITIALS).freeze
- DEFAULTS =
INITIAL_VALUES- KNOWN =
INITIAL_VALUES.keys.freeze
Class Method Summary collapse
- .all_border_longhands(value = nil) ⇒ Object
- .background_color(value) ⇒ Object
- .border_edge_longhands(part, value) ⇒ Object
- .border_shorthand(value) ⇒ Object
- .border_side_longhands(side, value = nil) ⇒ Object
- .border_style?(value) ⇒ Boolean
- .border_width?(value) ⇒ Boolean
- .color_token?(value) ⇒ Boolean
- .css_wide_expansion(property, value) ⇒ Object
- .css_wide_keyword?(value) ⇒ Boolean
- .edge_values(value) ⇒ Object
- .expand_declaration(property, value) ⇒ Object
- .flex_flow(value) ⇒ Object
- .flex_flow_longhands(direction, wrap) ⇒ Object
- .flex_longhands(grow, shrink, basis) ⇒ Object
- .flex_shorthand(value) ⇒ Object
- .gap_longhands(row, column) ⇒ Object
- .gap_values(value) ⇒ Object
- .inherited?(property) ⇒ Boolean
- .initial_value(property) ⇒ Object
- .known?(property) ⇒ Boolean
- .numeric?(value) ⇒ Boolean
- .shorthand_expansion(property, value) ⇒ Object
- .split_tokens(value) ⇒ Object
Class Method Details
.all_border_longhands(value = nil) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/silk_layout/css/properties.rb', line 180 def self.all_border_longhands(value = nil) EDGES.flat_map do |side| BORDER_PARTS.map do |part| ["border-#{side}-#{part}", block_given? ? yield(side, part) : value] end end end |
.background_color(value) ⇒ Object
210 211 212 |
# File 'lib/silk_layout/css/properties.rb', line 210 def self.background_color(value) split_tokens(value).find { |token| color_token?(token) } || INITIAL_VALUES.fetch("background-color") end |
.border_edge_longhands(part, value) ⇒ Object
188 189 190 |
# File 'lib/silk_layout/css/properties.rb', line 188 def self.border_edge_longhands(part, value) EDGES.map { |side| ["border-#{side}-#{part}", value] } end |
.border_shorthand(value) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/silk_layout/css/properties.rb', line 198 def self.border_shorthand(value) split_tokens(value).each_with_object({}) do |token, parsed| if border_width?(token) parsed["width"] ||= token elsif border_style?(token) parsed["style"] ||= token else parsed["color"] ||= token end end end |
.border_side_longhands(side, value = nil) ⇒ Object
192 193 194 195 196 |
# File 'lib/silk_layout/css/properties.rb', line 192 def self.border_side_longhands(side, value = nil) BORDER_PARTS.map do |part| ["border-#{side}-#{part}", block_given? ? yield(part) : value] end end |
.border_style?(value) ⇒ Boolean
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/silk_layout/css/properties.rb', line 291 def self.border_style?(value) %w[ none hidden dotted dashed solid double groove ridge inset outset ].include?(value.to_s) end |
.border_width?(value) ⇒ Boolean
287 288 289 |
# File 'lib/silk_layout/css/properties.rb', line 287 def self.border_width?(value) value.to_s.match?(/\A[-+]?\d*\.?\d+(?:px)?\z/) || %w[thin medium thick].include?(value.to_s) end |
.color_token?(value) ⇒ Boolean
306 307 308 309 310 311 312 313 314 |
# File 'lib/silk_layout/css/properties.rb', line 306 def self.color_token?(value) raw = value.to_s.strip return false if raw.empty? return true if raw.start_with?("#") return true if SilkLayout::CSS::Color.parse(raw) return false if %w[none transparent inherit initial unset].include?(raw) !border_width?(raw) && !border_style?(raw) && !raw.include?("(") && !raw.include?("/") end |
.css_wide_expansion(property, value) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/silk_layout/css/properties.rb', line 107 def self.css_wide_expansion(property, value) case property when "margin", "padding" EDGES.map { |side| ["#{property}-#{side}", value] } when "border" all_border_longhands(value) when "border-width", "border-style", "border-color" border_edge_longhands(property.delete_prefix("border-"), value) when /\Aborder-(top|right|bottom|left)\z/ border_side_longhands(Regexp.last_match(1), value) when "background" [["background-color", value]] when "flex" flex_longhands(value, value, value) when "flex-flow" flex_flow_longhands(value, value) when "gap" gap_longhands(value, value) else [] end end |
.css_wide_keyword?(value) ⇒ Boolean
91 92 93 |
# File 'lib/silk_layout/css/properties.rb', line 91 def self.css_wide_keyword?(value) CSS_WIDE_KEYWORDS.include?(value.to_s.strip.downcase) end |
.edge_values(value) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/silk_layout/css/properties.rb', line 161 def self.edge_values(value) tokens = split_tokens(value) values = case tokens.length when 0 [] when 1 [tokens[0], tokens[0], tokens[0], tokens[0]] when 2 [tokens[0], tokens[1], tokens[0], tokens[1]] when 3 [tokens[0], tokens[1], tokens[2], tokens[1]] else [tokens[0], tokens[1], tokens[2], tokens[3]] end EDGES.zip(values).to_h end |
.expand_declaration(property, value) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/silk_layout/css/properties.rb', line 77 def self.(property, value) property = property.to_s.downcase value = value.to_s.strip = [[property, value]] .concat( if css_wide_keyword?(value) css_wide_expansion(property, value) else shorthand_expansion(property, value) end ) end |
.flex_flow(value) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/silk_layout/css/properties.rb', line 242 def self.flex_flow(value) parsed = split_tokens(value).each_with_object({}) do |token, acc| if %w[row row-reverse column column-reverse].include?(token) acc["direction"] = token elsif %w[nowrap wrap wrap-reverse].include?(token) acc["wrap"] = token end end { "direction" => parsed["direction"] || INITIAL_VALUES.fetch("flex-direction"), "wrap" => parsed["wrap"] || INITIAL_VALUES.fetch("flex-wrap") } end |
.flex_flow_longhands(direction, wrap) ⇒ Object
257 258 259 260 261 262 |
# File 'lib/silk_layout/css/properties.rb', line 257 def self.flex_flow_longhands(direction, wrap) [ ["flex-direction", direction], ["flex-wrap", wrap] ] end |
.flex_longhands(grow, shrink, basis) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/silk_layout/css/properties.rb', line 234 def self.flex_longhands(grow, shrink, basis) [ ["flex-grow", grow], ["flex-shrink", shrink], ["flex-basis", basis] ] end |
.flex_shorthand(value) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/silk_layout/css/properties.rb', line 214 def self.flex_shorthand(value) tokens = split_tokens(value) case tokens.join(" ") when "none" {"grow" => "0", "shrink" => "0", "basis" => "auto"} when "auto" {"grow" => "1", "shrink" => "1", "basis" => "auto"} else numbers = tokens.select { |token| numeric?(token) } basis = tokens.find { |token| !numeric?(token) } { "grow" => numbers[0] || "1", "shrink" => numbers[1] || "1", "basis" => basis || (numbers.any? ? "0px" : "auto") } end end |
.gap_longhands(row, column) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/silk_layout/css/properties.rb', line 272 def self.gap_longhands(row, column) [ ["row-gap", row], ["column-gap", column] ] end |
.gap_values(value) ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/silk_layout/css/properties.rb', line 264 def self.gap_values(value) values = split_tokens(value) row = values[0] || INITIAL_VALUES.fetch("row-gap") column = values[1] || row gap_longhands(row, column) end |
.inherited?(property) ⇒ Boolean
103 104 105 |
# File 'lib/silk_layout/css/properties.rb', line 103 def self.inherited?(property) INHERITED.include?(property) end |
.initial_value(property) ⇒ Object
95 96 97 |
# File 'lib/silk_layout/css/properties.rb', line 95 def self.initial_value(property) INITIAL_VALUES[property] end |
.known?(property) ⇒ Boolean
99 100 101 |
# File 'lib/silk_layout/css/properties.rb', line 99 def self.known?(property) KNOWN.include?(property) end |
.numeric?(value) ⇒ Boolean
283 284 285 |
# File 'lib/silk_layout/css/properties.rb', line 283 def self.numeric?(value) value.to_s.match?(/\A[-+]?\d*\.?\d+\z/) end |
.shorthand_expansion(property, value) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/silk_layout/css/properties.rb', line 130 def self.shorthand_expansion(property, value) case property when "margin", "padding" edge_values(value).map { |side, side_value| ["#{property}-#{side}", side_value] } when "border" parsed = border_shorthand(value) all_border_longhands do |side, part| parsed[part] || INITIAL_VALUES.fetch("border-#{side}-#{part}") end when "border-width", "border-style", "border-color" part = property.delete_prefix("border-") edge_values(value).map { |side, side_value| ["border-#{side}-#{part}", side_value] } when /\Aborder-(top|right|bottom|left)\z/ side = Regexp.last_match(1) parsed = border_shorthand(value) border_side_longhands(side) do |part| parsed[part] || INITIAL_VALUES.fetch("border-#{side}-#{part}") end when "background" [["background-color", background_color(value)]] when "flex" flex_shorthand(value).map { |part, part_value| ["flex-#{part}", part_value] } when "flex-flow" flex_flow(value).map { |part, part_value| ["flex-#{part}", part_value] } when "gap" gap_values(value) else [] end end |
.split_tokens(value) ⇒ Object
279 280 281 |
# File 'lib/silk_layout/css/properties.rb', line 279 def self.split_tokens(value) Values.split_tokens(value) end |