aliddle-sass 1.0
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/MIT-LICENSE +20 -0
- data/README.md +201 -0
- data/Rakefile +347 -0
- data/VERSION +1 -0
- data/VERSION_NAME +1 -0
- data/bin/sass +9 -0
- data/bin/sass-convert +8 -0
- data/bin/scss +9 -0
- data/extra/update_watch.rb +13 -0
- data/init.rb +18 -0
- data/lib/sass.rb +95 -0
- data/lib/sass/cache_stores.rb +15 -0
- data/lib/sass/cache_stores/base.rb +88 -0
- data/lib/sass/cache_stores/chain.rb +33 -0
- data/lib/sass/cache_stores/filesystem.rb +60 -0
- data/lib/sass/cache_stores/memory.rb +47 -0
- data/lib/sass/cache_stores/null.rb +25 -0
- data/lib/sass/callbacks.rb +66 -0
- data/lib/sass/css.rb +409 -0
- data/lib/sass/engine.rb +928 -0
- data/lib/sass/environment.rb +101 -0
- data/lib/sass/error.rb +201 -0
- data/lib/sass/exec.rb +707 -0
- data/lib/sass/importers.rb +22 -0
- data/lib/sass/importers/base.rb +139 -0
- data/lib/sass/importers/filesystem.rb +190 -0
- data/lib/sass/logger.rb +15 -0
- data/lib/sass/logger/base.rb +32 -0
- data/lib/sass/logger/log_level.rb +49 -0
- data/lib/sass/media.rb +213 -0
- data/lib/sass/plugin.rb +132 -0
- data/lib/sass/plugin/compiler.rb +406 -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 +60 -0
- data/lib/sass/plugin/rails.rb +47 -0
- data/lib/sass/plugin/staleness_checker.rb +183 -0
- data/lib/sass/railtie.rb +9 -0
- data/lib/sass/repl.rb +57 -0
- data/lib/sass/root.rb +7 -0
- data/lib/sass/script.rb +39 -0
- data/lib/sass/script/arg_list.rb +52 -0
- data/lib/sass/script/bool.rb +18 -0
- data/lib/sass/script/color.rb +606 -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 +237 -0
- data/lib/sass/script/functions.rb +1543 -0
- data/lib/sass/script/interpolation.rb +79 -0
- data/lib/sass/script/lexer.rb +348 -0
- data/lib/sass/script/list.rb +85 -0
- data/lib/sass/script/literal.rb +221 -0
- data/lib/sass/script/node.rb +99 -0
- data/lib/sass/script/null.rb +37 -0
- data/lib/sass/script/number.rb +453 -0
- data/lib/sass/script/operation.rb +110 -0
- data/lib/sass/script/parser.rb +495 -0
- data/lib/sass/script/string.rb +51 -0
- data/lib/sass/script/string_interpolation.rb +103 -0
- data/lib/sass/script/unary_operation.rb +69 -0
- data/lib/sass/script/variable.rb +58 -0
- data/lib/sass/scss.rb +16 -0
- data/lib/sass/scss/css_parser.rb +36 -0
- data/lib/sass/scss/parser.rb +1179 -0
- data/lib/sass/scss/rx.rb +133 -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 +54 -0
- data/lib/sass/selector.rb +452 -0
- data/lib/sass/selector/abstract_sequence.rb +94 -0
- data/lib/sass/selector/comma_sequence.rb +92 -0
- data/lib/sass/selector/sequence.rb +507 -0
- data/lib/sass/selector/simple.rb +119 -0
- data/lib/sass/selector/simple_sequence.rb +212 -0
- data/lib/sass/shared.rb +76 -0
- data/lib/sass/supports.rb +229 -0
- data/lib/sass/tree/charset_node.rb +22 -0
- data/lib/sass/tree/comment_node.rb +82 -0
- data/lib/sass/tree/content_node.rb +9 -0
- data/lib/sass/tree/css_import_node.rb +60 -0
- data/lib/sass/tree/debug_node.rb +18 -0
- data/lib/sass/tree/directive_node.rb +42 -0
- data/lib/sass/tree/each_node.rb +24 -0
- data/lib/sass/tree/extend_node.rb +36 -0
- data/lib/sass/tree/for_node.rb +36 -0
- data/lib/sass/tree/function_node.rb +34 -0
- data/lib/sass/tree/if_node.rb +52 -0
- data/lib/sass/tree/import_node.rb +75 -0
- data/lib/sass/tree/media_node.rb +58 -0
- data/lib/sass/tree/mixin_def_node.rb +38 -0
- data/lib/sass/tree/mixin_node.rb +39 -0
- data/lib/sass/tree/node.rb +196 -0
- data/lib/sass/tree/prop_node.rb +152 -0
- data/lib/sass/tree/return_node.rb +18 -0
- data/lib/sass/tree/root_node.rb +28 -0
- data/lib/sass/tree/rule_node.rb +132 -0
- data/lib/sass/tree/supports_node.rb +51 -0
- data/lib/sass/tree/trace_node.rb +32 -0
- data/lib/sass/tree/variable_node.rb +30 -0
- data/lib/sass/tree/visitors/base.rb +75 -0
- data/lib/sass/tree/visitors/check_nesting.rb +147 -0
- data/lib/sass/tree/visitors/convert.rb +316 -0
- data/lib/sass/tree/visitors/cssize.rb +229 -0
- data/lib/sass/tree/visitors/deep_copy.rb +102 -0
- data/lib/sass/tree/visitors/extend.rb +68 -0
- data/lib/sass/tree/visitors/perform.rb +446 -0
- data/lib/sass/tree/visitors/set_options.rb +125 -0
- data/lib/sass/tree/visitors/to_css.rb +230 -0
- data/lib/sass/tree/warn_node.rb +18 -0
- data/lib/sass/tree/while_node.rb +18 -0
- data/lib/sass/util.rb +906 -0
- data/lib/sass/util/multibyte_string_scanner.rb +155 -0
- data/lib/sass/util/subset_map.rb +109 -0
- data/lib/sass/util/test.rb +10 -0
- data/lib/sass/version.rb +126 -0
- data/rails/init.rb +1 -0
- data/test/Gemfile +3 -0
- data/test/Gemfile.lock +10 -0
- data/test/sass/cache_test.rb +89 -0
- data/test/sass/callbacks_test.rb +61 -0
- data/test/sass/conversion_test.rb +1760 -0
- data/test/sass/css2sass_test.rb +439 -0
- data/test/sass/data/hsl-rgb.txt +319 -0
- data/test/sass/engine_test.rb +3243 -0
- data/test/sass/exec_test.rb +86 -0
- data/test/sass/extend_test.rb +1461 -0
- data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
- data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
- data/test/sass/functions_test.rb +1139 -0
- data/test/sass/importer_test.rb +192 -0
- data/test/sass/logger_test.rb +58 -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 +550 -0
- data/test/sass/results/alt.css +4 -0
- data/test/sass/results/basic.css +9 -0
- data/test/sass/results/cached_import_option.css +3 -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/filename_fn.css +3 -0
- data/test/sass/results/if.css +3 -0
- data/test/sass/results/import.css +31 -0
- data/test/sass/results/import_charset.css +5 -0
- data/test/sass/results/import_charset_1_8.css +5 -0
- data/test/sass/results/import_charset_ibm866.css +5 -0
- data/test/sass/results/import_content.css +1 -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 +299 -0
- data/test/sass/script_test.rb +591 -0
- data/test/sass/scss/css_test.rb +1093 -0
- data/test/sass/scss/rx_test.rb +156 -0
- data/test/sass/scss/scss_test.rb +2043 -0
- data/test/sass/scss/test_helper.rb +37 -0
- data/test/sass/templates/_cached_import_option_partial.scss +1 -0
- data/test/sass/templates/_double_import_loop2.sass +1 -0
- data/test/sass/templates/_filename_fn_import.scss +11 -0
- data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
- data/test/sass/templates/_imported_charset_utf8.sass +4 -0
- data/test/sass/templates/_imported_content.sass +3 -0
- data/test/sass/templates/_partial.sass +2 -0
- data/test/sass/templates/_same_name_different_partiality.scss +1 -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/bork5.sass +3 -0
- data/test/sass/templates/cached_import_option.scss +3 -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/double_import_loop1.sass +1 -0
- data/test/sass/templates/expanded.sass +17 -0
- data/test/sass/templates/filename_fn.scss +18 -0
- data/test/sass/templates/if.sass +11 -0
- data/test/sass/templates/import.sass +12 -0
- data/test/sass/templates/import_charset.sass +9 -0
- data/test/sass/templates/import_charset_1_8.sass +6 -0
- data/test/sass/templates/import_charset_ibm866.sass +11 -0
- data/test/sass/templates/import_content.sass +4 -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_import.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/same_name_different_ext.sass +2 -0
- data/test/sass/templates/same_name_different_ext.scss +1 -0
- data/test/sass/templates/same_name_different_partiality.scss +1 -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/single_import_loop.sass +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/multibyte_string_scanner_test.rb +147 -0
- data/test/sass/util/subset_map_test.rb +91 -0
- data/test/sass/util_test.rb +313 -0
- data/test/test_helper.rb +80 -0
- metadata +348 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
# A visitor for setting options on the Sass tree
|
2
|
+
class Sass::Tree::Visitors::SetOptions < Sass::Tree::Visitors::Base
|
3
|
+
# @param root [Tree::Node] The root node of the tree to visit.
|
4
|
+
# @param options [{Symbol => Object}] The options has to set.
|
5
|
+
def self.visit(root, options); new(options).send(:visit, root); end
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def visit(node)
|
14
|
+
node.instance_variable_set('@options', @options)
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def visit_debug(node)
|
19
|
+
node.expr.options = @options
|
20
|
+
yield
|
21
|
+
end
|
22
|
+
|
23
|
+
def visit_each(node)
|
24
|
+
node.list.options = @options
|
25
|
+
yield
|
26
|
+
end
|
27
|
+
|
28
|
+
def visit_extend(node)
|
29
|
+
node.selector.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)}
|
30
|
+
yield
|
31
|
+
end
|
32
|
+
|
33
|
+
def visit_for(node)
|
34
|
+
node.from.options = @options
|
35
|
+
node.to.options = @options
|
36
|
+
yield
|
37
|
+
end
|
38
|
+
|
39
|
+
def visit_function(node)
|
40
|
+
node.args.each do |k, v|
|
41
|
+
k.options = @options
|
42
|
+
v.options = @options if v
|
43
|
+
end
|
44
|
+
yield
|
45
|
+
end
|
46
|
+
|
47
|
+
def visit_if(node)
|
48
|
+
node.expr.options = @options if node.expr
|
49
|
+
visit(node.else) if node.else
|
50
|
+
yield
|
51
|
+
end
|
52
|
+
|
53
|
+
def visit_import(node)
|
54
|
+
# We have no good way of propagating the new options through an Engine
|
55
|
+
# instance, so we just null it out. This also lets us avoid caching an
|
56
|
+
# imported Engine along with the importing source tree.
|
57
|
+
node.imported_file = nil
|
58
|
+
yield
|
59
|
+
end
|
60
|
+
|
61
|
+
def visit_mixindef(node)
|
62
|
+
node.args.each do |k, v|
|
63
|
+
k.options = @options
|
64
|
+
v.options = @options if v
|
65
|
+
end
|
66
|
+
yield
|
67
|
+
end
|
68
|
+
|
69
|
+
def visit_mixin(node)
|
70
|
+
node.args.each {|a| a.options = @options}
|
71
|
+
node.keywords.each {|k, v| v.options = @options}
|
72
|
+
yield
|
73
|
+
end
|
74
|
+
|
75
|
+
def visit_prop(node)
|
76
|
+
node.name.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)}
|
77
|
+
node.value.options = @options
|
78
|
+
yield
|
79
|
+
end
|
80
|
+
|
81
|
+
def visit_return(node)
|
82
|
+
node.expr.options = @options
|
83
|
+
yield
|
84
|
+
end
|
85
|
+
|
86
|
+
def visit_rule(node)
|
87
|
+
node.rule.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)}
|
88
|
+
yield
|
89
|
+
end
|
90
|
+
|
91
|
+
def visit_variable(node)
|
92
|
+
node.expr.options = @options
|
93
|
+
yield
|
94
|
+
end
|
95
|
+
|
96
|
+
def visit_warn(node)
|
97
|
+
node.expr.options = @options
|
98
|
+
yield
|
99
|
+
end
|
100
|
+
|
101
|
+
def visit_while(node)
|
102
|
+
node.expr.options = @options
|
103
|
+
yield
|
104
|
+
end
|
105
|
+
|
106
|
+
def visit_directive(node)
|
107
|
+
node.value.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)}
|
108
|
+
yield
|
109
|
+
end
|
110
|
+
|
111
|
+
def visit_media(node)
|
112
|
+
node.query.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)}
|
113
|
+
yield
|
114
|
+
end
|
115
|
+
|
116
|
+
def visit_cssimport(node)
|
117
|
+
node.query.each {|c| c.options = @options if c.is_a?(Sass::Script::Node)} if node.query
|
118
|
+
yield
|
119
|
+
end
|
120
|
+
|
121
|
+
def visit_supports(node)
|
122
|
+
node.condition.options = @options
|
123
|
+
yield
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
# A visitor for converting a Sass tree into CSS.
|
2
|
+
class Sass::Tree::Visitors::ToCss < Sass::Tree::Visitors::Base
|
3
|
+
protected
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@tabs = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def visit(node)
|
10
|
+
super
|
11
|
+
rescue Sass::SyntaxError => e
|
12
|
+
e.modify_backtrace(:filename => node.filename, :line => node.line)
|
13
|
+
raise e
|
14
|
+
end
|
15
|
+
|
16
|
+
def with_tabs(tabs)
|
17
|
+
old_tabs, @tabs = @tabs, tabs
|
18
|
+
yield
|
19
|
+
ensure
|
20
|
+
@tabs = old_tabs
|
21
|
+
end
|
22
|
+
|
23
|
+
def visit_root(node)
|
24
|
+
result = String.new
|
25
|
+
node.children.each do |child|
|
26
|
+
next if child.invisible?
|
27
|
+
child_str = visit(child)
|
28
|
+
result << child_str + (node.style == :compressed ? '' : "\n")
|
29
|
+
end
|
30
|
+
result.rstrip!
|
31
|
+
return "" if result.empty?
|
32
|
+
result << "\n"
|
33
|
+
unless Sass::Util.ruby1_8? || result.ascii_only?
|
34
|
+
if node.children.first.is_a?(Sass::Tree::CharsetNode)
|
35
|
+
begin
|
36
|
+
encoding = node.children.first.name
|
37
|
+
# Default to big-endian encoding, because we have to decide somehow
|
38
|
+
encoding << 'BE' if encoding =~ /\Autf-(16|32)\Z/i
|
39
|
+
result = result.encode(Encoding.find(encoding))
|
40
|
+
rescue EncodingError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
result = "@charset \"#{result.encoding.name}\";#{
|
45
|
+
node.style == :compressed ? '' : "\n"
|
46
|
+
}".encode(result.encoding) + result
|
47
|
+
end
|
48
|
+
result
|
49
|
+
rescue Sass::SyntaxError => e
|
50
|
+
e.sass_template ||= node.template
|
51
|
+
raise e
|
52
|
+
end
|
53
|
+
|
54
|
+
def visit_charset(node)
|
55
|
+
"@charset \"#{node.name}\";"
|
56
|
+
end
|
57
|
+
|
58
|
+
def visit_comment(node)
|
59
|
+
return if node.invisible?
|
60
|
+
spaces = (' ' * [@tabs - node.resolved_value[/^ */].size, 0].max)
|
61
|
+
|
62
|
+
content = node.resolved_value.gsub(/^/, spaces)
|
63
|
+
content.gsub!(%r{^(\s*)//(.*)$}) {|md| "#{$1}/*#{$2} */"} if node.type == :silent
|
64
|
+
content.gsub!(/\n +(\* *(?!\/))?/, ' ') if (node.style == :compact || node.style == :compressed) && node.type != :loud
|
65
|
+
content
|
66
|
+
end
|
67
|
+
|
68
|
+
def visit_directive(node)
|
69
|
+
was_in_directive = @in_directive
|
70
|
+
tab_str = ' ' * @tabs
|
71
|
+
return tab_str + node.resolved_value + ";" unless node.has_children
|
72
|
+
return tab_str + node.resolved_value + " {}" if node.children.empty?
|
73
|
+
@in_directive = @in_directive || !node.is_a?(Sass::Tree::MediaNode)
|
74
|
+
result = if node.style == :compressed
|
75
|
+
"#{node.resolved_value}{"
|
76
|
+
else
|
77
|
+
"#{tab_str}#{node.resolved_value} {" + (node.style == :compact ? ' ' : "\n")
|
78
|
+
end
|
79
|
+
was_prop = false
|
80
|
+
first = true
|
81
|
+
node.children.each do |child|
|
82
|
+
next if child.invisible?
|
83
|
+
if node.style == :compact
|
84
|
+
if child.is_a?(Sass::Tree::PropNode)
|
85
|
+
with_tabs(first || was_prop ? 0 : @tabs + 1) {result << visit(child) << ' '}
|
86
|
+
else
|
87
|
+
result[-1] = "\n" if was_prop
|
88
|
+
rendered = with_tabs(@tabs + 1) {visit(child).dup}
|
89
|
+
rendered = rendered.lstrip if first
|
90
|
+
result << rendered.rstrip + "\n"
|
91
|
+
end
|
92
|
+
was_prop = child.is_a?(Sass::Tree::PropNode)
|
93
|
+
first = false
|
94
|
+
elsif node.style == :compressed
|
95
|
+
result << (was_prop ? ";" : "") << with_tabs(0) {visit(child)}
|
96
|
+
was_prop = child.is_a?(Sass::Tree::PropNode)
|
97
|
+
else
|
98
|
+
result << with_tabs(@tabs + 1) {visit(child)} + "\n"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
result.rstrip + if node.style == :compressed
|
102
|
+
"}"
|
103
|
+
else
|
104
|
+
(node.style == :expanded ? "\n" : " ") + "}\n"
|
105
|
+
end
|
106
|
+
ensure
|
107
|
+
@in_directive = was_in_directive
|
108
|
+
end
|
109
|
+
|
110
|
+
def visit_media(node)
|
111
|
+
str = with_tabs(@tabs + node.tabs) {visit_directive(node)}
|
112
|
+
str.gsub!(/\n\Z/, '') unless node.style == :compressed || node.group_end
|
113
|
+
str
|
114
|
+
end
|
115
|
+
|
116
|
+
def visit_supports(node)
|
117
|
+
visit_media(node)
|
118
|
+
end
|
119
|
+
|
120
|
+
def visit_cssimport(node)
|
121
|
+
visit_directive(node)
|
122
|
+
end
|
123
|
+
|
124
|
+
def visit_prop(node)
|
125
|
+
return if node.resolved_value.empty?
|
126
|
+
tab_str = ' ' * (@tabs + node.tabs)
|
127
|
+
if node.style == :compressed
|
128
|
+
"#{tab_str}#{node.resolved_name}:#{node.resolved_value}"
|
129
|
+
elsif node.style == :compact
|
130
|
+
"#{tab_str}#{node.resolved_name}:#{node.resolved_value}"
|
131
|
+
else
|
132
|
+
"#{tab_str}#{node.resolved_name}: #{node.resolved_value};"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def visit_rule(node)
|
137
|
+
with_tabs(@tabs + node.tabs) do
|
138
|
+
rule_separator = node.style == :compressed ? ',' : ', '
|
139
|
+
line_separator =
|
140
|
+
case node.style
|
141
|
+
when :nested, :expanded; "\n"
|
142
|
+
when :compressed; ""
|
143
|
+
else; " "
|
144
|
+
end
|
145
|
+
rule_indent = ' ' * @tabs
|
146
|
+
per_rule_indent, total_indent = [:nested, :expanded].include?(node.style) ? [rule_indent, ''] : ['', rule_indent]
|
147
|
+
|
148
|
+
joined_rules = node.resolved_rules.members.map do |seq|
|
149
|
+
next if seq.has_placeholder?
|
150
|
+
rule_part = seq.to_a.join
|
151
|
+
if node.style == :compressed
|
152
|
+
rule_part.gsub!(/([^,])\s*\n\s*/m, '\1 ')
|
153
|
+
rule_part.gsub!(/\s*([,+>])\s*/m, '\1')
|
154
|
+
rule_part.strip!
|
155
|
+
end
|
156
|
+
rule_part
|
157
|
+
end.compact.join(rule_separator)
|
158
|
+
|
159
|
+
joined_rules.sub!(/\A\s*/, per_rule_indent)
|
160
|
+
joined_rules.gsub!(/\s*\n\s*/, "#{line_separator}#{per_rule_indent}")
|
161
|
+
total_rule = total_indent << joined_rules
|
162
|
+
|
163
|
+
to_return = ''
|
164
|
+
old_spaces = ' ' * @tabs
|
165
|
+
if node.style != :compressed
|
166
|
+
if node.options[:debug_info] && !@in_directive
|
167
|
+
to_return << visit(debug_info_rule(node.debug_info, node.options)) << "\n"
|
168
|
+
elsif node.options[:trace_selectors]
|
169
|
+
to_return << "#{old_spaces}/* "
|
170
|
+
to_return << node.stack_trace.join("\n #{old_spaces}")
|
171
|
+
to_return << " */\n"
|
172
|
+
elsif node.options[:line_comments]
|
173
|
+
to_return << "#{old_spaces}/* line #{node.line}"
|
174
|
+
|
175
|
+
if node.filename
|
176
|
+
relative_filename = if node.options[:css_filename]
|
177
|
+
begin
|
178
|
+
Pathname.new(node.filename).relative_path_from(
|
179
|
+
Pathname.new(File.dirname(node.options[:css_filename]))).to_s
|
180
|
+
rescue ArgumentError
|
181
|
+
nil
|
182
|
+
end
|
183
|
+
end
|
184
|
+
relative_filename ||= node.filename
|
185
|
+
to_return << ", #{relative_filename}"
|
186
|
+
end
|
187
|
+
|
188
|
+
to_return << " */\n"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
if node.style == :compact
|
193
|
+
properties = with_tabs(0) {node.children.map {|a| visit(a)}.join(' ')}
|
194
|
+
to_return << "#{total_rule} { #{properties} }#{"\n" if node.group_end}"
|
195
|
+
elsif node.style == :compressed
|
196
|
+
properties = with_tabs(0) {node.children.map {|a| visit(a)}.join(';')}
|
197
|
+
to_return << "#{total_rule}{#{properties}}"
|
198
|
+
else
|
199
|
+
properties = with_tabs(@tabs + 1) {node.children.map {|a| visit(a)}.join("\n")}
|
200
|
+
end_props = (node.style == :expanded ? "\n" + old_spaces : ' ')
|
201
|
+
to_return << "#{total_rule} {\n#{properties}#{end_props}}#{"\n" if node.group_end}"
|
202
|
+
end
|
203
|
+
|
204
|
+
to_return
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
private
|
209
|
+
|
210
|
+
def debug_info_rule(debug_info, options)
|
211
|
+
node = Sass::Tree::DirectiveNode.resolved("@media -sass-debug-info")
|
212
|
+
Sass::Util.hash_to_a(debug_info.map {|k, v| [k.to_s, v.to_s]}).each do |k, v|
|
213
|
+
rule = Sass::Tree::RuleNode.new([""])
|
214
|
+
rule.resolved_rules = Sass::Selector::CommaSequence.new(
|
215
|
+
[Sass::Selector::Sequence.new(
|
216
|
+
[Sass::Selector::SimpleSequence.new(
|
217
|
+
[Sass::Selector::Element.new(k.to_s.gsub(/[^\w-]/, "\\\\\\0"), nil)],
|
218
|
+
false)
|
219
|
+
])
|
220
|
+
])
|
221
|
+
prop = Sass::Tree::PropNode.new([""], Sass::Script::String.new(''), :new)
|
222
|
+
prop.resolved_name = "font-family"
|
223
|
+
prop.resolved_value = Sass::SCSS::RX.escape_ident(v.to_s)
|
224
|
+
rule << prop
|
225
|
+
node << rule
|
226
|
+
end
|
227
|
+
node.options = options.merge(:debug_info => false, :line_comments => false, :style => :compressed)
|
228
|
+
node
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Sass
|
2
|
+
module Tree
|
3
|
+
# A dynamic node representing a Sass `@warn` statement.
|
4
|
+
#
|
5
|
+
# @see Sass::Tree
|
6
|
+
class WarnNode < Node
|
7
|
+
# The expression to print.
|
8
|
+
# @return [Script::Node]
|
9
|
+
attr_accessor :expr
|
10
|
+
|
11
|
+
# @param expr [Script::Node] The expression to print
|
12
|
+
def initialize(expr)
|
13
|
+
@expr = expr
|
14
|
+
super()
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'sass/tree/node'
|
2
|
+
|
3
|
+
module Sass::Tree
|
4
|
+
# A dynamic node representing a Sass `@while` loop.
|
5
|
+
#
|
6
|
+
# @see Sass::Tree
|
7
|
+
class WhileNode < Node
|
8
|
+
# The parse tree for the continuation expression.
|
9
|
+
# @return [Script::Node]
|
10
|
+
attr_accessor :expr
|
11
|
+
|
12
|
+
# @param expr [Script::Node] See \{#expr}
|
13
|
+
def initialize(expr)
|
14
|
+
@expr = expr
|
15
|
+
super()
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/sass/util.rb
ADDED
@@ -0,0 +1,906 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'set'
|
3
|
+
require 'enumerator'
|
4
|
+
require 'stringio'
|
5
|
+
require 'rbconfig'
|
6
|
+
|
7
|
+
require 'sass/root'
|
8
|
+
require 'sass/util/subset_map'
|
9
|
+
|
10
|
+
module Sass
|
11
|
+
# A module containing various useful functions.
|
12
|
+
module Util
|
13
|
+
extend self
|
14
|
+
|
15
|
+
# An array of ints representing the Ruby version number.
|
16
|
+
# @api public
|
17
|
+
RUBY_VERSION = ::RUBY_VERSION.split(".").map {|s| s.to_i}
|
18
|
+
|
19
|
+
# The Ruby engine we're running under. Defaults to `"ruby"`
|
20
|
+
# if the top-level constant is undefined.
|
21
|
+
# @api public
|
22
|
+
RUBY_ENGINE = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "ruby"
|
23
|
+
|
24
|
+
# Returns the path of a file relative to the Sass root directory.
|
25
|
+
#
|
26
|
+
# @param file [String] The filename relative to the Sass root
|
27
|
+
# @return [String] The filename relative to the the working directory
|
28
|
+
def scope(file)
|
29
|
+
File.join(Sass::ROOT_DIR, file)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Converts an array of `[key, value]` pairs to a hash.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# to_hash([[:foo, "bar"], [:baz, "bang"]])
|
36
|
+
# #=> {:foo => "bar", :baz => "bang"}
|
37
|
+
# @param arr [Array<(Object, Object)>] An array of pairs
|
38
|
+
# @return [Hash] A hash
|
39
|
+
def to_hash(arr)
|
40
|
+
Hash[arr.compact]
|
41
|
+
end
|
42
|
+
|
43
|
+
# Maps the keys in a hash according to a block.
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# map_keys({:foo => "bar", :baz => "bang"}) {|k| k.to_s}
|
47
|
+
# #=> {"foo" => "bar", "baz" => "bang"}
|
48
|
+
# @param hash [Hash] The hash to map
|
49
|
+
# @yield [key] A block in which the keys are transformed
|
50
|
+
# @yieldparam key [Object] The key that should be mapped
|
51
|
+
# @yieldreturn [Object] The new value for the key
|
52
|
+
# @return [Hash] The mapped hash
|
53
|
+
# @see #map_vals
|
54
|
+
# @see #map_hash
|
55
|
+
def map_keys(hash)
|
56
|
+
to_hash(hash.map {|k, v| [yield(k), v]})
|
57
|
+
end
|
58
|
+
|
59
|
+
# Maps the values in a hash according to a block.
|
60
|
+
#
|
61
|
+
# @example
|
62
|
+
# map_values({:foo => "bar", :baz => "bang"}) {|v| v.to_sym}
|
63
|
+
# #=> {:foo => :bar, :baz => :bang}
|
64
|
+
# @param hash [Hash] The hash to map
|
65
|
+
# @yield [value] A block in which the values are transformed
|
66
|
+
# @yieldparam value [Object] The value that should be mapped
|
67
|
+
# @yieldreturn [Object] The new value for the value
|
68
|
+
# @return [Hash] The mapped hash
|
69
|
+
# @see #map_keys
|
70
|
+
# @see #map_hash
|
71
|
+
def map_vals(hash)
|
72
|
+
to_hash(hash.map {|k, v| [k, yield(v)]})
|
73
|
+
end
|
74
|
+
|
75
|
+
# Maps the key-value pairs of a hash according to a block.
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
# map_hash({:foo => "bar", :baz => "bang"}) {|k, v| [k.to_s, v.to_sym]}
|
79
|
+
# #=> {"foo" => :bar, "baz" => :bang}
|
80
|
+
# @param hash [Hash] The hash to map
|
81
|
+
# @yield [key, value] A block in which the key-value pairs are transformed
|
82
|
+
# @yieldparam [key] The hash key
|
83
|
+
# @yieldparam [value] The hash value
|
84
|
+
# @yieldreturn [(Object, Object)] The new value for the `[key, value]` pair
|
85
|
+
# @return [Hash] The mapped hash
|
86
|
+
# @see #map_keys
|
87
|
+
# @see #map_vals
|
88
|
+
def map_hash(hash)
|
89
|
+
# Using &block here completely hoses performance on 1.8.
|
90
|
+
to_hash(hash.map {|k, v| yield k, v})
|
91
|
+
end
|
92
|
+
|
93
|
+
# Computes the powerset of the given array.
|
94
|
+
# This is the set of all subsets of the array.
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# powerset([1, 2, 3]) #=>
|
98
|
+
# Set[Set[], Set[1], Set[2], Set[3], Set[1, 2], Set[2, 3], Set[1, 3], Set[1, 2, 3]]
|
99
|
+
# @param arr [Enumerable]
|
100
|
+
# @return [Set<Set>] The subsets of `arr`
|
101
|
+
def powerset(arr)
|
102
|
+
arr.inject([Set.new].to_set) do |powerset, el|
|
103
|
+
new_powerset = Set.new
|
104
|
+
powerset.each do |subset|
|
105
|
+
new_powerset << subset
|
106
|
+
new_powerset << subset + [el]
|
107
|
+
end
|
108
|
+
new_powerset
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Restricts a number to falling within a given range.
|
113
|
+
# Returns the number if it falls within the range,
|
114
|
+
# or the closest value in the range if it doesn't.
|
115
|
+
#
|
116
|
+
# @param value [Numeric]
|
117
|
+
# @param range [Range<Numeric>]
|
118
|
+
# @return [Numeric]
|
119
|
+
def restrict(value, range)
|
120
|
+
[[value, range.first].max, range.last].min
|
121
|
+
end
|
122
|
+
|
123
|
+
# Concatenates all strings that are adjacent in an array,
|
124
|
+
# while leaving other elements as they are.
|
125
|
+
#
|
126
|
+
# @example
|
127
|
+
# merge_adjacent_strings([1, "foo", "bar", 2, "baz"])
|
128
|
+
# #=> [1, "foobar", 2, "baz"]
|
129
|
+
# @param arr [Array]
|
130
|
+
# @return [Array] The enumerable with strings merged
|
131
|
+
def merge_adjacent_strings(arr)
|
132
|
+
# Optimize for the common case of one element
|
133
|
+
return arr if arr.size < 2
|
134
|
+
arr.inject([]) do |a, e|
|
135
|
+
if e.is_a?(String)
|
136
|
+
if a.last.is_a?(String)
|
137
|
+
a.last << e
|
138
|
+
else
|
139
|
+
a << e.dup
|
140
|
+
end
|
141
|
+
else
|
142
|
+
a << e
|
143
|
+
end
|
144
|
+
a
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Intersperses a value in an enumerable, as would be done with `Array#join`
|
149
|
+
# but without concatenating the array together afterwards.
|
150
|
+
#
|
151
|
+
# @param enum [Enumerable]
|
152
|
+
# @param val
|
153
|
+
# @return [Array]
|
154
|
+
def intersperse(enum, val)
|
155
|
+
enum.inject([]) {|a, e| a << e << val}[0...-1]
|
156
|
+
end
|
157
|
+
|
158
|
+
# Substitutes a sub-array of one array with another sub-array.
|
159
|
+
#
|
160
|
+
# @param ary [Array] The array in which to make the substitution
|
161
|
+
# @param from [Array] The sequence of elements to replace with `to`
|
162
|
+
# @param to [Array] The sequence of elements to replace `from` with
|
163
|
+
def substitute(ary, from, to)
|
164
|
+
res = ary.dup
|
165
|
+
i = 0
|
166
|
+
while i < res.size
|
167
|
+
if res[i...i+from.size] == from
|
168
|
+
res[i...i+from.size] = to
|
169
|
+
end
|
170
|
+
i += 1
|
171
|
+
end
|
172
|
+
res
|
173
|
+
end
|
174
|
+
|
175
|
+
# Destructively strips whitespace from the beginning and end
|
176
|
+
# of the first and last elements, respectively,
|
177
|
+
# in the array (if those elements are strings).
|
178
|
+
#
|
179
|
+
# @param arr [Array]
|
180
|
+
# @return [Array] `arr`
|
181
|
+
def strip_string_array(arr)
|
182
|
+
arr.first.lstrip! if arr.first.is_a?(String)
|
183
|
+
arr.last.rstrip! if arr.last.is_a?(String)
|
184
|
+
arr
|
185
|
+
end
|
186
|
+
|
187
|
+
# Return an array of all possible paths through the given arrays.
|
188
|
+
#
|
189
|
+
# @param arrs [Array<Array>]
|
190
|
+
# @return [Array<Arrays>]
|
191
|
+
#
|
192
|
+
# @example
|
193
|
+
# paths([[1, 2], [3, 4], [5]]) #=>
|
194
|
+
# # [[1, 3, 5],
|
195
|
+
# # [2, 3, 5],
|
196
|
+
# # [1, 4, 5],
|
197
|
+
# # [2, 4, 5]]
|
198
|
+
def paths(arrs)
|
199
|
+
arrs.inject([[]]) do |paths, arr|
|
200
|
+
flatten(arr.map {|e| paths.map {|path| path + [e]}}, 1)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Computes a single longest common subsequence for `x` and `y`.
|
205
|
+
# If there are more than one longest common subsequences,
|
206
|
+
# the one returned is that which starts first in `x`.
|
207
|
+
#
|
208
|
+
# @param x [Array]
|
209
|
+
# @param y [Array]
|
210
|
+
# @yield [a, b] An optional block to use in place of a check for equality
|
211
|
+
# between elements of `x` and `y`.
|
212
|
+
# @yieldreturn [Object, nil] If the two values register as equal,
|
213
|
+
# this will return the value to use in the LCS array.
|
214
|
+
# @return [Array] The LCS
|
215
|
+
def lcs(x, y, &block)
|
216
|
+
x = [nil, *x]
|
217
|
+
y = [nil, *y]
|
218
|
+
block ||= proc {|a, b| a == b && a}
|
219
|
+
lcs_backtrace(lcs_table(x, y, &block), x, y, x.size-1, y.size-1, &block)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Converts a Hash to an Array. This is usually identical to `Hash#to_a`,
|
223
|
+
# with the following exceptions:
|
224
|
+
#
|
225
|
+
# * In Ruby 1.8, `Hash#to_a` is not deterministically ordered, but this is.
|
226
|
+
# * In Ruby 1.9 when running tests, this is ordered in the same way it would
|
227
|
+
# be under Ruby 1.8 (sorted key order rather than insertion order).
|
228
|
+
#
|
229
|
+
# @param hash [Hash]
|
230
|
+
# @return [Array]
|
231
|
+
def hash_to_a(hash)
|
232
|
+
return hash.to_a unless ruby1_8? || defined?(Test::Unit)
|
233
|
+
return hash.sort_by {|k, v| k}
|
234
|
+
end
|
235
|
+
|
236
|
+
# Performs the equivalent of `enum.group_by.to_a`, but with a guaranteed
|
237
|
+
# order. Unlike [#hash_to_a], the resulting order isn't sorted key order;
|
238
|
+
# instead, it's the same order as `#group_by` has under Ruby 1.9 (key
|
239
|
+
# appearance order).
|
240
|
+
#
|
241
|
+
# @param enum [Enumerable]
|
242
|
+
# @return [Array<[Object, Array]>] An array of pairs.
|
243
|
+
def group_by_to_a(enum, &block)
|
244
|
+
return enum.group_by(&block).to_a unless ruby1_8?
|
245
|
+
order = {}
|
246
|
+
arr = []
|
247
|
+
enum.group_by do |e|
|
248
|
+
res = block[e]
|
249
|
+
unless order.include?(res)
|
250
|
+
order[res] = order.size
|
251
|
+
end
|
252
|
+
res
|
253
|
+
end.each do |key, vals|
|
254
|
+
arr[order[key]] = [key, vals]
|
255
|
+
end
|
256
|
+
arr
|
257
|
+
end
|
258
|
+
|
259
|
+
# Returns a sub-array of `minuend` containing only elements that are also in
|
260
|
+
# `subtrahend`. Ensures that the return value has the same order as
|
261
|
+
# `minuend`, even on Rubinius where that's not guaranteed by {Array#-}.
|
262
|
+
#
|
263
|
+
# @param minuend [Array]
|
264
|
+
# @param subtrahend [Array]
|
265
|
+
# @return [Array]
|
266
|
+
def array_minus(minuend, subtrahend)
|
267
|
+
return minuend - subtrahend unless rbx?
|
268
|
+
set = Set.new(minuend) - subtrahend
|
269
|
+
minuend.select {|e| set.include?(e)}
|
270
|
+
end
|
271
|
+
|
272
|
+
# Returns a string description of the character that caused an
|
273
|
+
# `Encoding::UndefinedConversionError`.
|
274
|
+
#
|
275
|
+
# @param [Encoding::UndefinedConversionError]
|
276
|
+
# @return [String]
|
277
|
+
def undefined_conversion_error_char(e)
|
278
|
+
# Rubinius (as of 2.0.0.rc1) pre-quotes the error character.
|
279
|
+
return e.error_char if rbx?
|
280
|
+
# JRuby (as of 1.7.2) doesn't have an error_char field on
|
281
|
+
# Encoding::UndefinedConversionError.
|
282
|
+
return e.error_char.dump unless jruby?
|
283
|
+
e.message[/^"[^"]+"/] #"
|
284
|
+
end
|
285
|
+
|
286
|
+
# Asserts that `value` falls within `range` (inclusive), leaving
|
287
|
+
# room for slight floating-point errors.
|
288
|
+
#
|
289
|
+
# @param name [String] The name of the value. Used in the error message.
|
290
|
+
# @param range [Range] The allowed range of values.
|
291
|
+
# @param value [Numeric, Sass::Script::Number] The value to check.
|
292
|
+
# @param unit [String] The unit of the value. Used in error reporting.
|
293
|
+
# @return [Numeric] `value` adjusted to fall within range, if it
|
294
|
+
# was outside by a floating-point margin.
|
295
|
+
def check_range(name, range, value, unit='')
|
296
|
+
grace = (-0.00001..0.00001)
|
297
|
+
str = value.to_s
|
298
|
+
value = value.value if value.is_a?(Sass::Script::Number)
|
299
|
+
return value if range.include?(value)
|
300
|
+
return range.first if grace.include?(value - range.first)
|
301
|
+
return range.last if grace.include?(value - range.last)
|
302
|
+
raise ArgumentError.new(
|
303
|
+
"#{name} #{str} must be between #{range.first}#{unit} and #{range.last}#{unit}")
|
304
|
+
end
|
305
|
+
|
306
|
+
# Returns whether or not `seq1` is a subsequence of `seq2`. That is, whether
|
307
|
+
# or not `seq2` contains every element in `seq1` in the same order (and
|
308
|
+
# possibly more elements besides).
|
309
|
+
#
|
310
|
+
# @param seq1 [Array]
|
311
|
+
# @param seq2 [Array]
|
312
|
+
# @return [Boolean]
|
313
|
+
def subsequence?(seq1, seq2)
|
314
|
+
i = j = 0
|
315
|
+
loop do
|
316
|
+
return true if i == seq1.size
|
317
|
+
return false if j == seq2.size
|
318
|
+
i += 1 if seq1[i] == seq2[j]
|
319
|
+
j += 1
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
# Returns information about the caller of the previous method.
|
324
|
+
#
|
325
|
+
# @param entry [String] An entry in the `#caller` list, or a similarly formatted string
|
326
|
+
# @return [[String, Fixnum, (String, nil)]] An array containing the filename, line, and method name of the caller.
|
327
|
+
# The method name may be nil
|
328
|
+
def caller_info(entry = nil)
|
329
|
+
# JRuby evaluates `caller` incorrectly when it's in an actual default argument.
|
330
|
+
entry ||= caller[1]
|
331
|
+
info = entry.scan(/^(.*?):(-?.*?)(?::.*`(.+)')?$/).first
|
332
|
+
info[1] = info[1].to_i
|
333
|
+
# This is added by Rubinius to designate a block, but we don't care about it.
|
334
|
+
info[2].sub!(/ \{\}\Z/, '') if info[2]
|
335
|
+
info
|
336
|
+
end
|
337
|
+
|
338
|
+
# Returns whether one version string represents a more recent version than another.
|
339
|
+
#
|
340
|
+
# @param v1 [String] A version string.
|
341
|
+
# @param v2 [String] Another version string.
|
342
|
+
# @return [Boolean]
|
343
|
+
def version_gt(v1, v2)
|
344
|
+
# Construct an array to make sure the shorter version is padded with nil
|
345
|
+
Array.new([v1.length, v2.length].max).zip(v1.split("."), v2.split(".")) do |_, p1, p2|
|
346
|
+
p1 ||= "0"
|
347
|
+
p2 ||= "0"
|
348
|
+
release1 = p1 =~ /^[0-9]+$/
|
349
|
+
release2 = p2 =~ /^[0-9]+$/
|
350
|
+
if release1 && release2
|
351
|
+
# Integer comparison if both are full releases
|
352
|
+
p1, p2 = p1.to_i, p2.to_i
|
353
|
+
next if p1 == p2
|
354
|
+
return p1 > p2
|
355
|
+
elsif !release1 && !release2
|
356
|
+
# String comparison if both are prereleases
|
357
|
+
next if p1 == p2
|
358
|
+
return p1 > p2
|
359
|
+
else
|
360
|
+
# If only one is a release, that one is newer
|
361
|
+
return release1
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
# Returns whether one version string represents the same or a more
|
367
|
+
# recent version than another.
|
368
|
+
#
|
369
|
+
# @param v1 [String] A version string.
|
370
|
+
# @param v2 [String] Another version string.
|
371
|
+
# @return [Boolean]
|
372
|
+
def version_geq(v1, v2)
|
373
|
+
version_gt(v1, v2) || !version_gt(v2, v1)
|
374
|
+
end
|
375
|
+
|
376
|
+
# Throws a NotImplementedError for an abstract method.
|
377
|
+
#
|
378
|
+
# @param obj [Object] `self`
|
379
|
+
# @raise [NotImplementedError]
|
380
|
+
def abstract(obj)
|
381
|
+
raise NotImplementedError.new("#{obj.class} must implement ##{caller_info[2]}")
|
382
|
+
end
|
383
|
+
|
384
|
+
# Silence all output to STDERR within a block.
|
385
|
+
#
|
386
|
+
# @yield A block in which no output will be printed to STDERR
|
387
|
+
def silence_warnings
|
388
|
+
the_real_stderr, $stderr = $stderr, StringIO.new
|
389
|
+
yield
|
390
|
+
ensure
|
391
|
+
$stderr = the_real_stderr
|
392
|
+
end
|
393
|
+
|
394
|
+
@@silence_warnings = false
|
395
|
+
# Silences all Sass warnings within a block.
|
396
|
+
#
|
397
|
+
# @yield A block in which no Sass warnings will be printed
|
398
|
+
def silence_sass_warnings
|
399
|
+
old_level, Sass.logger.log_level = Sass.logger.log_level, :error
|
400
|
+
yield
|
401
|
+
ensure
|
402
|
+
Sass.logger.log_level = old_level
|
403
|
+
end
|
404
|
+
|
405
|
+
# The same as `Kernel#warn`, but is silenced by \{#silence\_sass\_warnings}.
|
406
|
+
#
|
407
|
+
# @param msg [String]
|
408
|
+
def sass_warn(msg)
|
409
|
+
msg = msg + "\n" unless ruby1?
|
410
|
+
Sass.logger.warn(msg)
|
411
|
+
end
|
412
|
+
|
413
|
+
## Cross Rails Version Compatibility
|
414
|
+
|
415
|
+
# Returns the root of the Rails application,
|
416
|
+
# if this is running in a Rails context.
|
417
|
+
# Returns `nil` if no such root is defined.
|
418
|
+
#
|
419
|
+
# @return [String, nil]
|
420
|
+
def rails_root
|
421
|
+
if defined?(::Rails.root)
|
422
|
+
return ::Rails.root.to_s if ::Rails.root
|
423
|
+
raise "ERROR: Rails.root is nil!"
|
424
|
+
end
|
425
|
+
return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
|
426
|
+
return nil
|
427
|
+
end
|
428
|
+
|
429
|
+
# Returns the environment of the Rails application,
|
430
|
+
# if this is running in a Rails context.
|
431
|
+
# Returns `nil` if no such environment is defined.
|
432
|
+
#
|
433
|
+
# @return [String, nil]
|
434
|
+
def rails_env
|
435
|
+
return ::Rails.env.to_s if defined?(::Rails.env)
|
436
|
+
return RAILS_ENV.to_s if defined?(RAILS_ENV)
|
437
|
+
return nil
|
438
|
+
end
|
439
|
+
|
440
|
+
# Returns whether this environment is using ActionPack
|
441
|
+
# version 3.0.0 or greater.
|
442
|
+
#
|
443
|
+
# @return [Boolean]
|
444
|
+
def ap_geq_3?
|
445
|
+
ap_geq?("3.0.0.beta1")
|
446
|
+
end
|
447
|
+
|
448
|
+
# Returns whether this environment is using ActionPack
|
449
|
+
# of a version greater than or equal to that specified.
|
450
|
+
#
|
451
|
+
# @param version [String] The string version number to check against.
|
452
|
+
# Should be greater than or equal to Rails 3,
|
453
|
+
# because otherwise ActionPack::VERSION isn't autoloaded
|
454
|
+
# @return [Boolean]
|
455
|
+
def ap_geq?(version)
|
456
|
+
# The ActionPack module is always loaded automatically in Rails >= 3
|
457
|
+
return false unless defined?(ActionPack) && defined?(ActionPack::VERSION) &&
|
458
|
+
defined?(ActionPack::VERSION::STRING)
|
459
|
+
|
460
|
+
version_geq(ActionPack::VERSION::STRING, version)
|
461
|
+
end
|
462
|
+
|
463
|
+
# Returns an ActionView::Template* class.
|
464
|
+
# In pre-3.0 versions of Rails, most of these classes
|
465
|
+
# were of the form `ActionView::TemplateFoo`,
|
466
|
+
# while afterwards they were of the form `ActionView;:Template::Foo`.
|
467
|
+
#
|
468
|
+
# @param name [#to_s] The name of the class to get.
|
469
|
+
# For example, `:Error` will return `ActionView::TemplateError`
|
470
|
+
# or `ActionView::Template::Error`.
|
471
|
+
def av_template_class(name)
|
472
|
+
return ActionView.const_get("Template#{name}") if ActionView.const_defined?("Template#{name}")
|
473
|
+
return ActionView::Template.const_get(name.to_s)
|
474
|
+
end
|
475
|
+
|
476
|
+
## Cross-OS Compatibility
|
477
|
+
|
478
|
+
# Whether or not this is running on Windows.
|
479
|
+
#
|
480
|
+
# @return [Boolean]
|
481
|
+
def windows?
|
482
|
+
RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
|
483
|
+
end
|
484
|
+
|
485
|
+
# Whether or not this is running on IronRuby.
|
486
|
+
#
|
487
|
+
# @return [Boolean]
|
488
|
+
def ironruby?
|
489
|
+
RUBY_ENGINE == "ironruby"
|
490
|
+
end
|
491
|
+
|
492
|
+
# Whether or not this is running on Rubinius.
|
493
|
+
#
|
494
|
+
# @return [Boolean]
|
495
|
+
def rbx?
|
496
|
+
RUBY_ENGINE == "rbx"
|
497
|
+
end
|
498
|
+
|
499
|
+
# Whether or not this is running on JRuby.
|
500
|
+
#
|
501
|
+
# @return [Boolean]
|
502
|
+
def jruby?
|
503
|
+
RUBY_PLATFORM =~ /java/
|
504
|
+
end
|
505
|
+
|
506
|
+
# Returns an array of ints representing the JRuby version number.
|
507
|
+
#
|
508
|
+
# @return [Array<Fixnum>]
|
509
|
+
def jruby_version
|
510
|
+
$jruby_version ||= ::JRUBY_VERSION.split(".").map {|s| s.to_i}
|
511
|
+
end
|
512
|
+
|
513
|
+
# Like `Dir.glob`, but works with backslash-separated paths on Windows.
|
514
|
+
#
|
515
|
+
# @param path [String]
|
516
|
+
def glob(path, &block)
|
517
|
+
path = path.gsub('\\', '/') if windows?
|
518
|
+
Dir.glob(path, &block)
|
519
|
+
end
|
520
|
+
|
521
|
+
# Prepare a value for a destructuring assignment (e.g. `a, b =
|
522
|
+
# val`). This works around a performance bug when using
|
523
|
+
# ActiveSupport, and only needs to be called when `val` is likely
|
524
|
+
# to be `nil` reasonably often.
|
525
|
+
#
|
526
|
+
# See [this bug report](http://redmine.ruby-lang.org/issues/4917).
|
527
|
+
#
|
528
|
+
# @param val [Object]
|
529
|
+
# @return [Object]
|
530
|
+
def destructure(val)
|
531
|
+
val || []
|
532
|
+
end
|
533
|
+
|
534
|
+
## Cross-Ruby-Version Compatibility
|
535
|
+
|
536
|
+
# Whether or not this is running under a Ruby version under 2.0.
|
537
|
+
#
|
538
|
+
# @return [Boolean]
|
539
|
+
def ruby1?
|
540
|
+
Sass::Util::RUBY_VERSION[0] <= 1
|
541
|
+
end
|
542
|
+
|
543
|
+
# Whether or not this is running under Ruby 1.8 or lower.
|
544
|
+
#
|
545
|
+
# Note that IronRuby counts as Ruby 1.8,
|
546
|
+
# because it doesn't support the Ruby 1.9 encoding API.
|
547
|
+
#
|
548
|
+
# @return [Boolean]
|
549
|
+
def ruby1_8?
|
550
|
+
# IronRuby says its version is 1.9, but doesn't support any of the encoding APIs.
|
551
|
+
# We have to fall back to 1.8 behavior.
|
552
|
+
ironruby? || (Sass::Util::RUBY_VERSION[0] == 1 && Sass::Util::RUBY_VERSION[1] < 9)
|
553
|
+
end
|
554
|
+
|
555
|
+
# Whether or not this is running under Ruby 1.8.6 or lower.
|
556
|
+
# Note that lower versions are not officially supported.
|
557
|
+
#
|
558
|
+
# @return [Boolean]
|
559
|
+
def ruby1_8_6?
|
560
|
+
ruby1_8? && Sass::Util::RUBY_VERSION[2] < 7
|
561
|
+
end
|
562
|
+
|
563
|
+
# Wehter or not this is running under JRuby 1.6 or lower.
|
564
|
+
def jruby1_6?
|
565
|
+
jruby? && jruby_version[0] == 1 && jruby_version[1] < 7
|
566
|
+
end
|
567
|
+
|
568
|
+
# Whether or not this is running under MacRuby.
|
569
|
+
#
|
570
|
+
# @return [Boolean]
|
571
|
+
def macruby?
|
572
|
+
RUBY_ENGINE == 'macruby'
|
573
|
+
end
|
574
|
+
|
575
|
+
# Checks that the encoding of a string is valid in Ruby 1.9
|
576
|
+
# and cleans up potential encoding gotchas like the UTF-8 BOM.
|
577
|
+
# If it's not, yields an error string describing the invalid character
|
578
|
+
# and the line on which it occurrs.
|
579
|
+
#
|
580
|
+
# @param str [String] The string of which to check the encoding
|
581
|
+
# @yield [msg] A block in which an encoding error can be raised.
|
582
|
+
# Only yields if there is an encoding error
|
583
|
+
# @yieldparam msg [String] The error message to be raised
|
584
|
+
# @return [String] `str`, potentially with encoding gotchas like BOMs removed
|
585
|
+
def check_encoding(str)
|
586
|
+
if ruby1_8?
|
587
|
+
return str.gsub(/\A\xEF\xBB\xBF/, '') # Get rid of the UTF-8 BOM
|
588
|
+
elsif str.valid_encoding?
|
589
|
+
# Get rid of the Unicode BOM if possible
|
590
|
+
if str.encoding.name =~ /^UTF-(8|16|32)(BE|LE)?$/
|
591
|
+
return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding.name)), '')
|
592
|
+
else
|
593
|
+
return str
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
encoding = str.encoding
|
598
|
+
newlines = Regexp.new("\r\n|\r|\n".encode(encoding).force_encoding("binary"))
|
599
|
+
str.force_encoding("binary").split(newlines).each_with_index do |line, i|
|
600
|
+
begin
|
601
|
+
line.encode(encoding)
|
602
|
+
rescue Encoding::UndefinedConversionError => e
|
603
|
+
yield <<MSG.rstrip, i + 1
|
604
|
+
Invalid #{encoding.name} character #{undefined_conversion_error_char(e)}
|
605
|
+
MSG
|
606
|
+
end
|
607
|
+
end
|
608
|
+
return str
|
609
|
+
end
|
610
|
+
|
611
|
+
# Like {\#check\_encoding}, but also checks for a `@charset` declaration
|
612
|
+
# at the beginning of the file and uses that encoding if it exists.
|
613
|
+
#
|
614
|
+
# The Sass encoding rules are simple.
|
615
|
+
# If a `@charset` declaration exists,
|
616
|
+
# we assume that that's the original encoding of the document.
|
617
|
+
# Otherwise, we use whatever encoding Ruby has.
|
618
|
+
# Then we convert that to UTF-8 to process internally.
|
619
|
+
# The UTF-8 end result is what's returned by this method.
|
620
|
+
#
|
621
|
+
# @param str [String] The string of which to check the encoding
|
622
|
+
# @yield [msg] A block in which an encoding error can be raised.
|
623
|
+
# Only yields if there is an encoding error
|
624
|
+
# @yieldparam msg [String] The error message to be raised
|
625
|
+
# @return [(String, Encoding)] The original string encoded as UTF-8,
|
626
|
+
# and the source encoding of the string (or `nil` under Ruby 1.8)
|
627
|
+
# @raise [Encoding::UndefinedConversionError] if the source encoding
|
628
|
+
# cannot be converted to UTF-8
|
629
|
+
# @raise [ArgumentError] if the document uses an unknown encoding with `@charset`
|
630
|
+
def check_sass_encoding(str, &block)
|
631
|
+
return check_encoding(str, &block), nil if ruby1_8?
|
632
|
+
# We allow any printable ASCII characters but double quotes in the charset decl
|
633
|
+
bin = str.dup.force_encoding("BINARY")
|
634
|
+
encoding = Sass::Util::ENCODINGS_TO_CHECK.find do |enc|
|
635
|
+
re = Sass::Util::CHARSET_REGEXPS[enc]
|
636
|
+
re && bin =~ re
|
637
|
+
end
|
638
|
+
charset, bom = $1, $2
|
639
|
+
if charset
|
640
|
+
charset = charset.force_encoding(encoding).encode("UTF-8")
|
641
|
+
if endianness = encoding[/[BL]E$/]
|
642
|
+
begin
|
643
|
+
Encoding.find(charset + endianness)
|
644
|
+
charset << endianness
|
645
|
+
rescue ArgumentError # Encoding charset + endianness doesn't exist
|
646
|
+
end
|
647
|
+
end
|
648
|
+
str.force_encoding(charset)
|
649
|
+
elsif bom
|
650
|
+
str.force_encoding(encoding)
|
651
|
+
end
|
652
|
+
|
653
|
+
str = check_encoding(str, &block)
|
654
|
+
return str.encode("UTF-8"), str.encoding
|
655
|
+
end
|
656
|
+
|
657
|
+
unless ruby1_8?
|
658
|
+
# @private
|
659
|
+
def _enc(string, encoding)
|
660
|
+
string.encode(encoding).force_encoding("BINARY")
|
661
|
+
end
|
662
|
+
|
663
|
+
# We could automatically add in any non-ASCII-compatible encodings here,
|
664
|
+
# but there's not really a good way to do that
|
665
|
+
# without manually checking that each encoding
|
666
|
+
# encodes all ASCII characters properly,
|
667
|
+
# which takes long enough to affect the startup time of the CLI.
|
668
|
+
ENCODINGS_TO_CHECK = %w[UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE]
|
669
|
+
|
670
|
+
CHARSET_REGEXPS = Hash.new do |h, e|
|
671
|
+
h[e] =
|
672
|
+
begin
|
673
|
+
# /\A(?:\uFEFF)?@charset "(.*?)"|\A(\uFEFF)/
|
674
|
+
Regexp.new(/\A(?:#{_enc("\uFEFF", e)})?#{
|
675
|
+
_enc('@charset "', e)}(.*?)#{_enc('"', e)}|\A(#{
|
676
|
+
_enc("\uFEFF", e)})/)
|
677
|
+
rescue Encoding::ConverterNotFoundError => _
|
678
|
+
nil # JRuby on Java 5 doesn't support UTF-32
|
679
|
+
rescue
|
680
|
+
# /\A@charset "(.*?)"/
|
681
|
+
Regexp.new(/\A#{_enc('@charset "', e)}(.*?)#{_enc('"', e)}/)
|
682
|
+
end
|
683
|
+
end
|
684
|
+
end
|
685
|
+
|
686
|
+
# Checks to see if a class has a given method.
|
687
|
+
# For example:
|
688
|
+
#
|
689
|
+
# Sass::Util.has?(:public_instance_method, String, :gsub) #=> true
|
690
|
+
#
|
691
|
+
# Method collections like `Class#instance_methods`
|
692
|
+
# return strings in Ruby 1.8 and symbols in Ruby 1.9 and on,
|
693
|
+
# so this handles checking for them in a compatible way.
|
694
|
+
#
|
695
|
+
# @param attr [#to_s] The (singular) name of the method-collection method
|
696
|
+
# (e.g. `:instance_methods`, `:private_methods`)
|
697
|
+
# @param klass [Module] The class to check the methods of which to check
|
698
|
+
# @param method [String, Symbol] The name of the method do check for
|
699
|
+
# @return [Boolean] Whether or not the given collection has the given method
|
700
|
+
def has?(attr, klass, method)
|
701
|
+
klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
|
702
|
+
end
|
703
|
+
|
704
|
+
# A version of `Enumerable#enum_with_index` that works in Ruby 1.8 and 1.9.
|
705
|
+
#
|
706
|
+
# @param enum [Enumerable] The enumerable to get the enumerator for
|
707
|
+
# @return [Enumerator] The with-index enumerator
|
708
|
+
def enum_with_index(enum)
|
709
|
+
ruby1_8? ? enum.enum_with_index : enum.each_with_index
|
710
|
+
end
|
711
|
+
|
712
|
+
# A version of `Enumerable#enum_cons` that works in Ruby 1.8 and 1.9.
|
713
|
+
#
|
714
|
+
# @param enum [Enumerable] The enumerable to get the enumerator for
|
715
|
+
# @param n [Fixnum] The size of each cons
|
716
|
+
# @return [Enumerator] The consed enumerator
|
717
|
+
def enum_cons(enum, n)
|
718
|
+
ruby1_8? ? enum.enum_cons(n) : enum.each_cons(n)
|
719
|
+
end
|
720
|
+
|
721
|
+
# A version of `Enumerable#enum_slice` that works in Ruby 1.8 and 1.9.
|
722
|
+
#
|
723
|
+
# @param enum [Enumerable] The enumerable to get the enumerator for
|
724
|
+
# @param n [Fixnum] The size of each slice
|
725
|
+
# @return [Enumerator] The consed enumerator
|
726
|
+
def enum_slice(enum, n)
|
727
|
+
ruby1_8? ? enum.enum_slice(n) : enum.each_slice(n)
|
728
|
+
end
|
729
|
+
|
730
|
+
# Destructively removes all elements from an array that match a block, and
|
731
|
+
# returns the removed elements.
|
732
|
+
#
|
733
|
+
# @param array [Array] The array from which to remove elements.
|
734
|
+
# @yield [el] Called for each element.
|
735
|
+
# @yieldparam el [*] The element to test.
|
736
|
+
# @yieldreturn [Boolean] Whether or not to extract the element.
|
737
|
+
# @return [Array] The extracted elements.
|
738
|
+
def extract!(array)
|
739
|
+
out = []
|
740
|
+
array.reject! do |e|
|
741
|
+
next false unless yield e
|
742
|
+
out << e
|
743
|
+
true
|
744
|
+
end
|
745
|
+
out
|
746
|
+
end
|
747
|
+
|
748
|
+
# Returns the ASCII code of the given character.
|
749
|
+
#
|
750
|
+
# @param c [String] All characters but the first are ignored.
|
751
|
+
# @return [Fixnum] The ASCII code of `c`.
|
752
|
+
def ord(c)
|
753
|
+
ruby1_8? ? c[0] : c.ord
|
754
|
+
end
|
755
|
+
|
756
|
+
# Flattens the first `n` nested arrays in a cross-version manner.
|
757
|
+
#
|
758
|
+
# @param arr [Array] The array to flatten
|
759
|
+
# @param n [Fixnum] The number of levels to flatten
|
760
|
+
# @return [Array] The flattened array
|
761
|
+
def flatten(arr, n)
|
762
|
+
return arr.flatten(n) unless ruby1_8_6?
|
763
|
+
return arr if n == 0
|
764
|
+
arr.inject([]) {|res, e| e.is_a?(Array) ? res.concat(flatten(e, n - 1)) : res << e}
|
765
|
+
end
|
766
|
+
|
767
|
+
# Returns the hash code for a set in a cross-version manner.
|
768
|
+
# Aggravatingly, this is order-dependent in Ruby 1.8.6.
|
769
|
+
#
|
770
|
+
# @param set [Set]
|
771
|
+
# @return [Fixnum] The order-independent hashcode of `set`
|
772
|
+
def set_hash(set)
|
773
|
+
return set.hash unless ruby1_8_6?
|
774
|
+
set.map {|e| e.hash}.uniq.sort.hash
|
775
|
+
end
|
776
|
+
|
777
|
+
# Tests the hash-equality of two sets in a cross-version manner.
|
778
|
+
# Aggravatingly, this is order-dependent in Ruby 1.8.6.
|
779
|
+
#
|
780
|
+
# @param set1 [Set]
|
781
|
+
# @param set2 [Set]
|
782
|
+
# @return [Boolean] Whether or not the sets are hashcode equal
|
783
|
+
def set_eql?(set1, set2)
|
784
|
+
return set1.eql?(set2) unless ruby1_8_6?
|
785
|
+
set1.to_a.uniq.sort_by {|e| e.hash}.eql?(set2.to_a.uniq.sort_by {|e| e.hash})
|
786
|
+
end
|
787
|
+
|
788
|
+
# Like `Object#inspect`, but preserves non-ASCII characters rather than escaping them under Ruby 1.9.2.
|
789
|
+
# This is necessary so that the precompiled Haml template can be `#encode`d into `@options[:encoding]`
|
790
|
+
# before being evaluated.
|
791
|
+
#
|
792
|
+
# @param obj {Object}
|
793
|
+
# @return {String}
|
794
|
+
def inspect_obj(obj)
|
795
|
+
return obj.inspect unless version_geq(::RUBY_VERSION, "1.9.2")
|
796
|
+
return ':' + inspect_obj(obj.to_s) if obj.is_a?(Symbol)
|
797
|
+
return obj.inspect unless obj.is_a?(String)
|
798
|
+
'"' + obj.gsub(/[\x00-\x7F]+/) {|s| s.inspect[1...-1]} + '"'
|
799
|
+
end
|
800
|
+
|
801
|
+
# Extracts the non-string vlaues from an array containing both strings and non-strings.
|
802
|
+
# These values are replaced with escape sequences.
|
803
|
+
# This can be undone using \{#inject\_values}.
|
804
|
+
#
|
805
|
+
# This is useful e.g. when we want to do string manipulation
|
806
|
+
# on an interpolated string.
|
807
|
+
#
|
808
|
+
# The precise format of the resulting string is not guaranteed.
|
809
|
+
# However, it is guaranteed that newlines and whitespace won't be affected.
|
810
|
+
#
|
811
|
+
# @param arr [Array] The array from which values are extracted.
|
812
|
+
# @return [(String, Array)] The resulting string, and an array of extracted values.
|
813
|
+
def extract_values(arr)
|
814
|
+
values = []
|
815
|
+
return arr.map do |e|
|
816
|
+
next e.gsub('{', '{{') if e.is_a?(String)
|
817
|
+
values << e
|
818
|
+
next "{#{values.count - 1}}"
|
819
|
+
end.join, values
|
820
|
+
end
|
821
|
+
|
822
|
+
# Undoes \{#extract\_values} by transforming a string with escape sequences
|
823
|
+
# into an array of strings and non-string values.
|
824
|
+
#
|
825
|
+
# @param str [String] The string with escape sequences.
|
826
|
+
# @param values [Array] The array of values to inject.
|
827
|
+
# @return [Array] The array of strings and values.
|
828
|
+
def inject_values(str, values)
|
829
|
+
return [str.gsub('{{', '{')] if values.empty?
|
830
|
+
# Add an extra { so that we process the tail end of the string
|
831
|
+
result = (str + '{{').scan(/(.*?)(?:(\{\{)|\{(\d+)\})/m).map do |(pre, esc, n)|
|
832
|
+
[pre, esc ? '{' : '', n ? values[n.to_i] : '']
|
833
|
+
end.flatten(1)
|
834
|
+
result[-2] = '' # Get rid of the extra {
|
835
|
+
merge_adjacent_strings(result).reject {|s| s == ''}
|
836
|
+
end
|
837
|
+
|
838
|
+
# Allows modifications to be performed on the string form
|
839
|
+
# of an array containing both strings and non-strings.
|
840
|
+
#
|
841
|
+
# @param arr [Array] The array from which values are extracted.
|
842
|
+
# @yield [str] A block in which string manipulation can be done to the array.
|
843
|
+
# @yieldparam str [String] The string form of `arr`.
|
844
|
+
# @yieldreturn [String] The modified string.
|
845
|
+
# @return [Array] The modified, interpolated array.
|
846
|
+
def with_extracted_values(arr)
|
847
|
+
str, vals = extract_values(arr)
|
848
|
+
str = yield str
|
849
|
+
inject_values(str, vals)
|
850
|
+
end
|
851
|
+
|
852
|
+
## Static Method Stuff
|
853
|
+
|
854
|
+
# The context in which the ERB for \{#def\_static\_method} will be run.
|
855
|
+
class StaticConditionalContext
|
856
|
+
# @param set [#include?] The set of variables that are defined for this context.
|
857
|
+
def initialize(set)
|
858
|
+
@set = set
|
859
|
+
end
|
860
|
+
|
861
|
+
# Checks whether or not a variable is defined for this context.
|
862
|
+
#
|
863
|
+
# @param name [Symbol] The name of the variable
|
864
|
+
# @return [Boolean]
|
865
|
+
def method_missing(name, *args, &block)
|
866
|
+
super unless args.empty? && block.nil?
|
867
|
+
@set.include?(name)
|
868
|
+
end
|
869
|
+
end
|
870
|
+
|
871
|
+
private
|
872
|
+
|
873
|
+
# Calculates the memoization table for the Least Common Subsequence algorithm.
|
874
|
+
# Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS)
|
875
|
+
def lcs_table(x, y)
|
876
|
+
c = Array.new(x.size) {[]}
|
877
|
+
x.size.times {|i| c[i][0] = 0}
|
878
|
+
y.size.times {|j| c[0][j] = 0}
|
879
|
+
(1...x.size).each do |i|
|
880
|
+
(1...y.size).each do |j|
|
881
|
+
c[i][j] =
|
882
|
+
if yield x[i], y[j]
|
883
|
+
c[i-1][j-1] + 1
|
884
|
+
else
|
885
|
+
[c[i][j-1], c[i-1][j]].max
|
886
|
+
end
|
887
|
+
end
|
888
|
+
end
|
889
|
+
return c
|
890
|
+
end
|
891
|
+
|
892
|
+
# Computes a single longest common subsequence for arrays x and y.
|
893
|
+
# Algorithm from [Wikipedia](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Reading_out_an_LCS)
|
894
|
+
def lcs_backtrace(c, x, y, i, j, &block)
|
895
|
+
return [] if i == 0 || j == 0
|
896
|
+
if v = yield(x[i], y[j])
|
897
|
+
return lcs_backtrace(c, x, y, i-1, j-1, &block) << v
|
898
|
+
end
|
899
|
+
|
900
|
+
return lcs_backtrace(c, x, y, i, j-1, &block) if c[i][j-1] > c[i-1][j]
|
901
|
+
return lcs_backtrace(c, x, y, i-1, j, &block)
|
902
|
+
end
|
903
|
+
end
|
904
|
+
end
|
905
|
+
|
906
|
+
require 'sass/util/multibyte_string_scanner'
|