aliddle-sass 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (238) hide show
  1. data/.yardopts +11 -0
  2. data/CONTRIBUTING +3 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +201 -0
  5. data/Rakefile +347 -0
  6. data/VERSION +1 -0
  7. data/VERSION_NAME +1 -0
  8. data/bin/sass +9 -0
  9. data/bin/sass-convert +8 -0
  10. data/bin/scss +9 -0
  11. data/extra/update_watch.rb +13 -0
  12. data/init.rb +18 -0
  13. data/lib/sass.rb +95 -0
  14. data/lib/sass/cache_stores.rb +15 -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 +60 -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/callbacks.rb +66 -0
  21. data/lib/sass/css.rb +409 -0
  22. data/lib/sass/engine.rb +928 -0
  23. data/lib/sass/environment.rb +101 -0
  24. data/lib/sass/error.rb +201 -0
  25. data/lib/sass/exec.rb +707 -0
  26. data/lib/sass/importers.rb +22 -0
  27. data/lib/sass/importers/base.rb +139 -0
  28. data/lib/sass/importers/filesystem.rb +190 -0
  29. data/lib/sass/logger.rb +15 -0
  30. data/lib/sass/logger/base.rb +32 -0
  31. data/lib/sass/logger/log_level.rb +49 -0
  32. data/lib/sass/media.rb +213 -0
  33. data/lib/sass/plugin.rb +132 -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 +183 -0
  41. data/lib/sass/railtie.rb +9 -0
  42. data/lib/sass/repl.rb +57 -0
  43. data/lib/sass/root.rb +7 -0
  44. data/lib/sass/script.rb +39 -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 +237 -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 +348 -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 +495 -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/scss.rb +16 -0
  66. data/lib/sass/scss/css_parser.rb +36 -0
  67. data/lib/sass/scss/parser.rb +1179 -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/selector.rb +452 -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 +212 -0
  78. data/lib/sass/shared.rb +76 -0
  79. data/lib/sass/supports.rb +229 -0
  80. data/lib/sass/tree/charset_node.rb +22 -0
  81. data/lib/sass/tree/comment_node.rb +82 -0
  82. data/lib/sass/tree/content_node.rb +9 -0
  83. data/lib/sass/tree/css_import_node.rb +60 -0
  84. data/lib/sass/tree/debug_node.rb +18 -0
  85. data/lib/sass/tree/directive_node.rb +42 -0
  86. data/lib/sass/tree/each_node.rb +24 -0
  87. data/lib/sass/tree/extend_node.rb +36 -0
  88. data/lib/sass/tree/for_node.rb +36 -0
  89. data/lib/sass/tree/function_node.rb +34 -0
  90. data/lib/sass/tree/if_node.rb +52 -0
  91. data/lib/sass/tree/import_node.rb +75 -0
  92. data/lib/sass/tree/media_node.rb +58 -0
  93. data/lib/sass/tree/mixin_def_node.rb +38 -0
  94. data/lib/sass/tree/mixin_node.rb +39 -0
  95. data/lib/sass/tree/node.rb +196 -0
  96. data/lib/sass/tree/prop_node.rb +152 -0
  97. data/lib/sass/tree/return_node.rb +18 -0
  98. data/lib/sass/tree/root_node.rb +28 -0
  99. data/lib/sass/tree/rule_node.rb +132 -0
  100. data/lib/sass/tree/supports_node.rb +51 -0
  101. data/lib/sass/tree/trace_node.rb +32 -0
  102. data/lib/sass/tree/variable_node.rb +30 -0
  103. data/lib/sass/tree/visitors/base.rb +75 -0
  104. data/lib/sass/tree/visitors/check_nesting.rb +147 -0
  105. data/lib/sass/tree/visitors/convert.rb +316 -0
  106. data/lib/sass/tree/visitors/cssize.rb +229 -0
  107. data/lib/sass/tree/visitors/deep_copy.rb +102 -0
  108. data/lib/sass/tree/visitors/extend.rb +68 -0
  109. data/lib/sass/tree/visitors/perform.rb +446 -0
  110. data/lib/sass/tree/visitors/set_options.rb +125 -0
  111. data/lib/sass/tree/visitors/to_css.rb +230 -0
  112. data/lib/sass/tree/warn_node.rb +18 -0
  113. data/lib/sass/tree/while_node.rb +18 -0
  114. data/lib/sass/util.rb +906 -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/version.rb +126 -0
  119. data/rails/init.rb +1 -0
  120. data/test/Gemfile +3 -0
  121. data/test/Gemfile.lock +10 -0
  122. data/test/sass/cache_test.rb +89 -0
  123. data/test/sass/callbacks_test.rb +61 -0
  124. data/test/sass/conversion_test.rb +1760 -0
  125. data/test/sass/css2sass_test.rb +439 -0
  126. data/test/sass/data/hsl-rgb.txt +319 -0
  127. data/test/sass/engine_test.rb +3243 -0
  128. data/test/sass/exec_test.rb +86 -0
  129. data/test/sass/extend_test.rb +1461 -0
  130. data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
  131. data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
  132. data/test/sass/functions_test.rb +1139 -0
  133. data/test/sass/importer_test.rb +192 -0
  134. data/test/sass/logger_test.rb +58 -0
  135. data/test/sass/mock_importer.rb +49 -0
  136. data/test/sass/more_results/more1.css +9 -0
  137. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  138. data/test/sass/more_results/more_import.css +29 -0
  139. data/test/sass/more_templates/_more_partial.sass +2 -0
  140. data/test/sass/more_templates/more1.sass +23 -0
  141. data/test/sass/more_templates/more_import.sass +11 -0
  142. data/test/sass/plugin_test.rb +550 -0
  143. data/test/sass/results/alt.css +4 -0
  144. data/test/sass/results/basic.css +9 -0
  145. data/test/sass/results/cached_import_option.css +3 -0
  146. data/test/sass/results/compact.css +5 -0
  147. data/test/sass/results/complex.css +86 -0
  148. data/test/sass/results/compressed.css +1 -0
  149. data/test/sass/results/expanded.css +19 -0
  150. data/test/sass/results/filename_fn.css +3 -0
  151. data/test/sass/results/if.css +3 -0
  152. data/test/sass/results/import.css +31 -0
  153. data/test/sass/results/import_charset.css +5 -0
  154. data/test/sass/results/import_charset_1_8.css +5 -0
  155. data/test/sass/results/import_charset_ibm866.css +5 -0
  156. data/test/sass/results/import_content.css +1 -0
  157. data/test/sass/results/line_numbers.css +49 -0
  158. data/test/sass/results/mixins.css +95 -0
  159. data/test/sass/results/multiline.css +24 -0
  160. data/test/sass/results/nested.css +22 -0
  161. data/test/sass/results/options.css +1 -0
  162. data/test/sass/results/parent_ref.css +13 -0
  163. data/test/sass/results/script.css +16 -0
  164. data/test/sass/results/scss_import.css +31 -0
  165. data/test/sass/results/scss_importee.css +2 -0
  166. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  167. data/test/sass/results/subdir/subdir.css +3 -0
  168. data/test/sass/results/units.css +11 -0
  169. data/test/sass/results/warn.css +0 -0
  170. data/test/sass/results/warn_imported.css +0 -0
  171. data/test/sass/script_conversion_test.rb +299 -0
  172. data/test/sass/script_test.rb +591 -0
  173. data/test/sass/scss/css_test.rb +1093 -0
  174. data/test/sass/scss/rx_test.rb +156 -0
  175. data/test/sass/scss/scss_test.rb +2043 -0
  176. data/test/sass/scss/test_helper.rb +37 -0
  177. data/test/sass/templates/_cached_import_option_partial.scss +1 -0
  178. data/test/sass/templates/_double_import_loop2.sass +1 -0
  179. data/test/sass/templates/_filename_fn_import.scss +11 -0
  180. data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  181. data/test/sass/templates/_imported_charset_utf8.sass +4 -0
  182. data/test/sass/templates/_imported_content.sass +3 -0
  183. data/test/sass/templates/_partial.sass +2 -0
  184. data/test/sass/templates/_same_name_different_partiality.scss +1 -0
  185. data/test/sass/templates/alt.sass +16 -0
  186. data/test/sass/templates/basic.sass +23 -0
  187. data/test/sass/templates/bork1.sass +2 -0
  188. data/test/sass/templates/bork2.sass +2 -0
  189. data/test/sass/templates/bork3.sass +2 -0
  190. data/test/sass/templates/bork4.sass +2 -0
  191. data/test/sass/templates/bork5.sass +3 -0
  192. data/test/sass/templates/cached_import_option.scss +3 -0
  193. data/test/sass/templates/compact.sass +17 -0
  194. data/test/sass/templates/complex.sass +305 -0
  195. data/test/sass/templates/compressed.sass +15 -0
  196. data/test/sass/templates/double_import_loop1.sass +1 -0
  197. data/test/sass/templates/expanded.sass +17 -0
  198. data/test/sass/templates/filename_fn.scss +18 -0
  199. data/test/sass/templates/if.sass +11 -0
  200. data/test/sass/templates/import.sass +12 -0
  201. data/test/sass/templates/import_charset.sass +9 -0
  202. data/test/sass/templates/import_charset_1_8.sass +6 -0
  203. data/test/sass/templates/import_charset_ibm866.sass +11 -0
  204. data/test/sass/templates/import_content.sass +4 -0
  205. data/test/sass/templates/importee.less +2 -0
  206. data/test/sass/templates/importee.sass +19 -0
  207. data/test/sass/templates/line_numbers.sass +13 -0
  208. data/test/sass/templates/mixin_bork.sass +5 -0
  209. data/test/sass/templates/mixins.sass +76 -0
  210. data/test/sass/templates/multiline.sass +20 -0
  211. data/test/sass/templates/nested.sass +25 -0
  212. data/test/sass/templates/nested_bork1.sass +2 -0
  213. data/test/sass/templates/nested_bork2.sass +2 -0
  214. data/test/sass/templates/nested_bork3.sass +2 -0
  215. data/test/sass/templates/nested_bork4.sass +2 -0
  216. data/test/sass/templates/nested_import.sass +2 -0
  217. data/test/sass/templates/nested_mixin_bork.sass +6 -0
  218. data/test/sass/templates/options.sass +2 -0
  219. data/test/sass/templates/parent_ref.sass +25 -0
  220. data/test/sass/templates/same_name_different_ext.sass +2 -0
  221. data/test/sass/templates/same_name_different_ext.scss +1 -0
  222. data/test/sass/templates/same_name_different_partiality.scss +1 -0
  223. data/test/sass/templates/script.sass +101 -0
  224. data/test/sass/templates/scss_import.scss +11 -0
  225. data/test/sass/templates/scss_importee.scss +1 -0
  226. data/test/sass/templates/single_import_loop.sass +1 -0
  227. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  228. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  229. data/test/sass/templates/subdir/subdir.sass +6 -0
  230. data/test/sass/templates/units.sass +11 -0
  231. data/test/sass/templates/warn.sass +3 -0
  232. data/test/sass/templates/warn_imported.sass +4 -0
  233. data/test/sass/test_helper.rb +8 -0
  234. data/test/sass/util/multibyte_string_scanner_test.rb +147 -0
  235. data/test/sass/util/subset_map_test.rb +91 -0
  236. data/test/sass/util_test.rb +313 -0
  237. data/test/test_helper.rb +80 -0
  238. metadata +348 -0
@@ -0,0 +1,221 @@
1
+ module Sass::Script
2
+ # The abstract superclass for SassScript objects.
3
+ #
4
+ # Many of these methods, especially the ones that correspond to SassScript operations,
5
+ # are designed to be overridden by subclasses which may change the semantics somewhat.
6
+ # The operations listed here are just the defaults.
7
+ class Literal < Node
8
+ require 'sass/script/string'
9
+ require 'sass/script/number'
10
+ require 'sass/script/color'
11
+ require 'sass/script/bool'
12
+ require 'sass/script/null'
13
+ require 'sass/script/list'
14
+ require 'sass/script/arg_list'
15
+
16
+ # Returns the Ruby value of the literal.
17
+ # The type of this value varies based on the subclass.
18
+ #
19
+ # @return [Object]
20
+ attr_reader :value
21
+
22
+ # Creates a new literal.
23
+ #
24
+ # @param value [Object] The object for \{#value}
25
+ def initialize(value = nil)
26
+ @value = value
27
+ super()
28
+ end
29
+
30
+ # Returns an empty array.
31
+ #
32
+ # @return [Array<Node>] empty
33
+ # @see Node#children
34
+ def children
35
+ []
36
+ end
37
+
38
+ # @see Node#deep_copy
39
+ def deep_copy
40
+ dup
41
+ end
42
+
43
+ # Returns the options hash for this node.
44
+ #
45
+ # @return [{Symbol => Object}]
46
+ # @raise [Sass::SyntaxError] if the options hash hasn't been set.
47
+ # This should only happen when the literal was created
48
+ # outside of the parser and \{#to\_s} was called on it
49
+ def options
50
+ opts = super
51
+ return opts if opts
52
+ raise Sass::SyntaxError.new(<<MSG)
53
+ The #options attribute is not set on this #{self.class}.
54
+ This error is probably occurring because #to_s was called
55
+ on this literal within a custom Sass function without first
56
+ setting the #option attribute.
57
+ MSG
58
+ end
59
+
60
+ # The SassScript `==` operation.
61
+ # **Note that this returns a {Sass::Script::Bool} object,
62
+ # not a Ruby boolean**.
63
+ #
64
+ # @param other [Literal] The right-hand side of the operator
65
+ # @return [Bool] True if this literal is the same as the other,
66
+ # false otherwise
67
+ def eq(other)
68
+ Sass::Script::Bool.new(self.class == other.class && self.value == other.value)
69
+ end
70
+
71
+ # The SassScript `!=` operation.
72
+ # **Note that this returns a {Sass::Script::Bool} object,
73
+ # not a Ruby boolean**.
74
+ #
75
+ # @param other [Literal] The right-hand side of the operator
76
+ # @return [Bool] False if this literal is the same as the other,
77
+ # true otherwise
78
+ def neq(other)
79
+ Sass::Script::Bool.new(!eq(other).to_bool)
80
+ end
81
+
82
+ # The SassScript `==` operation.
83
+ # **Note that this returns a {Sass::Script::Bool} object,
84
+ # not a Ruby boolean**.
85
+ #
86
+ # @param other [Literal] The right-hand side of the operator
87
+ # @return [Bool] True if this literal is the same as the other,
88
+ # false otherwise
89
+ def unary_not
90
+ Sass::Script::Bool.new(!to_bool)
91
+ end
92
+
93
+ # The SassScript `=` operation
94
+ # (used for proprietary MS syntax like `alpha(opacity=20)`).
95
+ #
96
+ # @param other [Literal] The right-hand side of the operator
97
+ # @return [Script::String] A string containing both literals
98
+ # separated by `"="`
99
+ def single_eq(other)
100
+ Sass::Script::String.new("#{self.to_s}=#{other.to_s}")
101
+ end
102
+
103
+ # The SassScript `+` operation.
104
+ #
105
+ # @param other [Literal] The right-hand side of the operator
106
+ # @return [Script::String] A string containing both literals
107
+ # without any separation
108
+ def plus(other)
109
+ if other.is_a?(Sass::Script::String)
110
+ return Sass::Script::String.new(self.to_s + other.value, other.type)
111
+ end
112
+ Sass::Script::String.new(self.to_s + other.to_s)
113
+ end
114
+
115
+ # The SassScript `-` operation.
116
+ #
117
+ # @param other [Literal] The right-hand side of the operator
118
+ # @return [Script::String] A string containing both literals
119
+ # separated by `"-"`
120
+ def minus(other)
121
+ Sass::Script::String.new("#{self.to_s}-#{other.to_s}")
122
+ end
123
+
124
+ # The SassScript `/` operation.
125
+ #
126
+ # @param other [Literal] The right-hand side of the operator
127
+ # @return [Script::String] A string containing both literals
128
+ # separated by `"/"`
129
+ def div(other)
130
+ Sass::Script::String.new("#{self.to_s}/#{other.to_s}")
131
+ end
132
+
133
+ # The SassScript unary `+` operation (e.g. `+$a`).
134
+ #
135
+ # @param other [Literal] The right-hand side of the operator
136
+ # @return [Script::String] A string containing the literal
137
+ # preceded by `"+"`
138
+ def unary_plus
139
+ Sass::Script::String.new("+#{self.to_s}")
140
+ end
141
+
142
+ # The SassScript unary `-` operation (e.g. `-$a`).
143
+ #
144
+ # @param other [Literal] The right-hand side of the operator
145
+ # @return [Script::String] A string containing the literal
146
+ # preceded by `"-"`
147
+ def unary_minus
148
+ Sass::Script::String.new("-#{self.to_s}")
149
+ end
150
+
151
+ # The SassScript unary `/` operation (e.g. `/$a`).
152
+ #
153
+ # @param other [Literal] The right-hand side of the operator
154
+ # @return [Script::String] A string containing the literal
155
+ # preceded by `"/"`
156
+ def unary_div
157
+ Sass::Script::String.new("/#{self.to_s}")
158
+ end
159
+
160
+ # @return [String] A readable representation of the literal
161
+ def inspect
162
+ value.inspect
163
+ end
164
+
165
+ # @return [Boolean] `true` (the Ruby boolean value)
166
+ def to_bool
167
+ true
168
+ end
169
+
170
+ # Compares this object with another.
171
+ #
172
+ # @param other [Object] The object to compare with
173
+ # @return [Boolean] Whether or not this literal is equivalent to `other`
174
+ def ==(other)
175
+ eq(other).to_bool
176
+ end
177
+
178
+ # @return [Fixnum] The integer value of this literal
179
+ # @raise [Sass::SyntaxError] if this literal isn't an integer
180
+ def to_i
181
+ raise Sass::SyntaxError.new("#{self.inspect} is not an integer.")
182
+ end
183
+
184
+ # @raise [Sass::SyntaxError] if this literal isn't an integer
185
+ def assert_int!; to_i; end
186
+
187
+ # Returns the value of this literal as a list.
188
+ # Single literals are considered the same as single-element lists.
189
+ #
190
+ # @return [Array<Literal>] The of this literal as a list
191
+ def to_a
192
+ [self]
193
+ end
194
+
195
+ # Returns the string representation of this literal
196
+ # as it would be output to the CSS document.
197
+ #
198
+ # @return [String]
199
+ def to_s(opts = {})
200
+ raise Sass::SyntaxError.new("[BUG] All subclasses of Sass::Literal must implement #to_s.")
201
+ end
202
+ alias_method :to_sass, :to_s
203
+
204
+ # Returns whether or not this object is null.
205
+ #
206
+ # @return [Boolean] `false`
207
+ def null?
208
+ false
209
+ end
210
+
211
+ protected
212
+
213
+ # Evaluates the literal.
214
+ #
215
+ # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
216
+ # @return [Literal] This literal
217
+ def _perform(environment)
218
+ self
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,99 @@
1
+ module Sass::Script
2
+ # The abstract superclass for SassScript parse tree nodes.
3
+ #
4
+ # Use \{#perform} to evaluate a parse tree.
5
+ class Node
6
+ # The options hash for this node.
7
+ #
8
+ # @return [{Symbol => Object}]
9
+ attr_reader :options
10
+
11
+ # The line of the document on which this node appeared.
12
+ #
13
+ # @return [Fixnum]
14
+ attr_accessor :line
15
+
16
+ # Sets the options hash for this node,
17
+ # as well as for all child nodes.
18
+ # See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
19
+ #
20
+ # @param options [{Symbol => Object}] The options
21
+ def options=(options)
22
+ @options = options
23
+ children.each do |c|
24
+ if c.is_a? Hash
25
+ c.values.each {|v| v.options = options }
26
+ else
27
+ c.options = options
28
+ end
29
+ end
30
+ end
31
+
32
+ # Evaluates the node.
33
+ #
34
+ # \{#perform} shouldn't be overridden directly;
35
+ # instead, override \{#\_perform}.
36
+ #
37
+ # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
38
+ # @return [Literal] The SassScript object that is the value of the SassScript
39
+ def perform(environment)
40
+ _perform(environment)
41
+ rescue Sass::SyntaxError => e
42
+ e.modify_backtrace(:line => line)
43
+ raise e
44
+ end
45
+
46
+ # Returns all child nodes of this node.
47
+ #
48
+ # @return [Array<Node>]
49
+ def children
50
+ Sass::Util.abstract(self)
51
+ end
52
+
53
+ # Returns the text of this SassScript expression.
54
+ #
55
+ # @return [String]
56
+ def to_sass(opts = {})
57
+ Sass::Util.abstract(self)
58
+ end
59
+
60
+ # Returns a deep clone of this node.
61
+ # The child nodes are cloned, but options are not.
62
+ #
63
+ # @return [Node]
64
+ def deep_copy
65
+ Sass::Util.abstract(self)
66
+ end
67
+
68
+ protected
69
+
70
+ # Converts underscores to dashes if the :dasherize option is set.
71
+ def dasherize(s, opts)
72
+ if opts[:dasherize]
73
+ s.gsub(/_/,'-')
74
+ else
75
+ s
76
+ end
77
+ end
78
+
79
+ # Evaluates this node.
80
+ # Note that all {Literal} objects created within this method
81
+ # should have their \{#options} attribute set, probably via \{#opts}.
82
+ #
83
+ # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
84
+ # @return [Literal] The SassScript object that is the value of the SassScript
85
+ # @see #perform
86
+ def _perform(environment)
87
+ Sass::Util.abstract(self)
88
+ end
89
+
90
+ # Sets the \{#options} field on the given literal and returns it
91
+ #
92
+ # @param literal [Literal]
93
+ # @return [Literal]
94
+ def opts(literal)
95
+ literal.options = options
96
+ literal
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,37 @@
1
+ require 'sass/script/literal'
2
+
3
+ module Sass::Script
4
+ # A SassScript object representing a null value.
5
+ class Null < Literal
6
+ # Creates a new null literal.
7
+ def initialize
8
+ super nil
9
+ end
10
+
11
+ # @return [Boolean] `false` (the Ruby boolean value)
12
+ def to_bool
13
+ false
14
+ end
15
+
16
+ # @return [Boolean] `true`
17
+ def null?
18
+ true
19
+ end
20
+
21
+ # @return [String] '' (An empty string)
22
+ def to_s(opts = {})
23
+ ''
24
+ end
25
+
26
+ def to_sass(opts = {})
27
+ 'null'
28
+ end
29
+
30
+ # Returns a string representing a null value.
31
+ #
32
+ # @return [String]
33
+ def inspect
34
+ 'null'
35
+ end
36
+ end
37
+ end
@@ -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