oreorenasass 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +11 -0
  3. data/CONTRIBUTING +3 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +221 -0
  6. data/Rakefile +370 -0
  7. data/VERSION +1 -0
  8. data/VERSION_NAME +1 -0
  9. data/bin/sass +13 -0
  10. data/bin/sass-convert +12 -0
  11. data/bin/scss +13 -0
  12. data/extra/update_watch.rb +13 -0
  13. data/init.rb +18 -0
  14. data/lib/sass/cache_stores/base.rb +88 -0
  15. data/lib/sass/cache_stores/chain.rb +34 -0
  16. data/lib/sass/cache_stores/filesystem.rb +60 -0
  17. data/lib/sass/cache_stores/memory.rb +47 -0
  18. data/lib/sass/cache_stores/null.rb +25 -0
  19. data/lib/sass/cache_stores.rb +15 -0
  20. data/lib/sass/callbacks.rb +67 -0
  21. data/lib/sass/css.rb +407 -0
  22. data/lib/sass/engine.rb +1181 -0
  23. data/lib/sass/environment.rb +191 -0
  24. data/lib/sass/error.rb +198 -0
  25. data/lib/sass/exec/base.rb +187 -0
  26. data/lib/sass/exec/sass_convert.rb +264 -0
  27. data/lib/sass/exec/sass_scss.rb +424 -0
  28. data/lib/sass/exec.rb +9 -0
  29. data/lib/sass/features.rb +47 -0
  30. data/lib/sass/importers/base.rb +182 -0
  31. data/lib/sass/importers/filesystem.rb +211 -0
  32. data/lib/sass/importers.rb +22 -0
  33. data/lib/sass/logger/base.rb +30 -0
  34. data/lib/sass/logger/log_level.rb +45 -0
  35. data/lib/sass/logger.rb +12 -0
  36. data/lib/sass/media.rb +210 -0
  37. data/lib/sass/plugin/compiler.rb +565 -0
  38. data/lib/sass/plugin/configuration.rb +118 -0
  39. data/lib/sass/plugin/generic.rb +15 -0
  40. data/lib/sass/plugin/merb.rb +48 -0
  41. data/lib/sass/plugin/rack.rb +60 -0
  42. data/lib/sass/plugin/rails.rb +47 -0
  43. data/lib/sass/plugin/staleness_checker.rb +199 -0
  44. data/lib/sass/plugin.rb +133 -0
  45. data/lib/sass/railtie.rb +10 -0
  46. data/lib/sass/repl.rb +57 -0
  47. data/lib/sass/root.rb +7 -0
  48. data/lib/sass/script/css_lexer.rb +33 -0
  49. data/lib/sass/script/css_parser.rb +34 -0
  50. data/lib/sass/script/functions.rb +2626 -0
  51. data/lib/sass/script/lexer.rb +449 -0
  52. data/lib/sass/script/parser.rb +637 -0
  53. data/lib/sass/script/tree/funcall.rb +306 -0
  54. data/lib/sass/script/tree/interpolation.rb +118 -0
  55. data/lib/sass/script/tree/list_literal.rb +77 -0
  56. data/lib/sass/script/tree/literal.rb +45 -0
  57. data/lib/sass/script/tree/map_literal.rb +64 -0
  58. data/lib/sass/script/tree/node.rb +109 -0
  59. data/lib/sass/script/tree/operation.rb +103 -0
  60. data/lib/sass/script/tree/selector.rb +26 -0
  61. data/lib/sass/script/tree/string_interpolation.rb +104 -0
  62. data/lib/sass/script/tree/unary_operation.rb +69 -0
  63. data/lib/sass/script/tree/variable.rb +57 -0
  64. data/lib/sass/script/tree.rb +16 -0
  65. data/lib/sass/script/value/arg_list.rb +36 -0
  66. data/lib/sass/script/value/base.rb +240 -0
  67. data/lib/sass/script/value/bool.rb +35 -0
  68. data/lib/sass/script/value/color.rb +680 -0
  69. data/lib/sass/script/value/helpers.rb +262 -0
  70. data/lib/sass/script/value/list.rb +113 -0
  71. data/lib/sass/script/value/map.rb +70 -0
  72. data/lib/sass/script/value/null.rb +44 -0
  73. data/lib/sass/script/value/number.rb +530 -0
  74. data/lib/sass/script/value/string.rb +97 -0
  75. data/lib/sass/script/value.rb +11 -0
  76. data/lib/sass/script.rb +66 -0
  77. data/lib/sass/scss/css_parser.rb +42 -0
  78. data/lib/sass/scss/parser.rb +1209 -0
  79. data/lib/sass/scss/rx.rb +141 -0
  80. data/lib/sass/scss/script_lexer.rb +15 -0
  81. data/lib/sass/scss/script_parser.rb +25 -0
  82. data/lib/sass/scss/static_parser.rb +368 -0
  83. data/lib/sass/scss.rb +16 -0
  84. data/lib/sass/selector/abstract_sequence.rb +109 -0
  85. data/lib/sass/selector/comma_sequence.rb +175 -0
  86. data/lib/sass/selector/pseudo.rb +256 -0
  87. data/lib/sass/selector/sequence.rb +600 -0
  88. data/lib/sass/selector/simple.rb +117 -0
  89. data/lib/sass/selector/simple_sequence.rb +325 -0
  90. data/lib/sass/selector.rb +326 -0
  91. data/lib/sass/shared.rb +76 -0
  92. data/lib/sass/source/map.rb +210 -0
  93. data/lib/sass/source/position.rb +39 -0
  94. data/lib/sass/source/range.rb +41 -0
  95. data/lib/sass/stack.rb +120 -0
  96. data/lib/sass/supports.rb +227 -0
  97. data/lib/sass/tree/at_root_node.rb +83 -0
  98. data/lib/sass/tree/charset_node.rb +22 -0
  99. data/lib/sass/tree/comment_node.rb +82 -0
  100. data/lib/sass/tree/content_node.rb +9 -0
  101. data/lib/sass/tree/css_import_node.rb +60 -0
  102. data/lib/sass/tree/debug_node.rb +18 -0
  103. data/lib/sass/tree/directive_node.rb +59 -0
  104. data/lib/sass/tree/each_node.rb +24 -0
  105. data/lib/sass/tree/error_node.rb +18 -0
  106. data/lib/sass/tree/extend_node.rb +43 -0
  107. data/lib/sass/tree/for_node.rb +36 -0
  108. data/lib/sass/tree/function_node.rb +39 -0
  109. data/lib/sass/tree/if_node.rb +52 -0
  110. data/lib/sass/tree/import_node.rb +74 -0
  111. data/lib/sass/tree/keyframe_rule_node.rb +15 -0
  112. data/lib/sass/tree/media_node.rb +48 -0
  113. data/lib/sass/tree/mixin_def_node.rb +38 -0
  114. data/lib/sass/tree/mixin_node.rb +52 -0
  115. data/lib/sass/tree/node.rb +238 -0
  116. data/lib/sass/tree/prop_node.rb +171 -0
  117. data/lib/sass/tree/return_node.rb +19 -0
  118. data/lib/sass/tree/root_node.rb +44 -0
  119. data/lib/sass/tree/rule_node.rb +145 -0
  120. data/lib/sass/tree/supports_node.rb +38 -0
  121. data/lib/sass/tree/trace_node.rb +33 -0
  122. data/lib/sass/tree/variable_node.rb +36 -0
  123. data/lib/sass/tree/visitors/base.rb +72 -0
  124. data/lib/sass/tree/visitors/check_nesting.rb +177 -0
  125. data/lib/sass/tree/visitors/convert.rb +334 -0
  126. data/lib/sass/tree/visitors/cssize.rb +369 -0
  127. data/lib/sass/tree/visitors/deep_copy.rb +107 -0
  128. data/lib/sass/tree/visitors/extend.rb +68 -0
  129. data/lib/sass/tree/visitors/perform.rb +539 -0
  130. data/lib/sass/tree/visitors/set_options.rb +139 -0
  131. data/lib/sass/tree/visitors/to_css.rb +381 -0
  132. data/lib/sass/tree/warn_node.rb +18 -0
  133. data/lib/sass/tree/while_node.rb +18 -0
  134. data/lib/sass/util/cross_platform_random.rb +19 -0
  135. data/lib/sass/util/multibyte_string_scanner.rb +157 -0
  136. data/lib/sass/util/normalized_map.rb +130 -0
  137. data/lib/sass/util/ordered_hash.rb +192 -0
  138. data/lib/sass/util/subset_map.rb +110 -0
  139. data/lib/sass/util/test.rb +9 -0
  140. data/lib/sass/util.rb +1318 -0
  141. data/lib/sass/version.rb +124 -0
  142. data/lib/sass.rb +102 -0
  143. data/rails/init.rb +1 -0
  144. data/test/sass/cache_test.rb +131 -0
  145. data/test/sass/callbacks_test.rb +61 -0
  146. data/test/sass/compiler_test.rb +232 -0
  147. data/test/sass/conversion_test.rb +2054 -0
  148. data/test/sass/css2sass_test.rb +477 -0
  149. data/test/sass/data/hsl-rgb.txt +319 -0
  150. data/test/sass/encoding_test.rb +219 -0
  151. data/test/sass/engine_test.rb +3301 -0
  152. data/test/sass/exec_test.rb +86 -0
  153. data/test/sass/extend_test.rb +1661 -0
  154. data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
  155. data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
  156. data/test/sass/functions_test.rb +1926 -0
  157. data/test/sass/importer_test.rb +412 -0
  158. data/test/sass/logger_test.rb +58 -0
  159. data/test/sass/mock_importer.rb +49 -0
  160. data/test/sass/more_results/more1.css +9 -0
  161. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  162. data/test/sass/more_results/more_import.css +29 -0
  163. data/test/sass/more_templates/_more_partial.sass +2 -0
  164. data/test/sass/more_templates/more1.sass +23 -0
  165. data/test/sass/more_templates/more_import.sass +11 -0
  166. data/test/sass/plugin_test.rb +554 -0
  167. data/test/sass/results/alt.css +4 -0
  168. data/test/sass/results/basic.css +9 -0
  169. data/test/sass/results/cached_import_option.css +3 -0
  170. data/test/sass/results/compact.css +5 -0
  171. data/test/sass/results/complex.css +86 -0
  172. data/test/sass/results/compressed.css +1 -0
  173. data/test/sass/results/expanded.css +19 -0
  174. data/test/sass/results/filename_fn.css +3 -0
  175. data/test/sass/results/if.css +3 -0
  176. data/test/sass/results/import.css +31 -0
  177. data/test/sass/results/import_charset.css +5 -0
  178. data/test/sass/results/import_charset_1_8.css +5 -0
  179. data/test/sass/results/import_charset_ibm866.css +5 -0
  180. data/test/sass/results/import_content.css +1 -0
  181. data/test/sass/results/line_numbers.css +49 -0
  182. data/test/sass/results/mixins.css +95 -0
  183. data/test/sass/results/multiline.css +24 -0
  184. data/test/sass/results/nested.css +22 -0
  185. data/test/sass/results/options.css +1 -0
  186. data/test/sass/results/parent_ref.css +13 -0
  187. data/test/sass/results/script.css +16 -0
  188. data/test/sass/results/scss_import.css +31 -0
  189. data/test/sass/results/scss_importee.css +2 -0
  190. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  191. data/test/sass/results/subdir/subdir.css +3 -0
  192. data/test/sass/results/units.css +11 -0
  193. data/test/sass/results/warn.css +0 -0
  194. data/test/sass/results/warn_imported.css +0 -0
  195. data/test/sass/script_conversion_test.rb +328 -0
  196. data/test/sass/script_test.rb +1054 -0
  197. data/test/sass/scss/css_test.rb +1215 -0
  198. data/test/sass/scss/rx_test.rb +156 -0
  199. data/test/sass/scss/scss_test.rb +3900 -0
  200. data/test/sass/scss/test_helper.rb +37 -0
  201. data/test/sass/source_map_test.rb +977 -0
  202. data/test/sass/superselector_test.rb +191 -0
  203. data/test/sass/templates/_cached_import_option_partial.scss +1 -0
  204. data/test/sass/templates/_double_import_loop2.sass +1 -0
  205. data/test/sass/templates/_filename_fn_import.scss +11 -0
  206. data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  207. data/test/sass/templates/_imported_charset_utf8.sass +4 -0
  208. data/test/sass/templates/_imported_content.sass +3 -0
  209. data/test/sass/templates/_partial.sass +2 -0
  210. data/test/sass/templates/_same_name_different_partiality.scss +1 -0
  211. data/test/sass/templates/alt.sass +16 -0
  212. data/test/sass/templates/basic.sass +23 -0
  213. data/test/sass/templates/bork1.sass +2 -0
  214. data/test/sass/templates/bork2.sass +2 -0
  215. data/test/sass/templates/bork3.sass +2 -0
  216. data/test/sass/templates/bork4.sass +2 -0
  217. data/test/sass/templates/bork5.sass +3 -0
  218. data/test/sass/templates/cached_import_option.scss +3 -0
  219. data/test/sass/templates/compact.sass +17 -0
  220. data/test/sass/templates/complex.sass +305 -0
  221. data/test/sass/templates/compressed.sass +15 -0
  222. data/test/sass/templates/double_import_loop1.sass +1 -0
  223. data/test/sass/templates/expanded.sass +17 -0
  224. data/test/sass/templates/filename_fn.scss +18 -0
  225. data/test/sass/templates/if.sass +11 -0
  226. data/test/sass/templates/import.sass +12 -0
  227. data/test/sass/templates/import_charset.sass +9 -0
  228. data/test/sass/templates/import_charset_1_8.sass +6 -0
  229. data/test/sass/templates/import_charset_ibm866.sass +11 -0
  230. data/test/sass/templates/import_content.sass +4 -0
  231. data/test/sass/templates/importee.less +2 -0
  232. data/test/sass/templates/importee.sass +19 -0
  233. data/test/sass/templates/line_numbers.sass +13 -0
  234. data/test/sass/templates/mixin_bork.sass +5 -0
  235. data/test/sass/templates/mixins.sass +76 -0
  236. data/test/sass/templates/multiline.sass +20 -0
  237. data/test/sass/templates/nested.sass +25 -0
  238. data/test/sass/templates/nested_bork1.sass +2 -0
  239. data/test/sass/templates/nested_bork2.sass +2 -0
  240. data/test/sass/templates/nested_bork3.sass +2 -0
  241. data/test/sass/templates/nested_bork4.sass +2 -0
  242. data/test/sass/templates/nested_import.sass +2 -0
  243. data/test/sass/templates/nested_mixin_bork.sass +6 -0
  244. data/test/sass/templates/options.sass +2 -0
  245. data/test/sass/templates/parent_ref.sass +25 -0
  246. data/test/sass/templates/same_name_different_ext.sass +2 -0
  247. data/test/sass/templates/same_name_different_ext.scss +1 -0
  248. data/test/sass/templates/same_name_different_partiality.scss +1 -0
  249. data/test/sass/templates/script.sass +101 -0
  250. data/test/sass/templates/scss_import.scss +12 -0
  251. data/test/sass/templates/scss_importee.scss +1 -0
  252. data/test/sass/templates/single_import_loop.sass +1 -0
  253. data/test/sass/templates/subdir/import_up1.scss +1 -0
  254. data/test/sass/templates/subdir/import_up2.scss +1 -0
  255. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  256. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  257. data/test/sass/templates/subdir/subdir.sass +6 -0
  258. data/test/sass/templates/units.sass +11 -0
  259. data/test/sass/templates/warn.sass +3 -0
  260. data/test/sass/templates/warn_imported.sass +4 -0
  261. data/test/sass/test_helper.rb +8 -0
  262. data/test/sass/util/multibyte_string_scanner_test.rb +147 -0
  263. data/test/sass/util/normalized_map_test.rb +51 -0
  264. data/test/sass/util/subset_map_test.rb +91 -0
  265. data/test/sass/util_test.rb +467 -0
  266. data/test/sass/value_helpers_test.rb +179 -0
  267. data/test/test_helper.rb +109 -0
  268. metadata +386 -0
@@ -0,0 +1,600 @@
1
+ module Sass
2
+ module Selector
3
+ # An operator-separated sequence of
4
+ # {SimpleSequence simple selector sequences}.
5
+ class Sequence < AbstractSequence
6
+ # Sets the line of the Sass template on which this selector was declared.
7
+ # This also sets the line for all child selectors.
8
+ #
9
+ # @param line [Fixnum]
10
+ # @return [Fixnum]
11
+ def line=(line)
12
+ members.each {|m| m.line = line if m.is_a?(SimpleSequence)}
13
+ line
14
+ end
15
+
16
+ # Sets the name of the file in which this selector was declared,
17
+ # or `nil` if it was not declared in a file (e.g. on stdin).
18
+ # This also sets the filename for all child selectors.
19
+ #
20
+ # @param filename [String, nil]
21
+ # @return [String, nil]
22
+ def filename=(filename)
23
+ members.each {|m| m.filename = filename if m.is_a?(SimpleSequence)}
24
+ filename
25
+ end
26
+
27
+ # The array of {SimpleSequence simple selector sequences}, operators, and
28
+ # newlines. The operators are strings such as `"+"` and `">"` representing
29
+ # the corresponding CSS operators, or interpolated SassScript. Newlines
30
+ # are also newline strings; these aren't semantically relevant, but they
31
+ # do affect formatting.
32
+ #
33
+ # @return [Array<SimpleSequence, String|Array<Sass::Tree::Node, String>>]
34
+ attr_reader :members
35
+
36
+ # @param seqs_and_ops [Array<SimpleSequence, String|Array<Sass::Tree::Node, String>>]
37
+ # See \{#members}
38
+ def initialize(seqs_and_ops)
39
+ @members = seqs_and_ops
40
+ end
41
+
42
+ # Resolves the {Parent} selectors within this selector
43
+ # by replacing them with the given parent selector,
44
+ # handling commas appropriately.
45
+ #
46
+ # @param super_cseq [CommaSequence] The parent selector
47
+ # @param implicit_parent [Boolean] Whether the the parent
48
+ # selector should automatically be prepended to the resolved
49
+ # selector if it contains no parent refs.
50
+ # @return [CommaSequence] This selector, with parent references resolved
51
+ # @raise [Sass::SyntaxError] If a parent selector is invalid
52
+ def resolve_parent_refs(super_cseq, implicit_parent)
53
+ members = @members.dup
54
+ nl = (members.first == "\n" && members.shift)
55
+ contains_parent_ref = members.any? do |seq_or_op|
56
+ seq_or_op.is_a?(SimpleSequence) && seq_or_op.members.first.is_a?(Parent)
57
+ end
58
+ return CommaSequence.new([self]) if !implicit_parent && !contains_parent_ref
59
+
60
+ unless contains_parent_ref
61
+ old_members, members = members, []
62
+ members << nl if nl
63
+ members << SimpleSequence.new([Parent.new], false)
64
+ members += old_members
65
+ end
66
+
67
+ CommaSequence.new(Sass::Util.paths(members.map do |sseq_or_op|
68
+ next [sseq_or_op] unless sseq_or_op.is_a?(SimpleSequence)
69
+ sseq_or_op.resolve_parent_refs(super_cseq).members
70
+ end).map do |path|
71
+ Sequence.new(path.map do |seq_or_op|
72
+ next seq_or_op unless seq_or_op.is_a?(Sequence)
73
+ seq_or_op.members
74
+ end.flatten)
75
+ end)
76
+ end
77
+
78
+ # Non-destructively extends this selector with the extensions specified in a hash
79
+ # (which should come from {Sass::Tree::Visitors::Cssize}).
80
+ #
81
+ # @param extends [Sass::Util::SubsetMap{Selector::Simple =>
82
+ # Sass::Tree::Visitors::Cssize::Extend}]
83
+ # The extensions to perform on this selector
84
+ # @param parent_directives [Array<Sass::Tree::DirectiveNode>]
85
+ # The directives containing this selector.
86
+ # @param replace [Boolean]
87
+ # Whether to replace the original selector entirely or include
88
+ # it in the result.
89
+ # @param seen [Set<Array<Selector::Simple>>]
90
+ # The set of simple sequences that are currently being replaced.
91
+ # @param original [Boolean]
92
+ # Whether this is the original selector being extended, as opposed to
93
+ # the result of a previous extension that's being re-extended.
94
+ # @return [Array<Sequence>] A list of selectors generated
95
+ # by extending this selector with `extends`.
96
+ # These correspond to a {CommaSequence}'s {CommaSequence#members members array}.
97
+ # @see CommaSequence#do_extend
98
+ def do_extend(extends, parent_directives, replace, seen, original)
99
+ extended_not_expanded = members.map do |sseq_or_op|
100
+ next [[sseq_or_op]] unless sseq_or_op.is_a?(SimpleSequence)
101
+ extended = sseq_or_op.do_extend(extends, parent_directives, replace, seen)
102
+
103
+ # The First Law of Extend says that the generated selector should have
104
+ # specificity greater than or equal to that of the original selector.
105
+ # In order to ensure that, we record the original selector's
106
+ # (`extended.first`) original specificity.
107
+ extended.first.add_sources!([self]) if original && !has_placeholder?
108
+
109
+ extended.map {|seq| seq.members}
110
+ end
111
+ weaves = Sass::Util.paths(extended_not_expanded).map {|path| weave(path)}
112
+ trim(weaves).map {|p| Sequence.new(p)}
113
+ end
114
+
115
+ # Unifies this with another selector sequence to produce a selector
116
+ # that matches (a subset of) the intersection of the two inputs.
117
+ #
118
+ # @param other [Sequence]
119
+ # @return [CommaSequence, nil] The unified selector, or nil if unification failed.
120
+ # @raise [Sass::SyntaxError] If this selector cannot be unified.
121
+ # This will only ever occur when a dynamic selector,
122
+ # such as {Parent} or {Interpolation}, is used in unification.
123
+ # Since these selectors should be resolved
124
+ # by the time extension and unification happen,
125
+ # this exception will only ever be raised as a result of programmer error
126
+ def unify(other)
127
+ base = members.last
128
+ other_base = other.members.last
129
+ return unless base.is_a?(SimpleSequence) && other_base.is_a?(SimpleSequence)
130
+ return unless (unified = other_base.unify(base))
131
+
132
+ woven = weave([members[0...-1], other.members[0...-1] + [unified]])
133
+ CommaSequence.new(woven.map {|w| Sequence.new(w)})
134
+ end
135
+
136
+ # Returns whether or not this selector matches all elements
137
+ # that the given selector matches (as well as possibly more).
138
+ #
139
+ # @example
140
+ # (.foo).superselector?(.foo.bar) #=> true
141
+ # (.foo).superselector?(.bar) #=> false
142
+ # @param cseq [Sequence]
143
+ # @return [Boolean]
144
+ def superselector?(seq)
145
+ _superselector?(members, seq.members)
146
+ end
147
+
148
+ # @see AbstractSequence#to_s
149
+ def to_s
150
+ @members.join(" ").gsub(/ ?\n ?/, "\n")
151
+ end
152
+
153
+ # Returns a string representation of the sequence.
154
+ # This is basically the selector string.
155
+ #
156
+ # @return [String]
157
+ def inspect
158
+ members.map {|m| m.inspect}.join(" ")
159
+ end
160
+
161
+ # Add to the {SimpleSequence#sources} sets of the child simple sequences.
162
+ # This destructively modifies this sequence's members array, but not the
163
+ # child simple sequences.
164
+ #
165
+ # @param sources [Set<Sequence>]
166
+ def add_sources!(sources)
167
+ members.map! {|m| m.is_a?(SimpleSequence) ? m.with_more_sources(sources) : m}
168
+ end
169
+
170
+ # Converts the subject operator "!", if it exists, into a ":has()"
171
+ # selector.
172
+ #
173
+ # @retur [Sequence]
174
+ def subjectless
175
+ pre_subject = []
176
+ has = []
177
+ subject = nil
178
+ members.each do |sseq_or_op|
179
+ if subject
180
+ has << sseq_or_op
181
+ elsif sseq_or_op.is_a?(String) || !sseq_or_op.subject?
182
+ pre_subject << sseq_or_op
183
+ else
184
+ subject = sseq_or_op.dup
185
+ subject.members = sseq_or_op.members.dup
186
+ subject.subject = false
187
+ has = []
188
+ end
189
+ end
190
+
191
+ return self unless subject
192
+
193
+ unless has.empty?
194
+ subject.members << Pseudo.new(:class, 'has', nil, CommaSequence.new([Sequence.new(has)]))
195
+ end
196
+ Sequence.new(pre_subject + [subject])
197
+ end
198
+
199
+ private
200
+
201
+ # Conceptually, this expands "parenthesized selectors". That is, if we
202
+ # have `.A .B {@extend .C}` and `.D .C {...}`, this conceptually expands
203
+ # into `.D .C, .D (.A .B)`, and this function translates `.D (.A .B)` into
204
+ # `.D .A .B, .A .D .B`. For thoroughness, `.A.D .B` would also be
205
+ # required, but including merged selectors results in exponential output
206
+ # for very little gain.
207
+ #
208
+ # @param path [Array<Array<SimpleSequence or String>>]
209
+ # A list of parenthesized selector groups.
210
+ # @return [Array<Array<SimpleSequence or String>>] A list of fully-expanded selectors.
211
+ def weave(path)
212
+ # This function works by moving through the selector path left-to-right,
213
+ # building all possible prefixes simultaneously.
214
+ prefixes = [[]]
215
+
216
+ path.each do |current|
217
+ next if current.empty?
218
+ current = current.dup
219
+ last_current = [current.pop]
220
+ prefixes = Sass::Util.flatten(prefixes.map do |prefix|
221
+ sub = subweave(prefix, current)
222
+ next [] unless sub
223
+ sub.map {|seqs| seqs + last_current}
224
+ end, 1)
225
+ end
226
+ prefixes
227
+ end
228
+
229
+ # This interweaves two lists of selectors,
230
+ # returning all possible orderings of them (including using unification)
231
+ # that maintain the relative ordering of the input arrays.
232
+ #
233
+ # For example, given `.foo .bar` and `.baz .bang`,
234
+ # this would return `.foo .bar .baz .bang`, `.foo .bar.baz .bang`,
235
+ # `.foo .baz .bar .bang`, `.foo .baz .bar.bang`, `.foo .baz .bang .bar`,
236
+ # and so on until `.baz .bang .foo .bar`.
237
+ #
238
+ # Semantically, for selectors A and B, this returns all selectors `AB_i`
239
+ # such that the union over all i of elements matched by `AB_i X` is
240
+ # identical to the intersection of all elements matched by `A X` and all
241
+ # elements matched by `B X`. Some `AB_i` are elided to reduce the size of
242
+ # the output.
243
+ #
244
+ # @param seq1 [Array<SimpleSequence or String>]
245
+ # @param seq2 [Array<SimpleSequence or String>]
246
+ # @return [Array<Array<SimpleSequence or String>>]
247
+ def subweave(seq1, seq2)
248
+ return [seq2] if seq1.empty?
249
+ return [seq1] if seq2.empty?
250
+
251
+ seq1, seq2 = seq1.dup, seq2.dup
252
+ init = merge_initial_ops(seq1, seq2)
253
+ return unless init
254
+ fin = merge_final_ops(seq1, seq2)
255
+ return unless fin
256
+ seq1 = group_selectors(seq1)
257
+ seq2 = group_selectors(seq2)
258
+ lcs = Sass::Util.lcs(seq2, seq1) do |s1, s2|
259
+ next s1 if s1 == s2
260
+ next unless s1.first.is_a?(SimpleSequence) && s2.first.is_a?(SimpleSequence)
261
+ next s2 if parent_superselector?(s1, s2)
262
+ next s1 if parent_superselector?(s2, s1)
263
+ end
264
+
265
+ diff = [[init]]
266
+ until lcs.empty?
267
+ diff << chunks(seq1, seq2) {|s| parent_superselector?(s.first, lcs.first)} << [lcs.shift]
268
+ seq1.shift
269
+ seq2.shift
270
+ end
271
+ diff << chunks(seq1, seq2) {|s| s.empty?}
272
+ diff += fin.map {|sel| sel.is_a?(Array) ? sel : [sel]}
273
+ diff.reject! {|c| c.empty?}
274
+
275
+ Sass::Util.paths(diff).map {|p| p.flatten}.reject {|p| path_has_two_subjects?(p)}
276
+ end
277
+
278
+ # Extracts initial selector combinators (`"+"`, `">"`, `"~"`, and `"\n"`)
279
+ # from two sequences and merges them together into a single array of
280
+ # selector combinators.
281
+ #
282
+ # @param seq1 [Array<SimpleSequence or String>]
283
+ # @param seq2 [Array<SimpleSequence or String>]
284
+ # @return [Array<String>, nil] If there are no operators in the merged
285
+ # sequence, this will be the empty array. If the operators cannot be
286
+ # merged, this will be nil.
287
+ def merge_initial_ops(seq1, seq2)
288
+ ops1, ops2 = [], []
289
+ ops1 << seq1.shift while seq1.first.is_a?(String)
290
+ ops2 << seq2.shift while seq2.first.is_a?(String)
291
+
292
+ newline = false
293
+ newline ||= !!ops1.shift if ops1.first == "\n"
294
+ newline ||= !!ops2.shift if ops2.first == "\n"
295
+
296
+ # If neither sequence is a subsequence of the other, they cannot be
297
+ # merged successfully
298
+ lcs = Sass::Util.lcs(ops1, ops2)
299
+ return unless lcs == ops1 || lcs == ops2
300
+ (newline ? ["\n"] : []) + (ops1.size > ops2.size ? ops1 : ops2)
301
+ end
302
+
303
+ # Extracts final selector combinators (`"+"`, `">"`, `"~"`) and the
304
+ # selectors to which they apply from two sequences and merges them
305
+ # together into a single array.
306
+ #
307
+ # @param seq1 [Array<SimpleSequence or String>]
308
+ # @param seq2 [Array<SimpleSequence or String>]
309
+ # @return [Array<SimpleSequence or String or
310
+ # Array<Array<SimpleSequence or String>>]
311
+ # If there are no trailing combinators to be merged, this will be the
312
+ # empty array. If the trailing combinators cannot be merged, this will
313
+ # be nil. Otherwise, this will contained the merged selector. Array
314
+ # elements are [Sass::Util#paths]-style options; conceptually, an "or"
315
+ # of multiple selectors.
316
+ # @comment
317
+ # rubocop:disable MethodLength
318
+ def merge_final_ops(seq1, seq2, res = [])
319
+ ops1, ops2 = [], []
320
+ ops1 << seq1.pop while seq1.last.is_a?(String)
321
+ ops2 << seq2.pop while seq2.last.is_a?(String)
322
+
323
+ # Not worth the headache of trying to preserve newlines here. The most
324
+ # important use of newlines is at the beginning of the selector to wrap
325
+ # across lines anyway.
326
+ ops1.reject! {|o| o == "\n"}
327
+ ops2.reject! {|o| o == "\n"}
328
+
329
+ return res if ops1.empty? && ops2.empty?
330
+ if ops1.size > 1 || ops2.size > 1
331
+ # If there are multiple operators, something hacky's going on. If one
332
+ # is a supersequence of the other, use that, otherwise give up.
333
+ lcs = Sass::Util.lcs(ops1, ops2)
334
+ return unless lcs == ops1 || lcs == ops2
335
+ res.unshift(*(ops1.size > ops2.size ? ops1 : ops2).reverse)
336
+ return res
337
+ end
338
+
339
+ # This code looks complicated, but it's actually just a bunch of special
340
+ # cases for interactions between different combinators.
341
+ op1, op2 = ops1.first, ops2.first
342
+ if op1 && op2
343
+ sel1 = seq1.pop
344
+ sel2 = seq2.pop
345
+ if op1 == '~' && op2 == '~'
346
+ if sel1.superselector?(sel2)
347
+ res.unshift sel2, '~'
348
+ elsif sel2.superselector?(sel1)
349
+ res.unshift sel1, '~'
350
+ else
351
+ merged = sel1.unify(sel2)
352
+ res.unshift [
353
+ [sel1, '~', sel2, '~'],
354
+ [sel2, '~', sel1, '~'],
355
+ ([merged, '~'] if merged)
356
+ ].compact
357
+ end
358
+ elsif (op1 == '~' && op2 == '+') || (op1 == '+' && op2 == '~')
359
+ if op1 == '~'
360
+ tilde_sel, plus_sel = sel1, sel2
361
+ else
362
+ tilde_sel, plus_sel = sel2, sel1
363
+ end
364
+
365
+ if tilde_sel.superselector?(plus_sel)
366
+ res.unshift plus_sel, '+'
367
+ else
368
+ merged = plus_sel.unify(tilde_sel)
369
+ res.unshift [
370
+ [tilde_sel, '~', plus_sel, '+'],
371
+ ([merged, '+'] if merged)
372
+ ].compact
373
+ end
374
+ elsif op1 == '>' && %w[~ +].include?(op2)
375
+ res.unshift sel2, op2
376
+ seq1.push sel1, op1
377
+ elsif op2 == '>' && %w[~ +].include?(op1)
378
+ res.unshift sel1, op1
379
+ seq2.push sel2, op2
380
+ elsif op1 == op2
381
+ merged = sel1.unify(sel2)
382
+ return unless merged
383
+ res.unshift merged, op1
384
+ else
385
+ # Unknown selector combinators can't be unified
386
+ return
387
+ end
388
+ return merge_final_ops(seq1, seq2, res)
389
+ elsif op1
390
+ seq2.pop if op1 == '>' && seq2.last && seq2.last.superselector?(seq1.last)
391
+ res.unshift seq1.pop, op1
392
+ return merge_final_ops(seq1, seq2, res)
393
+ else # op2
394
+ seq1.pop if op2 == '>' && seq1.last && seq1.last.superselector?(seq2.last)
395
+ res.unshift seq2.pop, op2
396
+ return merge_final_ops(seq1, seq2, res)
397
+ end
398
+ end
399
+ # @comment
400
+ # rubocop:enable MethodLength
401
+
402
+ # Takes initial subsequences of `seq1` and `seq2` and returns all
403
+ # orderings of those subsequences. The initial subsequences are determined
404
+ # by a block.
405
+ #
406
+ # Destructively removes the initial subsequences of `seq1` and `seq2`.
407
+ #
408
+ # For example, given `(A B C | D E)` and `(1 2 | 3 4 5)` (with `|`
409
+ # denoting the boundary of the initial subsequence), this would return
410
+ # `[(A B C 1 2), (1 2 A B C)]`. The sequences would then be `(D E)` and
411
+ # `(3 4 5)`.
412
+ #
413
+ # @param seq1 [Array]
414
+ # @param seq2 [Array]
415
+ # @yield [a] Used to determine when to cut off the initial subsequences.
416
+ # Called repeatedly for each sequence until it returns true.
417
+ # @yieldparam a [Array] A final subsequence of one input sequence after
418
+ # cutting off some initial subsequence.
419
+ # @yieldreturn [Boolean] Whether or not to cut off the initial subsequence
420
+ # here.
421
+ # @return [Array<Array>] All possible orderings of the initial subsequences.
422
+ def chunks(seq1, seq2)
423
+ chunk1 = []
424
+ chunk1 << seq1.shift until yield seq1
425
+ chunk2 = []
426
+ chunk2 << seq2.shift until yield seq2
427
+ return [] if chunk1.empty? && chunk2.empty?
428
+ return [chunk2] if chunk1.empty?
429
+ return [chunk1] if chunk2.empty?
430
+ [chunk1 + chunk2, chunk2 + chunk1]
431
+ end
432
+
433
+ # Groups a sequence into subsequences. The subsequences are determined by
434
+ # strings; adjacent non-string elements will be put into separate groups,
435
+ # but any element adjacent to a string will be grouped with that string.
436
+ #
437
+ # For example, `(A B "C" D E "F" G "H" "I" J)` will become `[(A) (B "C" D)
438
+ # (E "F" G "H" "I" J)]`.
439
+ #
440
+ # @param seq [Array]
441
+ # @return [Array<Array>]
442
+ def group_selectors(seq)
443
+ newseq = []
444
+ tail = seq.dup
445
+ until tail.empty?
446
+ head = []
447
+ begin
448
+ head << tail.shift
449
+ end while !tail.empty? && head.last.is_a?(String) || tail.first.is_a?(String)
450
+ newseq << head
451
+ end
452
+ newseq
453
+ end
454
+
455
+ # Given two selector sequences, returns whether `seq1` is a
456
+ # superselector of `seq2`; that is, whether `seq1` matches every
457
+ # element `seq2` matches.
458
+ #
459
+ # @param seq1 [Array<SimpleSequence or String>]
460
+ # @param seq2 [Array<SimpleSequence or String>]
461
+ # @return [Boolean]
462
+ def _superselector?(seq1, seq2)
463
+ seq1 = seq1.reject {|e| e == "\n"}
464
+ seq2 = seq2.reject {|e| e == "\n"}
465
+ # Selectors with leading or trailing operators are neither
466
+ # superselectors nor subselectors.
467
+ return if seq1.last.is_a?(String) || seq2.last.is_a?(String) ||
468
+ seq1.first.is_a?(String) || seq2.first.is_a?(String)
469
+ # More complex selectors are never superselectors of less complex ones
470
+ return if seq1.size > seq2.size
471
+ return seq1.first.superselector?(seq2.last, seq2[0...-1]) if seq1.size == 1
472
+
473
+ _, si = Sass::Util.enum_with_index(seq2).find do |e, i|
474
+ return if i == seq2.size - 1
475
+ next if e.is_a?(String)
476
+ seq1.first.superselector?(e, seq2[0...i])
477
+ end
478
+ return unless si
479
+
480
+ if seq1[1].is_a?(String)
481
+ return unless seq2[si + 1].is_a?(String)
482
+ # .foo ~ .bar is a superselector of .foo + .bar
483
+ return unless seq1[1] == "~" ? seq2[si + 1] != ">" : seq1[1] == seq2[si + 1]
484
+ return _superselector?(seq1[2..-1], seq2[si + 2..-1])
485
+ elsif seq2[si + 1].is_a?(String)
486
+ return unless seq2[si + 1] == ">"
487
+ return _superselector?(seq1[1..-1], seq2[si + 2..-1])
488
+ else
489
+ return _superselector?(seq1[1..-1], seq2[si + 1..-1])
490
+ end
491
+ end
492
+
493
+ # Like \{#_superselector?}, but compares the selectors in the
494
+ # context of parent selectors, as though they shared an implicit
495
+ # base simple selector. For example, `B` is not normally a
496
+ # superselector of `B A`, since it doesn't match `A` elements.
497
+ # However, it is a parent superselector, since `B X` is a
498
+ # superselector of `B A X`.
499
+ #
500
+ # @param seq1 [Array<SimpleSequence or String>]
501
+ # @param seq2 [Array<SimpleSequence or String>]
502
+ # @return [Boolean]
503
+ def parent_superselector?(seq1, seq2)
504
+ base = Sass::Selector::SimpleSequence.new([Sass::Selector::Placeholder.new('<temp>')],
505
+ false)
506
+ _superselector?(seq1 + [base], seq2 + [base])
507
+ end
508
+
509
+ # Removes redundant selectors from between multiple lists of
510
+ # selectors. This takes a list of lists of selector sequences;
511
+ # each individual list is assumed to have no redundancy within
512
+ # itself. A selector is only removed if it's redundant with a
513
+ # selector in another list.
514
+ #
515
+ # "Redundant" here means that one selector is a superselector of
516
+ # the other. The more specific selector is removed.
517
+ #
518
+ # @param seqses [Array<Array<Array<SimpleSequence or String>>>]
519
+ # @return [Array<Array<SimpleSequence or String>>]
520
+ def trim(seqses)
521
+ # Avoid truly horrific quadratic behavior. TODO: I think there
522
+ # may be a way to get perfect trimming without going quadratic.
523
+ return Sass::Util.flatten(seqses, 1) if seqses.size > 100
524
+
525
+ # Keep the results in a separate array so we can be sure we aren't
526
+ # comparing against an already-trimmed selector. This ensures that two
527
+ # identical selectors don't mutually trim one another.
528
+ result = seqses.dup
529
+
530
+ # This is n^2 on the sequences, but only comparing between
531
+ # separate sequences should limit the quadratic behavior.
532
+ seqses.each_with_index do |seqs1, i|
533
+ result[i] = seqs1.reject do |seq1|
534
+ # The maximum specificity of the sources that caused [seq1] to be
535
+ # generated. In order for [seq1] to be removed, there must be
536
+ # another selector that's a superselector of it *and* that has
537
+ # specificity greater or equal to this.
538
+ max_spec = _sources(seq1).map do |seq|
539
+ spec = seq.specificity
540
+ spec.is_a?(Range) ? spec.max : spec
541
+ end.max || 0
542
+
543
+ result.any? do |seqs2|
544
+ next if seqs1.equal?(seqs2)
545
+ # Second Law of Extend: the specificity of a generated selector
546
+ # should never be less than the specificity of the extending
547
+ # selector.
548
+ #
549
+ # See https://github.com/nex3/sass/issues/324.
550
+ seqs2.any? do |seq2|
551
+ spec2 = _specificity(seq2)
552
+ spec2 = spec2.begin if spec2.is_a?(Range)
553
+ spec2 >= max_spec && _superselector?(seq2, seq1)
554
+ end
555
+ end
556
+ end
557
+ end
558
+ Sass::Util.flatten(result, 1)
559
+ end
560
+
561
+ def _hash
562
+ members.reject {|m| m == "\n"}.hash
563
+ end
564
+
565
+ def _eql?(other)
566
+ other.members.reject {|m| m == "\n"}.eql?(members.reject {|m| m == "\n"})
567
+ end
568
+
569
+ private
570
+
571
+ def path_has_two_subjects?(path)
572
+ subject = false
573
+ path.each do |sseq_or_op|
574
+ next unless sseq_or_op.is_a?(SimpleSequence)
575
+ next unless sseq_or_op.subject?
576
+ return true if subject
577
+ subject = true
578
+ end
579
+ false
580
+ end
581
+
582
+ def _sources(seq)
583
+ s = Set.new
584
+ seq.map {|sseq_or_op| s.merge sseq_or_op.sources if sseq_or_op.is_a?(SimpleSequence)}
585
+ s
586
+ end
587
+
588
+ def extended_not_expanded_to_s(extended_not_expanded)
589
+ extended_not_expanded.map do |choices|
590
+ choices = choices.map do |sel|
591
+ next sel.first.to_s if sel.size == 1
592
+ "#{sel.join ' '}"
593
+ end
594
+ next choices.first if choices.size == 1 && !choices.include?(' ')
595
+ "(#{choices.join ', '})"
596
+ end.join ' '
597
+ end
598
+ end
599
+ end
600
+ end