Skip to content

Commit 2e8ef21

Browse files
author
Ruben Swieringa
committed
Add support for numbers in functions
1 parent c6d5941 commit 2e8ef21

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.bundle/
22
/.yardoc
3+
/.rvmrc
34
/Gemfile.lock
45
/_yardoc/
56
/coverage/

lib/sassc/native/native_functions_api.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ module Native
3737
# ADDAPI bool ADDCALL sass_string_is_quoted(const union Sass_Value* v);
3838
attach_function :sass_string_is_quoted, [:sass_value_ptr], :bool
3939

40+
# ADDAPI const char* ADDCALL sass_number_get_value (const union Sass_Value* v);
41+
attach_function :sass_number_get_value, [:sass_value_ptr], :double
42+
43+
# ADDAPI const char* ADDCALL sass_number_get_unit (const union Sass_Value* v);
44+
attach_function :sass_number_get_unit, [:sass_value_ptr], :string
45+
4046
def self.string_get_type(native_value)
4147
string_is_quoted(native_value) ? :string : :identifier
4248
end

lib/sassc/script/value_conversion.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def self.from_native(native_value, options)
1010
type = Native.string_get_type(native_value)
1111
argument = Script::String.new(value, type)
1212

13+
argument
14+
when :sass_number
15+
value = Native.number_get_value(native_value)
16+
unit = Native.number_get_unit(native_value)
17+
argument = Sass::Script::Value::Number.new(value, unit)
18+
1319
argument
1420
when :sass_color
1521
red, green, blue, alpha = Native.color_get_r(native_value), Native.color_get_g(native_value), Native.color_get_b(native_value), Native.color_get_a(native_value)
@@ -29,6 +35,8 @@ def self.to_native(value)
2935
String.new(value).to_native
3036
when "Color"
3137
Color.new(value).to_native
38+
when "Number"
39+
Number.new(value).to_native
3240
else
3341
raise UnsupportedValue.new("Sass return type #{value_name} unsupported")
3442
end
@@ -39,4 +47,5 @@ def self.to_native(value)
3947

4048
require_relative "value_conversion/base"
4149
require_relative "value_conversion/string"
50+
require_relative "value_conversion/number"
4251
require_relative "value_conversion/color"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module SassC
2+
module Script
3+
module ValueConversion
4+
class Number < Base
5+
def to_native
6+
Native::make_number(@value.value, @value.numerator_units.first)
7+
end
8+
end
9+
end
10+
end
11+
end

test/functions_test.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ def test_function_that_returns_a_color
6464
CSS
6565
end
6666

67+
def test_function_that_returns_a_number
68+
assert_sass <<-SCSS, <<-CSS
69+
div { width: returns-a-number(); }
70+
SCSS
71+
div { width: -312rem; }
72+
CSS
73+
end
74+
75+
def test_function_that_takes_a_number
76+
assert_sass <<-SCSS, <<-CSS
77+
div { display: returns-arg(42.1px); }
78+
SCSS
79+
div { display: 42.1px; }
80+
CSS
81+
end
82+
6783
def test_function_with_optional_arguments
6884
assert_sass <<-SCSS, <<-EXPECTED_CSS
6985
div {
@@ -87,14 +103,14 @@ def test_functions_may_accept_sass_color_type
87103
end
88104

89105
def test_function_with_unsupported_tag
90-
engine = Engine.new("div {url: function_with_unsupported_tag(1);}")
106+
engine = Engine.new("div {url: function_with_unsupported_tag(());}")
91107

92108
exception = assert_raises(SassC::SyntaxError) do
93109
engine.render
94110
end
95111

96-
assert_match /Sass argument of type sass_number unsupported/, exception.message
97-
assert_equal "[SassC::FunctionsHandler] Sass argument of type sass_number unsupported", stderr_output
112+
assert_match /Sass argument of type sass_list unsupported/, exception.message
113+
assert_equal "[SassC::FunctionsHandler] Sass argument of type sass_list unsupported", stderr_output
98114
end
99115

100116
def test_function_with_error
@@ -151,7 +167,7 @@ def function_that_raises_errors
151167
raise StandardError, "Intentional wrong thing happened somewhere inside the custom function"
152168
end
153169

154-
def function_with_unsupported_tag(number)
170+
def function_with_unsupported_tag(value)
155171
end
156172

157173
def nice_color_argument(color)
@@ -162,6 +178,14 @@ def returns_a_color
162178
return Script::Color.new(red: 0, green: 0, blue: 0)
163179
end
164180

181+
def returns_a_number
182+
return Sass::Script::Value::Number.new(-312,'rem')
183+
end
184+
185+
def returns_arg ( arg )
186+
return arg
187+
end
188+
165189
def returns_sass_value
166190
return Sass::Script::Value::Color.new(red: 0, green: 0, blue: 0)
167191
end

test/native_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def test_custom_function
9696
assert_equal :sass_string, Native.value_get_tag(string)
9797
assert_equal "hello", Native.string_get_value(string)
9898

99+
number = Native.make_number(123.4, "rem")
100+
assert_equal 123.4, Native.number_get_value(number)
101+
assert_equal "rem", Native.number_get_unit(number)
102+
99103
Native.compile_data_context(data_context)
100104

101105
css = Native.context_get_output_string(context)

0 commit comments

Comments
 (0)