minicss 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64d957ba44321d035f80d2f0a1d1f23d268c33c8a169a869192fa0dc6cf51dcc
4
- data.tar.gz: 758db44357f78c2f43c2a714bb9cfcfba5e8b001e0ec3fc0c9a10b967e2ef005
3
+ metadata.gz: e760746162c4857ca6a36f4f2381453f2d7420db69f4a43c53b0efd27f6aa1ea
4
+ data.tar.gz: 6e14e05aa4438edad1f239f8c23539c5107bad324817db6ebc2ab93237652fd0
5
5
  SHA512:
6
- metadata.gz: c1f6547128ba426bd55bbe0a6fb23302810082216de1df340c8f22a17ef9400c912ab6b2b1a0c362130fd0cee5524a40ff923ac69d6ae55abbef5f5621929dfe
7
- data.tar.gz: 244a8692b6e8e950551270f82bd7b4a3fddb52ee00a47637f17ad5bd13fbdec78da25f6e1ba1208f29ce641f93350e137b815f08f173e178a99e73b49c6402d8
6
+ metadata.gz: 2627f1c78884b10f993acf14419eb5e21fe30e642dda506efa8497be1699f483e523e41407ec8fbfcb065394a71b63be0b516c1a9792a2d1d8a7a1888f2a0789
7
+ data.tar.gz: abb10fd9efb0a4643d4058cd8134f638a2038dd9711dd8c55dc6f3ae2e541130d5cf6bdc8eae9028ebe752a6f669b72cb26f267ec53ed66eefa36bf835df01cd
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`
@@ -29,7 +29,19 @@ module MiniCSS
29
29
  def left_token = LEFT_TOKENS[associated_token]
30
30
 
31
31
  def literal
32
- [left_token, @value.map(&:literal), right_token].join
32
+ [left_token, @value.map { convert_value(it) }.join, right_token].join
33
+ end
34
+
35
+ def convert_value(val)
36
+ case val.kind
37
+ when :ident, :delim
38
+ val.literal
39
+ when :string
40
+ q = val.opts[:quoting]
41
+ [q, val.literal, q].join
42
+ else
43
+ raise "Unexpected type in value: #{val.kind}"
44
+ end
33
45
  end
34
46
  end
35
47
  end
data/lib/minicss/sel.rb CHANGED
@@ -9,29 +9,29 @@ module MiniCSS
9
9
  STRING_SENTINEL = "\uE001"
10
10
 
11
11
  TOKENS = {
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
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
- "pseudo-element" => Regexp.new(
25
- TOKENS["pseudo-element"].source.sub("(?<argument>¶*)", "(?<argument>.*)"),
26
- TOKENS["pseudo-element"].options
24
+ pseudo_element: Regexp.new(
25
+ TOKENS[:pseudo_element].source.sub("(?<argument>¶*)", "(?<argument>.*)"),
26
+ TOKENS[:pseudo_element].options
27
27
  ),
28
- "pseudo-class" => Regexp.new(
29
- TOKENS["pseudo-class"].source.sub("(?<argument>¶*)", "(?<argument>.*)"),
30
- TOKENS["pseudo-class"].options
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(%w[combinator comma]).freeze
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] == "comma" }
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] == "comma"
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: "list", list: selectors }
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] == "combinator"
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: "relative",
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: "complex",
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: "compound", list: tokens.dup }
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 "list"
258
+ when :list
259
259
  node[:list].each { |child| flatten(child, node, &block) }
260
- when "complex"
260
+ when :complex
261
261
  flatten(node[:left], node, &block)
262
262
  flatten(node[:right], node, &block)
263
- when "relative"
263
+ when :relative
264
264
  flatten(node[:right], node, &block)
265
- when "compound"
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] == "pseudo-class"
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 "list"
319
+ when :list
320
320
  list_or_node[:list].map { |node| stringify(node) }.join(",")
321
- when "relative"
321
+ when :relative
322
322
  list_or_node[:combinator] + stringify(list_or_node[:right])
323
- when "complex"
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 "compound"
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] == "list"
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 "id"
360
+ when :id
361
361
  ret[0] += 1
362
- when "class", "attribute"
362
+ when :class, :attribute
363
363
  ret[1] += 1
364
- when "pseudo-element", "type"
364
+ when :pseudo_element, :type
365
365
  ret[2] += 1
366
- when "pseudo-class"
366
+ when :pseudo_class
367
367
  next if token[:name] == "where"
368
368
 
369
369
  unless RECURSIVE_PSEUDO_CLASSES.include?(token[:name]) && token[:subtree]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniCSS
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minicss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vito Sartori
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-10-01 00:00:00.000000000 Z
10
+ date: 2025-10-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: MiniCSS is a pure-Ruby CSS parsing library
13
13
  email: