Sha256: 1829ac4e590c5bddd525bbabffe84e723834f72b46a24e409731db4eedc3075b

Contents?: true

Size: 509 Bytes

Versions: 8

Compression:

Stored size: 509 Bytes

Contents

module InlineSvg::TransformPipeline::Transformations
  class DataAttributes < Transformation
    def transform(doc)
      doc = Nokogiri::XML::Document.parse(doc.to_html)
      svg = doc.at_css 'svg'
      with_valid_hash_from(self.value).each_pair do |name, data|
        svg["data-#{dasherize(name)}"] = data
      end
      doc
    end

    private

    def with_valid_hash_from(hash)
      Hash.try_convert(hash) || {}
    end

    def dasherize(string)
      string.to_s.gsub(/_/, "-")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
inline_svg-1.2.1 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-1.2.0 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-1.1.0 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-1.0.1 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-1.0.0 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-0.12.1 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-0.12.0 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb
inline_svg-0.11.1 lib/inline_svg/transform_pipeline/transformations/data_attributes.rb