Module: InlineSvg::ActionView::Helpers

Defined in:
lib/inline_svg/action_view/helpers.rb

Instance Method Summary collapse

Instance Method Details

#inline_svg(filename, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inline_svg/action_view/helpers.rb', line 9

def inline_svg(filename, options={})
  file = File.read(Rails.root.join('app', 'assets', 'images', filename))
  doc = Nokogiri::HTML::DocumentFragment.parse file

  # remove comments from svg file
  if options[:nocomment].present?
    doc = Loofah.fragment(doc.to_s).scrub!(:strip)
  end

  svg = doc.at_css 'svg'
  if options[:class]
    svg['class'] = options[:class]
  end
  if options[:title].present?
    title = Nokogiri::XML::Node.new("title", doc)
    title.content = options[:title]
    svg.add_child title
  end
  if options[:desc].present?
    desc = Nokogiri::XML::Node.new("desc", doc)
    desc.content = options[:desc]
    svg.add_child desc
  end
  doc.to_html.html_safe
end