lib/css_parser.rb in css_parser-1.7.1 vs lib/css_parser.rb in css_parser-1.8.0

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true + require 'addressable/uri' require 'uri' require 'net/https' require 'digest/md5' require 'zlib' @@ -11,11 +12,10 @@ require 'css_parser/rule_set' require 'css_parser/regexps' require 'css_parser/parser' module CssParser - # Merge multiple CSS RuleSets by cascading according to the CSS 2.1 cascading rules # (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order). # # Takes one or more RuleSet objects. # @@ -54,14 +54,14 @@ # this should be a Class method def self.merge(*rule_sets) @folded_declaration_cache = {} # in case called like CssParser.merge([rule_set, rule_set]) - rule_sets.flatten! if rule_sets[0].kind_of?(Array) + rule_sets.flatten! if rule_sets[0].is_a?(Array) - unless rule_sets.all? {|rs| rs.kind_of?(CssParser::RuleSet)} - raise ArgumentError, "all parameters must be CssParser::RuleSets." + unless rule_sets.all? { |rs| rs.is_a?(CssParser::RuleSet) } + raise ArgumentError, 'all parameters must be CssParser::RuleSets.' end return rule_sets[0] if rule_sets.length == 1 # Internal storage of CSS properties that we will keep @@ -69,42 +69,31 @@ rule_sets.each do |rule_set| rule_set.expand_shorthand! specificity = rule_set.specificity - unless specificity - if rule_set.selectors.length == 0 - specificity = 0 - else - specificity = rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0 - end - end + specificity ||= rule_set.selectors.map { |s| calculate_specificity(s) }.compact.max || 0 rule_set.each_declaration do |property, value, is_important| # Add the property to the list to be folded per http://www.w3.org/TR/CSS21/cascade.html#cascading-order - if not properties.has_key?(property) - properties[property] = {:value => value, :specificity => specificity, :is_important => is_important} + if not properties.key?(property) + properties[property] = {value: value, specificity: specificity, is_important: is_important} elsif is_important if not properties[property][:is_important] or properties[property][:specificity] <= specificity - properties[property] = {:value => value, :specificity => specificity, :is_important => is_important} + properties[property] = {value: value, specificity: specificity, is_important: is_important} end elsif properties[property][:specificity] < specificity or properties[property][:specificity] == specificity unless properties[property][:is_important] - properties[property] = {:value => value, :specificity => specificity, :is_important => is_important} + properties[property] = {value: value, specificity: specificity, is_important: is_important} end end - end + end end - merged = RuleSet.new(nil, nil) - - properties.each do |property, details| - if details[:is_important] - merged[property.strip] = details[:value].strip.gsub(/\;\Z/, '') + '!important' - else - merged[property.strip] = details[:value].strip - end + merged = properties.each_with_object(RuleSet.new(nil, nil)) do |(property, details), rule_set| + value = details[:value].strip + rule_set[property.strip] = details[:is_important] ? "#{value.gsub(/;\Z/, '')}!important" : value end merged.create_shorthand! merged end @@ -126,11 +115,11 @@ c = selector.scan(NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX_NC).length d = selector.scan(ELEMENTS_AND_PSEUDO_ELEMENTS_RX_NC).length "#{a}#{b}#{c}#{d}".to_i rescue - return 0 + 0 end # Make <tt>url()</tt> links absolute. # # Takes a block of CSS and returns it with all relative URIs converted to absolute URIs. @@ -143,26 +132,27 @@ # ==== Example # CssParser.convert_uris("body { background: url('../style/yellow.png?abc=123') };", # "http://example.org/style/basic.css").inspect # => "body { background: url('http://example.org/style/yellow.png?abc=123') };" def self.convert_uris(css, base_uri) - base_uri = Addressable::URI.parse(base_uri) unless base_uri.kind_of?(Addressable::URI) + base_uri = Addressable::URI.parse(base_uri) unless base_uri.is_a?(Addressable::URI) css.gsub(URI_RX) do - uri = $1.to_s - uri.gsub!(/["']+/, '') + uri = Regexp.last_match(1).to_s.gsub(/["']+/, '') # Don't process URLs that are already absolute - unless uri =~ /^[a-z]+\:\/\//i + unless uri.match(%r{^[a-z]+://}i) begin - uri = base_uri + uri - rescue; end + uri = base_uri.join(uri) + rescue + nil + end end - "url('#{uri.to_s}')" + "url('#{uri}')" end end def self.sanitize_media_query(raw) - mq = raw.to_s.gsub(/[\s]+/, ' ') + mq = raw.to_s.gsub(/\s+/, ' ') mq.strip! mq = 'all' if mq.empty? mq.to_sym end end