Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/inline_svg/action_view/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ def inline_svg(filename, transform_params = {})
render_inline_svg(filename, transform_params)
end

def inline_svg_data_url(filename, transform_params = {})
svg = render_inline_svg(filename, transform_params)
svg = svg.sub(/<\?xml.*?\?>/, "").strip
svg = svg.gsub(/<!--.*?-->/m, "")
svg = svg.gsub(/\s+/, " ")
base64 = [svg].pack("m0")
"data:image/svg+xml;base64,#{base64}"
end

private

def render_inline_svg(filename, transform_params = {})
Expand Down
72 changes: 72 additions & 0 deletions spec/helpers/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,76 @@ def transform(doc)
describe '#inline_svg_tag' do
it_behaves_like "inline_svg helper", helper_method: :inline_svg_pack_tag
end

describe '#inline_svg_data_url' do
let(:helper) { (Class.new { include InlineSvg::ActionView::Helpers }).new }

it 'returns a base64 data URL for a simple SVG' do
input_svg = '<svg xmlns="http://www.w3.org/2000/svg"></svg>'
allow(InlineSvg::AssetFile).to receive(:named).with('simple.svg').and_return(input_svg)
data_url = helper.inline_svg_data_url('simple.svg')
expect(data_url).to start_with('data:image/svg+xml;base64,')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<svg')
expect(decoded).to include('</svg>')
end

it 'removes XML declaration and minifies whitespace' do
input_svg = "<?xml version=\"1.0\"?><svg> <rect/> </svg>"
allow(InlineSvg::AssetFile).to receive(:named).with('xml.svg').and_return(input_svg)
data_url = helper.inline_svg_data_url('xml.svg')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).not_to include('<?xml')
expect(decoded).to eq('<svg> <rect></rect> </svg>')
end

it 'applies transformations' do
input_svg = '<svg></svg>'
allow(InlineSvg::AssetFile).to receive(:named).with('title.svg').and_return(input_svg)
data_url = helper.inline_svg_data_url('title.svg', title: 'Test Title')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<title>Test Title</title>')
end
end

it 'normalizes all whitespace (tabs, newlines, multiple spaces) to a single space' do
input_svg = "<svg>\n\t <rect/>\n\t</svg>"
allow(InlineSvg::AssetFile).to receive(:named).with('whitespace.svg').and_return(input_svg)
data_url = helper.inline_svg_data_url('whitespace.svg')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to eq('<svg> <rect></rect> </svg>')
end

it 'removes comments from SVG content' do
input_svg = '<svg><!-- comment --><rect/></svg>'
allow(InlineSvg::AssetFile).to receive(:named).with('comment.svg').and_return(input_svg)
data_url = helper.inline_svg_data_url('comment.svg')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<svg><rect></rect></svg>')
expect(decoded).not_to include('<!--')
end

it 'returns a base64 data URL for fallback SVG if original is missing' do
allow(InlineSvg::AssetFile).to receive(:named).with('missing.svg').and_raise(InlineSvg::AssetFile::FileNotFound.new)
fallback_svg = '<svg><rect/></svg>'
allow(InlineSvg::AssetFile).to receive(:named).with('fallback.svg').and_return(fallback_svg)
data_url = helper.inline_svg_data_url('missing.svg', fallback: 'fallback.svg')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<svg><rect></rect></svg>')
end

it 'returns a placeholder SVG as base64 data URL if file is missing and no fallback' do
allow(InlineSvg::AssetFile).to receive(:named).with('missing.svg').and_raise(InlineSvg::AssetFile::FileNotFound.new)
data_url = helper.inline_svg_data_url('missing.svg')
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<svg></svg>')
end

it 'escapes malicious input in placeholder SVG as base64 data URL' do
malicious = "--></svg><script>alert(1)</script><svg>.svg"
allow(InlineSvg::AssetFile).to receive(:named).with(malicious).and_raise(InlineSvg::AssetFile::FileNotFound.new)
data_url = helper.inline_svg_data_url(malicious)
decoded = data_url.sub('data:image/svg+xml;base64,', '').unpack1('m0')
expect(decoded).to include('<svg></svg>')
end
end