css_parser 1.20.0 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7685aa4f55ebd1d9abde202f1d4f03e9215e494ae01daf44713286311e339e7f
4
- data.tar.gz: 2d60924c9ada315c9bf8c4e5ce8186b27df4f5a6e5cddef4287f4df190c45a63
3
+ metadata.gz: c8880ebd6978e8a8a3108ea93b4d567252aee152b28f237dab31809f1488f42e
4
+ data.tar.gz: 4e8379acf246b7fe2573a2ada4fc49c01e9c3b2de43fe80d07f42390f8f16450
5
5
  SHA512:
6
- metadata.gz: 65bce8402032a1b8f159e1133eb1cc76f7ca104113d3e8191fd7eab2eda6796409f50a6208e58e867cd057b8128bf584dcf7b59de7f9e829512ec711fbed4322
7
- data.tar.gz: 5dcc28bc5cebe61327e3d492502d849d59c438d7704d4e355f8b5c845b2d02897972df53a5b842bcb5da19baf92854e9014fdf9a100519a3c9c3d8cc1912f17a
6
+ metadata.gz: 480947bb06a6c40ab955f76ff9eb7ba9ac5caf160d7fe343d16901e9f37210c9ccab22a07a4d3165ec26bec4cd28547f530718978d2d969ed398c8195351a0c0
7
+ data.tar.gz: e7a757c9dc096e71d5f617343f0900bacddea42bef4b26ca68f2830728a480ace908807dc4db82b6b0b77a11c1b2c9295ad814bb2aaa80fd069c959cd58afe65
@@ -359,9 +359,9 @@ module CssParser
359
359
  in_at_media_rule = false
360
360
  in_media_block = false
361
361
 
362
- current_selectors = String.new
363
- current_media_query = String.new
364
- current_declarations = String.new
362
+ current_selectors = +''
363
+ current_media_query = +''
364
+ current_declarations = +''
365
365
 
366
366
  # once we are in a rule, we will use this to store where we started if we are capturing offsets
367
367
  rule_start = nil
@@ -405,18 +405,19 @@ module CssParser
405
405
  media_types: current_media_queries
406
406
  }
407
407
  if options[:capture_offsets]
408
- add_rule_options.merge!(filename: options[:filename], offset: rule_start..end_offset)
408
+ add_rule_options[:filename] = options[:filename]
409
+ add_rule_options[:offset] = rule_start..end_offset
409
410
  end
410
411
  add_rule!(**add_rule_options)
411
412
  end
412
413
 
413
- current_selectors = String.new
414
- current_declarations = String.new
414
+ current_selectors = +''
415
+ current_declarations = +''
415
416
 
416
417
  # restart our search for selectors and declarations
417
418
  rule_start = nil if options[:capture_offsets]
418
419
  end
419
- elsif token =~ /@media/i
420
+ elsif /@media/i.match?(token)
420
421
  # found '@media', reset current media_types
421
422
  in_at_media_rule = true
422
423
  current_media_queries = []
@@ -426,14 +427,14 @@ module CssParser
426
427
  in_at_media_rule = false
427
428
  in_media_block = true
428
429
  current_media_queries << CssParser.sanitize_media_query(current_media_query)
429
- current_media_query = String.new
430
+ current_media_query = +''
430
431
  elsif token.include?(',')
431
432
  # new media query begins
432
433
  token.tr!(',', ' ')
433
434
  token.strip!
434
435
  current_media_query << token << ' '
435
436
  current_media_queries << CssParser.sanitize_media_query(current_media_query)
436
- current_media_query = String.new
437
+ current_media_query = +''
437
438
  else
438
439
  token.strip!
439
440
  # special-case the ( and ) tokens to remove inner-whitespace
@@ -478,7 +479,8 @@ module CssParser
478
479
  media_types: current_media_queries
479
480
  }
480
481
  if options[:capture_offsets]
