css_parser 1.19.1 → 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: '09aa105491fc4a0047380d522d09033787cabe34436e08bdb4ac7002ee2fbbde'
4
- data.tar.gz: 17a8b0825e77ed66bdceb75fcda6b8d93574d8e5b3c1d4fc5586e34d87013d88
3
+ metadata.gz: c8880ebd6978e8a8a3108ea93b4d567252aee152b28f237dab31809f1488f42e
4
+ data.tar.gz: 4e8379acf246b7fe2573a2ada4fc49c01e9c3b2de43fe80d07f42390f8f16450
5
5
  SHA512:
6
- metadata.gz: 34a7782bab86eb88756ac8b90e8873dd8330f6895d852a151c8ad51ed1de93d3fcf1cf5c3a3764e5f9811a0fac995d740057a50856762b5d5aa738bee036fc63
7
- data.tar.gz: 4cf1c27c7f13463ba549e66028622861cd00c9bc9434a40c39f2d1c44c16b37e739747dee0d058d56718360d6902d7fd464ff4da69fe455ef58c35d338c2fa1f
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
@@ -677,12 +679,7 @@ module CssParser
677
679
  end
678
680
 
679
681
  if charset
680
- if String.method_defined?(:encode)
681
- src.encode!('UTF-8', charset)
682
- else
683
- ic = Iconv.new('UTF-8//IGNORE', charset)
684
- src = ic.iconv(src)
685
- end
682
+ src.encode!('UTF-8', charset)
686
683
  end
687
684
  rescue
688
685
  @redirect_count = nil
@@ -723,7 +720,7 @@ module CssParser
723
720
  nodes = {}
724
721
  lines.each do |line|
725
722
  parts = line.split(':', 2)
726
- if parts[1] =~ /:/
723
+ if parts[1].include?(':')
727
724
  nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
728
725
  else
729
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.19.1'.freeze
4
+ VERSION = '1.21.0'.freeze
5
5
  end
data/lib/css_parser.rb CHANGED
@@ -6,7 +6,6 @@ require 'net/https'
6
6
  require 'digest/md5'
7
7
  require 'zlib'
8
8
  require 'stringio'
9
- require 'iconv' unless String.method_defined?(:encode)
10
9
 
11
10
  require 'css_parser/version'
12
11
  require 'css_parser/rule_set'
@@ -58,7 +57,7 @@ module CssParser
58
57
  # in case called like CssParser.merge([rule_set, rule_set])
59
58
  rule_sets.flatten! if rule_sets[0].is_a?(Array)
60
59
 
61
- unless rule_sets.all? { |rs| rs.is_a?(CssParser::RuleSet) }
60
+ unless rule_sets.all?(CssParser::RuleSet)
62
61
  raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
63
62
  end
64
63
 
@@ -71,7 +70,7 @@ module CssParser
71
70
  rule_set.expand_shorthand!
72
71
 
73
72
  specificity = rule_set.specificity
74
- 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
75
74
 
76
75
  rule_set.each_declaration do |property, value, is_important|
77
76
  # Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order
@@ -139,7 +138,7 @@ module CssParser
139
138
  css.gsub(URI_RX) do
140
139
  uri = Regexp.last_match(1).to_s.gsub(/["']+/, '')
141
140
  # Don't process URLs that are already absolute
142
- unless uri.match(%r{^[a-z]+://}i)
141
+ unless uri.match?(%r{^[a-z]+://}i)
143
142
  begin
144
143
  uri = base_uri.join(uri)
145
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.19.1
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-10-13 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