sass 3.1.0.alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +11 -0
- data/CONTRIBUTING +3 -0
- data/EDGE_GEM_VERSION +1 -0
- data/MIT-LICENSE +20 -0
- data/README.md +201 -0
- data/REVISION +1 -0
- data/Rakefile +353 -0
- data/VERSION +1 -0
- data/VERSION_NAME +1 -0
- data/bin/css2sass +13 -0
- data/bin/sass +8 -0
- data/bin/sass-convert +7 -0
- data/extra/update_watch.rb +13 -0
- data/init.rb +18 -0
- data/lib/sass.rb +71 -0
- data/lib/sass/cache_store.rb +208 -0
- data/lib/sass/callbacks.rb +66 -0
- data/lib/sass/css.rb +294 -0
- data/lib/sass/engine.rb +792 -0
- data/lib/sass/environment.rb +143 -0
- data/lib/sass/error.rb +201 -0
- data/lib/sass/exec.rb +619 -0
- data/lib/sass/importers.rb +22 -0
- data/lib/sass/importers/base.rb +138 -0
- data/lib/sass/importers/filesystem.rb +121 -0
- data/lib/sass/less.rb +363 -0
- data/lib/sass/plugin.rb +126 -0
- data/lib/sass/plugin/compiler.rb +346 -0
- data/lib/sass/plugin/configuration.rb +123 -0
- data/lib/sass/plugin/generic.rb +15 -0
- data/lib/sass/plugin/merb.rb +48 -0
- data/lib/sass/plugin/rack.rb +47 -0
- data/lib/sass/plugin/rails.rb +41 -0
- data/lib/sass/plugin/staleness_checker.rb +145 -0
- data/lib/sass/railtie.rb +8 -0
- data/lib/sass/repl.rb +58 -0
- data/lib/sass/root.rb +7 -0
- data/lib/sass/script.rb +63 -0
- data/lib/sass/script/bool.rb +18 -0
- data/lib/sass/script/color.rb +490 -0
- data/lib/sass/script/css_lexer.rb +29 -0
- data/lib/sass/script/css_parser.rb +31 -0
- data/lib/sass/script/funcall.rb +78 -0
- data/lib/sass/script/functions.rb +852 -0
- data/lib/sass/script/interpolation.rb +70 -0
- data/lib/sass/script/lexer.rb +337 -0
- data/lib/sass/script/literal.rb +236 -0
- data/lib/sass/script/node.rb +101 -0
- data/lib/sass/script/number.rb +420 -0
- data/lib/sass/script/operation.rb +92 -0
- data/lib/sass/script/parser.rb +392 -0
- data/lib/sass/script/string.rb +67 -0
- data/lib/sass/script/string_interpolation.rb +93 -0
- data/lib/sass/script/unary_operation.rb +57 -0
- data/lib/sass/script/variable.rb +48 -0
- data/lib/sass/scss.rb +17 -0
- data/lib/sass/scss/css_parser.rb +51 -0
- data/lib/sass/scss/parser.rb +838 -0
- data/lib/sass/scss/rx.rb +126 -0
- data/lib/sass/scss/sass_parser.rb +11 -0
- data/lib/sass/scss/script_lexer.rb +15 -0
- data/lib/sass/scss/script_parser.rb +25 -0
- data/lib/sass/scss/static_parser.rb +40 -0
- data/lib/sass/selector.rb +361 -0
- data/lib/sass/selector/abstract_sequence.rb +62 -0
- data/lib/sass/selector/comma_sequence.rb +82 -0
- data/lib/sass/selector/sequence.rb +236 -0
- data/lib/sass/selector/simple.rb +113 -0
- data/lib/sass/selector/simple_sequence.rb +135 -0
- data/lib/sass/shared.rb +78 -0
- data/lib/sass/tree/comment_node.rb +128 -0
- data/lib/sass/tree/debug_node.rb +36 -0
- data/lib/sass/tree/directive_node.rb +75 -0
- data/lib/sass/tree/extend_node.rb +65 -0
- data/lib/sass/tree/for_node.rb +67 -0
- data/lib/sass/tree/if_node.rb +81 -0
- data/lib/sass/tree/import_node.rb +124 -0
- data/lib/sass/tree/mixin_def_node.rb +60 -0
- data/lib/sass/tree/mixin_node.rb +123 -0
- data/lib/sass/tree/node.rb +490 -0
- data/lib/sass/tree/prop_node.rb +220 -0
- data/lib/sass/tree/root_node.rb +125 -0
- data/lib/sass/tree/rule_node.rb +273 -0
- data/lib/sass/tree/variable_node.rb +39 -0
- data/lib/sass/tree/warn_node.rb +42 -0
- data/lib/sass/tree/while_node.rb +48 -0
- data/lib/sass/util.rb +687 -0
- data/lib/sass/util/subset_map.rb +101 -0
- data/lib/sass/version.rb +109 -0
- data/rails/init.rb +1 -0
- data/test/sass/cache_test.rb +74 -0
- data/test/sass/callbacks_test.rb +61 -0
- data/test/sass/conversion_test.rb +1210 -0
- data/test/sass/css2sass_test.rb +364 -0
- data/test/sass/data/hsl-rgb.txt +319 -0
- data/test/sass/engine_test.rb +2273 -0
- data/test/sass/extend_test.rb +1348 -0
- data/test/sass/functions_test.rb +565 -0
- data/test/sass/importer_test.rb +104 -0
- data/test/sass/less_conversion_test.rb +632 -0
- data/test/sass/mock_importer.rb +49 -0
- data/test/sass/more_results/more1.css +9 -0
- data/test/sass/more_results/more1_with_line_comments.css +26 -0
- data/test/sass/more_results/more_import.css +29 -0
- data/test/sass/more_templates/_more_partial.sass +2 -0
- data/test/sass/more_templates/more1.sass +23 -0
- data/test/sass/more_templates/more_import.sass +11 -0
- data/test/sass/plugin_test.rb +430 -0
- data/test/sass/results/alt.css +4 -0
- data/test/sass/results/basic.css +9 -0
- data/test/sass/results/compact.css +5 -0
- data/test/sass/results/complex.css +86 -0
- data/test/sass/results/compressed.css +1 -0
- data/test/sass/results/expanded.css +19 -0
- data/test/sass/results/import.css +31 -0
- data/test/sass/results/line_numbers.css +49 -0
- data/test/sass/results/mixins.css +95 -0
- data/test/sass/results/multiline.css +24 -0
- data/test/sass/results/nested.css +22 -0
- data/test/sass/results/options.css +1 -0
- data/test/sass/results/parent_ref.css +13 -0
- data/test/sass/results/script.css +16 -0
- data/test/sass/results/scss_import.css +31 -0
- data/test/sass/results/scss_importee.css +2 -0
- data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
- data/test/sass/results/subdir/subdir.css +3 -0
- data/test/sass/results/units.css +11 -0
- data/test/sass/results/warn.css +0 -0
- data/test/sass/results/warn_imported.css +0 -0
- data/test/sass/script_conversion_test.rb +254 -0
- data/test/sass/script_test.rb +459 -0
- data/test/sass/scss/css_test.rb +897 -0
- data/test/sass/scss/rx_test.rb +156 -0
- data/test/sass/scss/scss_test.rb +1088 -0
- data/test/sass/scss/test_helper.rb +37 -0
- data/test/sass/templates/_partial.sass +2 -0
- data/test/sass/templates/alt.sass +16 -0
- data/test/sass/templates/basic.sass +23 -0
- data/test/sass/templates/bork1.sass +2 -0
- data/test/sass/templates/bork2.sass +2 -0
- data/test/sass/templates/bork3.sass +2 -0
- data/test/sass/templates/bork4.sass +2 -0
- data/test/sass/templates/compact.sass +17 -0
- data/test/sass/templates/complex.sass +305 -0
- data/test/sass/templates/compressed.sass +15 -0
- data/test/sass/templates/expanded.sass +17 -0
- data/test/sass/templates/import.sass +12 -0
- data/test/sass/templates/importee.less +2 -0
- data/test/sass/templates/importee.sass +19 -0
- data/test/sass/templates/line_numbers.sass +13 -0
- data/test/sass/templates/mixin_bork.sass +5 -0
- data/test/sass/templates/mixins.sass +76 -0
- data/test/sass/templates/multiline.sass +20 -0
- data/test/sass/templates/nested.sass +25 -0
- data/test/sass/templates/nested_bork1.sass +2 -0
- data/test/sass/templates/nested_bork2.sass +2 -0
- data/test/sass/templates/nested_bork3.sass +2 -0
- data/test/sass/templates/nested_bork4.sass +2 -0
- data/test/sass/templates/nested_mixin_bork.sass +6 -0
- data/test/sass/templates/options.sass +2 -0
- data/test/sass/templates/parent_ref.sass +25 -0
- data/test/sass/templates/script.sass +101 -0
- data/test/sass/templates/scss_import.scss +11 -0
- data/test/sass/templates/scss_importee.scss +1 -0
- data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
- data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
- data/test/sass/templates/subdir/subdir.sass +6 -0
- data/test/sass/templates/units.sass +11 -0
- data/test/sass/templates/warn.sass +3 -0
- data/test/sass/templates/warn_imported.sass +4 -0
- data/test/sass/test_helper.rb +8 -0
- data/test/sass/util/subset_map_test.rb +91 -0
- data/test/sass/util_test.rb +275 -0
- data/test/test_helper.rb +64 -0
- data/vendor/fssm/LICENSE +20 -0
- data/vendor/fssm/README.markdown +55 -0
- data/vendor/fssm/Rakefile +59 -0
- data/vendor/fssm/VERSION.yml +5 -0
- data/vendor/fssm/example.rb +9 -0
- data/vendor/fssm/fssm.gemspec +77 -0
- data/vendor/fssm/lib/fssm.rb +33 -0
- data/vendor/fssm/lib/fssm/backends/fsevents.rb +36 -0
- data/vendor/fssm/lib/fssm/backends/inotify.rb +26 -0
- data/vendor/fssm/lib/fssm/backends/polling.rb +25 -0
- data/vendor/fssm/lib/fssm/backends/rubycocoa/fsevents.rb +131 -0
- data/vendor/fssm/lib/fssm/monitor.rb +26 -0
- data/vendor/fssm/lib/fssm/path.rb +91 -0
- data/vendor/fssm/lib/fssm/pathname.rb +502 -0
- data/vendor/fssm/lib/fssm/state/directory.rb +57 -0
- data/vendor/fssm/lib/fssm/state/file.rb +24 -0
- data/vendor/fssm/lib/fssm/support.rb +63 -0
- data/vendor/fssm/lib/fssm/tree.rb +176 -0
- data/vendor/fssm/profile/prof-cache.rb +40 -0
- data/vendor/fssm/profile/prof-fssm-pathname.html +1231 -0
- data/vendor/fssm/profile/prof-pathname.rb +68 -0
- data/vendor/fssm/profile/prof-plain-pathname.html +988 -0
- data/vendor/fssm/profile/prof.html +2379 -0
- data/vendor/fssm/spec/path_spec.rb +75 -0
- data/vendor/fssm/spec/root/duck/quack.txt +0 -0
- data/vendor/fssm/spec/root/file.css +0 -0
- data/vendor/fssm/spec/root/file.rb +0 -0
- data/vendor/fssm/spec/root/file.yml +0 -0
- data/vendor/fssm/spec/root/moo/cow.txt +0 -0
- data/vendor/fssm/spec/spec_helper.rb +14 -0
- metadata +297 -0
@@ -0,0 +1,101 @@
|
|
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 context in which this node was parsed,
|
12
|
+
# which determines how some operations are performed.
|
13
|
+
#
|
14
|
+
# Can be `:equals`, which means it's part of a `$var = val` or `prop = val` assignment,
|
15
|
+
# or `:default`, which means it's anywhere else
|
16
|
+
# (including `$var: val` and `prop: val` assignments,
|
17
|
+
# `#{}`-interpolations,
|
18
|
+
# and other script contexts such as `@if` conditions).
|
19
|
+
#
|
20
|
+
# @return [Symbol]
|
21
|
+
attr_reader :context
|
22
|
+
|
23
|
+
# The line of the document on which this node appeared.
|
24
|
+
#
|
25
|
+
# @return [Fixnum]
|
26
|
+
attr_accessor :line
|
27
|
+
|
28
|
+
# Sets the options hash for this node,
|
29
|
+
# as well as for all child nodes.
|
30
|
+
# See {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
|
31
|
+
#
|
32
|
+
# @param options [{Symbol => Object}] The options
|
33
|
+
def options=(options)
|
34
|
+
@options = options
|
35
|
+
children.each {|c| c.options = options}
|
36
|
+
end
|
37
|
+
|
38
|
+
# Sets the context for this node,
|
39
|
+
# as well as for all child nodes.
|
40
|
+
#
|
41
|
+
# @param context [Symbol]
|
42
|
+
# @see #context
|
43
|
+
def context=(context)
|
44
|
+
@context = context
|
45
|
+
children.each {|c| c.context = context}
|
46
|
+
end
|
47
|
+
|
48
|
+
# Creates a new script node.
|
49
|
+
def initialize
|
50
|
+
@context = :default
|
51
|
+
end
|
52
|
+
|
53
|
+
# Evaluates the node.
|
54
|
+
#
|
55
|
+
# \{#perform} shouldn't be overridden directly;
|
56
|
+
# instead, override \{#\_perform}.
|
57
|
+
#
|
58
|
+
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
|
59
|
+
# @return [Literal] The SassScript object that is the value of the SassScript
|
60
|
+
def perform(environment)
|
61
|
+
_perform(environment)
|
62
|
+
rescue Sass::SyntaxError => e
|
63
|
+
e.modify_backtrace(:line => line)
|
64
|
+
raise e
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns all child nodes of this node.
|
68
|
+
#
|
69
|
+
# @return [Array<Node>]
|
70
|
+
def children
|
71
|
+
Sass::Util.abstract(self)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the text of this SassScript expression.
|
75
|
+
#
|
76
|
+
# @return [String]
|
77
|
+
def to_sass(opts = {})
|
78
|
+
Sass::Util.abstract(self)
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
# Converts underscores to dashes if the :dasherize option is set.
|
84
|
+
def dasherize(s, opts)
|
85
|
+
if opts[:dasherize]
|
86
|
+
s.gsub(/_/,'-')
|
87
|
+
else
|
88
|
+
s
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Evaluates this node.
|
93
|
+
#
|
94
|
+
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
|
95
|
+
# @return [Literal] The SassScript object that is the value of the SassScript
|
96
|
+
# @see #perform
|
97
|
+
def _perform(environment)
|
98
|
+
Sass::Util.abstract(self)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,420 @@
|
|
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
|
+
# The precision with which numbers will be printed to CSS files.
|
39
|
+
# For example, if this is `1000.0`,
|
40
|
+
# `3.1415926` will be printed as `3.142`.
|
41
|
+
# @api public
|
42
|
+
PRECISION = 1000.0
|
43
|
+
|
44
|
+
# @param value [Numeric] The value of the number
|
45
|
+
# @param numerator_units [Array<String>] See \{#numerator\_units}
|
46
|
+
# @param denominator_units [Array<String>] See \{#denominator\_units}
|
47
|
+
def initialize(value, numerator_units = [], denominator_units = [])
|
48
|
+
super(value)
|
49
|
+
@numerator_units = numerator_units
|
50
|
+
@denominator_units = denominator_units
|
51
|
+
normalize!
|
52
|
+
end
|
53
|
+
|
54
|
+
# The SassScript `+` operation.
|
55
|
+
# Its functionality depends on the type of its argument:
|
56
|
+
#
|
57
|
+
# {Number}
|
58
|
+
# : Adds the two numbers together, converting units if possible.
|
59
|
+
#
|
60
|
+
# {Color}
|
61
|
+
# : Adds this number to each of the RGB color channels.
|
62
|
+
#
|
63
|
+
# {Literal}
|
64
|
+
# : See {Literal#plus}.
|
65
|
+
#
|
66
|
+
# @param other [Literal] The right-hand side of the operator
|
67
|
+
# @return [Literal] The result of the operation
|
68
|
+
# @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
|
69
|
+
def plus(other)
|
70
|
+
if other.is_a? Number
|
71
|
+
operate(other, :+)
|
72
|
+
elsif other.is_a?(Color)
|
73
|
+
other.plus(self)
|
74
|
+
else
|
75
|
+
super
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# The SassScript binary `-` operation (e.g. `$a - $b`).
|
80
|
+
# Its functionality depends on the type of its argument:
|
81
|
+
#
|
82
|
+
# {Number}
|
83
|
+
# : Subtracts this number from the other, converting units if possible.
|
84
|
+
#
|
85
|
+
# {Literal}
|
86
|
+
# : See {Literal#minus}.
|
87
|
+
#
|
88
|
+
# @param other [Literal] The right-hand side of the operator
|
89
|
+
# @return [Literal] The result of the operation
|
90
|
+
# @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
|
91
|
+
def minus(other)
|
92
|
+
if other.is_a? Number
|
93
|
+
operate(other, :-)
|
94
|
+
else
|
95
|
+
super
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# The SassScript unary `+` operation (e.g. `+$a`).
|
100
|
+
#
|
101
|
+
# @return [Number] The value of this number
|
102
|
+
def unary_plus
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
# The SassScript unary `-` operation (e.g. `-$a`).
|
107
|
+
#
|
108
|
+
# @return [Number] The negative value of this number
|
109
|
+
def unary_minus
|
110
|
+
Number.new(-value, numerator_units, denominator_units)
|
111
|
+
end
|
112
|
+
|
113
|
+
# The SassScript `*` operation.
|
114
|
+
# Its functionality depends on the type of its argument:
|
115
|
+
#
|
116
|
+
# {Number}
|
117
|
+
# : Multiplies the two numbers together, converting units appropriately.
|
118
|
+
#
|
119
|
+
# {Color}
|
120
|
+
# : Multiplies each of the RGB color channels by this number.
|
121
|
+
#
|
122
|
+
# @param other [Number, Color] The right-hand side of the operator
|
123
|
+
# @return [Number, Color] The result of the operation
|
124
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
125
|
+
def times(other)
|
126
|
+
if other.is_a? Number
|
127
|
+
operate(other, :*)
|
128
|
+
elsif other.is_a? Color
|
129
|
+
other.times(self)
|
130
|
+
else
|
131
|
+
raise NoMethodError.new(nil, :times)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# The SassScript `/` operation.
|
136
|
+
# Its functionality depends on the type of its argument:
|
137
|
+
#
|
138
|
+
# {Number}
|
139
|
+
# : Divides this number by the other, converting units appropriately.
|
140
|
+
#
|
141
|
+
# {Literal}
|
142
|
+
# : See {Literal#div}.
|
143
|
+
#
|
144
|
+
# @param other [Literal] The right-hand side of the operator
|
145
|
+
# @return [Literal] The result of the operation
|
146
|
+
def div(other)
|
147
|
+
if other.is_a? Number
|
148
|
+
res = operate(other, :/)
|
149
|
+
if self.original && other.original && context != :equals
|
150
|
+
res.original = "#{self.original}/#{other.original}"
|
151
|
+
end
|
152
|
+
res
|
153
|
+
else
|
154
|
+
super
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# The SassScript `%` operation.
|
159
|
+
#
|
160
|
+
# @param other [Number] The right-hand side of the operator
|
161
|
+
# @return [Number] This number modulo the other
|
162
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
163
|
+
# @raise [Sass::UnitConversionError] if `other` has any units
|
164
|
+
def mod(other)
|
165
|
+
if other.is_a?(Number)
|
166
|
+
unless other.unitless?
|
167
|
+
raise Sass::UnitConversionError.new("Cannot modulo by a number with units: #{other.inspect}.")
|
168
|
+
end
|
169
|
+
operate(other, :%)
|
170
|
+
else
|
171
|
+
raise NoMethodError.new(nil, :mod)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# The SassScript `==` operation.
|
176
|
+
#
|
177
|
+
# @param other [Literal] The right-hand side of the operator
|
178
|
+
# @return [Boolean] Whether this number is equal to the other object
|
179
|
+
def eq(other)
|
180
|
+
return Sass::Script::Bool.new(false) unless other.is_a?(Sass::Script::Number)
|
181
|
+
this = self
|
182
|
+
begin
|
183
|
+
if unitless?
|
184
|
+
this = this.coerce(other.numerator_units, other.denominator_units)
|
185
|
+
else
|
186
|
+
other = other.coerce(numerator_units, denominator_units)
|
187
|
+
end
|
188
|
+
rescue Sass::UnitConversionError
|
189
|
+
return Sass::Script::Bool.new(false)
|
190
|
+
end
|
191
|
+
|
192
|
+
Sass::Script::Bool.new(this.value == other.value)
|
193
|
+
end
|
194
|
+
|
195
|
+
# The SassScript `>` operation.
|
196
|
+
#
|
197
|
+
# @param other [Number] The right-hand side of the operator
|
198
|
+
# @return [Boolean] Whether this number is greater than the other
|
199
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
200
|
+
def gt(other)
|
201
|
+
raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
|
202
|
+
operate(other, :>)
|
203
|
+
end
|
204
|
+
|
205
|
+
# The SassScript `>=` operation.
|
206
|
+
#
|
207
|
+
# @param other [Number] The right-hand side of the operator
|
208
|
+
# @return [Boolean] Whether this number is greater than or equal to the other
|
209
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
210
|
+
def gte(other)
|
211
|
+
raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
|
212
|
+
operate(other, :>=)
|
213
|
+
end
|
214
|
+
|
215
|
+
# The SassScript `<` operation.
|
216
|
+
#
|
217
|
+
# @param other [Number] The right-hand side of the operator
|
218
|
+
# @return [Boolean] Whether this number is less than the other
|
219
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
220
|
+
def lt(other)
|
221
|
+
raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
|
222
|
+
operate(other, :<)
|
223
|
+
end
|
224
|
+
|
225
|
+
# The SassScript `<=` operation.
|
226
|
+
#
|
227
|
+
# @param other [Number] The right-hand side of the operator
|
228
|
+
# @return [Boolean] Whether this number is less than or equal to the other
|
229
|
+
# @raise [NoMethodError] if `other` is an invalid type
|
230
|
+
def lte(other)
|
231
|
+
raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
|
232
|
+
operate(other, :<=)
|
233
|
+
end
|
234
|
+
|
235
|
+
# @return [String] The CSS representation of this number
|
236
|
+
# @raise [Sass::SyntaxError] if this number has units that can't be used in CSS
|
237
|
+
# (e.g. `px*in`)
|
238
|
+
def to_s
|
239
|
+
return original if original
|
240
|
+
raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
|
241
|
+
inspect
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns a readable representation of this number.
|
245
|
+
#
|
246
|
+
# This representation is valid CSS (and valid SassScript)
|
247
|
+
# as long as there is only one unit.
|
248
|
+
#
|
249
|
+
# @return [String] The representation
|
250
|
+
def inspect(opts = {})
|
251
|
+
value =
|
252
|
+
if self.value.is_a?(Float) && (self.value.infinite? || self.value.nan?)
|
253
|
+
self.value
|
254
|
+
elsif int?
|
255
|
+
self.value.to_i
|
256
|
+
else
|
257
|
+
(self.value * PRECISION).round / PRECISION
|
258
|
+
end
|
259
|
+
"#{value}#{unit_str}"
|
260
|
+
end
|
261
|
+
alias_method :to_sass, :inspect
|
262
|
+
|
263
|
+
# @return [Fixnum] The integer value of the number
|
264
|
+
# @raise [Sass::SyntaxError] if the number isn't an integer
|
265
|
+
def to_i
|
266
|
+
super unless int?
|
267
|
+
return value
|
268
|
+
end
|
269
|
+
|
270
|
+
# @return [Boolean] Whether or not this number is an integer.
|
271
|
+
def int?
|
272
|
+
value % 1 == 0.0
|
273
|
+
end
|
274
|
+
|
275
|
+
# @return [Boolean] Whether or not this number has no units.
|
276
|
+
def unitless?
|
277
|
+
numerator_units.empty? && denominator_units.empty?
|
278
|
+
end
|
279
|
+
|
280
|
+
# @return [Boolean] Whether or not this number has units that can be represented in CSS
|
281
|
+
# (that is, zero or one \{#numerator\_units}).
|
282
|
+
def legal_units?
|
283
|
+
(numerator_units.empty? || numerator_units.size == 1) && denominator_units.empty?
|
284
|
+
end
|
285
|
+
|
286
|
+
# Returns this number converted to other units.
|
287
|
+
# The conversion takes into account the relationship between e.g. mm and cm,
|
288
|
+
# as well as between e.g. in and cm.
|
289
|
+
#
|
290
|
+
# If this number has no units, it will simply return itself
|
291
|
+
# with the given units.
|
292
|
+
#
|
293
|
+
# An incompatible coercion, e.g. between px and cm, will raise an error.
|
294
|
+
#
|
295
|
+
# @param num_units [Array<String>] The numerator units to coerce this number into.
|
296
|
+
# See {\#numerator\_units}
|
297
|
+
# @param den_units [Array<String>] The denominator units to coerce this number into.
|
298
|
+
# See {\#denominator\_units}
|
299
|
+
# @return [Number] The number with the new units
|
300
|
+
# @raise [Sass::UnitConversionError] if the given units are incompatible with the number's
|
301
|
+
# current units
|
302
|
+
def coerce(num_units, den_units)
|
303
|
+
Number.new(if unitless?
|
304
|
+
self.value
|
305
|
+
else
|
306
|
+
self.value * coercion_factor(self.numerator_units, num_units) /
|
307
|
+
coercion_factor(self.denominator_units, den_units)
|
308
|
+
end, num_units, den_units)
|
309
|
+
end
|
310
|
+
|
311
|
+
# @param other [Number] A number to decide if it can be compared with this number.
|
312
|
+
# @return [Boolean] Whether or not this number can be compared with the other.
|
313
|
+
def comparable_to?(other)
|
314
|
+
begin
|
315
|
+
operate(other, :+)
|
316
|
+
true
|
317
|
+
rescue Sass::UnitConversionError
|
318
|
+
false
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
# Returns a human readable representation of the units in this number.
|
323
|
+
# For complex units this takes the form of:
|
324
|
+
# numerator_unit1 * numerator_unit2 / denominator_unit1 * denominator_unit2
|
325
|
+
# @return [String] a string that represents the units in this number
|
326
|
+
def unit_str
|
327
|
+
rv = numerator_units.sort.join("*")
|
328
|
+
if denominator_units.any?
|
329
|
+
rv << "/"
|
330
|
+
rv << denominator_units.sort.join("*")
|
331
|
+
end
|
332
|
+
rv
|
333
|
+
end
|
334
|
+
|
335
|
+
private
|
336
|
+
|
337
|
+
def operate(other, operation)
|
338
|
+
this = self
|
339
|
+
if [:+, :-, :<=, :<, :>, :>=].include?(operation)
|
340
|
+
if unitless?
|
341
|
+
this = this.coerce(other.numerator_units, other.denominator_units)
|
342
|
+
else
|
343
|
+
other = other.coerce(numerator_units, denominator_units)
|
344
|
+
end
|
345
|
+
end
|
346
|
+
# avoid integer division
|
347
|
+
value = (:/ == operation) ? this.value.to_f : this.value
|
348
|
+
result = value.send(operation, other.value)
|
349
|
+
|
350
|
+
if result.is_a?(Numeric)
|
351
|
+
Number.new(result, *compute_units(this, other, operation))
|
352
|
+
else # Boolean op
|
353
|
+
Bool.new(result)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
def coercion_factor(from_units, to_units)
|
358
|
+
# get a list of unmatched units
|
359
|
+
from_units, to_units = sans_common_units(from_units, to_units)
|
360
|
+
|
361
|
+
if from_units.size != to_units.size || !convertable?(from_units | to_units)
|
362
|
+
raise Sass::UnitConversionError.new("Incompatible units: '#{from_units.join('*')}' and '#{to_units.join('*')}'.")
|
363
|
+
end
|
364
|
+
|
365
|
+
from_units.zip(to_units).inject(1) {|m,p| m * conversion_factor(p[0], p[1]) }
|
366
|
+
end
|
367
|
+
|
368
|
+
def compute_units(this, other, operation)
|
369
|
+
case operation
|
370
|
+
when :*
|
371
|
+
[this.numerator_units + other.numerator_units, this.denominator_units + other.denominator_units]
|
372
|
+
when :/
|
373
|
+
[this.numerator_units + other.denominator_units, this.denominator_units + other.numerator_units]
|
374
|
+
else
|
375
|
+
[this.numerator_units, this.denominator_units]
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
def normalize!
|
380
|
+
return if unitless?
|
381
|
+
@numerator_units, @denominator_units = sans_common_units(numerator_units, denominator_units)
|
382
|
+
|
383
|
+
@denominator_units.each_with_index do |d, i|
|
384
|
+
if convertable?(d) && (u = @numerator_units.detect(&method(:convertable?)))
|
385
|
+
@value /= conversion_factor(d, u)
|
386
|
+
@denominator_units.delete_at(i)
|
387
|
+
@numerator_units.delete_at(@numerator_units.index(u))
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
# A hash of unit names to their index in the conversion table
|
393
|
+
CONVERTABLE_UNITS = {"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4}
|
394
|
+
CONVERSION_TABLE = [[ 1, 2.54, 6, 25.4, 72 ], # in
|
395
|
+
[ nil, 1, 2.36220473, 10, 28.3464567], # cm
|
396
|
+
[ nil, nil, 1, 4.23333333, 12 ], # pc
|
397
|
+
[ nil, nil, nil, 1, 2.83464567], # mm
|
398
|
+
[ nil, nil, nil, nil, 1 ]] # pt
|
399
|
+
|
400
|
+
def conversion_factor(from_unit, to_unit)
|
401
|
+
res = CONVERSION_TABLE[CONVERTABLE_UNITS[from_unit]][CONVERTABLE_UNITS[to_unit]]
|
402
|
+
return 1.0 / conversion_factor(to_unit, from_unit) if res.nil?
|
403
|
+
res
|
404
|
+
end
|
405
|
+
|
406
|
+
def convertable?(units)
|
407
|
+
Array(units).all?(&CONVERTABLE_UNITS.method(:include?))
|
408
|
+
end
|
409
|
+
|
410
|
+
def sans_common_units(units1, units2)
|
411
|
+
units2 = units2.dup
|
412
|
+
# Can't just use -, because we want px*px to coerce properly to px*mm
|
413
|
+
return units1.map do |u|
|
414
|
+
next u unless j = units2.index(u)
|
415
|
+
units2.delete_at(j)
|
416
|
+
nil
|
417
|
+
end.compact, units2
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|