minicss 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +2 -0
- data/lib/minicss/sel.rb +39 -39
- data/lib/minicss/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d32254aa5d942d02f056cbd0b3d6a788c0707c468ccba07770962d5b2dd92bfa
|
4
|
+
data.tar.gz: ffad88b6e385e369efc7dd1a42c9aa2c9994192a90b382a8474d019e59a35fe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3aadae8a7813c78073e512d25f752a50434a378a110898b19a6a101cad1b7c4f537cb5fa15a69264e590505e81fa8e67be21c778f3f41a13983442317f8d68a8
|
7
|
+
data.tar.gz: ddbe9a7d6aba782d31c693fd8a7c0aa980caab2619f46aff399993b29fa3a1f5b17a258ce656413bc459a2d67d185449ebb4ec88cceea17115e555bd770141b8
|
data/.rubocop.yml
CHANGED
@@ -4,6 +4,7 @@ AllCops:
|
|
4
4
|
SuggestExtensions: false
|
5
5
|
Exclude:
|
6
6
|
- spec/parsing_tests/*
|
7
|
+
- vendor/**/*
|
7
8
|
|
8
9
|
Style/StringLiterals:
|
9
10
|
EnforcedStyle: double_quotes
|
@@ -64,3 +65,6 @@ Naming/PredicateMethod:
|
|
64
65
|
Lint/HashCompareByIdentity:
|
65
66
|
Exclude:
|
66
67
|
- lib/minicss/sel.rb
|
68
|
+
|
69
|
+
Layout/FirstHashElementIndentation:
|
70
|
+
EnforcedStyle: consistent
|
data/README.md
CHANGED
@@ -134,6 +134,8 @@ You can introspect the original token stream to report accurate diagnostics.
|
|
134
134
|
|
135
135
|
## Development
|
136
136
|
|
137
|
+
- Clone the repository, and run `git submodule update --init` to fetch the
|
138
|
+
required fixtures.
|
137
139
|
- Install dependencies: `bundle install`
|
138
140
|
- Run the test suite: `bundle exec rspec`
|
139
141
|
- Check style and linting: `bundle exec rubocop`
|
data/lib/minicss/sel.rb
CHANGED
@@ -9,29 +9,29 @@ module MiniCSS
|
|
9
9
|
STRING_SENTINEL = "\uE001"
|
10
10
|
|
11
11
|
TOKENS = {
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
attribute: /\[\s*(?:(?<namespace>\*|[-\w\p{^ASCII}]*)\|)?(?<name>[-\w\p{^ASCII}]+)\s*(?:(?<operator>\W?=)\s*(?<value>.+?)\s*(\s(?<caseSensitive>[iIsS]))?\s*)?\]/u,
|
13
|
+
id: /#(?<name>[-\w\p{^ASCII}]+)/u,
|
14
|
+
class: /\.(?<name>[-\w\p{^ASCII}]+)/u,
|
15
|
+
comma: /\s*,\s*/u,
|
16
|
+
combinator: /\s*[\s>+~]\s*/u,
|
17
|
+
pseudo_element: /::(?<name>[-\w\p{^ASCII}]+)(?:\((?<argument>¶*)\))?/u,
|
18
|
+
pseudo_class: /:(?<name>[-\w\p{^ASCII}]+)(?:\((?<argument>¶*)\))?/u,
|
19
|
+
universal: /(?:(?<namespace>\*|[-\w\p{^ASCII}]*)\|)?\*/u,
|
20
|
+
type: /(?:(?<namespace>\*|[-\w\p{^ASCII}]*)\|)?(?<name>[-\w\p{^ASCII}]+)/u
|
21
21
|
}.freeze
|
22
22
|
|
23
23
|
ARGUMENT_PATTERNS = {
|
24
|
-
|
25
|
-
TOKENS[
|
26
|
-
TOKENS[
|
24
|
+
pseudo_element: Regexp.new(
|
25
|
+
TOKENS[:pseudo_element].source.sub("(?<argument>¶*)", "(?<argument>.*)"),
|
26
|
+
TOKENS[:pseudo_element].options
|
27
27
|
),
|
28
|
-
|
29
|
-
TOKENS[
|
30
|
-
TOKENS[
|
28
|
+
pseudo_class: Regexp.new(
|
29
|
+
TOKENS[:pseudo_class].source.sub("(?<argument>¶*)", "(?<argument>.*)"),
|
30
|
+
TOKENS[:pseudo_class].options
|
31
31
|
)
|
32
32
|
}.freeze
|
33
33
|
|
34
|
-
TRIM_TOKENS = Set.new(%
|
34
|
+
TRIM_TOKENS = Set.new(%i[combinator comma]).freeze
|
35
35
|
|
36
36
|
RECURSIVE_PSEUDO_CLASSES = Set.new(
|
37
37
|
%w[not is where has matches -moz-any -webkit-any nth-child nth-last-child]
|
@@ -97,7 +97,7 @@ module MiniCSS
|
|
97
97
|
|
98
98
|
parts << before if before && !before.empty?
|
99
99
|
|
100
|
-
named = match.named_captures.transform_keys(&:to_sym)
|
100
|
+
named = match.named_captures.transform_keys(&:to_sym).compact
|
101
101
|
parts << named.merge(type: type, content: content)
|
102
102
|
|
103
103
|
parts << after if after && !after.empty?
|
@@ -189,7 +189,7 @@ module MiniCSS
|
|
189
189
|
end
|
190
190
|
|
191
191
|
match.named_captures.each do |key, value|
|
192
|
-
token[key.to_sym] = value
|
192
|
+
token[key.to_sym] = value if value
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
@@ -197,12 +197,12 @@ module MiniCSS
|
|
197
197
|
end
|
198
198
|
|
199
199
|
def nest_tokens(tokens, list: true)
|
200
|
-
if list && tokens.any? { |t| t[:type] ==
|
200
|
+
if list && tokens.any? { |t| t[:type] == :comma }
|
201
201
|
selectors = []
|
202
202
|
temp = []
|
203
203
|
|
204
204
|
tokens.each_with_index do |token, index|
|
205
|
-
if token[:type] ==
|
205
|
+
if token[:type] == :comma
|
206
206
|
raise ArgumentError, "Incorrect comma at #{index}" if temp.empty?
|
207
207
|
|
208
208
|
selectors << nest_tokens(temp, list: false)
|
@@ -215,26 +215,26 @@ module MiniCSS
|
|
215
215
|
raise ArgumentError, "Trailing comma" if temp.empty?
|
216
216
|
|
217
217
|
selectors << nest_tokens(temp, list: false)
|
218
|
-
return { type:
|
218
|
+
return { type: :list, list: selectors }
|
219
219
|
end
|
220
220
|
|
221
221
|
(tokens.length - 1).downto(0) do |i|
|
222
222
|
token = tokens[i]
|
223
|
-
next unless token[:type] ==
|
223
|
+
next unless token[:type] == :combinator
|
224
224
|
|
225
225
|
left = tokens[0...i]
|
226
226
|
right = tokens[(i + 1)..] || []
|
227
227
|
|
228
228
|
if left.empty?
|
229
229
|
return {
|
230
|
-
type:
|
230
|
+
type: :relative,
|
231
231
|
combinator: token[:content],
|
232
232
|
right: nest_tokens(right)
|
233
233
|
}
|
234
234
|
end
|
235
235
|
|
236
236
|
return {
|
237
|
-
type:
|
237
|
+
type: :complex,
|
238
238
|
combinator: token[:content],
|
239
239
|
left: nest_tokens(left),
|
240
240
|
right: nest_tokens(right)
|
@@ -247,7 +247,7 @@ module MiniCSS
|
|
247
247
|
when 1
|
248
248
|
tokens.first
|
249
249
|
else
|
250
|
-
{ type:
|
250
|
+
{ type: :compound, list: tokens.dup }
|
251
251
|
end
|
252
252
|
end
|
253
253
|
|
@@ -255,14 +255,14 @@ module MiniCSS
|
|
255
255
|
return enum_for(:flatten, node, parent) unless block_given?
|
256
256
|
|
257
257
|
case node[:type]
|
258
|
-
when
|
258
|
+
when :list
|
259
259
|
node[:list].each { |child| flatten(child, node, &block) }
|
260
|
-
when
|
260
|
+
when :complex
|
261
261
|
flatten(node[:left], node, &block)
|
262
262
|
flatten(node[:right], node, &block)
|
263
|
-
when
|
263
|
+
when :relative
|
264
264
|
flatten(node[:right], node, &block)
|
265
|
-
when
|
265
|
+
when :compound
|
266
266
|
node[:list].each { |token| block.call(token, node) }
|
267
267
|
else
|
268
268
|
block.call(node, parent)
|
@@ -287,7 +287,7 @@ module MiniCSS
|
|
287
287
|
return ast unless recursive
|
288
288
|
|
289
289
|
flatten(ast).each do |token, _|
|
290
|
-
next unless token[:type] ==
|
290
|
+
next unless token[:type] == :pseudo_class
|
291
291
|
|
292
292
|
argument = token[:argument]
|
293
293
|
next unless argument
|
@@ -316,15 +316,15 @@ module MiniCSS
|
|
316
316
|
return list_or_node.map { |token| token[:content] }.join if list_or_node.is_a?(Array)
|
317
317
|
|
318
318
|
case list_or_node[:type]
|
319
|
-
when
|
319
|
+
when :list
|
320
320
|
list_or_node[:list].map { |node| stringify(node) }.join(",")
|
321
|
-
when
|
321
|
+
when :relative
|
322
322
|
list_or_node[:combinator] + stringify(list_or_node[:right])
|
323
|
-
when
|
323
|
+
when :complex
|
324
324
|
stringify(list_or_node[:left]) +
|
325
325
|
list_or_node[:combinator] +
|
326
326
|
stringify(list_or_node[:right])
|
327
|
-
when
|
327
|
+
when :compound
|
328
328
|
list_or_node[:list].map { |node| stringify(node) }.join
|
329
329
|
else
|
330
330
|
list_or_node[:content]
|
@@ -343,7 +343,7 @@ module MiniCSS
|
|
343
343
|
ast = parse(selector, recursive: true) if selector.is_a?(String)
|
344
344
|
return [] unless ast
|
345
345
|
|
346
|
-
if ast.is_a?(Hash) && ast[:type] ==
|
346
|
+
if ast.is_a?(Hash) && ast[:type] == :list
|
347
347
|
base = 10
|
348
348
|
specificities = ast[:list].map do |entry|
|
349
349
|
sp = specificity(entry)
|
@@ -357,13 +357,13 @@ module MiniCSS
|
|
357
357
|
ret = [0, 0, 0]
|
358
358
|
flatten(ast).each do |token, _|
|
359
359
|
case token[:type]
|
360
|
-
when
|
360
|
+
when :id
|
361
361
|
ret[0] += 1
|
362
|
-
when
|
362
|
+
when :class, :attribute
|
363
363
|
ret[1] += 1
|
364
|
-
when
|
364
|
+
when :pseudo_element, :type
|
365
365
|
ret[2] += 1
|
366
|
-
when
|
366
|
+
when :pseudo_class
|
367
367
|
next if token[:name] == "where"
|
368
368
|
|
369
369
|
unless RECURSIVE_PSEUDO_CLASSES.include?(token[:name]) && token[:subtree]
|
data/lib/minicss/version.rb
CHANGED