lib/prawn/svg/css/stylesheets.rb in prawn-svg-0.34.2 vs lib/prawn/svg/css/stylesheets.rb in prawn-svg-0.35.0

- old
+ new

@@ -37,18 +37,18 @@ css_parser.each_rule_set(media) do |rule_set, _| declarations = [] rule_set.each_declaration { |*data| declarations << data } rule_set.selectors.each do |selector_text| - if selector = Prawn::SVG::CSS::SelectorParser.parse(selector_text) - xpath = css_selector_to_xpath(selector) - specificity = calculate_specificity(selector) - specificity << order - order += 1 + next unless (selector = Prawn::SVG::CSS::SelectorParser.parse(selector_text)) - xpath_styles << [xpath, declarations, specificity] - end + xpath = css_selector_to_xpath(selector) + specificity = calculate_specificity(selector) + specificity << order + order += 1 + + xpath_styles << [xpath, declarations, specificity] end end xpath_styles.sort_by(&:last) end @@ -64,47 +64,47 @@ element_styles end def xpath_quote(value) - %{"#{value.gsub('\\', '\\\\').gsub('"', '\\"')}"} if value + %("#{value.gsub('\\', '\\\\').gsub('"', '\\"')}") if value end def css_selector_to_xpath(selector) selector.map do |element| pseudo_classes = Set.new(element[:pseudo_class]) require_function_name = false result = case element[:combinator] when :child - +"/" + +'/' when :adjacent pseudo_classes << 'first-child' - +"/following-sibling::" + +'/following-sibling::' when :siblings - +"/following-sibling::" + +'/following-sibling::' else - +"//" + +'//' end positions = [] pseudo_classes.each do |pc| case pc - when "first-child" then positions << '1' - when "last-child" then positions << 'last()' + when 'first-child' then positions << '1' + when 'last-child' then positions << 'last()' when /^nth-child\((\d+)\)$/ then positions << $1 end end - if !positions.empty? - result << "*" unless require_function_name + unless positions.empty? + result << '*' unless require_function_name require_function_name = true logic = if positions.length == 1 positions.first else - positions.map { |position| "position()=#{position}" }.join(" and ") + positions.map { |position| "position()=#{position}" }.join(' and ') end result << "[#{logic}]" end @@ -119,21 +119,21 @@ (element[:attribute] || []).each do |key, operator, value| case operator when nil result << "[@#{key}]" - when "=" + when '=' result << "[@#{key}=#{xpath_quote value}]" - when "^=" + when '^=' result << "[starts-with(@#{key}, #{xpath_quote value})]" - when "$=" + when '$=' result << "[substring(@#{key}, string-length(@#{key}) - #{value.length - 1}) = #{xpath_quote value})]" - when "*=" + when '*=' result << "[contains(@#{key}, #{xpath_quote value})]" - when "~=" + when '~=' result << "[contains(concat(' ',@#{key},' '), #{xpath_quote " #{value} "})]" - when "|=" + when '|=' result << "[contains(concat('-',@#{key},'-'), #{xpath_quote "-#{value}-"})]" end end result @@ -143,10 +143,10 @@ def calculate_specificity(selector) selector.reduce([0, 0, 0]) do |(a, b, c), element| [ a + (element[:id] || []).length, b + (element[:class] || []).length + (element[:attribute] || []).length + (element[:pseudo_class] || []).length, - c + (element[:name] && element[:name] != "*" ? 1 : 0) + c + (element[:name] && element[:name] != '*' ? 1 : 0) ] end end end end