481
- add_rule_options.merge!(filename: options[:filename], offset: rule_start..end_offset)
482
+ add_rule_options[:filename] = options[:filename]
483
+ add_rule_options[:offset] = rule_start..end_offset
482
484
  end
483
485
  add_rule!(**add_rule_options)
484
486
  end
@@ -718,7 +720,7 @@ module CssParser
718
720
  nodes = {}
719
721
  lines.each do |line|
720
722
  parts = line.split(':', 2)
721
- if parts[1] =~ /:/
723
+ if parts[1].include?(':')
722
724
  nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
723
725
  else
724
726
  nodes[parts[0].to_s.strip] = parts[1].to_s.strip
@@ -11,8 +11,10 @@ module CssParser
11
11
  BACKGROUND_PROPERTIES = ['background-color', 'background-image', 'background-repeat', 'background-position', 'background-size', 'background-attachment'].freeze
12
12
  LIST_STYLE_PROPERTIES = ['list-style-type', 'list-style-position', 'list-style-image'].freeze
13
13
  FONT_STYLE_PROPERTIES = ['font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family'].freeze
14
+ FONT_WEIGHT_PROPERTIES = ['font-style', 'font-weight', 'font-variant'].freeze
14
15
  BORDER_STYLE_PROPERTIES = ['border-width', 'border-style', 'border-color'].freeze
15
16
  BORDER_PROPERTIES = ['border', 'border-left', 'border-right', 'border-top', 'border-bottom'].freeze
17
+ DIMENSION_DIRECTIONS = [:top, :right, :bottom, :left].freeze
16
18
 
17
19
  NUMBER_OF_DIMENSIONS = 4
18
20
 
@@ -196,7 +198,7 @@ module CssParser
196
198
  end
197
199
 
198
200
  def to_s(options = {})
199
- str = declarations.reduce(String.new) do |memo, (prop, value)|
201
+ str = declarations.reduce(+'') do |memo, (prop, value)|
200
202
  importance = options[:force_important] || value.important ? ' !important' : ''
201
203
  memo << "#{prop}: #{value.value}#{importance}; "
202
204
  end
@@ -455,17 +457,17 @@ module CssParser
455
457
  else
456
458
  font_props['font-family'] = m
457
459
  end
458
- elsif m =~ /normal|inherit/i
459
- ['font-style', 'font-weight', 'font-variant'].each do |font_prop|
460
+ elsif /normal|inherit/i.match?(m)
461
+ FONT_WEIGHT_PROPERTIES.each do |font_prop|
460
462
  font_props[font_prop] ||= m
461
463
  end
462
- elsif m =~ /italic|oblique/i
464
+ elsif /italic|oblique/i.match?(m)
463
465
  font_props['font-style'] = m
464
- elsif m =~ /small-caps/i
466
+ elsif /small-caps/i.match?(m)
465
467
  font_props['font-variant'] = m
466
- elsif m =~ /[1-9]00$|bold|bolder|lighter/i
468
+ elsif /[1-9]00$|bold|bolder|lighter/i.match?(m)
467
469
  font_props['font-weight'] = m
468
- elsif m =~ CssParser::FONT_UNITS_RX
470
+ elsif CssParser::FONT_UNITS_RX.match?(m)
469
471
  if m.include?('/')
470
472
  font_props['font-size'], font_props['line-height'] = m.split('/', 2)
471
473
  else
@@ -488,7 +490,7 @@ module CssParser
488
490
  value = declaration.value.dup
489
491
 
490
492
  replacement =
491
- if value =~ CssParser::RE_INHERIT
493
+ if CssParser::RE_INHERIT.match?(value)
492
494
  LIST_STYLE_PROPERTIES.to_h { |key| [key, 'inherit'] }
493
495
  else
