Sha256: 62bee83e29f33b2e03ed4a0cde482b62b637b7c84d8cc6f2af079098f34de083

Contents?: true

Size: 872 Bytes

Versions: 1

Compression:

Stored size: 872 Bytes

Contents

require 'nokogiri'
require 'securerandom'

module DragonflySvg
  module Processors
    class ExtendIds
      def call(content, append_str = SecureRandom.urlsafe_base64(8), options = {})
        raise UnsupportedFormat unless content.ext
        raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext.downcase)

        doc = Nokogiri::XML(content.data)

        # nodes with id attributes
        doc.xpath('//*[@id]').each do |node|
          node_id = node.get_attribute 'id'
          node.set_attribute 'id', [node_id, append_str].join('-')
        end

        # nodes with id references
        doc.xpath('//*[@href]').each do |node|
          node_href = node.get_attribute 'href'
          node.set_attribute 'href', [node_href, append_str].join('-')
        end

        content.update(doc.to_xml, 'name' => 'temp.svg')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dragonfly_svg-1.0.1 lib/dragonfly_svg/processors/extend_ids.rb