sass 3.2.19 → 3.4.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +3 -1
- data/CODE_OF_CONDUCT.md +10 -0
- data/CONTRIBUTING.md +148 -0
- data/MIT-LICENSE +1 -1
- data/README.md +87 -61
- data/Rakefile +119 -15
- data/VERSION +1 -1
- data/VERSION_DATE +1 -1
- data/VERSION_NAME +1 -1
- data/bin/sass +1 -1
- data/bin/scss +1 -1
- data/extra/sass-spec-ref.sh +32 -0
- data/extra/update_watch.rb +1 -1
- data/lib/sass/cache_stores/base.rb +2 -2
- data/lib/sass/cache_stores/chain.rb +2 -1
- data/lib/sass/cache_stores/filesystem.rb +8 -12
- data/lib/sass/cache_stores/memory.rb +5 -6
- data/lib/sass/cache_stores/null.rb +2 -2
- data/lib/sass/callbacks.rb +3 -2
- data/lib/sass/css.rb +22 -23
- data/lib/sass/deprecation.rb +55 -0
- data/lib/sass/engine.rb +487 -191
- data/lib/sass/environment.rb +172 -58
- data/lib/sass/error.rb +21 -24
- data/lib/sass/exec/base.rb +199 -0
- data/lib/sass/exec/sass_convert.rb +283 -0
- data/lib/sass/exec/sass_scss.rb +440 -0
- data/lib/sass/exec.rb +5 -703
- data/lib/sass/features.rb +47 -0
- data/lib/sass/importers/base.rb +50 -7
- data/lib/sass/importers/deprecated_path.rb +51 -0
- data/lib/sass/importers/filesystem.rb +54 -21
- data/lib/sass/importers.rb +1 -0
- data/lib/sass/logger/base.rb +9 -5
- data/lib/sass/logger/delayed.rb +50 -0
- data/lib/sass/logger/log_level.rb +3 -7
- data/lib/sass/logger.rb +9 -7
- data/lib/sass/media.rb +20 -23
- data/lib/sass/plugin/compiler.rb +321 -145
- data/lib/sass/plugin/configuration.rb +45 -34
- data/lib/sass/plugin/merb.rb +3 -3
- data/lib/sass/plugin/rack.rb +3 -3
- data/lib/sass/plugin/rails.rb +1 -1
- data/lib/sass/plugin/staleness_checker.rb +6 -6
- data/lib/sass/plugin.rb +9 -8
- data/lib/sass/repl.rb +3 -3
- data/lib/sass/script/css_lexer.rb +8 -4
- data/lib/sass/script/css_parser.rb +4 -2
- data/lib/sass/script/css_variable_warning.rb +52 -0
- data/lib/sass/script/functions.rb +1583 -433
- data/lib/sass/script/lexer.rb +198 -79
- data/lib/sass/script/parser.rb +463 -133
- data/lib/sass/script/tree/funcall.rb +313 -0
- data/lib/sass/script/tree/interpolation.rb +223 -0
- data/lib/sass/script/tree/list_literal.rb +104 -0
- data/lib/sass/script/tree/literal.rb +49 -0
- data/lib/sass/script/tree/map_literal.rb +64 -0
- data/lib/sass/script/{node.rb → tree/node.rb} +42 -14
- data/lib/sass/script/tree/operation.rb +156 -0
- data/lib/sass/script/tree/selector.rb +26 -0
- data/lib/sass/script/tree/string_interpolation.rb +125 -0
- data/lib/sass/script/{unary_operation.rb → tree/unary_operation.rb} +6 -6
- data/lib/sass/script/tree/variable.rb +57 -0
- data/lib/sass/script/tree.rb +16 -0
- data/lib/sass/script/{arg_list.rb → value/arg_list.rb} +9 -25
- data/lib/sass/script/value/base.rb +241 -0
- data/lib/sass/script/value/bool.rb +35 -0
- data/lib/sass/script/value/color.rb +698 -0
- data/lib/sass/script/value/helpers.rb +272 -0
- data/lib/sass/script/value/list.rb +113 -0
- data/lib/sass/script/value/map.rb +70 -0
- data/lib/sass/script/{null.rb → value/null.rb} +14 -7
- data/lib/sass/script/{number.rb → value/number.rb} +196 -86
- data/lib/sass/script/value/string.rb +138 -0
- data/lib/sass/script/value.rb +11 -0
- data/lib/sass/script.rb +38 -11
- data/lib/sass/scss/css_parser.rb +25 -5
- data/lib/sass/scss/parser.rb +532 -458
- data/lib/sass/scss/rx.rb +21 -14
- data/lib/sass/scss/static_parser.rb +328 -9
- data/lib/sass/scss.rb +0 -2
- data/lib/sass/selector/abstract_sequence.rb +36 -19
- data/lib/sass/selector/comma_sequence.rb +125 -26
- data/lib/sass/selector/pseudo.rb +266 -0
- data/lib/sass/selector/sequence.rb +200 -71
- data/lib/sass/selector/simple.rb +30 -32
- data/lib/sass/selector/simple_sequence.rb +193 -64
- data/lib/sass/selector.rb +65 -194
- data/lib/sass/shared.rb +2 -2
- data/lib/sass/source/map.rb +213 -0
- data/lib/sass/source/position.rb +39 -0
- data/lib/sass/source/range.rb +41 -0
- data/lib/sass/stack.rb +120 -0
- data/lib/sass/supports.rb +19 -23
- data/lib/sass/tree/at_root_node.rb +83 -0
- data/lib/sass/tree/charset_node.rb +1 -1
- data/lib/sass/tree/comment_node.rb +4 -4
- data/lib/sass/tree/css_import_node.rb +19 -11
- data/lib/sass/tree/debug_node.rb +2 -2
- data/lib/sass/tree/directive_node.rb +21 -4
- data/lib/sass/tree/each_node.rb +8 -8
- data/lib/sass/tree/error_node.rb +18 -0
- data/lib/sass/tree/extend_node.rb +14 -7
- data/lib/sass/tree/for_node.rb +4 -4
- data/lib/sass/tree/function_node.rb +14 -4
- data/lib/sass/tree/if_node.rb +1 -1
- data/lib/sass/tree/import_node.rb +10 -10
- data/lib/sass/tree/keyframe_rule_node.rb +15 -0
- data/lib/sass/tree/media_node.rb +4 -14
- data/lib/sass/tree/mixin_def_node.rb +4 -4
- data/lib/sass/tree/mixin_node.rb +21 -8
- data/lib/sass/tree/node.rb +59 -15
- data/lib/sass/tree/prop_node.rb +42 -24
- data/lib/sass/tree/return_node.rb +3 -2
- data/lib/sass/tree/root_node.rb +19 -3
- data/lib/sass/tree/rule_node.rb +49 -26
- data/lib/sass/tree/supports_node.rb +0 -13
- data/lib/sass/tree/trace_node.rb +2 -1
- data/lib/sass/tree/variable_node.rb +9 -3
- data/lib/sass/tree/visitors/base.rb +5 -8
- data/lib/sass/tree/visitors/check_nesting.rb +62 -36
- data/lib/sass/tree/visitors/convert.rb +111 -76
- data/lib/sass/tree/visitors/cssize.rb +206 -74
- data/lib/sass/tree/visitors/deep_copy.rb +11 -6
- data/lib/sass/tree/visitors/extend.rb +19 -17
- data/lib/sass/tree/visitors/perform.rb +308 -190
- data/lib/sass/tree/visitors/set_options.rb +21 -7
- data/lib/sass/tree/visitors/to_css.rb +273 -92
- data/lib/sass/tree/warn_node.rb +2 -2
- data/lib/sass/tree/while_node.rb +2 -2
- data/lib/sass/util/cross_platform_random.rb +19 -0
- data/lib/sass/util/normalized_map.rb +129 -0
- data/lib/sass/util/ordered_hash.rb +192 -0
- data/lib/sass/util/subset_map.rb +5 -5
- data/lib/sass/util/test.rb +0 -1
- data/lib/sass/util.rb +620 -193
- data/lib/sass/version.rb +22 -24
- data/lib/sass.rb +27 -13
- data/test/sass/cache_test.rb +62 -20
- data/test/sass/callbacks_test.rb +1 -1
- data/test/sass/compiler_test.rb +236 -0
- data/test/sass/conversion_test.rb +472 -44
- data/test/sass/css2sass_test.rb +73 -5
- data/test/sass/css_variable_test.rb +132 -0
- data/test/sass/encoding_test.rb +219 -0
- data/test/sass/engine_test.rb +618 -415
- data/test/sass/exec_test.rb +12 -2
- data/test/sass/extend_test.rb +419 -168
- data/test/sass/functions_test.rb +931 -93
- data/test/sass/importer_test.rb +250 -21
- data/test/sass/logger_test.rb +1 -1
- data/test/sass/more_results/more_import.css +1 -1
- data/test/sass/more_templates/more1.sass +10 -10
- data/test/sass/more_templates/more_import.sass +2 -2
- data/test/sass/plugin_test.rb +26 -34
- data/test/sass/results/compact.css +1 -1
- data/test/sass/results/complex.css +4 -4
- data/test/sass/results/expanded.css +1 -1
- data/test/sass/results/import.css +1 -1
- data/test/sass/results/import_charset_ibm866.css +2 -2
- data/test/sass/results/mixins.css +17 -17
- data/test/sass/results/nested.css +1 -1
- data/test/sass/results/parent_ref.css +2 -2
- data/test/sass/results/script.css +5 -5
- data/test/sass/results/scss_import.css +1 -1
- data/test/sass/script_conversion_test.rb +97 -39
- data/test/sass/script_test.rb +911 -102
- data/test/sass/scss/css_test.rb +215 -34
- data/test/sass/scss/rx_test.rb +8 -4
- data/test/sass/scss/scss_test.rb +2424 -325
- data/test/sass/source_map_test.rb +1055 -0
- data/test/sass/superselector_test.rb +210 -0
- data/test/sass/templates/_partial.sass +1 -1
- data/test/sass/templates/basic.sass +10 -10
- data/test/sass/templates/bork1.sass +1 -1
- data/test/sass/templates/bork5.sass +1 -1
- data/test/sass/templates/compact.sass +10 -10
- data/test/sass/templates/complex.sass +187 -187
- data/test/sass/templates/compressed.sass +10 -10
- data/test/sass/templates/expanded.sass +10 -10
- data/test/sass/templates/import.sass +2 -2
- data/test/sass/templates/importee.sass +3 -3
- data/test/sass/templates/mixins.sass +22 -22
- data/test/sass/templates/multiline.sass +4 -4
- data/test/sass/templates/nested.sass +13 -13
- data/test/sass/templates/parent_ref.sass +12 -12
- data/test/sass/templates/script.sass +70 -70
- data/test/sass/templates/scss_import.scss +2 -1
- data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +1 -1
- data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +2 -2
- data/test/sass/templates/subdir/subdir.sass +3 -3
- data/test/sass/templates/units.sass +10 -10
- data/test/sass/test_helper.rb +1 -1
- data/test/sass/util/multibyte_string_scanner_test.rb +11 -3
- data/test/sass/util/normalized_map_test.rb +51 -0
- data/test/sass/util/subset_map_test.rb +2 -2
- data/test/sass/util_test.rb +99 -43
- data/test/sass/value_helpers_test.rb +179 -0
- data/test/sass-spec.yml +3 -0
- data/test/test_helper.rb +42 -12
- data/vendor/listen/CHANGELOG.md +1 -228
- data/vendor/listen/Gemfile +5 -15
- data/vendor/listen/README.md +111 -77
- data/vendor/listen/Rakefile +0 -42
- data/vendor/listen/lib/listen/adapter.rb +195 -82
- data/vendor/listen/lib/listen/adapters/bsd.rb +27 -64
- data/vendor/listen/lib/listen/adapters/darwin.rb +21 -58
- data/vendor/listen/lib/listen/adapters/linux.rb +23 -55
- data/vendor/listen/lib/listen/adapters/polling.rb +25 -34
- data/vendor/listen/lib/listen/adapters/windows.rb +50 -46
- data/vendor/listen/lib/listen/directory_record.rb +96 -61
- data/vendor/listen/lib/listen/listener.rb +135 -37
- data/vendor/listen/lib/listen/turnstile.rb +9 -5
- data/vendor/listen/lib/listen/version.rb +1 -1
- data/vendor/listen/lib/listen.rb +33 -19
- data/vendor/listen/listen.gemspec +6 -0
- data/vendor/listen/spec/listen/adapter_spec.rb +43 -77
- data/vendor/listen/spec/listen/adapters/polling_spec.rb +8 -8
- data/vendor/listen/spec/listen/directory_record_spec.rb +81 -56
- data/vendor/listen/spec/listen/listener_spec.rb +128 -39
- data/vendor/listen/spec/listen_spec.rb +15 -21
- data/vendor/listen/spec/spec_helper.rb +4 -0
- data/vendor/listen/spec/support/adapter_helper.rb +52 -15
- data/vendor/listen/spec/support/directory_record_helper.rb +7 -5
- data/vendor/listen/spec/support/listeners_helper.rb +30 -7
- metadata +161 -111
- data/CONTRIBUTING +0 -3
- data/lib/sass/script/bool.rb +0 -18
- data/lib/sass/script/color.rb +0 -606
- data/lib/sass/script/funcall.rb +0 -245
- data/lib/sass/script/interpolation.rb +0 -79
- data/lib/sass/script/list.rb +0 -85
- data/lib/sass/script/literal.rb +0 -221
- data/lib/sass/script/operation.rb +0 -110
- data/lib/sass/script/string.rb +0 -51
- data/lib/sass/script/string_interpolation.rb +0 -103
- data/lib/sass/script/variable.rb +0 -58
- data/lib/sass/scss/script_lexer.rb +0 -15
- data/lib/sass/scss/script_parser.rb +0 -25
- data/test/Gemfile +0 -3
- data/test/Gemfile.lock +0 -10
- data/vendor/listen/lib/listen/dependency_manager.rb +0 -126
- data/vendor/listen/lib/listen/multi_listener.rb +0 -143
- data/vendor/listen/spec/listen/dependency_manager_spec.rb +0 -107
- data/vendor/listen/spec/listen/multi_listener_spec.rb +0 -174
@@ -2,6 +2,8 @@ module Sass
|
|
2
2
|
module Selector
|
3
3
|
# A comma-separated sequence of selectors.
|
4
4
|
class CommaSequence < AbstractSequence
|
5
|
+
@@compound_extend_deprecation = Sass::Deprecation.new
|
6
|
+
|
5
7
|
# The comma-separated selector sequences
|
6
8
|
# represented by this class.
|
7
9
|
#
|
@@ -18,24 +20,30 @@ module Sass
|
|
18
20
|
# handling commas appropriately.
|
19
21
|
#
|
20
22
|
# @param super_cseq [CommaSequence] The parent selector
|
23
|
+
# @param implicit_parent [Boolean] Whether the the parent
|
24
|
+
# selector should automatically be prepended to the resolved
|
25
|
+
# selector if it contains no parent refs.
|
21
26
|
# @return [CommaSequence] This selector, with parent references resolved
|
22
27
|
# @raise [Sass::SyntaxError] If a parent selector is invalid
|
23
|
-
def resolve_parent_refs(super_cseq)
|
28
|
+
def resolve_parent_refs(super_cseq, implicit_parent = true)
|
24
29
|
if super_cseq.nil?
|
25
|
-
if
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '&'.")
|
30
|
+
if contains_parent_ref?
|
31
|
+
raise Sass::SyntaxError.new(
|
32
|
+
"Base-level rules cannot contain the parent-selector-referencing character '&'.")
|
31
33
|
end
|
32
34
|
return self
|
33
35
|
end
|
34
36
|
|
35
|
-
CommaSequence.new(
|
36
|
-
super_cseq.members
|
37
|
-
|
38
|
-
|
37
|
+
CommaSequence.new(Sass::Util.flatten_vertically(@members.map do |seq|
|
38
|
+
seq.resolve_parent_refs(super_cseq, implicit_parent).members
|
39
|
+
end))
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns whether there's a {Parent} selector anywhere in this sequence.
|
43
|
+
#
|
44
|
+
# @return [Boolean]
|
45
|
+
def contains_parent_ref?
|
46
|
+
@members.any? {|sel| sel.contains_parent_ref?}
|
39
47
|
end
|
40
48
|
|
41
49
|
# Non-destrucively extends this selector with the extensions specified in a hash
|
@@ -49,18 +57,109 @@ module Sass
|
|
49
57
|
# The extensions to perform on this selector
|
50
58
|
# @param parent_directives [Array<Sass::Tree::DirectiveNode>]
|
51
59
|
# The directives containing this selector.
|
60
|
+
# @param replace [Boolean]
|
61
|
+
# Whether to replace the original selector entirely or include
|
62
|
+
# it in the result.
|
63
|
+
# @param seen [Set<Array<Selector::Simple>>]
|
64
|
+
# The set of simple sequences that are currently being replaced.
|
65
|
+
# @param original [Boolean]
|
66
|
+
# Whether this is the original selector being extended, as opposed to
|
67
|
+
# the result of a previous extension that's being re-extended.
|
52
68
|
# @return [CommaSequence] A copy of this selector,
|
53
69
|
# with extensions made according to `extends`
|
54
|
-
def do_extend(extends, parent_directives
|
70
|
+
def do_extend(extends, parent_directives = [], replace = false, seen = Set.new,
|
71
|
+
original = true)
|
55
72
|
CommaSequence.new(members.map do |seq|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
73
|
+
seq.do_extend(extends, parent_directives, replace, seen, original)
|
74
|
+
end.flatten)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns whether or not this selector matches all elements
|
78
|
+
# that the given selector matches (as well as possibly more).
|
79
|
+
#
|
80
|
+
# @example
|
81
|
+
# (.foo).superselector?(.foo.bar) #=> true
|
82
|
+
# (.foo).superselector?(.bar) #=> false
|
83
|
+
# @param cseq [CommaSequence]
|
84
|
+
# @return [Boolean]
|
85
|
+
def superselector?(cseq)
|
86
|
+
cseq.members.all? {|seq1| members.any? {|seq2| seq2.superselector?(seq1)}}
|
87
|
+
end
|
88
|
+
|
89
|
+
# Populates a subset map that can then be used to extend
|
90
|
+
# selectors. This registers an extension with this selector as
|
91
|
+
# the extender and `extendee` as the extendee.
|
92
|
+
#
|
93
|
+
# @param extends [Sass::Util::SubsetMap{Selector::Simple =>
|
94
|
+
# Sass::Tree::Visitors::Cssize::Extend}]
|
95
|
+
# The subset map representing the extensions to perform.
|
96
|
+
# @param extendee [CommaSequence] The selector being extended.
|
97
|
+
# @param extend_node [Sass::Tree::ExtendNode]
|
98
|
+
# The node that caused this extension.
|
99
|
+
# @param parent_directives [Array<Sass::Tree::DirectiveNode>]
|
100
|
+
# The parent directives containing `extend_node`.
|
101
|
+
# @param allow_compound_target [Boolean]
|
102
|
+
# Whether `extendee` is allowed to contain compound selectors.
|
103
|
+
# @raise [Sass::SyntaxError] if this extension is invalid.
|
104
|
+
def populate_extends(extends, extendee, extend_node = nil, parent_directives = [],
|
105
|
+
allow_compound_target = false)
|
106
|
+
extendee.members.each do |seq|
|
107
|
+
if seq.members.size > 1
|
108
|
+
raise Sass::SyntaxError.new("Can't extend #{seq}: can't extend nested selectors")
|
109
|
+
end
|
110
|
+
|
111
|
+
sseq = seq.members.first
|
112
|
+
if !sseq.is_a?(Sass::Selector::SimpleSequence)
|
113
|
+
raise Sass::SyntaxError.new("Can't extend #{seq}: invalid selector")
|
114
|
+
elsif sseq.members.any? {|ss| ss.is_a?(Sass::Selector::Parent)}
|
115
|
+
raise Sass::SyntaxError.new("Can't extend #{seq}: can't extend parent selectors")
|
116
|
+
end
|
117
|
+
|
118
|
+
sel = sseq.members
|
119
|
+
if !allow_compound_target && sel.length > 1
|
120
|
+
@@compound_extend_deprecation.warn(sseq.filename, sseq.line, <<WARNING)
|
121
|
+
Extending a compound selector, #{sseq}, is deprecated and will not be supported in a future release.
|
122
|
+
See https://github.com/sass/sass/issues/1599 for details.
|
123
|
+
WARNING
|
124
|
+
end
|
125
|
+
|
126
|
+
members.each do |member|
|
127
|
+
unless member.members.last.is_a?(Sass::Selector::SimpleSequence)
|
128
|
+
raise Sass::SyntaxError.new("#{member} can't extend: invalid selector")
|
129
|
+
end
|
130
|
+
|
131
|
+
extends[sel] = Sass::Tree::Visitors::Cssize::Extend.new(
|
132
|
+
member, sel, extend_node, parent_directives, :not_found)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Unifies this with another comma selector to produce a selector
|
138
|
+
# that matches (a subset of) the intersection of the two inputs.
|
139
|
+
#
|
140
|
+
# @param other [CommaSequence]
|
141
|
+
# @return [CommaSequence, nil] The unified selector, or nil if unification failed.
|
142
|
+
# @raise [Sass::SyntaxError] If this selector cannot be unified.
|
143
|
+
# This will only ever occur when a dynamic selector,
|
144
|
+
# such as {Parent} or {Interpolation}, is used in unification.
|
145
|
+
# Since these selectors should be resolved
|
146
|
+
# by the time extension and unification happen,
|
147
|
+
# this exception will only ever be raised as a result of programmer error
|
148
|
+
def unify(other)
|
149
|
+
results = members.map {|seq1| other.members.map {|seq2| seq1.unify(seq2)}}.flatten.compact
|
150
|
+
results.empty? ? nil : CommaSequence.new(results.map {|cseq| cseq.members}.flatten)
|
151
|
+
end
|
152
|
+
|
153
|
+
# Returns a SassScript representation of this selector.
|
154
|
+
#
|
155
|
+
# @return [Sass::Script::Value::List]
|
156
|
+
def to_sass_script
|
157
|
+
Sass::Script::Value::List.new(members.map do |seq|
|
158
|
+
Sass::Script::Value::List.new(seq.members.map do |component|
|
159
|
+
next if component == "\n"
|
160
|
+
Sass::Script::Value::String.new(component.to_s)
|
161
|
+
end.compact, :space)
|
162
|
+
end, :comma)
|
64
163
|
end
|
65
164
|
|
66
165
|
# Returns a string representation of the sequence.
|
@@ -71,11 +170,11 @@ module Sass
|
|
71
170
|
members.map {|m| m.inspect}.join(", ")
|
72
171
|
end
|
73
172
|
|
74
|
-
# @see
|
75
|
-
def
|
76
|
-
|
77
|
-
|
78
|
-
|
173
|
+
# @see AbstractSequence#to_s
|
174
|
+
def to_s(opts = {})
|
175
|
+
@members.map {|m| m.to_s(opts)}.
|
176
|
+
join(opts[:style] == :compressed ? "," : ", ").
|
177
|
+
gsub(", \n", ",\n")
|
79
178
|
end
|
80
179
|
|
81
180
|
private
|
@@ -85,7 +184,7 @@ module Sass
|
|
85
184
|
end
|
86
185
|
|
87
186
|
def _eql?(other)
|
88
|
-
other.class == self.class && other.members.eql?(
|
187
|
+
other.class == self.class && other.members.eql?(members)
|
89
188
|
end
|
90
189
|
end
|
91
190
|
end
|
@@ -0,0 +1,266 @@
|
|
1
|
+
module Sass
|
2
|
+
module Selector
|
3
|
+
# A pseudoclass (e.g. `:visited`) or pseudoelement (e.g. `::first-line`)
|
4
|
+
# selector. It can have arguments (e.g. `:nth-child(2n+1)`) which can
|
5
|
+
# contain selectors (e.g. `:nth-child(2n+1 of .foo)`).
|
6
|
+
class Pseudo < Simple
|
7
|
+
# Some pseudo-class-syntax selectors are actually considered
|
8
|
+
# pseudo-elements and must be treated differently. This is a list of such
|
9
|
+
# selectors.
|
10
|
+
#
|
11
|
+
# @return [Set<String>]
|
12
|
+
ACTUALLY_ELEMENTS = %w(after before first-line first-letter).to_set
|
13
|
+
|
14
|
+
# Like \{#type}, but returns the type of selector this looks like, rather
|
15
|
+
# than the type it is semantically. This only differs from type for
|
16
|
+
# selectors in \{ACTUALLY\_ELEMENTS}.
|
17
|
+
#
|
18
|
+
# @return [Symbol]
|
19
|
+
attr_reader :syntactic_type
|
20
|
+
|
21
|
+
# The name of the selector.
|
22
|
+
#
|
23
|
+
# @return [String]
|
24
|
+
attr_reader :name
|
25
|
+
|
26
|
+
# The argument to the selector,
|
27
|
+
# or `nil` if no argument was given.
|
28
|
+
#
|
29
|
+
# @return [String, nil]
|
30
|
+
attr_reader :arg
|
31
|
+
|
32
|
+
# The selector argument, or `nil` if no selector exists.
|
33
|
+
#
|
34
|
+
# If this and \{#arg\} are both set, \{#arg\} is considered a non-selector
|
35
|
+
# prefix.
|
36
|
+
#
|
37
|
+
# @return [CommaSequence]
|
38
|
+
attr_reader :selector
|
39
|
+
|
40
|
+
# @param syntactic_type [Symbol] See \{#syntactic_type}
|
41
|
+
# @param name [String] See \{#name}
|
42
|
+
# @param arg [nil, String] See \{#arg}
|
43
|
+
# @param selector [nil, CommaSequence] See \{#selector}
|
44
|
+
def initialize(syntactic_type, name, arg, selector)
|
45
|
+
@syntactic_type = syntactic_type
|
46
|
+
@name = name
|
47
|
+
@arg = arg
|
48
|
+
@selector = selector
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns a copy of this with \{#selector} set to \{#new\_selector}.
|
52
|
+
#
|
53
|
+
# @param new_selector [CommaSequence]
|
54
|
+
# @return [Array<Simple>]
|
55
|
+
def with_selector(new_selector)
|
56
|
+
result = Pseudo.new(syntactic_type, name, arg,
|
57
|
+
CommaSequence.new(new_selector.members.map do |seq|
|
58
|
+
next seq unless seq.members.length == 1
|
59
|
+
sseq = seq.members.first
|
60
|
+
next seq unless sseq.is_a?(SimpleSequence) && sseq.members.length == 1
|
61
|
+
sel = sseq.members.first
|
62
|
+
next seq unless sel.is_a?(Pseudo) && sel.selector
|
63
|
+
|
64
|
+
case normalized_name
|
65
|
+
when 'not'
|
66
|
+
# In theory, if there's a nested :not its contents should be
|
67
|
+
# unified with the return value. For example, if :not(.foo)
|
68
|
+
# extends .bar, :not(.bar) should become .foo:not(.bar). However,
|
69
|
+
# this is a narrow edge case and supporting it properly would make
|
70
|
+
# this code and the code calling it a lot more complicated, so
|
71
|
+
# it's not supported for now.
|
72
|
+
next [] unless sel.normalized_name == 'matches'
|
73
|
+
sel.selector.members
|
74
|
+
when 'matches', 'any', 'current', 'nth-child', 'nth-last-child'
|
75
|
+
# As above, we could theoretically support :not within :matches, but
|
76
|
+
# doing so would require this method and its callers to handle much
|
77
|
+
# more complex cases that likely aren't worth the pain.
|
78
|
+
next [] unless sel.name == name && sel.arg == arg
|
79
|
+
sel.selector.members
|
80
|
+
when 'has', 'host', 'host-context'
|
81
|
+
# We can't expand nested selectors here, because each layer adds an
|
82
|
+
# additional layer of semantics. For example, `:has(:has(img))`
|
83
|
+
# doesn't match `<div><img></div>` but `:has(img)` does.
|
84
|
+
sel
|
85
|
+
else
|
86
|
+
[]
|
87
|
+
end
|
88
|
+
end.flatten))
|
89
|
+
|
90
|
+
# Older browsers support :not but only with a single complex selector.
|
91
|
+
# In order to support those browsers, we break up the contents of a :not
|
92
|
+
# unless it originally contained a selector list.
|
93
|
+
return [result] unless normalized_name == 'not'
|
94
|
+
return [result] if selector.members.length > 1
|
95
|
+
result.selector.members.map do |seq|
|
96
|
+
Pseudo.new(syntactic_type, name, arg, CommaSequence.new([seq]))
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# The type of the selector. `:class` if this is a pseudoclass selector,
|
101
|
+
# `:element` if it's a pseudoelement.
|
102
|
+
#
|
103
|
+
# @return [Symbol]
|
104
|
+
def type
|
105
|
+
ACTUALLY_ELEMENTS.include?(normalized_name) ? :element : syntactic_type
|
106
|
+
end
|
107
|
+
|
108
|
+
# Like \{#name\}, but without any vendor prefix.
|
109
|
+
#
|
110
|
+
# @return [String]
|
111
|
+
def normalized_name
|
112
|
+
@normalized_name ||= name.gsub(/^-[a-zA-Z0-9]+-/, '')
|
113
|
+
end
|
114
|
+
|
115
|
+
# @see Selector#to_s
|
116
|
+
def to_s(opts = {})
|
117
|
+
res = (syntactic_type == :class ? ":" : "::") + @name
|
118
|
+
if @arg || @selector
|
119
|
+
res << "("
|
120
|
+
res << @arg.strip if @arg
|
121
|
+
res << " " if @arg && @selector
|
122
|
+
res << @selector.to_s(opts) if @selector
|
123
|
+
res << ")"
|
124
|
+
end
|
125
|
+
res
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns `nil` if this is a pseudoelement selector
|
129
|
+
# and `sels` contains a pseudoelement selector different than this one.
|
130
|
+
#
|
131
|
+
# @see SimpleSequence#unify
|
132
|
+
def unify(sels)
|
133
|
+
return if type == :element && sels.any? do |sel|
|
134
|
+
sel.is_a?(Pseudo) && sel.type == :element &&
|
135
|
+
(sel.name != name || sel.arg != arg || sel.selector != selector)
|
136
|
+
end
|
137
|
+
super
|
138
|
+
end
|
139
|
+
|
140
|
+
# Returns whether or not this selector matches all elements
|
141
|
+
# that the given selector matches (as well as possibly more).
|
142
|
+
#
|
143
|
+
# @example
|
144
|
+
# (.foo).superselector?(.foo.bar) #=> true
|
145
|
+
# (.foo).superselector?(.bar) #=> false
|
146
|
+
# @param their_sseq [SimpleSequence]
|
147
|
+
# @param parents [Array<SimpleSequence, String>] The parent selectors of `their_sseq`, if any.
|
148
|
+
# @return [Boolean]
|
149
|
+
def superselector?(their_sseq, parents = [])
|
150
|
+
case normalized_name
|
151
|
+
when 'matches', 'any'
|
152
|
+
# :matches can be a superselector of another selector in one of two
|
153
|
+
# ways. Either its constituent selectors can be a superset of those of
|
154
|
+
# another :matches in the other selector, or any of its constituent
|
155
|
+
# selectors can individually be a superselector of the other selector.
|
156
|
+
(their_sseq.selector_pseudo_classes[normalized_name] || []).any? do |their_sel|
|
157
|
+
next false unless their_sel.is_a?(Pseudo)
|
158
|
+
next false unless their_sel.name == name
|
159
|
+
selector.superselector?(their_sel.selector)
|
160
|
+
end || selector.members.any? do |our_seq|
|
161
|
+
their_seq = Sequence.new(parents + [their_sseq])
|
162
|
+
our_seq.superselector?(their_seq)
|
163
|
+
end
|
164
|
+
when 'has', 'host', 'host-context'
|
165
|
+
# Like :matches, :has (et al) can be a superselector of another
|
166
|
+
# selector if its constituent selectors are a superset of those of
|
167
|
+
# another :has in the other selector. However, the :matches other case
|
168
|
+
# doesn't work, because :has refers to nested elements.
|
169
|
+
(their_sseq.selector_pseudo_classes[normalized_name] || []).any? do |their_sel|
|
170
|
+
next false unless their_sel.is_a?(Pseudo)
|
171
|
+
next false unless their_sel.name == name
|
172
|
+
selector.superselector?(their_sel.selector)
|
173
|
+
end
|
174
|
+
when 'not'
|
175
|
+
selector.members.all? do |our_seq|
|
176
|
+
their_sseq.members.any? do |their_sel|
|
177
|
+
if their_sel.is_a?(Element) || their_sel.is_a?(Id)
|
178
|
+
# `:not(a)` is a superselector of `h1` and `:not(#foo)` is a
|
179
|
+
# superselector of `#bar`.
|
180
|
+
our_sseq = our_seq.members.last
|
181
|
+
next false unless our_sseq.is_a?(SimpleSequence)
|
182
|
+
our_sseq.members.any? do |our_sel|
|
183
|
+
our_sel.class == their_sel.class && our_sel != their_sel
|
184
|
+
end
|
185
|
+
else
|
186
|
+
next false unless their_sel.is_a?(Pseudo)
|
187
|
+
next false unless their_sel.name == name
|
188
|
+
# :not(X) is a superselector of :not(Y) exactly when Y is a
|
189
|
+
# superselector of X.
|
190
|
+
their_sel.selector.superselector?(CommaSequence.new([our_seq]))
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
when 'current'
|
195
|
+
(their_sseq.selector_pseudo_classes['current'] || []).any? do |their_current|
|
196
|
+
next false if their_current.name != name
|
197
|
+
# Explicitly don't check for nested superselector relationships
|
198
|
+
# here. :current(.foo) isn't always a superselector of
|
199
|
+
# :current(.foo.bar), since it matches the *innermost* ancestor of
|
200
|
+
# the current element that matches the selector. For example:
|
201
|
+
#
|
202
|
+
# <div class="foo bar">
|
203
|
+
# <p class="foo">
|
204
|
+
# <span>current element</span>
|
205
|
+
# </p>
|
206
|
+
# </div>
|
207
|
+
#
|
208
|
+
# Here :current(.foo) would match the p element and *not* the div
|
209
|
+
# element, whereas :current(.foo.bar) would match the div and not
|
210
|
+
# the p.
|
211
|
+
selector == their_current.selector
|
212
|
+
end
|
213
|
+
when 'nth-child', 'nth-last-child'
|
214
|
+
their_sseq.members.any? do |their_sel|
|
215
|
+
# This misses a few edge cases. For example, `:nth-child(n of X)`
|
216
|
+
# is a superselector of `X`, and `:nth-child(2n of X)` is a
|
217
|
+
# superselector of `:nth-child(4n of X)`. These seem rare enough
|
218
|
+
# not to be worth worrying about, though.
|
219
|
+
next false unless their_sel.is_a?(Pseudo)
|
220
|
+
next false unless their_sel.name == name
|
221
|
+
next false unless their_sel.arg == arg
|
222
|
+
selector.superselector?(their_sel.selector)
|
223
|
+
end
|
224
|
+
else
|
225
|
+
throw "[BUG] Unknown selector pseudo class #{name}"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
# @see AbstractSequence#specificity
|
230
|
+
def specificity
|
231
|
+
return 1 if type == :element
|
232
|
+
return SPECIFICITY_BASE unless selector
|
233
|
+
@specificity ||=
|
234
|
+
if normalized_name == 'not'
|
235
|
+
min = 0
|
236
|
+
max = 0
|
237
|
+
selector.members.each do |seq|
|
238
|
+
spec = seq.specificity
|
239
|
+
if spec.is_a?(Range)
|
240
|
+
min = Sass::Util.max(spec.begin, min)
|
241
|
+
max = Sass::Util.max(spec.end, max)
|
242
|
+
else
|
243
|
+
min = Sass::Util.max(spec, min)
|
244
|
+
max = Sass::Util.max(spec, max)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
min == max ? max : (min..max)
|
248
|
+
else
|
249
|
+
min = 0
|
250
|
+
max = 0
|
251
|
+
selector.members.each do |seq|
|
252
|
+
spec = seq.specificity
|
253
|
+
if spec.is_a?(Range)
|
254
|
+
min = Sass::Util.min(spec.begin, min)
|
255
|
+
max = Sass::Util.max(spec.end, max)
|
256
|
+
else
|
257
|
+
min = Sass::Util.min(spec, min)
|
258
|
+
max = Sass::Util.max(spec, max)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
min == max ? max : (min..max)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|