diff --git a/lib/inline_svg/transform_pipeline.rb b/lib/inline_svg/transform_pipeline.rb index 4c6c351..965a51e 100644 --- a/lib/inline_svg/transform_pipeline.rb +++ b/lib/inline_svg/transform_pipeline.rb @@ -4,7 +4,7 @@ def self.generate_html_from(svg_file, transform_params) document = Nokogiri::XML::Document.parse(svg_file) Transformations.lookup(transform_params).reduce(document) do |doc, transformer| transformer.transform(doc) - end.to_html + end.to_html.strip end end end diff --git a/spec/helpers/inline_svg_spec.rb b/spec/helpers/inline_svg_spec.rb index b83346b..190f7f3 100644 --- a/spec/helpers/inline_svg_spec.rb +++ b/spec/helpers/inline_svg_spec.rb @@ -86,9 +86,7 @@ def transform(doc) with('missing.svg'). and_raise(InlineSvg::AssetFile::FileNotFound.new) - fallback_file = <<-SVG - -SVG + fallback_file = '' allow(InlineSvg::AssetFile).to receive(:named).with('fallback.svg').and_return(fallback_file) expect(helper.send(helper_method, 'missing.svg', fallback: 'fallback.svg')).to eq fallback_file end @@ -99,9 +97,7 @@ def transform(doc) context "and no options" do it "returns a html safe version of the file's contents" do - example_file = <<-SVG - -SVG + example_file = '' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(example_file) expect(helper.send(helper_method, 'some-file')).to eq example_file end @@ -109,12 +105,8 @@ def transform(doc) context "and the 'title' option" do it "adds the title node to the SVG output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG -A title -SVG + input_svg = '' + expected_output = 'A title' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', title: 'A title')).to eq expected_output end @@ -122,12 +114,8 @@ def transform(doc) context "and the 'desc' option" do it "adds the description node to the SVG output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG -A description -SVG + input_svg = '' + expected_output = 'A description' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', desc: 'A description')).to eq expected_output end @@ -135,12 +123,8 @@ def transform(doc) context "and the 'nocomment' option" do it "strips comments and other unknown/unsafe nodes from the output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG - -SVG + input_svg = '' + expected_output = '' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', nocomment: true)).to eq expected_output end @@ -148,12 +132,8 @@ def transform(doc) context "and the 'aria_hidden' option" do it "sets 'aria-hidden=true' in the output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG - -SVG + input_svg = '' + expected_output = '' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', aria_hidden: true)).to eq expected_output end @@ -161,12 +141,8 @@ def transform(doc) context "and all options" do it "applies all expected transformations to the output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG -A titleA description -SVG + input_svg = '' + expected_output = 'A titleA description' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', title: 'A title', desc: 'A description', nocomment: true)).to eq expected_output end @@ -184,12 +160,8 @@ def transform(doc) end it "applies custm transformations to the output" do - input_svg = <<-SVG - -SVG - expected_output = <<-SVG - -SVG + input_svg = '' + expected_output = '' allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq expected_output end @@ -212,7 +184,7 @@ def transform(doc) allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) - expect(helper.send(helper_method, 'some-file')).to eq "\n" + expect(helper.send(helper_method, 'some-file')).to eq "" end end @@ -222,7 +194,7 @@ def transform(doc) allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) - expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq "\n" + expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq "" end end end @@ -251,17 +223,27 @@ def transform(doc) expect(InlineSvg::IOResource).to receive(:===).with(io_object).and_return(true) expect(InlineSvg::IOResource).to receive(:read).with(io_object).and_return("") output = helper.send(helper_method, io_object) - expect(output).to eq "\n" + expect(output).to eq "" expect(output).to be_html_safe end it 'return valid svg for file' do output = helper.send(helper_method, File.new(file_path)) - expect(output).to eq "\n" + expect(output).to eq "" expect(output).to be_html_safe end end + + context 'default output' do + it "returns an SVG tag without any pre or post whitespace characters" do + input_svg = '' + + allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg) + + expect(helper.send(helper_method, 'some-file')).to eq "" + end + end end describe '#inline_svg' do