Skip to content

Commit 4ff340c

Browse files
committed
add a basic check for names of enabled and disabled svgo plugins
1 parent 517f372 commit 4ff340c

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Correct environment variable to specify `jpeg-recompress` location [@toy](https://github.com/toy)
77
* Added --benchmark, to compare performance of each tool [#217](https://github.com/toy/image_optim/issues/217) [#218](https://github.com/toy/image_optim/pull/218) [@gurgeous](https://github.com/gurgeous)
88
* Don't require presence of `git` in gemspec [@toy](https://github.com/toy)
9+
* Add a basic check for names of enabled and disabled svgo plugins [@toy](https://github.com/toy)
910

1011
## v0.31.4 (2024-11-19)
1112

lib/image_optim/worker/svgo.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ class ImageOptim
77
class Worker
88
# https://github.com/svg/svgo
99
class Svgo < Worker
10+
PLUGIN_NAME_R = /\A[a-zA-Z]+\z/.freeze
11+
1012
DISABLE_PLUGINS_OPTION =
1113
option(:disable_plugins, [], 'List of plugins to disable') do |v|
12-
Array(v).map(&:to_s)
14+
parse_plugin_names(v)
1315
end
1416

1517
ENABLE_PLUGINS_OPTION =
1618
option(:enable_plugins, [], 'List of plugins to enable') do |v|
17-
Array(v).map(&:to_s)
19+
parse_plugin_names(v)
1820
end
1921

2022
ALLOW_LOSSY_OPTION =
@@ -49,6 +51,18 @@ def optimize(src, dst, options = {})
4951
args.unshift "--precision=#{precision}" if allow_lossy
5052
execute(:svgo, args, options) && optimized?(src, dst)
5153
end
54+
55+
private
56+
57+
def parse_plugin_names(value)
58+
Array(value).map(&:to_s).select do |name|
59+
if name =~ PLUGIN_NAME_R
60+
true
61+
else
62+
warn "Doesn't look like svgo plugin name: #{name}"
63+
end
64+
end
65+
end
5266
end
5367
end
5468
end

spec/image_optim/worker/svgo_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44
require 'image_optim/worker/svgo'
55

66
describe ImageOptim::Worker::Svgo do
7+
%i[
8+
disable_plugins
9+
enable_plugins
10+
].each do |option|
11+
describe "#{option} option" do
12+
let(:subject){ described_class.new(ImageOptim.new, value).send(option) }
13+
14+
context 'default' do
15+
let(:value){ {} }
16+
17+
it{ is_expected.to eq([]) }
18+
end
19+
20+
context 'when passed single valid value' do
21+
let(:value){ {option => :pluginName} }
22+
23+
it 'converts it to a string array' do
24+
is_expected.to eq(%w[pluginName])
25+
end
26+
end
27+
28+
context 'when passed multiple valid values' do
29+
let(:value){ {option => %i[pluginName anotherName]} }
30+
31+
it 'converts them to a string array' do
32+
is_expected.to eq(%w[pluginName anotherName])
33+
end
34+
end
35+
36+
context 'when given invalid values' do
37+
let(:value){ {option => %w[1abc pluginName alert() anotherName]} }
38+
39+
it 'warns and skips them' do
40+
expect_any_instance_of(described_class).
41+
to receive(:warn).with('Doesn\'t look like svgo plugin name: 1abc')
42+
expect_any_instance_of(described_class).
43+
to receive(:warn).with('Doesn\'t look like svgo plugin name: alert()')
44+
is_expected.to eq(%w[pluginName anotherName])
45+
end
46+
end
47+
end
48+
end
49+
750
describe 'precision option' do
851
describe 'default' do
952
subject{ described_class::PRECISION_OPTION.default }

0 commit comments

Comments
 (0)