xass 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +39 -0
- data/README.md +131 -193
- data/lib/initialize.rb +8 -0
- data/lib/xass.rb +67 -0
- data/spec/secrets.yml +4 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/test_spec.rb +5 -0
- data/template/secrets.yml +2 -0
- data/xass.gemspec +15 -0
- metadata +53 -294
- data/.yardopts +0 -11
- data/CONTRIBUTING +0 -3
- data/MIT-LICENSE +0 -20
- data/Rakefile +0 -349
- data/VERSION +0 -1
- data/VERSION_NAME +0 -1
- data/bin/push +0 -13
- data/bin/sass +0 -13
- data/bin/sass-convert +0 -12
- data/bin/scss +0 -13
- data/extra/update_watch.rb +0 -13
- data/init.rb +0 -18
- data/lib/sass/cache_stores/base.rb +0 -88
- data/lib/sass/cache_stores/chain.rb +0 -33
- data/lib/sass/cache_stores/filesystem.rb +0 -64
- data/lib/sass/cache_stores/memory.rb +0 -47
- data/lib/sass/cache_stores/null.rb +0 -25
- data/lib/sass/cache_stores.rb +0 -15
- data/lib/sass/callbacks.rb +0 -66
- data/lib/sass/css.rb +0 -409
- data/lib/sass/engine.rb +0 -930
- data/lib/sass/environment.rb +0 -101
- data/lib/sass/error.rb +0 -201
- data/lib/sass/exec.rb +0 -707
- data/lib/sass/importers/base.rb +0 -139
- data/lib/sass/importers/filesystem.rb +0 -186
- data/lib/sass/importers.rb +0 -22
- data/lib/sass/logger/base.rb +0 -32
- data/lib/sass/logger/log_level.rb +0 -49
- data/lib/sass/logger.rb +0 -15
- data/lib/sass/media.rb +0 -213
- data/lib/sass/plugin/compiler.rb +0 -406
- data/lib/sass/plugin/configuration.rb +0 -123
- data/lib/sass/plugin/generic.rb +0 -15
- data/lib/sass/plugin/merb.rb +0 -48
- data/lib/sass/plugin/rack.rb +0 -60
- data/lib/sass/plugin/rails.rb +0 -47
- data/lib/sass/plugin/staleness_checker.rb +0 -199
- data/lib/sass/plugin.rb +0 -133
- data/lib/sass/railtie.rb +0 -10
- data/lib/sass/repl.rb +0 -57
- data/lib/sass/root.rb +0 -7
- data/lib/sass/script/arg_list.rb +0 -52
- data/lib/sass/script/bool.rb +0 -18
- data/lib/sass/script/color.rb +0 -606
- data/lib/sass/script/css_lexer.rb +0 -29
- data/lib/sass/script/css_parser.rb +0 -31
- data/lib/sass/script/funcall.rb +0 -245
- data/lib/sass/script/functions.rb +0 -1543
- data/lib/sass/script/interpolation.rb +0 -79
- data/lib/sass/script/lexer.rb +0 -345
- data/lib/sass/script/list.rb +0 -85
- data/lib/sass/script/literal.rb +0 -221
- data/lib/sass/script/node.rb +0 -99
- data/lib/sass/script/null.rb +0 -37
- data/lib/sass/script/number.rb +0 -453
- data/lib/sass/script/operation.rb +0 -110
- data/lib/sass/script/parser.rb +0 -502
- data/lib/sass/script/string.rb +0 -51
- data/lib/sass/script/string_interpolation.rb +0 -103
- data/lib/sass/script/unary_operation.rb +0 -69
- data/lib/sass/script/variable.rb +0 -58
- data/lib/sass/script.rb +0 -39
- data/lib/sass/scss/css_parser.rb +0 -36
- data/lib/sass/scss/parser.rb +0 -1180
- data/lib/sass/scss/rx.rb +0 -133
- data/lib/sass/scss/script_lexer.rb +0 -15
- data/lib/sass/scss/script_parser.rb +0 -25
- data/lib/sass/scss/static_parser.rb +0 -54
- data/lib/sass/scss.rb +0 -16
- data/lib/sass/selector/abstract_sequence.rb +0 -94
- data/lib/sass/selector/comma_sequence.rb +0 -92
- data/lib/sass/selector/sequence.rb +0 -507
- data/lib/sass/selector/simple.rb +0 -119
- data/lib/sass/selector/simple_sequence.rb +0 -215
- data/lib/sass/selector.rb +0 -452
- data/lib/sass/shared.rb +0 -76
- data/lib/sass/supports.rb +0 -229
- data/lib/sass/tree/charset_node.rb +0 -22
- data/lib/sass/tree/comment_node.rb +0 -82
- data/lib/sass/tree/content_node.rb +0 -9
- data/lib/sass/tree/css_import_node.rb +0 -60
- data/lib/sass/tree/debug_node.rb +0 -18
- data/lib/sass/tree/directive_node.rb +0 -42
- data/lib/sass/tree/each_node.rb +0 -24
- data/lib/sass/tree/extend_node.rb +0 -36
- data/lib/sass/tree/for_node.rb +0 -36
- data/lib/sass/tree/function_node.rb +0 -34
- data/lib/sass/tree/if_node.rb +0 -52
- data/lib/sass/tree/import_node.rb +0 -75
- data/lib/sass/tree/media_node.rb +0 -58
- data/lib/sass/tree/mixin_def_node.rb +0 -38
- data/lib/sass/tree/mixin_node.rb +0 -39
- data/lib/sass/tree/node.rb +0 -196
- data/lib/sass/tree/prop_node.rb +0 -152
- data/lib/sass/tree/return_node.rb +0 -18
- data/lib/sass/tree/root_node.rb +0 -78
- data/lib/sass/tree/rule_node.rb +0 -132
- data/lib/sass/tree/supports_node.rb +0 -51
- data/lib/sass/tree/trace_node.rb +0 -32
- data/lib/sass/tree/variable_node.rb +0 -30
- data/lib/sass/tree/visitors/base.rb +0 -75
- data/lib/sass/tree/visitors/check_nesting.rb +0 -147
- data/lib/sass/tree/visitors/convert.rb +0 -316
- data/lib/sass/tree/visitors/cssize.rb +0 -241
- data/lib/sass/tree/visitors/deep_copy.rb +0 -102
- data/lib/sass/tree/visitors/extend.rb +0 -68
- data/lib/sass/tree/visitors/perform.rb +0 -446
- data/lib/sass/tree/visitors/set_options.rb +0 -125
- data/lib/sass/tree/visitors/to_css.rb +0 -228
- data/lib/sass/tree/warn_node.rb +0 -18
- data/lib/sass/tree/while_node.rb +0 -18
- data/lib/sass/util/multibyte_string_scanner.rb +0 -155
- data/lib/sass/util/subset_map.rb +0 -109
- data/lib/sass/util/test.rb +0 -10
- data/lib/sass/util.rb +0 -948
- data/lib/sass/version.rb +0 -126
- data/lib/sass.rb +0 -95
- data/rails/init.rb +0 -1
- data/test/Gemfile +0 -3
- data/test/Gemfile.lock +0 -10
- data/test/sass/cache_test.rb +0 -89
- data/test/sass/callbacks_test.rb +0 -61
- data/test/sass/conversion_test.rb +0 -1760
- data/test/sass/css2sass_test.rb +0 -458
- data/test/sass/data/hsl-rgb.txt +0 -319
- data/test/sass/engine_test.rb +0 -3244
- data/test/sass/exec_test.rb +0 -86
- data/test/sass/extend_test.rb +0 -1482
- data/test/sass/fixtures/test_staleness_check_across_importers.css +0 -1
- data/test/sass/fixtures/test_staleness_check_across_importers.scss +0 -1
- data/test/sass/functions_test.rb +0 -1139
- data/test/sass/importer_test.rb +0 -192
- data/test/sass/logger_test.rb +0 -58
- data/test/sass/mock_importer.rb +0 -49
- data/test/sass/more_results/more1.css +0 -9
- data/test/sass/more_results/more1_with_line_comments.css +0 -26
- data/test/sass/more_results/more_import.css +0 -29
- data/test/sass/more_templates/_more_partial.sass +0 -2
- data/test/sass/more_templates/more1.sass +0 -23
- data/test/sass/more_templates/more_import.sass +0 -11
- data/test/sass/plugin_test.rb +0 -564
- data/test/sass/results/alt.css +0 -4
- data/test/sass/results/basic.css +0 -9
- data/test/sass/results/cached_import_option.css +0 -3
- data/test/sass/results/compact.css +0 -5
- data/test/sass/results/complex.css +0 -86
- data/test/sass/results/compressed.css +0 -1
- data/test/sass/results/expanded.css +0 -19
- data/test/sass/results/filename_fn.css +0 -3
- data/test/sass/results/if.css +0 -3
- data/test/sass/results/import.css +0 -31
- data/test/sass/results/import_charset.css +0 -5
- data/test/sass/results/import_charset_1_8.css +0 -5
- data/test/sass/results/import_charset_ibm866.css +0 -5
- data/test/sass/results/import_content.css +0 -1
- data/test/sass/results/line_numbers.css +0 -49
- data/test/sass/results/mixins.css +0 -95
- data/test/sass/results/multiline.css +0 -24
- data/test/sass/results/nested.css +0 -22
- data/test/sass/results/options.css +0 -1
- data/test/sass/results/parent_ref.css +0 -13
- data/test/sass/results/script.css +0 -16
- data/test/sass/results/scss_import.css +0 -31
- data/test/sass/results/scss_importee.css +0 -2
- data/test/sass/results/subdir/nested_subdir/nested_subdir.css +0 -1
- data/test/sass/results/subdir/subdir.css +0 -3
- data/test/sass/results/units.css +0 -11
- data/test/sass/results/warn_imported.css +0 -0
- data/test/sass/script_conversion_test.rb +0 -299
- data/test/sass/script_test.rb +0 -622
- data/test/sass/scss/css_test.rb +0 -1100
- data/test/sass/scss/rx_test.rb +0 -156
- data/test/sass/scss/scss_test.rb +0 -2106
- data/test/sass/scss/test_helper.rb +0 -37
- data/test/sass/templates/_cached_import_option_partial.scss +0 -1
- data/test/sass/templates/_double_import_loop2.sass +0 -1
- data/test/sass/templates/_filename_fn_import.scss +0 -11
- data/test/sass/templates/_imported_charset_ibm866.sass +0 -4
- data/test/sass/templates/_imported_charset_utf8.sass +0 -4
- data/test/sass/templates/_imported_content.sass +0 -3
- data/test/sass/templates/_partial.sass +0 -2
- data/test/sass/templates/_same_name_different_partiality.scss +0 -1
- data/test/sass/templates/alt.sass +0 -16
- data/test/sass/templates/basic.sass +0 -23
- data/test/sass/templates/bork1.sass +0 -2
- data/test/sass/templates/bork2.sass +0 -2
- data/test/sass/templates/bork3.sass +0 -2
- data/test/sass/templates/bork4.sass +0 -2
- data/test/sass/templates/bork5.sass +0 -3
- data/test/sass/templates/cached_import_option.scss +0 -3
- data/test/sass/templates/compact.sass +0 -17
- data/test/sass/templates/complex.sass +0 -305
- data/test/sass/templates/compressed.sass +0 -15
- data/test/sass/templates/double_import_loop1.sass +0 -1
- data/test/sass/templates/expanded.sass +0 -17
- data/test/sass/templates/filename_fn.scss +0 -18
- data/test/sass/templates/if.sass +0 -11
- data/test/sass/templates/import.sass +0 -12
- data/test/sass/templates/import_charset.sass +0 -9
- data/test/sass/templates/import_charset_1_8.sass +0 -6
- data/test/sass/templates/import_charset_ibm866.sass +0 -11
- data/test/sass/templates/import_content.sass +0 -4
- data/test/sass/templates/importee.less +0 -2
- data/test/sass/templates/importee.sass +0 -19
- data/test/sass/templates/line_numbers.sass +0 -13
- data/test/sass/templates/mixin_bork.sass +0 -5
- data/test/sass/templates/mixins.sass +0 -76
- data/test/sass/templates/multiline.sass +0 -20
- data/test/sass/templates/nested.sass +0 -25
- data/test/sass/templates/nested_bork1.sass +0 -2
- data/test/sass/templates/nested_bork2.sass +0 -2
- data/test/sass/templates/nested_bork3.sass +0 -2
- data/test/sass/templates/nested_bork4.sass +0 -2
- data/test/sass/templates/nested_import.sass +0 -2
- data/test/sass/templates/nested_mixin_bork.sass +0 -6
- data/test/sass/templates/options.sass +0 -2
- data/test/sass/templates/parent_ref.sass +0 -25
- data/test/sass/templates/same_name_different_ext.sass +0 -2
- data/test/sass/templates/same_name_different_ext.scss +0 -1
- data/test/sass/templates/same_name_different_partiality.scss +0 -1
- data/test/sass/templates/script.sass +0 -101
- data/test/sass/templates/scss_import.scss +0 -11
- data/test/sass/templates/scss_importee.scss +0 -1
- data/test/sass/templates/single_import_loop.sass +0 -1
- data/test/sass/templates/subdir/import_up1.scss +0 -1
- data/test/sass/templates/subdir/import_up2.scss +0 -1
- data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +0 -2
- data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +0 -3
- data/test/sass/templates/subdir/subdir.sass +0 -6
- data/test/sass/templates/units.sass +0 -11
- data/test/sass/templates/warn.sass +0 -3
- data/test/sass/templates/warn_imported.sass +0 -4
- data/test/sass/test_helper.rb +0 -8
- data/test/sass/util/multibyte_string_scanner_test.rb +0 -147
- data/test/sass/util/subset_map_test.rb +0 -91
- data/test/sass/util_test.rb +0 -382
- data/test/test_helper.rb +0 -80
- /data/{test/sass/results/warn.css → template/files/.gitkeep} +0 -0
data/lib/sass/script/number.rb
DELETED
@@ -1,453 +0,0 @@
|
|
1
|
-
require 'sass/script/literal'
|
2
|
-
|
3
|
-
module Sass::Script
|
4
|
-
# A SassScript object representing a number.
|
5
|
-
# SassScript numbers can have decimal values,
|
6
|
-
# and can also have units.
|
7
|
-
# For example, `12`, `1px`, and `10.45em`
|
8
|
-
# are all valid values.
|
9
|
-
#
|
10
|
-
# Numbers can also have more complex units, such as `1px*em/in`.
|
11
|
-
# These cannot be inputted directly in Sass code at the moment.
|
12
|
-
class Number < Literal
|
13
|
-
# The Ruby value of the number.
|
14
|
-
#
|
15
|
-
# @return [Numeric]
|
16
|
-
attr_reader :value
|
17
|
-
|
18
|
-
# A list of units in the numerator of the number.
|
19
|
-
# For example, `1px*em/in*cm` would return `["px", "em"]`
|
20
|
-
# @return [Array<String>]
|
21
|
-
attr_reader :numerator_units
|
22
|
-
|
23
|
-
# A list of units in the denominator of the number.
|
24
|
-
# For example, `1px*em/in*cm` would return `["in", "cm"]`
|
25
|
-
# @return [Array<String>]
|
26
|
-
attr_reader :denominator_units
|
27
|
-
|
28
|
-
# The original representation of this number.
|
29
|
-
# For example, although the result of `1px/2px` is `0.5`,
|
30
|
-
# the value of `#original` is `"1px/2px"`.
|
31
|
-
#
|
32
|
-
# This is only non-nil when the original value should be used as the CSS value,
|
33
|
-
# as in `font: 1px/2px`.
|
34
|
-
#
|
35
|
-
# @return [Boolean, nil]
|
36
|
-
attr_accessor :original
|
37
|
-
|
38
|
-
def self.precision
|
39
|
-
@precision ||= 5
|
40
|
-
end
|
41
|
-
|
42
|
-
# Sets the number of digits of precision
|
43
|
-
# For example, if this is `3`,
|
44
|
-
# `3.1415926` will be printed as `3.142`.
|
45
|
-
def self.precision=(digits)
|
46
|
-
@precision = digits.round
|
47
|
-
@precision_factor = 10.0**@precision
|
48
|
-
end
|
49
|
-
|
50
|
-
# the precision factor used in numeric output
|
51
|
-
# it is derived from the `precision` method.
|
52
|
-
def self.precision_factor
|
53
|
-
@precision_factor ||= 10.0**precision
|
54
|
-
end
|
55
|
-
|
56
|
-
# Handles the deprecation warning for the PRECISION constant
|
57
|
-
# This can be removed in 3.2.
|
58
|
-
def self.const_missing(const)
|
59
|
-
if const == :PRECISION
|
60
|
-
Sass::Util.sass_warn("Sass::Script::Number::PRECISION is deprecated and will be removed in a future release. Use Sass::Script::Number.precision_factor instead.")
|
61
|
-
const_set(:PRECISION, self.precision_factor)
|
62
|
-
else
|
63
|
-
super
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# Used so we don't allocate two new arrays for each new number.
|
68
|
-
NO_UNITS = []
|
69
|
-
|
70
|
-
# @param value [Numeric] The value of the number
|
71
|
-
# @param numerator_units [Array<String>] See \{#numerator\_units}
|
72
|
-
# @param denominator_units [Array<String>] See \{#denominator\_units}
|
73
|
-
def initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS)
|
74
|
-
super(value)
|
75
|
-
@numerator_units = numerator_units
|
76
|
-
@denominator_units = denominator_units
|
77
|
-
normalize!
|
78
|
-
end
|
79
|
-
|
80
|
-
# The SassScript `+` operation.
|
81
|
-
# Its functionality depends on the type of its argument:
|
82
|
-
#
|
83
|
-
# {Number}
|
84
|
-
# : Adds the two numbers together, converting units if possible.
|
85
|
-
#
|
86
|
-
# {Color}
|
87
|
-
# : Adds this number to each of the RGB color channels.
|
88
|
-
#
|
89
|
-
# {Literal}
|
90
|
-
# : See {Literal#plus}.
|
91
|
-
#
|
92
|
-
# @param other [Literal] The right-hand side of the operator
|
93
|
-
# @return [Literal] The result of the operation
|
94
|
-
# @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
|
95
|
-
def plus(other)
|
96
|
-
if other.is_a? Number
|
97
|
-
operate(other, :+)
|
98
|
-
elsif other.is_a?(Color)
|
99
|
-
other.plus(self)
|
100
|
-
else
|
101
|
-
super
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
# The SassScript binary `-` operation (e.g. `$a - $b`).
|
106
|
-
# Its functionality depends on the type of its argument:
|
107
|
-
#
|
108
|
-
# {Number}
|
109
|
-
# : Subtracts this number from the other, converting units if possible.
|
110
|
-
#
|
111
|
-
# {Literal}
|
112
|
-
# : See {Literal#minus}.
|
113
|
-
#
|
114
|
-
# @param other [Literal] The right-hand side of the operator
|
115
|
-
# @return [Literal] The result of the operation
|
116
|
-
# @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
|
117
|
-
def minus(other)
|
118
|
-
if other.is_a? Number
|
119
|
-
operate(other, :-)
|
120
|
-
else
|
121
|
-
super
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
# The SassScript unary `+` operation (e.g. `+$a`).
|
126
|
-
#
|
127
|
-
# @return [Number] The value of this number
|
128
|
-
def unary_plus
|
129
|
-
self
|
130
|
-
end
|
131
|
-
|
132
|
-
# The SassScript unary `-` operation (e.g. `-$a`).
|
133
|
-
#
|
134
|
-
# @return [Number] The negative value of this number
|
135
|
-
def unary_minus
|
136
|
-
Number.new(-value, @numerator_units, @denominator_units)
|
137
|
-
end
|
138
|
-
|
139
|
-
# The SassScript `*` operation.
|
140
|
-
# Its functionality depends on the type of its argument:
|
141
|
-
#
|
142
|
-
# {Number}
|
143
|
-
# : Multiplies the two numbers together, converting units appropriately.
|
144
|
-
#
|
145
|
-
# {Color}
|
146
|
-
# : Multiplies each of the RGB color channels by this number.
|
147
|
-
#
|
148
|
-
# @param other [Number, Color] The right-hand side of the operator
|
149
|
-
# @return [Number, Color] The result of the operation
|
150
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
151
|
-
def times(other)
|
152
|
-
if other.is_a? Number
|
153
|
-
operate(other, :*)
|
154
|
-
elsif other.is_a? Color
|
155
|
-
other.times(self)
|
156
|
-
else
|
157
|
-
raise NoMethodError.new(nil, :times)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
# The SassScript `/` operation.
|
162
|
-
# Its functionality depends on the type of its argument:
|
163
|
-
#
|
164
|
-
# {Number}
|
165
|
-
# : Divides this number by the other, converting units appropriately.
|
166
|
-
#
|
167
|
-
# {Literal}
|
168
|
-
# : See {Literal#div}.
|
169
|
-
#
|
170
|
-
# @param other [Literal] The right-hand side of the operator
|
171
|
-
# @return [Literal] The result of the operation
|
172
|
-
def div(other)
|
173
|
-
if other.is_a? Number
|
174
|
-
res = operate(other, :/)
|
175
|
-
if self.original && other.original
|
176
|
-
res.original = "#{self.original}/#{other.original}"
|
177
|
-
end
|
178
|
-
res
|
179
|
-
else
|
180
|
-
super
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
# The SassScript `%` operation.
|
185
|
-
#
|
186
|
-
# @param other [Number] The right-hand side of the operator
|
187
|
-
# @return [Number] This number modulo the other
|
188
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
189
|
-
# @raise [Sass::UnitConversionError] if `other` has any units
|
190
|
-
def mod(other)
|
191
|
-
if other.is_a?(Number)
|
192
|
-
unless other.unitless?
|
193
|
-
raise Sass::UnitConversionError.new("Cannot modulo by a number with units: #{other.inspect}.")
|
194
|
-
end
|
195
|
-
operate(other, :%)
|
196
|
-
else
|
197
|
-
raise NoMethodError.new(nil, :mod)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
# The SassScript `==` operation.
|
202
|
-
#
|
203
|
-
# @param other [Literal] The right-hand side of the operator
|
204
|
-
# @return [Boolean] Whether this number is equal to the other object
|
205
|
-
def eq(other)
|
206
|
-
return Sass::Script::Bool.new(false) unless other.is_a?(Sass::Script::Number)
|
207
|
-
this = self
|
208
|
-
begin
|
209
|
-
if unitless?
|
210
|
-
this = this.coerce(other.numerator_units, other.denominator_units)
|
211
|
-
else
|
212
|
-
other = other.coerce(@numerator_units, @denominator_units)
|
213
|
-
end
|
214
|
-
rescue Sass::UnitConversionError
|
215
|
-
return Sass::Script::Bool.new(false)
|
216
|
-
end
|
217
|
-
|
218
|
-
Sass::Script::Bool.new(this.value == other.value)
|
219
|
-
end
|
220
|
-
|
221
|
-
# The SassScript `>` operation.
|
222
|
-
#
|
223
|
-
# @param other [Number] The right-hand side of the operator
|
224
|
-
# @return [Boolean] Whether this number is greater than the other
|
225
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
226
|
-
def gt(other)
|
227
|
-
raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
|
228
|
-
operate(other, :>)
|
229
|
-
end
|
230
|
-
|
231
|
-
# The SassScript `>=` operation.
|
232
|
-
#
|
233
|
-
# @param other [Number] The right-hand side of the operator
|
234
|
-
# @return [Boolean] Whether this number is greater than or equal to the other
|
235
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
236
|
-
def gte(other)
|
237
|
-
raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
|
238
|
-
operate(other, :>=)
|
239
|
-
end
|
240
|
-
|
241
|
-
# The SassScript `<` operation.
|
242
|
-
#
|
243
|
-
# @param other [Number] The right-hand side of the operator
|
244
|
-
# @return [Boolean] Whether this number is less than the other
|
245
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
246
|
-
def lt(other)
|
247
|
-
raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
|
248
|
-
operate(other, :<)
|
249
|
-
end
|
250
|
-
|
251
|
-
# The SassScript `<=` operation.
|
252
|
-
#
|
253
|
-
# @param other [Number] The right-hand side of the operator
|
254
|
-
# @return [Boolean] Whether this number is less than or equal to the other
|
255
|
-
# @raise [NoMethodError] if `other` is an invalid type
|
256
|
-
def lte(other)
|
257
|
-
raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
|
258
|
-
operate(other, :<=)
|
259
|
-
end
|
260
|
-
|
261
|
-
# @return [String] The CSS representation of this number
|
262
|
-
# @raise [Sass::SyntaxError] if this number has units that can't be used in CSS
|
263
|
-
# (e.g. `px*in`)
|
264
|
-
def to_s(opts = {})
|
265
|
-
return original if original
|
266
|
-
raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
|
267
|
-
inspect
|
268
|
-
end
|
269
|
-
|
270
|
-
# Returns a readable representation of this number.
|
271
|
-
#
|
272
|
-
# This representation is valid CSS (and valid SassScript)
|
273
|
-
# as long as there is only one unit.
|
274
|
-
#
|
275
|
-
# @return [String] The representation
|
276
|
-
def inspect(opts = {})
|
277
|
-
value = self.class.round(self.value)
|
278
|
-
unitless? ? value.to_s : "#{value}#{unit_str}"
|
279
|
-
end
|
280
|
-
alias_method :to_sass, :inspect
|
281
|
-
|
282
|
-
# @return [Fixnum] The integer value of the number
|
283
|
-
# @raise [Sass::SyntaxError] if the number isn't an integer
|
284
|
-
def to_i
|
285
|
-
super unless int?
|
286
|
-
return value
|
287
|
-
end
|
288
|
-
|
289
|
-
# @return [Boolean] Whether or not this number is an integer.
|
290
|
-
def int?
|
291
|
-
value % 1 == 0.0
|
292
|
-
end
|
293
|
-
|
294
|
-
# @return [Boolean] Whether or not this number has no units.
|
295
|
-
def unitless?
|
296
|
-
@numerator_units.empty? && @denominator_units.empty?
|
297
|
-
end
|
298
|
-
|
299
|
-
# @return [Boolean] Whether or not this number has units that can be represented in CSS
|
300
|
-
# (that is, zero or one \{#numerator\_units}).
|
301
|
-
def legal_units?
|
302
|
-
(@numerator_units.empty? || @numerator_units.size == 1) && @denominator_units.empty?
|
303
|
-
end
|
304
|
-
|
305
|
-
# Returns this number converted to other units.
|
306
|
-
# The conversion takes into account the relationship between e.g. mm and cm,
|
307
|
-
# as well as between e.g. in and cm.
|
308
|
-
#
|
309
|
-
# If this number has no units, it will simply return itself
|
310
|
-
# with the given units.
|
311
|
-
#
|
312
|
-
# An incompatible coercion, e.g. between px and cm, will raise an error.
|
313
|
-
#
|
314
|
-
# @param num_units [Array<String>] The numerator units to coerce this number into.
|
315
|
-
# See {\#numerator\_units}
|
316
|
-
# @param den_units [Array<String>] The denominator units to coerce this number into.
|
317
|
-
# See {\#denominator\_units}
|
318
|
-
# @return [Number] The number with the new units
|
319
|
-
# @raise [Sass::UnitConversionError] if the given units are incompatible with the number's
|
320
|
-
# current units
|
321
|
-
def coerce(num_units, den_units)
|
322
|
-
Number.new(if unitless?
|
323
|
-
self.value
|
324
|
-
else
|
325
|
-
self.value * coercion_factor(@numerator_units, num_units) /
|
326
|
-
coercion_factor(@denominator_units, den_units)
|
327
|
-
end, num_units, den_units)
|
328
|
-
end
|
329
|
-
|
330
|
-
# @param other [Number] A number to decide if it can be compared with this number.
|
331
|
-
# @return [Boolean] Whether or not this number can be compared with the other.
|
332
|
-
def comparable_to?(other)
|
333
|
-
begin
|
334
|
-
operate(other, :+)
|
335
|
-
true
|
336
|
-
rescue Sass::UnitConversionError
|
337
|
-
false
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
# Returns a human readable representation of the units in this number.
|
342
|
-
# For complex units this takes the form of:
|
343
|
-
# numerator_unit1 * numerator_unit2 / denominator_unit1 * denominator_unit2
|
344
|
-
# @return [String] a string that represents the units in this number
|
345
|
-
def unit_str
|
346
|
-
rv = @numerator_units.sort.join("*")
|
347
|
-
if @denominator_units.any?
|
348
|
-
rv << "/"
|
349
|
-
rv << @denominator_units.sort.join("*")
|
350
|
-
end
|
351
|
-
rv
|
352
|
-
end
|
353
|
-
|
354
|
-
private
|
355
|
-
|
356
|
-
# @private
|
357
|
-
def self.round(num)
|
358
|
-
if num.is_a?(Float) && (num.infinite? || num.nan?)
|
359
|
-
num
|
360
|
-
elsif num % 1 == 0.0
|
361
|
-
num.to_i
|
362
|
-
else
|
363
|
-
((num * self.precision_factor).round / self.precision_factor).to_f
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
OPERATIONS = [:+, :-, :<=, :<, :>, :>=]
|
368
|
-
|
369
|
-
def operate(other, operation)
|
370
|
-
this = self
|
371
|
-
if OPERATIONS.include?(operation)
|
372
|
-
if unitless?
|
373
|
-
this = this.coerce(other.numerator_units, other.denominator_units)
|
374
|
-
else
|
375
|
-
other = other.coerce(@numerator_units, @denominator_units)
|
376
|
-
end
|
377
|
-
end
|
378
|
-
# avoid integer division
|
379
|
-
value = (:/ == operation) ? this.value.to_f : this.value
|
380
|
-
result = value.send(operation, other.value)
|
381
|
-
|
382
|
-
if result.is_a?(Numeric)
|
383
|
-
Number.new(result, *compute_units(this, other, operation))
|
384
|
-
else # Boolean op
|
385
|
-
Bool.new(result)
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
def coercion_factor(from_units, to_units)
|
390
|
-
# get a list of unmatched units
|
391
|
-
from_units, to_units = sans_common_units(from_units, to_units)
|
392
|
-
|
393
|
-
if from_units.size != to_units.size || !convertable?(from_units | to_units)
|
394
|
-
raise Sass::UnitConversionError.new("Incompatible units: '#{from_units.join('*')}' and '#{to_units.join('*')}'.")
|
395
|
-
end
|
396
|
-
|
397
|
-
from_units.zip(to_units).inject(1) {|m,p| m * conversion_factor(p[0], p[1]) }
|
398
|
-
end
|
399
|
-
|
400
|
-
def compute_units(this, other, operation)
|
401
|
-
case operation
|
402
|
-
when :*
|
403
|
-
[this.numerator_units + other.numerator_units, this.denominator_units + other.denominator_units]
|
404
|
-
when :/
|
405
|
-
[this.numerator_units + other.denominator_units, this.denominator_units + other.numerator_units]
|
406
|
-
else
|
407
|
-
[this.numerator_units, this.denominator_units]
|
408
|
-
end
|
409
|
-
end
|
410
|
-
|
411
|
-
def normalize!
|
412
|
-
return if unitless?
|
413
|
-
@numerator_units, @denominator_units = sans_common_units(@numerator_units, @denominator_units)
|
414
|
-
|
415
|
-
@denominator_units.each_with_index do |d, i|
|
416
|
-
if convertable?(d) && (u = @numerator_units.detect(&method(:convertable?)))
|
417
|
-
@value /= conversion_factor(d, u)
|
418
|
-
@denominator_units.delete_at(i)
|
419
|
-
@numerator_units.delete_at(@numerator_units.index(u))
|
420
|
-
end
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
# A hash of unit names to their index in the conversion table
|
425
|
-
CONVERTABLE_UNITS = {"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4, "px" => 5 }
|
426
|
-
CONVERSION_TABLE = [[ 1, 2.54, 6, 25.4, 72 , 96 ], # in
|
427
|
-
[ nil, 1, 2.36220473, 10, 28.3464567, 37.795275591], # cm
|
428
|
-
[ nil, nil, 1, 4.23333333, 12 , 16 ], # pc
|
429
|
-
[ nil, nil, nil, 1, 2.83464567, 3.7795275591], # mm
|
430
|
-
[ nil, nil, nil, nil, 1 , 1.3333333333], # pt
|
431
|
-
[ nil, nil, nil, nil, nil , 1 ]] # px
|
432
|
-
|
433
|
-
def conversion_factor(from_unit, to_unit)
|
434
|
-
res = CONVERSION_TABLE[CONVERTABLE_UNITS[from_unit]][CONVERTABLE_UNITS[to_unit]]
|
435
|
-
return 1.0 / conversion_factor(to_unit, from_unit) if res.nil?
|
436
|
-
res
|
437
|
-
end
|
438
|
-
|
439
|
-
def convertable?(units)
|
440
|
-
Array(units).all? {|u| CONVERTABLE_UNITS.include?(u)}
|
441
|
-
end
|
442
|
-
|
443
|
-
def sans_common_units(units1, units2)
|
444
|
-
units2 = units2.dup
|
445
|
-
# Can't just use -, because we want px*px to coerce properly to px*mm
|
446
|
-
return units1.map do |u|
|
447
|
-
next u unless j = units2.index(u)
|
448
|
-
units2.delete_at(j)
|
449
|
-
nil
|
450
|
-
end.compact, units2
|
451
|
-
end
|
452
|
-
end
|
453
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'sass/script/string'
|
3
|
-
require 'sass/script/number'
|
4
|
-
require 'sass/script/color'
|
5
|
-
require 'sass/script/functions'
|
6
|
-
require 'sass/script/unary_operation'
|
7
|
-
require 'sass/script/interpolation'
|
8
|
-
require 'sass/script/string_interpolation'
|
9
|
-
|
10
|
-
module Sass::Script
|
11
|
-
# A SassScript parse node representing a binary operation,
|
12
|
-
# such as `$a + $b` or `"foo" + 1`.
|
13
|
-
class Operation < Node
|
14
|
-
attr_reader :operand1
|
15
|
-
attr_reader :operand2
|
16
|
-
attr_reader :operator
|
17
|
-
|
18
|
-
# @param operand1 [Script::Node] The parse-tree node
|
19
|
-
# for the right-hand side of the operator
|
20
|
-
# @param operand2 [Script::Node] The parse-tree node
|
21
|
-
# for the left-hand side of the operator
|
22
|
-
# @param operator [Symbol] The operator to perform.
|
23
|
-
# This should be one of the binary operator names in {Lexer::OPERATORS}
|
24
|
-
def initialize(operand1, operand2, operator)
|
25
|
-
@operand1 = operand1
|
26
|
-
@operand2 = operand2
|
27
|
-
@operator = operator
|
28
|
-
super()
|
29
|
-
end
|
30
|
-
|
31
|
-
# @return [String] A human-readable s-expression representation of the operation
|
32
|
-
def inspect
|
33
|
-
"(#{@operator.inspect} #{@operand1.inspect} #{@operand2.inspect})"
|
34
|
-
end
|
35
|
-
|
36
|
-
# @see Node#to_sass
|
37
|
-
def to_sass(opts = {})
|
38
|
-
o1 = operand_to_sass @operand1, :left, opts
|
39
|
-
o2 = operand_to_sass @operand2, :right, opts
|
40
|
-
sep =
|
41
|
-
case @operator
|
42
|
-
when :comma; ", "
|
43
|
-
when :space; " "
|
44
|
-
else; " #{Lexer::OPERATORS_REVERSE[@operator]} "
|
45
|
-
end
|
46
|
-
"#{o1}#{sep}#{o2}"
|
47
|
-
end
|
48
|
-
|
49
|
-
# Returns the operands for this operation.
|
50
|
-
#
|
51
|
-
# @return [Array<Node>]
|
52
|
-
# @see Node#children
|
53
|
-
def children
|
54
|
-
[@operand1, @operand2]
|
55
|
-
end
|
56
|
-
|
57
|
-
# @see Node#deep_copy
|
58
|
-
def deep_copy
|
59
|
-
node = dup
|
60
|
-
node.instance_variable_set('@operand1', @operand1.deep_copy)
|
61
|
-
node.instance_variable_set('@operand2', @operand2.deep_copy)
|
62
|
-
node
|
63
|
-
end
|
64
|
-
|
65
|
-
protected
|
66
|
-
|
67
|
-
# Evaluates the operation.
|
68
|
-
#
|
69
|
-
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
|
70
|
-
# @return [Literal] The SassScript object that is the value of the operation
|
71
|
-
# @raise [Sass::SyntaxError] if the operation is undefined for the operands
|
72
|
-
def _perform(environment)
|
73
|
-
literal1 = @operand1.perform(environment)
|
74
|
-
|
75
|
-
# Special-case :and and :or to support short-circuiting.
|
76
|
-
if @operator == :and
|
77
|
-
return literal1.to_bool ? @operand2.perform(environment) : literal1
|
78
|
-
elsif @operator == :or
|
79
|
-
return literal1.to_bool ? literal1 : @operand2.perform(environment)
|
80
|
-
end
|
81
|
-
|
82
|
-
literal2 = @operand2.perform(environment)
|
83
|
-
|
84
|
-
if (literal1.is_a?(Null) || literal2.is_a?(Null)) && @operator != :eq && @operator != :neq
|
85
|
-
raise Sass::SyntaxError.new("Invalid null operation: \"#{literal1.inspect} #{@operator} #{literal2.inspect}\".")
|
86
|
-
end
|
87
|
-
|
88
|
-
begin
|
89
|
-
opts(literal1.send(@operator, literal2))
|
90
|
-
rescue NoMethodError => e
|
91
|
-
raise e unless e.name.to_s == @operator.to_s
|
92
|
-
raise Sass::SyntaxError.new("Undefined operation: \"#{literal1} #{@operator} #{literal2}\".")
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
private
|
97
|
-
|
98
|
-
def operand_to_sass(op, side, opts)
|
99
|
-
return "(#{op.to_sass(opts)})" if op.is_a?(List)
|
100
|
-
return op.to_sass(opts) unless op.is_a?(Operation)
|
101
|
-
|
102
|
-
pred = Sass::Script::Parser.precedence_of(@operator)
|
103
|
-
sub_pred = Sass::Script::Parser.precedence_of(op.operator)
|
104
|
-
assoc = Sass::Script::Parser.associative?(@operator)
|
105
|
-
return "(#{op.to_sass(opts)})" if sub_pred < pred ||
|
106
|
-
(side == :right && sub_pred == pred && !assoc)
|
107
|
-
op.to_sass(opts)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|