Sha256: 8eef318aa7b1c6c5ffbd6c8a0c708c20d0269a4ddc3eb2cc4b6da80f8d610f37

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

class SvgSprite
  class SVG
    attr_reader :filepath, :name, :optimize, :stroke, :fill

    def initialize(filepath, name:, optimize:, stroke:, fill:)
      @name = name
      @filepath = filepath
      @optimize = optimize
      @stroke = stroke
      @fill = fill
    end

    def symbol
      @symbol ||= xml.css("svg").first.clone.tap do |node|
        node.name = "symbol"
        node.set_attribute :id, id

        process_stroke(node)
        process_fill(node)
      end
    end

    def width
      symbol[:width]
    end

    def height
      symbol[:height]
    end

    def id
      @id ||= [name, File.basename(filepath, ".*")].join("--")
    end

    private def xml
      @xml ||= begin
        contents = File.read(filepath)
        contents = SvgOptimizer.optimize(contents) if optimize
        Nokogiri::XML(contents)
      end
    end

    private def process_stroke(node)
      process_attribute(node, "stroke", stroke)
    end

    private def process_fill(node)
      process_attribute(node, "fill", fill)
    end

    private def process_attribute(symbol, attribute, value)
      return unless value

      symbol.css("[#{attribute}]").each do |node|
        if value == "current-color" && node[attribute] != "none"
          node.set_attribute(attribute, "currentColor")
        end

        if value == "remove" && node[attribute] != "none"
          node.remove_attribute(attribute)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
svg_sprite-1.0.1 lib/svg_sprite/svg.rb