Skip to content

Commit 1a1e2e0

Browse files
authored
Merge pull request #150 from fredboyle/fix/remove_extra_space_from_output
fix: remove any whitespace from SVG tag output
2 parents 3500196 + 11469cd commit 1a1e2e0

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

lib/inline_svg/transform_pipeline.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def self.generate_html_from(svg_file, transform_params)
44
document = Nokogiri::XML::Document.parse(svg_file)
55
Transformations.lookup(transform_params).reduce(document) do |doc, transformer|
66
transformer.transform(doc)
7-
end.to_html
7+
end.to_html.strip
88
end
99
end
1010
end

spec/helpers/inline_svg_spec.rb

+28-46
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def transform(doc)
8686
with('missing.svg').
8787
and_raise(InlineSvg::AssetFile::FileNotFound.new)
8888

89-
fallback_file = <<-SVG
90-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>
91-
SVG
89+
fallback_file = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>'
9290
allow(InlineSvg::AssetFile).to receive(:named).with('fallback.svg').and_return(fallback_file)
9391
expect(helper.send(helper_method, 'missing.svg', fallback: 'fallback.svg')).to eq fallback_file
9492
end
@@ -99,74 +97,52 @@ def transform(doc)
9997

10098
context "and no options" do
10199
it "returns a html safe version of the file's contents" do
102-
example_file = <<-SVG
103-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>
104-
SVG
100+
example_file = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>'
105101
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(example_file)
106102
expect(helper.send(helper_method, 'some-file')).to eq example_file
107103
end
108104
end
109105

110106
context "and the 'title' option" do
111107
it "adds the title node to the SVG output" do
112-
input_svg = <<-SVG
113-
<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
114-
SVG
115-
expected_output = <<-SVG
116-
<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><title>A title</title></svg>
117-
SVG
108+
input_svg = '<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>'
109+
expected_output = '<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><title>A title</title></svg>'
118110
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
119111
expect(helper.send(helper_method, 'some-file', title: 'A title')).to eq expected_output
120112
end
121113
end
122114

123115
context "and the 'desc' option" do
124116
it "adds the description node to the SVG output" do
125-
input_svg = <<-SVG
126-
<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>
127-
SVG
128-
expected_output = <<-SVG
129-
<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><desc>A description</desc></svg>
130-
SVG
117+
input_svg = '<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"></svg>'
118+
expected_output = '<svg xmlns="http://www.w3.org/2000/svg" role="presentation" xml:lang="en"><desc>A description</desc></svg>'
131119
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
132120
expect(helper.send(helper_method, 'some-file', desc: 'A description')).to eq expected_output
133121
end
134122
end
135123

136124
context "and the 'nocomment' option" do
137125
it "strips comments and other unknown/unsafe nodes from the output" do
138-
input_svg = <<-SVG
139-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>
140-
SVG
141-
expected_output = <<-SVG
142-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>
143-
SVG
126+
input_svg = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>'
127+
expected_output = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>'
144128
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
145129
expect(helper.send(helper_method, 'some-file', nocomment: true)).to eq expected_output
146130
end
147131
end
148132

149133
context "and the 'aria_hidden' option" do
150134
it "sets 'aria-hidden=true' in the output" do
151-
input_svg = <<-SVG
152-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>
153-
SVG
154-
expected_output = <<-SVG
155-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en" aria-hidden="true"></svg>
156-
SVG
135+
input_svg = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"></svg>'
136+
expected_output = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en" aria-hidden="true"></svg>'
157137
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
158138
expect(helper.send(helper_method, 'some-file', aria_hidden: true)).to eq expected_output
159139
end
160140
end
161141

162142
context "and all options" do
163143
it "applies all expected transformations to the output" do
164-
input_svg = <<-SVG
165-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>
166-
SVG
167-
expected_output = <<-SVG
168-
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><title>A title</title><desc>A description</desc></svg>
169-
SVG
144+
input_svg = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>'
145+
expected_output = '<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><title>A title</title><desc>A description</desc></svg>'
170146
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
171147
expect(helper.send(helper_method, 'some-file', title: 'A title', desc: 'A description', nocomment: true)).to eq expected_output
172148
end
@@ -184,12 +160,8 @@ def transform(doc)
184160
end
185161

186162
it "applies custm transformations to the output" do
187-
input_svg = <<-SVG
188-
<svg></svg>
189-
SVG
190-
expected_output = <<-SVG
191-
<svg custom="some value"></svg>
192-
SVG
163+
input_svg = '<svg></svg>'
164+
expected_output = '<svg custom="some value"></svg>'
193165
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
194166
expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq expected_output
195167
end
@@ -212,7 +184,7 @@ def transform(doc)
212184

213185
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
214186

215-
expect(helper.send(helper_method, 'some-file')).to eq "<svg custom=\"default value\"></svg>\n"
187+
expect(helper.send(helper_method, 'some-file')).to eq "<svg custom=\"default value\"></svg>"
216188
end
217189
end
218190

@@ -222,7 +194,7 @@ def transform(doc)
222194

223195
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
224196

225-
expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq "<svg custom=\"some value\"></svg>\n"
197+
expect(helper.send(helper_method, 'some-file', custom: 'some value')).to eq "<svg custom=\"some value\"></svg>"
226198
end
227199
end
228200
end
@@ -251,17 +223,27 @@ def transform(doc)
251223
expect(InlineSvg::IOResource).to receive(:===).with(io_object).and_return(true)
252224
expect(InlineSvg::IOResource).to receive(:read).with(io_object).and_return("<svg><!-- Test IO --></svg>")
253225
output = helper.send(helper_method, io_object)
254-
expect(output).to eq "<svg><!-- Test IO --></svg>\n"
226+
expect(output).to eq "<svg><!-- Test IO --></svg>"
255227
expect(output).to be_html_safe
256228
end
257229

258230
it 'return valid svg for file' do
259231
output = helper.send(helper_method, File.new(file_path))
260-
expect(output).to eq "<svg xmlns=\"http://www.w3.org/2000/svg\" xml:lang=\"en\" role=\"presentation\"><!-- This is a test comment --></svg>\n"
232+
expect(output).to eq "<svg xmlns=\"http://www.w3.org/2000/svg\" xml:lang=\"en\" role=\"presentation\"><!-- This is a test comment --></svg>"
261233
expect(output).to be_html_safe
262234
end
263235

264236
end
237+
238+
context 'default output' do
239+
it "returns an SVG tag without any pre or post whitespace characters" do
240+
input_svg = '<svg></svg>'
241+
242+
allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
243+
244+
expect(helper.send(helper_method, 'some-file')).to eq "<svg></svg>"
245+
end
246+
end
265247
end
266248

267249
describe '#inline_svg' do

0 commit comments

Comments
 (0)