xass 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +11 -0
  3. data/CONTRIBUTING +3 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +201 -0
  6. data/Rakefile +349 -0
  7. data/VERSION +1 -0
  8. data/VERSION_NAME +1 -0
  9. data/bin/push +13 -0
  10. data/bin/sass +13 -0
  11. data/bin/sass-convert +12 -0
  12. data/bin/scss +13 -0
  13. data/extra/update_watch.rb +13 -0
  14. data/init.rb +18 -0
  15. data/lib/sass/cache_stores/base.rb +88 -0
  16. data/lib/sass/cache_stores/chain.rb +33 -0
  17. data/lib/sass/cache_stores/filesystem.rb +64 -0
  18. data/lib/sass/cache_stores/memory.rb +47 -0
  19. data/lib/sass/cache_stores/null.rb +25 -0
  20. data/lib/sass/cache_stores.rb +15 -0
  21. data/lib/sass/callbacks.rb +66 -0
  22. data/lib/sass/css.rb +409 -0
  23. data/lib/sass/engine.rb +930 -0
  24. data/lib/sass/environment.rb +101 -0
  25. data/lib/sass/error.rb +201 -0
  26. data/lib/sass/exec.rb +707 -0
  27. data/lib/sass/importers/base.rb +139 -0
  28. data/lib/sass/importers/filesystem.rb +186 -0
  29. data/lib/sass/importers.rb +22 -0
  30. data/lib/sass/logger/base.rb +32 -0
  31. data/lib/sass/logger/log_level.rb +49 -0
  32. data/lib/sass/logger.rb +15 -0
  33. data/lib/sass/media.rb +213 -0
  34. data/lib/sass/plugin/compiler.rb +406 -0
  35. data/lib/sass/plugin/configuration.rb +123 -0
  36. data/lib/sass/plugin/generic.rb +15 -0
  37. data/lib/sass/plugin/merb.rb +48 -0
  38. data/lib/sass/plugin/rack.rb +60 -0
  39. data/lib/sass/plugin/rails.rb +47 -0
  40. data/lib/sass/plugin/staleness_checker.rb +199 -0
  41. data/lib/sass/plugin.rb +133 -0
  42. data/lib/sass/railtie.rb +10 -0
  43. data/lib/sass/repl.rb +57 -0
  44. data/lib/sass/root.rb +7 -0
  45. data/lib/sass/script/arg_list.rb +52 -0
  46. data/lib/sass/script/bool.rb +18 -0
  47. data/lib/sass/script/color.rb +606 -0
  48. data/lib/sass/script/css_lexer.rb +29 -0
  49. data/lib/sass/script/css_parser.rb +31 -0
  50. data/lib/sass/script/funcall.rb +245 -0
  51. data/lib/sass/script/functions.rb +1543 -0
  52. data/lib/sass/script/interpolation.rb +79 -0
  53. data/lib/sass/script/lexer.rb +345 -0
  54. data/lib/sass/script/list.rb +85 -0
  55. data/lib/sass/script/literal.rb +221 -0
  56. data/lib/sass/script/node.rb +99 -0
  57. data/lib/sass/script/null.rb +37 -0
  58. data/lib/sass/script/number.rb +453 -0
  59. data/lib/sass/script/operation.rb +110 -0
  60. data/lib/sass/script/parser.rb +502 -0
  61. data/lib/sass/script/string.rb +51 -0
  62. data/lib/sass/script/string_interpolation.rb +103 -0
  63. data/lib/sass/script/unary_operation.rb +69 -0
  64. data/lib/sass/script/variable.rb +58 -0
  65. data/lib/sass/script.rb +39 -0
  66. data/lib/sass/scss/css_parser.rb +36 -0
  67. data/lib/sass/scss/parser.rb +1180 -0
  68. data/lib/sass/scss/rx.rb +133 -0
  69. data/lib/sass/scss/script_lexer.rb +15 -0
  70. data/lib/sass/scss/script_parser.rb +25 -0
  71. data/lib/sass/scss/static_parser.rb +54 -0
  72. data/lib/sass/scss.rb +16 -0
  73. data/lib/sass/selector/abstract_sequence.rb +94 -0
  74. data/lib/sass/selector/comma_sequence.rb +92 -0
  75. data/lib/sass/selector/sequence.rb +507 -0
  76. data/lib/sass/selector/simple.rb +119 -0
  77. data/lib/sass/selector/simple_sequence.rb +215 -0
  78. data/lib/sass/selector.rb +452 -0
  79. data/lib/sass/shared.rb +76 -0
  80. data/lib/sass/supports.rb +229 -0
  81. data/lib/sass/tree/charset_node.rb +22 -0
  82. data/lib/sass/tree/comment_node.rb +82 -0
  83. data/lib/sass/tree/content_node.rb +9 -0
  84. data/lib/sass/tree/css_import_node.rb +60 -0
  85. data/lib/sass/tree/debug_node.rb +18 -0
  86. data/lib/sass/tree/directive_node.rb +42 -0
  87. data/lib/sass/tree/each_node.rb +24 -0
  88. data/lib/sass/tree/extend_node.rb +36 -0
  89. data/lib/sass/tree/for_node.rb +36 -0
  90. data/lib/sass/tree/function_node.rb +34 -0
  91. data/lib/sass/tree/if_node.rb +52 -0
  92. data/lib/sass/tree/import_node.rb +75 -0
  93. data/lib/sass/tree/media_node.rb +58 -0
  94. data/lib/sass/tree/mixin_def_node.rb +38 -0
  95. data/lib/sass/tree/mixin_node.rb +39 -0
  96. data/lib/sass/tree/node.rb +196 -0
  97. data/lib/sass/tree/prop_node.rb +152 -0
  98. data/lib/sass/tree/return_node.rb +18 -0
  99. data/lib/sass/tree/root_node.rb +78 -0
  100. data/lib/sass/tree/rule_node.rb +132 -0
  101. data/lib/sass/tree/supports_node.rb +51 -0
  102. data/lib/sass/tree/trace_node.rb +32 -0
  103. data/lib/sass/tree/variable_node.rb +30 -0
  104. data/lib/sass/tree/visitors/base.rb +75 -0
  105. data/lib/sass/tree/visitors/check_nesting.rb +147 -0
  106. data/lib/sass/tree/visitors/convert.rb +316 -0
  107. data/lib/sass/tree/visitors/cssize.rb +241 -0
  108. data/lib/sass/tree/visitors/deep_copy.rb +102 -0
  109. data/lib/sass/tree/visitors/extend.rb +68 -0
  110. data/lib/sass/tree/visitors/perform.rb +446 -0
  111. data/lib/sass/tree/visitors/set_options.rb +125 -0
  112. data/lib/sass/tree/visitors/to_css.rb +228 -0
  113. data/lib/sass/tree/warn_node.rb +18 -0
  114. data/lib/sass/tree/while_node.rb +18 -0
  115. data/lib/sass/util/multibyte_string_scanner.rb +155 -0
  116. data/lib/sass/util/subset_map.rb +109 -0
  117. data/lib/sass/util/test.rb +10 -0
  118. data/lib/sass/util.rb +948 -0
  119. data/lib/sass/version.rb +126 -0
  120. data/lib/sass.rb +95 -0
  121. data/rails/init.rb +1 -0
  122. data/test/Gemfile +3 -0
  123. data/test/Gemfile.lock +10 -0
  124. data/test/sass/cache_test.rb +89 -0
  125. data/test/sass/callbacks_test.rb +61 -0
  126. data/test/sass/conversion_test.rb +1760 -0
  127. data/test/sass/css2sass_test.rb +458 -0
  128. data/test/sass/data/hsl-rgb.txt +319 -0
  129. data/test/sass/engine_test.rb +3244 -0
  130. data/test/sass/exec_test.rb +86 -0
  131. data/test/sass/extend_test.rb +1482 -0
  132. data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
  133. data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
  134. data/test/sass/functions_test.rb +1139 -0
  135. data/test/sass/importer_test.rb +192 -0
  136. data/test/sass/logger_test.rb +58 -0
  137. data/test/sass/mock_importer.rb +49 -0
  138. data/test/sass/more_results/more1.css +9 -0
  139. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  140. data/test/sass/more_results/more_import.css +29 -0
  141. data/test/sass/more_templates/_more_partial.sass +2 -0
  142. data/test/sass/more_templates/more1.sass +23 -0
  143. data/test/sass/more_templates/more_import.sass +11 -0
  144. data/test/sass/plugin_test.rb +564 -0
  145. data/test/sass/results/alt.css +4 -0
  146. data/test/sass/results/basic.css +9 -0
  147. data/test/sass/results/cached_import_option.css +3 -0
  148. data/test/sass/results/compact.css +5 -0
  149. data/test/sass/results/complex.css +86 -0
  150. data/test/sass/results/compressed.css +1 -0
  151. data/test/sass/results/expanded.css +19 -0
  152. data/test/sass/results/filename_fn.css +3 -0
  153. data/test/sass/results/if.css +3 -0
  154. data/test/sass/results/import.css +31 -0
  155. data/test/sass/results/import_charset.css +5 -0
  156. data/test/sass/results/import_charset_1_8.css +5 -0
  157. data/test/sass/results/import_charset_ibm866.css +5 -0
  158. data/test/sass/results/import_content.css +1 -0
  159. data/test/sass/results/line_numbers.css +49 -0
  160. data/test/sass/results/mixins.css +95 -0
  161. data/test/sass/results/multiline.css +24 -0
  162. data/test/sass/results/nested.css +22 -0
  163. data/test/sass/results/options.css +1 -0
  164. data/test/sass/results/parent_ref.css +13 -0
  165. data/test/sass/results/script.css +16 -0
  166. data/test/sass/results/scss_import.css +31 -0
  167. data/test/sass/results/scss_importee.css +2 -0
  168. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  169. data/test/sass/results/subdir/subdir.css +3 -0
  170. data/test/sass/results/units.css +11 -0
  171. data/test/sass/results/warn.css +0 -0
  172. data/test/sass/results/warn_imported.css +0 -0
  173. data/test/sass/script_conversion_test.rb +299 -0
  174. data/test/sass/script_test.rb +622 -0
  175. data/test/sass/scss/css_test.rb +1100 -0
  176. data/test/sass/scss/rx_test.rb +156 -0
  177. data/test/sass/scss/scss_test.rb +2106 -0
  178. data/test/sass/scss/test_helper.rb +37 -0
  179. data/test/sass/templates/_cached_import_option_partial.scss +1 -0
  180. data/test/sass/templates/_double_import_loop2.sass +1 -0
  181. data/test/sass/templates/_filename_fn_import.scss +11 -0
  182. data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  183. data/test/sass/templates/_imported_charset_utf8.sass +4 -0
  184. data/test/sass/templates/_imported_content.sass +3 -0
  185. data/test/sass/templates/_partial.sass +2 -0
  186. data/test/sass/templates/_same_name_different_partiality.scss +1 -0
  187. data/test/sass/templates/alt.sass +16 -0
  188. data/test/sass/templates/basic.sass +23 -0
  189. data/test/sass/templates/bork1.sass +2 -0
  190. data/test/sass/templates/bork2.sass +2 -0
  191. data/test/sass/templates/bork3.sass +2 -0
  192. data/test/sass/templates/bork4.sass +2 -0
  193. data/test/sass/templates/bork5.sass +3 -0
  194. data/test/sass/templates/cached_import_option.scss +3 -0
  195. data/test/sass/templates/compact.sass +17 -0
  196. data/test/sass/templates/complex.sass +305 -0
  197. data/test/sass/templates/compressed.sass +15 -0
  198. data/test/sass/templates/double_import_loop1.sass +1 -0
  199. data/test/sass/templates/expanded.sass +17 -0
  200. data/test/sass/templates/filename_fn.scss +18 -0
  201. data/test/sass/templates/if.sass +11 -0
  202. data/test/sass/templates/import.sass +12 -0
  203. data/test/sass/templates/import_charset.sass +9 -0
  204. data/test/sass/templates/import_charset_1_8.sass +6 -0
  205. data/test/sass/templates/import_charset_ibm866.sass +11 -0
  206. data/test/sass/templates/import_content.sass +4 -0
  207. data/test/sass/templates/importee.less +2 -0
  208. data/test/sass/templates/importee.sass +19 -0
  209. data/test/sass/templates/line_numbers.sass +13 -0
  210. data/test/sass/templates/mixin_bork.sass +5 -0
  211. data/test/sass/templates/mixins.sass +76 -0
  212. data/test/sass/templates/multiline.sass +20 -0
  213. data/test/sass/templates/nested.sass +25 -0
  214. data/test/sass/templates/nested_bork1.sass +2 -0
  215. data/test/sass/templates/nested_bork2.sass +2 -0
  216. data/test/sass/templates/nested_bork3.sass +2 -0
  217. data/test/sass/templates/nested_bork4.sass +2 -0
  218. data/test/sass/templates/nested_import.sass +2 -0
  219. data/test/sass/templates/nested_mixin_bork.sass +6 -0
  220. data/test/sass/templates/options.sass +2 -0
  221. data/test/sass/templates/parent_ref.sass +25 -0
  222. data/test/sass/templates/same_name_different_ext.sass +2 -0
  223. data/test/sass/templates/same_name_different_ext.scss +1 -0
  224. data/test/sass/templates/same_name_different_partiality.scss +1 -0
  225. data/test/sass/templates/script.sass +101 -0
  226. data/test/sass/templates/scss_import.scss +11 -0
  227. data/test/sass/templates/scss_importee.scss +1 -0
  228. data/test/sass/templates/single_import_loop.sass +1 -0
  229. data/test/sass/templates/subdir/import_up1.scss +1 -0
  230. data/test/sass/templates/subdir/import_up2.scss +1 -0
  231. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  232. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  233. data/test/sass/templates/subdir/subdir.sass +6 -0
  234. data/test/sass/templates/units.sass +11 -0
  235. data/test/sass/templates/warn.sass +3 -0
  236. data/test/sass/templates/warn_imported.sass +4 -0
  237. data/test/sass/test_helper.rb +8 -0
  238. data/test/sass/util/multibyte_string_scanner_test.rb +147 -0
  239. data/test/sass/util/subset_map_test.rb +91 -0
  240. data/test/sass/util_test.rb +382 -0
  241. data/test/test_helper.rb +80 -0
  242. metadata +354 -0
@@ -0,0 +1,453 @@
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
@@ -0,0 +1,110 @@
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