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,18 @@
1
+ module Sass
2
+ module Tree
3
+ # A dynamic node representing returning from a function.
4
+ #
5
+ # @see Sass::Tree
6
+ class ReturnNode < Node
7
+ # The expression to return.
8
+ # @type [Script::Node]
9
+ attr_accessor :expr
10
+
11
+ # @param expr [Script::Node] The expression to return
12
+ def initialize(expr)
13
+ @expr = expr
14
+ super()
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module Sass
2
+ module Tree
3
+ # A static node that is the root node of the Sass document.
4
+ class RootNode < Node
5
+ # The Sass template from which this node was created
6
+ #
7
+ # @param template [String]
8
+ attr_reader :template
9
+
10
+ # @param template [String] The Sass template from which this node was created
11
+ def initialize(template)
12
+ super()
13
+ @template = template
14
+ end
15
+
16
+ # Runs the dynamic Sass code *and* computes the CSS for the tree.
17
+ # @see #to_s
18
+ def render
19
+ Visitors::CheckNesting.visit(self)
20
+ result = Visitors::Perform.visit(self)
21
+ Visitors::CheckNesting.visit(result) # Check again to validate mixins
22
+ result, extends = Visitors::Cssize.visit(result)
23
+ Visitors::Extend.visit(result, extends)
24
+ result.to_s
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,132 @@
1
+ require 'pathname'
2
+ require 'uri'
3
+
4
+ module Sass::Tree
5
+ # A static node reprenting a CSS rule.
6
+ #
7
+ # @see Sass::Tree
8
+ class RuleNode < Node
9
+ # The character used to include the parent selector
10
+ PARENT = '&'
11
+
12
+ # The CSS selector for this rule,
13
+ # interspersed with {Sass::Script::Node}s
14
+ # representing `#{}`-interpolation.
15
+ # Any adjacent strings will be merged together.
16
+ #
17
+ # @return [Array<String, Sass::Script::Node>]
18
+ attr_accessor :rule
19
+
20
+ # The CSS selector for this rule,
21
+ # without any unresolved interpolation
22
+ # but with parent references still intact.
23
+ # It's only set once {Tree::Node#perform} has been called.
24
+ #
25
+ # @return [Selector::CommaSequence]
26
+ attr_accessor :parsed_rules
27
+
28
+ # The CSS selector for this rule,
29
+ # without any unresolved interpolation or parent references.
30
+ # It's only set once {Tree::Visitors::Cssize} has been run.
31
+ #
32
+ # @return [Selector::CommaSequence]
33
+ attr_accessor :resolved_rules
34
+
35
+ # How deep this rule is indented
36
+ # relative to a base-level rule.
37
+ # This is only greater than 0 in the case that:
38
+ #
39
+ # * This node is in a CSS tree
40
+ # * The style is :nested
41
+ # * This is a child rule of another rule
42
+ # * The parent rule has properties, and thus will be rendered
43
+ #
44
+ # @return [Fixnum]
45
+ attr_accessor :tabs
46
+
47
+ # Whether or not this rule is the last rule in a nested group.
48
+ # This is only set in a CSS tree.
49
+ #
50
+ # @return [Boolean]
51
+ attr_accessor :group_end
52
+
53
+ # The stack trace.
54
+ # This is only readable in a CSS tree as it is written during the perform step
55
+ # and only when the :trace_selectors option is set.
56
+ #
57
+ # @return [Array<String>]
58
+ attr_accessor :stack_trace
59
+
60
+ # @param rule [Array<String, Sass::Script::Node>]
61
+ # The CSS rule. See \{#rule}
62
+ def initialize(rule)
63
+ merged = Sass::Util.merge_adjacent_strings(rule)
64
+ @rule = Sass::Util.strip_string_array(merged)
65
+ @tabs = 0
66
+ try_to_parse_non_interpolated_rules
67
+ super()
68
+ end
69
+
70
+ # If we've precached the parsed selector, set the line on it, too.
71
+ def line=(line)
72
+ @parsed_rules.line = line if @parsed_rules
73
+ super
74
+ end
75
+
76
+ # If we've precached the parsed selector, set the filename on it, too.
77
+ def filename=(filename)
78
+ @parsed_rules.filename = filename if @parsed_rules
79
+ super
80
+ end
81
+
82
+ # Compares the contents of two rules.
83
+ #
84
+ # @param other [Object] The object to compare with
85
+ # @return [Boolean] Whether or not this node and the other object
86
+ # are the same
87
+ def ==(other)
88
+ self.class == other.class && rule == other.rule && super
89
+ end
90
+
91
+ # Adds another {RuleNode}'s rules to this one's.
92
+ #
93
+ # @param node [RuleNode] The other node
94
+ def add_rules(node)
95
+ @rule = Sass::Util.strip_string_array(
96
+ Sass::Util.merge_adjacent_strings(@rule + ["\n"] + node.rule))
97
+ try_to_parse_non_interpolated_rules
98
+ end
99
+
100
+ # @return [Boolean] Whether or not this rule is continued on the next line
101
+ def continued?
102
+ last = @rule.last
103
+ last.is_a?(String) && last[-1] == ?,
104
+ end
105
+
106
+ # A hash that will be associated with this rule in the CSS document
107
+ # if the {file:SASS_REFERENCE.md#debug_info-option `:debug_info` option} is enabled.
108
+ # This data is used by e.g. [the FireSass Firebug extension](https://addons.mozilla.org/en-US/firefox/addon/103988).
109
+ #
110
+ # @return [{#to_s => #to_s}]
111
+ def debug_info
112
+ {:filename => filename && ("file://" + URI.escape(File.expand_path(filename))),
113
+ :line => self.line}
114
+ end
115
+
116
+ # A rule node is invisible if it has only placeholder selectors.
117
+ def invisible?
118
+ resolved_rules.members.all? {|seq| seq.has_placeholder?}
119
+ end
120
+
121
+ private
122
+
123
+ def try_to_parse_non_interpolated_rules
124
+ if @rule.all? {|t| t.kind_of?(String)}
125
+ # We don't use real filename/line info because we don't have it yet.
126
+ # When we get it, we'll set it on the parsed rules if possible.
127
+ parser = Sass::SCSS::StaticParser.new(@rule.join.strip, '', 1)
128
+ @parsed_rules = parser.parse_selector rescue nil
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,51 @@
1
+ module Sass::Tree
2
+ # A static node representing a `@supports` rule.
3
+ # `@supports` rules behave differently from other directives
4
+ # in that when they're nested within rules,
5
+ # they bubble up to top-level.
6
+ #
7
+ # @see Sass::Tree
8
+ class SupportsNode < DirectiveNode
9
+ # The name, which may include a browser prefix.
10
+ #
11
+ # @return [String]
12
+ attr_accessor :name
13
+
14
+ # The supports condition.
15
+ #
16
+ # @return [Sass::Supports::Condition]
17
+ attr_accessor :condition
18
+
19
+ # @see RuleNode#tabs
20
+ attr_accessor :tabs
21
+
22
+ # @see RuleNode#group_end
23
+ attr_accessor :group_end
24
+
25
+ # @param condition [Sass::Supports::Condition] See \{#condition}
26
+ def initialize(name, condition)
27
+ @name = name
28
+ @condition = condition
29
+ @tabs = 0
30
+ super('')
31
+ end
32
+
33
+ # @see DirectiveNode#value
34
+ def value; raise NotImplementedError; end
35
+
36
+ # @see DirectiveNode#resolved_value
37
+ def resolved_value
38
+ @resolved_value ||= "@#{name} #{condition.to_css}"
39
+ end
40
+
41
+ # True when the directive has no visible children.
42
+ #
43
+ # @return [Boolean]
44
+ def invisible?
45
+ children.all? {|c| c.invisible?}
46
+ end
47
+
48
+ # @see Node#bubbles?
49
+ def bubbles?; true; end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ require 'sass/tree/node'
2
+
3
+ module Sass::Tree
4
+ # A solely static node left over after a mixin include or @content has been performed.
5
+ # Its sole purpose is to wrap exceptions to add to the backtrace.
6
+ #
7
+ # @see Sass::Tree
8
+ class TraceNode < Node
9
+ # The name of the trace entry to add.
10
+ # @return [String]
11
+ attr_reader :name
12
+
13
+ # @param name [String] The name of the trace entry to add.
14
+ def initialize(name)
15
+ @name = name
16
+ self.has_children = true
17
+ super()
18
+ end
19
+
20
+ # Initializes this node from an existing node.
21
+ # @param name [String] The name of the trace entry to add.
22
+ # @param mixin [Node] The node to copy information from.
23
+ # @return [TraceNode]
24
+ def self.from_node(name, node)
25
+ trace = new(name)
26
+ trace.line = node.line
27
+ trace.filename = node.filename
28
+ trace.options = node.options
29
+ trace
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ module Sass
2
+ module Tree
3
+ # A dynamic node representing a variable definition.
4
+ #
5
+ # @see Sass::Tree
6
+ class VariableNode < Node
7
+ # The name of the variable.
8
+ # @return [String]
9
+ attr_reader :name
10
+
11
+ # The parse tree for the variable value.
12
+ # @return [Script::Node]
13
+ attr_accessor :expr
14
+
15
+ # Whether this is a guarded variable assignment (`!default`).
16
+ # @return [Boolean]
17
+ attr_reader :guarded
18
+
19
+ # @param name [String] The name of the variable
20
+ # @param expr [Script::Node] See \{#expr}
21
+ # @param guarded [Boolean] See \{#guarded}
22
+ def initialize(name, expr, guarded)
23
+ @name = name
24
+ @expr = expr
25
+ @guarded = guarded
26
+ super()
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,75 @@
1
+ # Visitors are used to traverse the Sass parse tree.
2
+ # Visitors should extend {Visitors::Base},
3
+ # which provides a small amount of scaffolding for traversal.
4
+ module Sass::Tree::Visitors
5
+ # The abstract base class for Sass visitors.
6
+ # Visitors should extend this class,
7
+ # then implement `visit_*` methods for each node they care about
8
+ # (e.g. `visit_rule` for {RuleNode} or `visit_for` for {ForNode}).
9
+ # These methods take the node in question as argument.
10
+ # They may `yield` to visit the child nodes of the current node.
11
+ #
12
+ # *Note*: due to the unusual nature of {Sass::Tree::IfNode},
13
+ # special care must be taken to ensure that it is properly handled.
14
+ # In particular, there is no built-in scaffolding
15
+ # for dealing with the return value of `@else` nodes.
16
+ #
17
+ # @abstract
18
+ class Base
19
+ # Runs the visitor on a tree.
20
+ #
21
+ # @param root [Tree::Node] The root node of the Sass tree.
22
+ # @return [Object] The return value of \{#visit} for the root node.
23
+ def self.visit(root)
24
+ new.send(:visit, root)
25
+ end
26
+
27
+ protected
28
+
29
+ # Runs the visitor on the given node.
30
+ # This can be overridden by subclasses that need to do something for each node.
31
+ #
32
+ # @param node [Tree::Node] The node to visit.
33
+ # @return [Object] The return value of the `visit_*` method for this node.
34
+ def visit(node)
35
+ method = "visit_#{node_name node}"
36
+ if self.respond_to?(method, true)
37
+ self.send(method, node) {visit_children(node)}
38
+ else
39
+ visit_children(node)
40
+ end
41
+ end
42
+
43
+ # Visit the child nodes for a given node.
44
+ # This can be overridden by subclasses that need to do something
45
+ # with the child nodes' return values.
46
+ #
47
+ # This method is run when `visit_*` methods `yield`,
48
+ # and its return value is returned from the `yield`.
49
+ #
50
+ # @param parent [Tree::Node] The parent node of the children to visit.
51
+ # @return [Array<Object>] The return values of the `visit_*` methods for the children.
52
+ def visit_children(parent)
53
+ parent.children.map {|c| visit(c)}
54
+ end
55
+
56
+ NODE_NAME_RE = /.*::(.*?)Node$/
57
+
58
+ # Returns the name of a node as used in the `visit_*` method.
59
+ #
60
+ # @param [Tree::Node] node The node.
61
+ # @return [String] The name.
62
+ def node_name(node)
63
+ @@node_names ||= {}
64
+ @@node_names[node.class.name] ||= node.class.name.gsub(NODE_NAME_RE, '\\1').downcase
65
+ end
66
+
67
+ # `yield`s, then runs the visitor on the `@else` clause if the node has one.
68
+ # This exists to ensure that the contents of the `@else` clause get visited.
69
+ def visit_if(node)
70
+ yield
71
+ visit(node.else) if node.else
72
+ node
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,147 @@
1
+ # A visitor for checking that all nodes are properly nested.
2
+ class Sass::Tree::Visitors::CheckNesting < Sass::Tree::Visitors::Base
3
+ protected
4
+
5
+ def initialize
6
+ @parents = []
7
+ end
8
+
9
+ def visit(node)
10
+ if error = @parent && (
11
+ try_send("invalid_#{node_name @parent}_child?", @parent, node) ||
12
+ try_send("invalid_#{node_name node}_parent?", @parent, node))
13
+ raise Sass::SyntaxError.new(error)
14
+ end
15
+ super
16
+ rescue Sass::SyntaxError => e
17
+ e.modify_backtrace(:filename => node.filename, :line => node.line)
18
+ raise e
19
+ end
20
+
21
+ CONTROL_NODES = [Sass::Tree::EachNode, Sass::Tree::ForNode, Sass::Tree::IfNode,
22
+ Sass::Tree::WhileNode, Sass::Tree::TraceNode]
23
+ SCRIPT_NODES = [Sass::Tree::ImportNode] + CONTROL_NODES
24
+ def visit_children(parent)
25
+ old_parent = @parent
26
+ @parent = parent unless is_any_of?(parent, SCRIPT_NODES) ||
27
+ (parent.bubbles? && !old_parent.is_a?(Sass::Tree::RootNode))
28
+ @parents.push parent
29
+ super
30
+ ensure
31
+ @parent = old_parent
32
+ @parents.pop
33
+ end
34
+
35
+ def visit_root(node)
36
+ yield
37
+ rescue Sass::SyntaxError => e
38
+ e.sass_template ||= node.template
39
+ raise e
40
+ end
41
+
42
+ def visit_import(node)
43
+ yield
44
+ rescue Sass::SyntaxError => e
45
+ e.modify_backtrace(:filename => node.children.first.filename)
46
+ e.add_backtrace(:filename => node.filename, :line => node.line)
47
+ raise e
48
+ end
49
+
50
+ def visit_mixindef(node)
51
+ @current_mixin_def, old_mixin_def = node, @current_mixin_def
52
+ yield
53
+ ensure
54
+ @current_mixin_def = old_mixin_def
55
+ end
56
+
57
+ def invalid_content_parent?(parent, child)
58
+ if @current_mixin_def
59
+ @current_mixin_def.has_content = true
60
+ nil
61
+ else
62
+ "@content may only be used within a mixin."
63
+ end
64
+ end
65
+
66
+ def invalid_charset_parent?(parent, child)
67
+ "@charset may only be used at the root of a document." unless parent.is_a?(Sass::Tree::RootNode)
68
+ end
69
+
70
+ VALID_EXTEND_PARENTS = [Sass::Tree::RuleNode, Sass::Tree::MixinDefNode, Sass::Tree::MixinNode]
71
+ def invalid_extend_parent?(parent, child)
72
+ unless is_any_of?(parent, VALID_EXTEND_PARENTS)
73
+ return "Extend directives may only be used within rules."
74
+ end
75
+ end
76
+
77
+ INVALID_IMPORT_PARENTS = CONTROL_NODES +
78
+ [Sass::Tree::MixinDefNode, Sass::Tree::MixinNode]
79
+ def invalid_import_parent?(parent, child)
80
+ unless (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
81
+ return "Import directives may not be used within control directives or mixins."
82
+ end
83
+ return if parent.is_a?(Sass::Tree::RootNode)
84
+ return "CSS import directives may only be used at the root of a document." if child.css_import?
85
+ rescue Sass::SyntaxError => e
86
+ e.modify_backtrace(:filename => child.imported_file.options[:filename])
87
+ e.add_backtrace(:filename => child.filename, :line => child.line)
88
+ raise e
89
+ end
90
+
91
+ def invalid_mixindef_parent?(parent, child)
92
+ unless (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
93
+ return "Mixins may not be defined within control directives or other mixins."
94
+ end
95
+ end
96
+
97
+ def invalid_function_parent?(parent, child)
98
+ unless (@parents.map {|p| p.class} & INVALID_IMPORT_PARENTS).empty?
99
+ return "Functions may not be defined within control directives or other mixins."
100
+ end
101
+ end
102
+
103
+ VALID_FUNCTION_CHILDREN = [
104
+ Sass::Tree::CommentNode, Sass::Tree::DebugNode, Sass::Tree::ReturnNode,
105
+ Sass::Tree::VariableNode, Sass::Tree::WarnNode
106
+ ] + CONTROL_NODES
107
+ def invalid_function_child?(parent, child)
108
+ unless is_any_of?(child, VALID_FUNCTION_CHILDREN)
109
+ "Functions can only contain variable declarations and control directives."
110
+ end
111
+ end
112
+
113
+ VALID_PROP_CHILDREN = [Sass::Tree::CommentNode, Sass::Tree::PropNode, Sass::Tree::MixinNode] + CONTROL_NODES
114
+ def invalid_prop_child?(parent, child)
115
+ unless is_any_of?(child, VALID_PROP_CHILDREN)
116
+ "Illegal nesting: Only properties may be nested beneath properties."
117
+ end
118
+ end
119
+
120
+ VALID_PROP_PARENTS = [Sass::Tree::RuleNode, Sass::Tree::PropNode,
121
+ Sass::Tree::MixinDefNode, Sass::Tree::DirectiveNode,
122
+ Sass::Tree::MixinNode]
123
+ def invalid_prop_parent?(parent, child)
124
+ unless is_any_of?(parent, VALID_PROP_PARENTS)
125
+ "Properties are only allowed within rules, directives, mixin includes, or other properties." + child.pseudo_class_selector_message
126
+ end
127
+ end
128
+
129
+ def invalid_return_parent?(parent, child)
130
+ "@return may only be used within a function." unless parent.is_a?(Sass::Tree::FunctionNode)
131
+ end
132
+
133
+ private
134
+
135
+ def is_any_of?(val, classes)
136
+ for c in classes
137
+ return true if val.is_a?(c)
138
+ end
139
+ return false
140
+ end
141
+
142
+ def try_send(method, *args)
143
+ return unless respond_to?(method, true)
144
+ send(method, *args)
145
+ end
146
+ end
147
+