Skip to content

Commit 1198af3

Browse files
author
Ruben Swieringa
committed
Add support for maps in functions
1 parent 2e8ef21 commit 1198af3

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lib/sassc/native/native_functions_api.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ module Native
2424
# ADDAPI union Sass_Value* ADDCALL sass_make_color (double r, double g, double b, double a);
2525
attach_function :sass_make_color, [:double, :double, :double, :double], :sass_value_ptr
2626

27+
# ADDAPI union Sass_Value* ADDCALL sass_make_map (size_t len);
28+
attach_function :sass_make_map, [:size_t], :sass_value_ptr
29+
30+
# ADDAPI void ADDCALL sass_map_set_key (union Sass_Value* v, size_t i, union Sass_Value*);
31+
attach_function :sass_map_set_key, [:sass_value_ptr, :size_t, :sass_value_ptr], :void
32+
33+
# ADDAPI union Sass_Value* ADDCALL sass_map_get_key (const union Sass_Value* v, size_t i);
34+
attach_function :sass_map_get_key, [:sass_value_ptr, :size_t], :sass_value_ptr
35+
36+
# ADDAPI void ADDCALL sass_map_set_value (union Sass_Value* v, size_t i, union Sass_Value*);
37+
attach_function :sass_map_set_value, [:sass_value_ptr, :size_t, :sass_value_ptr], :void
38+
39+
# ADDAPI union Sass_Value* ADDCALL sass_map_get_value (const union Sass_Value* v, size_t i);
40+
attach_function :sass_map_get_value, [:sass_value_ptr, :size_t], :sass_value_ptr
41+
42+
# ADDAPI size_t ADDCALL sass_map_get_length (const union Sass_Value* v);
43+
attach_function :sass_map_get_length, [:sass_value_ptr], :size_t
44+
2745
# ADDAPI union Sass_Value* ADDCALL sass_make_error (const char* msg);
2846
attach_function :sass_make_error, [:string], :sass_value_ptr
2947

lib/sassc/script/value_conversion.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ def self.from_native(native_value, options)
2323
argument = Script::Color.new([red, green, blue, alpha])
2424
argument.options = options
2525

26+
argument
27+
when :sass_map
28+
values = {}
29+
length = Native::map_get_length native_value
30+
31+
(0..length-1).each do |index|
32+
key = Native::map_get_key(native_value, index)
33+
value = Native::map_get_value(native_value, index)
34+
values[from_native(key, options)] = from_native(value, options)
35+
end
36+
37+
argument = Sass::Script::Value::Map.new values
2638
argument
2739
else
2840
raise UnsupportedValue.new("Sass argument of type #{value_tag} unsupported")
@@ -37,6 +49,8 @@ def self.to_native(value)
3749
Color.new(value).to_native
3850
when "Number"
3951
Number.new(value).to_native
52+
when "Map"
53+
Map.new(value).to_native
4054
else
4155
raise UnsupportedValue.new("Sass return type #{value_name} unsupported")
4256
end
@@ -49,3 +63,4 @@ def self.to_native(value)
4963
require_relative "value_conversion/string"
5064
require_relative "value_conversion/number"
5165
require_relative "value_conversion/color"
66+
require_relative "value_conversion/map"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module SassC
2+
module Script
3+
module ValueConversion
4+
class Map < Base
5+
def to_native
6+
hash = @value.to_h
7+
native_map = Native::make_map( hash.size )
8+
hash.each_with_index do |(key, value), index|
9+
key = ValueConversion.to_native key
10+
value = ValueConversion.to_native value
11+
Native::map_set_key( native_map, index, key )
12+
Native::map_set_value( native_map, index, value )
13+
end
14+
return native_map
15+
end
16+
end
17+
end
18+
end
19+
end

test/functions_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ def test_function_that_returns_a_sass_value
133133
CSS
134134
end
135135

136+
def test_function_that_returns_a_sass_map
137+
assert_sass <<-SCSS, <<-CSS
138+
$my-map: returns-sass-map();
139+
div { background: map-get( $my-map, color ); }
140+
SCSS
141+
div { background: black; }
142+
CSS
143+
end
144+
145+
def test_function_that_takes_a_sass_map
146+
assert_sass <<-SCSS, <<-CSS
147+
div { background-color: map-get( returns-arg(( dark: black, light: white )), dark ); }
148+
SCSS
149+
div { background-color: black; }
150+
CSS
151+
end
152+
136153
private
137154

138155
def assert_sass(sass, expected_css)
@@ -190,6 +207,15 @@ def returns_sass_value
190207
return Sass::Script::Value::Color.new(red: 0, green: 0, blue: 0)
191208
end
192209

210+
def returns_sass_map
211+
key = Script::String.new("color", "string")
212+
value = Sass::Script::Value::Color.new(red: 0, green: 0, blue: 0)
213+
values = {}
214+
values[key] = value
215+
map = Sass::Script::Value::Map.new values
216+
return map
217+
end
218+
193219
module Compass
194220
def stylesheet_path(path)
195221
Script::String.new("/css/#{path.value}", :identifier)

0 commit comments

Comments
 (0)