Skip to content

Commit bb6be16

Browse files
committed
fix perf copps
1 parent 9c8ba02 commit bb6be16

5 files changed

Lines changed: 24 additions & 27 deletions

File tree

.rubocop.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ Metrics/ModuleLength:
4242
Metrics/PerceivedComplexity:
4343
Enabled: false
4444

45-
# TODO: Progressively fix performance offences and remove this setting
46-
Performance:
47-
Enabled: false
48-
49-
Performance/RegexpMatch:
50-
Enabled: true
51-
5245
Style/AndOr:
5346
Enabled: false
5447

lib/css_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def self.merge(*rule_sets)
5757
# in case called like CssParser.merge([rule_set, rule_set])
5858
rule_sets.flatten! if rule_sets[0].is_a?(Array)
5959

60-
unless rule_sets.all? { |rs| rs.is_a?(CssParser::RuleSet) }
60+
unless rule_sets.all?(CssParser::RuleSet)
6161
raise ArgumentError, 'all parameters must be CssParser::RuleSets.'
6262
end
6363

@@ -70,7 +70,7 @@ def self.merge(*rule_sets)
7070
rule_set.expand_shorthand!
7171

7272
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
7474

7575
rule_set.each_declaration do |property, value, is_important|
7676
# Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order

lib/css_parser/parser.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc:
359359
in_at_media_rule = false
360360
in_media_block = false
361361

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 = +''
365365

366366
# once we are in a rule, we will use this to store where we started if we are capturing offsets
367367
rule_start = nil
@@ -405,13 +405,14 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc:
405405
media_types: current_media_queries
406406
}
407407
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
409410
end
410411
add_rule!(**add_rule_options)
411412
end
412413

413-
current_selectors = String.new
414-
current_declarations = String.new
414+
current_selectors = +''
415+
current_declarations = +''
415416

416417
# restart our search for selectors and declarations
417418
rule_start = nil if options[:capture_offsets]
@@ -426,14 +427,14 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc:
426427
in_at_media_rule = false
427428
in_media_block = true
428429
current_media_queries << CssParser.sanitize_media_query(current_media_query)
429-
current_media_query = String.new
430+
current_media_query = +''
430431
elsif token.include?(',')
431432
# new media query begins
432433
token.tr!(',', ' ')
433434
token.strip!
434435
current_media_query << token << ' '
435436
current_media_queries << CssParser.sanitize_media_query(current_media_query)
436-
current_media_query = String.new
437+
current_media_query = +''
437438
else
438439
token.strip!
439440
# special-case the ( and ) tokens to remove inner-whitespace
@@ -478,7 +479,8 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc:
478479
media_types: current_media_queries
479480
}
480481
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
482484
end
483485
add_rule!(**add_rule_options)
484486
end
@@ -718,7 +720,7 @@ def css_node_to_h(hash, key, val)
718720
nodes = {}
719721
lines.each do |line|
720722
parts = line.split(':', 2)
721-
if /:/.match?(parts[1])
723+
if parts[1].include?(':')
722724
nodes[parts[0]] = css_node_to_h(hash, parts[0], parts[1])
723725
else
724726
nodes[parts[0].to_s.strip] = parts[1].to_s.strip

lib/css_parser/rule_set.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class RuleSet
1111
BACKGROUND_PROPERTIES = ['background-color', 'background-image', 'background-repeat', 'background-position', 'background-size', 'background-attachment'].freeze
1212
LIST_STYLE_PROPERTIES = ['list-style-type', 'list-style-position', 'list-style-image'].freeze
1313
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
1415
BORDER_STYLE_PROPERTIES = ['border-width', 'border-style', 'border-color'].freeze
1516
BORDER_PROPERTIES = ['border', 'border-left', 'border-right', 'border-top', 'border-bottom'].freeze
17+
DIMENSION_DIRECTIONS = [:top, :right, :bottom, :left].freeze
1618

1719
NUMBER_OF_DIMENSIONS = 4
1820

@@ -196,7 +198,7 @@ def replace_declaration!(property, replacements, preserve_importance: false)
196198
end
197199

198200
def to_s(options = {})
199-
str = declarations.reduce(String.new) do |memo, (prop, value)|
201+
str = declarations.reduce(+'') do |memo, (prop, value)|
200202
importance = options[:force_important] || value.important ? ' !important' : ''
201203
memo << "#{prop}: #{value.value}#{importance}; "
202204
end
@@ -456,7 +458,7 @@ def expand_font_shorthand! # :nodoc:
456458
font_props['font-family'] = m
457459
end
458460
elsif /normal|inherit/i.match?(m)
459-
['font-style', 'font-weight', 'font-variant'].each do |font_prop|
461+
FONT_WEIGHT_PROPERTIES.each do |font_prop|
460462
font_props[font_prop] ||= m
461463
end
462464
elsif /italic|oblique/i.match?(m)
@@ -554,15 +556,15 @@ def create_background_shorthand! # :nodoc:
554556
#
555557
# TODO: this is extremely similar to create_background_shorthand! and should be combined
556558
def create_border_shorthand! # :nodoc:
557-
values = BORDER_STYLE_PROPERTIES.map do |property|
559+
values = BORDER_STYLE_PROPERTIES.filter_map do |property|
558560
next unless (declaration = declarations[property])
559561
next if declaration.important
560562
# can't merge if any value contains a space (i.e. has multiple values)
561563
# we temporarily remove any spaces after commas for the check (inside rgba, etc...)
562564
next if /\s/.match?(declaration.value.gsub(/,\s/, ',').strip)
563565

564566
declaration.value
565-
end.compact
567+
end
566568

567569
return if values.size != BORDER_STYLE_PROPERTIES.size
568570

@@ -579,7 +581,7 @@ def create_dimensions_shorthand! # :nodoc:
579581
return if declarations.size < NUMBER_OF_DIMENSIONS
580582

581583
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|
583585
next unless (declaration = declarations[dimensions[index]])
584586

585587
result[side] = declaration.value
@@ -602,7 +604,7 @@ def create_dimensions_shorthand! # :nodoc:
602604
def create_font_shorthand! # :nodoc:
603605
return unless FONT_STYLE_PROPERTIES.all? { |prop| declarations.key?(prop) }
604606

605-
new_value = String.new
607+
new_value = +''
606608
['font-style', 'font-variant', 'font-weight'].each do |property|
607609
unless declarations[property].value == 'normal'
608610
new_value << declarations[property].value << ' '
@@ -639,7 +641,7 @@ def compute_dimensions_shorthand(values)
639641
return [:top] if values.values.uniq.count == 1
640642

641643
# `/* top | right | bottom | left */`
642-
return [:top, :right, :bottom, :left] if values[:left] != values[:right]
644+
return DIMENSION_DIRECTIONS if values[:left] != values[:right]
643645

644646
# Vertical are the same & horizontal are the same, `/* vertical | horizontal */`
645647
return [:top, :left] if values[:top] == values[:bottom]

test/test_css_parser_loading.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_loading_malformed_content_strings
141141
file_name = File.expand_path('fixtures/import-malformed.css', __dir__)
142142
@cp.load_file!(file_name)
143143
@cp.each_selector do |_sel, dec, _spec|
144-
assert_nil dec =~ /wellformed/
144+
assert_nil dec.include?('wellformed')
145145
end
146146
end
147147

0 commit comments

Comments
 (0)