494
496
  {
@@ -554,15 +556,15 @@ module CssParser
554
556
  #
555
557
  # TODO: this is extremely similar to create_background_shorthand! and should be combined
556
558
  def create_border_shorthand! # :nodoc:
557
- values = BORDER_STYLE_PROPERTIES.map do |property|
559
+ values = BORDER_STYLE_PROPERTIES.filter_map do |property|
558
560
  next unless (declaration = declarations[property])
559
561
  next if declaration.important
560
562
  # can't merge if any value contains a space (i.e. has multiple values)
561
563
  # we temporarily remove any spaces after commas for the check (inside rgba, etc...)
562
- next if declaration.value.gsub(/,\s/, ',').strip =~ /\s/
564
+ next if /\s/.match?(declaration.value.gsub(/,\s/, ',').strip)
563
565
 
564
566
  declaration.value
565
- end.compact
567
+ end
566
568
 
567
569
  return if values.size != BORDER_STYLE_PROPERTIES.size
568
570
 
@@ -579,7 +581,7 @@ module CssParser
579
581
  return if declarations.size < NUMBER_OF_DIMENSIONS
580
582
 
581
583
  DIMENSIONS.each do |property, dimensions|
582
- values = [:top, :right, :bottom, :left].each_with_index.with_object({}) do |(side, index), result|
584
+ values = DIMENSION_DIRECTIONS.each_with_index.with_object({}) do |(side, index), result|
583
585
  next unless (declaration = declarations[dimensions[index]])
584
586
 
585
587
  result[side] = declaration.value
@@ -602,7 +604,7 @@ module CssParser
602
604
  def create_font_shorthand! # :nodoc:
603
605
  return unless FONT_STYLE_PROPERTIES.all? { |prop| declarations.key?(prop) }
604
606
 
605
- new_value = String.new
607
+ new_value = +''
606
608
  ['font-style', 'font-variant', 'font-weight'].each do |property|
607
609
  unless declarations[property].value == 'normal'
608
610
  new_value << declarations[property].value << ' '
@@ -639,7 +641,7 @@ module CssParser
639
641
  return [:top] if values.values.uniq.count == 1
640
642
 
641
643
  # `/* top | right | bottom | left */`
642
- return [:top, :right, :bottom, :left] if values[:left] != values[:right]
644
+ return DIMENSION_DIRECTIONS if values[:left] != values[:right]
643
645
 
644
646
  # Vertical are the same & horizontal are the same, `/* vertical | horizontal */`
645
647
  return [:top, :left] if values[:top] == values[:bottom]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CssParser
4
- VERSION = '1.20.0'.freeze
4
+ VERSION = '1.21.0'.freeze
5
5
  end
data/lib/css_parser.rb CHANGED
@@ -57,7 +57,7 @@ module CssParser
57
57
  # in case called like CssParser.merge([rule_set, rule_set])
58
58
  rule_sets.flatten! if rule_sets[0].is_a?(Array)
59
59
 
60
- unless rule_sets.all? { |rs| rs.is_a?(CssParser::RuleSet) }
60
+ unless rule_sets.all?(CssParser::RuleSet)
61
61
  raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
62
62
  end
63
63
 
@@ -70,7 +70,7 @@ module CssParser
70
70
  rule_set.expand_shorthand!
71
71
 
72
72
  specificity = rule_set.specificity
73
- specificity ||= rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0
73
+ specificity ||= rule_set.selectors.filter_map { |s| calculate_specificity(s) }.max || 0
74
74
 
75
75
  rule_set.each_declaration do |property, value, is_important|
76
76
  # Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
@@ -138,7 +138,7 @@ module CssParser
138
138
  css.gsub(URI_RX) do
139
139
  uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
140
140
  # Don't process URLs that are already absolute
141
- unless uri.match(%r{^[a-z]+://}i)
141
+ unless uri.match?(%r{^[a-z]+://}i)
142
142
  begin
143
143
  uri = base_uri.join(uri)
144
144
  rescue
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: css_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.20.0
4
+ version: 1.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dunae
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-12 00:00:00.000000000 Z
11
+ date: 2024-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable