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