svg_optimizer 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of svg_optimizer might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3f96c3fad6ba2b506bf3f6cf7b36eb040944125
4
- data.tar.gz: 268be369b5313dbce1c1dbde5ff5bc8bdcaa4838
3
+ metadata.gz: 252e3ad0e74992fe85def8ef8d749d2f55975b6a
4
+ data.tar.gz: 0ab2451d8cf7ee5ec69a0cf6462adcba953575e2
5
5
  SHA512:
6
- metadata.gz: 398a67942d5b8dccb24cbd5b0edd0deadeba51c60f17380ec9846079b05db9b66050271f973ac872c788e84159dd81991b8fb186284151a53b0a7819a7169eaa
7
- data.tar.gz: a63c514ed5b9b288a6cfcc0f72b06f40cbaaed7426d1e7e7873c962fd49770ab9f33146b01fbad3b5459282e75178a9ae39db518a873730591fb3202708d89e0
6
+ metadata.gz: bfd401c30de6a280c6daddc788678f8635f0e179b7119ddc9ead993dbfbd0d08d70c30cf61153a6a6e7ff224dbdfd8b336c0027c65cbb916b8d115e262d966e6
7
+ data.tar.gz: 0db71a133efeaa39992777d7a34fd4c1c309afafb4367fa1b9741fa73af26f4755cdb834ee3f2e8f80948794bf03f3190397e0a24e9e34e6e8395fffca9292a2
@@ -6,7 +6,6 @@ require "svg_optimizer/version"
6
6
  require "svg_optimizer/plugins/base"
7
7
  require "svg_optimizer/plugins/cleanup_attribute"
8
8
  require "svg_optimizer/plugins/cleanup_id"
9
- require "svg_optimizer/plugins/collapse_groups"
10
9
  require "svg_optimizer/plugins/remove_comment"
11
10
  require "svg_optimizer/plugins/remove_metadata"
12
11
  require "svg_optimizer/plugins/remove_empty_text_node"
@@ -22,21 +21,20 @@ require "svg_optimizer/plugins/remove_description"
22
21
 
23
22
  module SvgOptimizer
24
23
  DEFAULT_PLUGINS = %w[
24
+ RemoveTitle
25
+ RemoveDescription
26
+ RemoveUnusedNamespace
25
27
  CleanupAttribute
26
28
  CleanupId
27
29
  RemoveComment
28
30
  RemoveMetadata
29
31
  RemoveEditorNamespace
30
32
  RemoveHiddenElement
31
- RemoveUnusedNamespace
32
33
  RemoveRasterImage
33
34
  RemoveEmptyAttribute
34
- CollapseGroups
35
35
  RemoveUselessStrokeAndFill
36
36
  RemoveEmptyTextNode
37
37
  RemoveEmptyContainer
38
- RemoveTitle
39
- RemoveDescription
40
38
  ].map {|name| Plugins.const_get(name) }
41
39
 
42
40
  def self.optimize(contents, plugins = DEFAULT_PLUGINS)
@@ -3,50 +3,54 @@
3
3
  module SvgOptimizer
4
4
  module Plugins
5
5
  class RemoveEditorNamespace < Base
6
- NAMESPACES = %w[
7
- http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd
8
- http://www.inkscape.org/namespaces/inkscape
9
- http://ns.adobe.com/AdobeIllustrator/10.0/
10
- http://ns.adobe.com/Graphs/1.0/
11
- http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/
12
- http://ns.adobe.com/Variables/1.0/
13
- http://ns.adobe.com/SaveForWeb/1.0/
14
- http://ns.adobe.com/Extensibility/1.0/
15
- http://ns.adobe.com/Flows/1.0/
16
- http://ns.adobe.com/ImageReplacement/1.0/
17
- http://ns.adobe.com/GenericCustomNamespace/1.0/
18
- http://ns.adobe.com/XPath/1.0
19
- http://www.bohemiancoding.com/sketch/ns
6
+ NAMESPACES = [
7
+ "http://ns.adobe.com/AdobeIllustrator/10.0/",
8
+ "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/",
9
+ "http://ns.adobe.com/Extensibility/1.0/",
10
+ "http://ns.adobe.com/Flows/1.0/",
11
+ "http://ns.adobe.com/GenericCustomNamespace/1.0/",
12
+ "http://ns.adobe.com/Graphs/1.0/",
13
+ "http://ns.adobe.com/ImageReplacement/1.0/",
14
+ "http://ns.adobe.com/SaveForWeb/1.0/",
15
+ "http://ns.adobe.com/Variables/1.0/",
16
+ "http://ns.adobe.com/XPath/1.0",
17
+ "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
18
+ "http://www.bohemiancoding.com/sketch/ns",
19
+ "http://www.inkscape.org/namespaces/inkscape"
20
20
  ].freeze
21
21
 
22
22
  def process
23
- namespaces = xml.namespaces
24
- remove_namespaced_attributes
25
- xml.remove_namespaces!
23
+ xml.namespaces.each do |full_name, href|
24
+ _, name = full_name.split(":")
25
+ next unless NAMESPACES.include?(href)
26
26
 
27
- namespaces.each do |name, value|
28
- next if NAMESPACES.include?(value)
27
+ remove_namespaced_attributes(name, href)
28
+ end
29
+
30
+ xml.root.namespace_definitions.each do |namespace|
31
+ remove_namespace(namespace) if NAMESPACES.include?(namespace.href)
32
+ end
33
+ end
34
+
35
+ def remove_namespaced_attributes(name, href)
36
+ xml.xpath("//@*[namespace-uri()='#{href}']").each do |node|
37
+ remove_matching_attribute(node, name)
38
+ end
29
39
 
30
- _, name = name.split(":")
31
- xml.root.add_namespace name, value
40
+ xml.xpath("//*[@#{name}:*]").each do |node|
41
+ remove_matching_attribute(node, name)
32
42
  end
33
43
  end
34
44
 
35
- def namespaces_to_be_removed
36
- xml.namespaces.map do |name, value|
37
- _, name = name.split(":")
38
- name if NAMESPACES.include?(value)
39
- end.compact
45
+ def remove_namespace(namespace)
46
+ source = xml.root.to_s.gsub(/ *xmlns:#{namespace.prefix}=".*?"/, "")
47
+ xml.root = Nokogiri::XML(source).root
40
48
  end
41
49
 
42
- def remove_namespaced_attributes
43
- namespaces_to_be_removed.each do |ns|
44
- xml.xpath("//*[@#{ns}:*]").each do |node|
45
- node.attributes.each do |_, attr|
46
- next unless attr.namespace
47
- attr.remove if attr.namespace.prefix == ns
48
- end
49
- end
50
+ def remove_matching_attribute(node, name)
51
+ node.attributes.each do |_, attr|
52
+ next unless attr.namespace
53
+ attr.remove if attr.namespace.prefix == name
50
54
  end
51
55
  end
52
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SvgOptimizer
4
- VERSION = "0.2.4".freeze
4
+ VERSION = "0.2.5".freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_optimizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -107,7 +107,6 @@ files:
107
107
  - lib/svg_optimizer/plugins/base.rb
108
108
  - lib/svg_optimizer/plugins/cleanup_attribute.rb
109
109
  - lib/svg_optimizer/plugins/cleanup_id.rb
110
- - lib/svg_optimizer/plugins/collapse_groups.rb
111
110
  - lib/svg_optimizer/plugins/remove_comment.rb
112
111
  - lib/svg_optimizer/plugins/remove_description.rb
113
112
  - lib/svg_optimizer/plugins/remove_editor_namespace.rb
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SvgOptimizer
4
- module Plugins
5
- class CollapseGroups < Base
6
- COLLAPSABLE_ATTRS = %w[transform class].freeze
7
-
8
- def process
9
- xml.css("g:not(:empty)").each do |group|
10
- collapse(group)
11
- end
12
- end
13
-
14
- private
15
-
16
- def collapse(group)
17
- collapse_attributes(group)
18
- group.swap(group.children) if collapse?(group)
19
- end
20
-
21
- def collapse_attributes(group)
22
- child = group.first_element_child
23
- return unless collapse_attributes?(group, child)
24
-
25
- group.attributes.each do |attr, val|
26
- if COLLAPSABLE_ATTRS.include?(attr)
27
- child[attr] = [val, child[attr]].compact.join(" ")
28
- else
29
- child[attr] ||= val
30
- end
31
-
32
- group.delete(attr)
33
- end
34
- end
35
-
36
- def collapse?(group)
37
- group.attributes.empty? &&
38
- !group.element_children.map(&:name).include?("animate")
39
- end
40
-
41
- def collapse_attributes?(group, child)
42
- return unless group.attributes.any?
43
- return unless group.element_children.length == 1
44
- return if child.has_attribute?("id")
45
- return unless !group.has_attribute?("clip-path") ||
46
- child.name == "g" &&
47
- !group.has_attribute?("transform") &&
48
- !child.has_attribute?("transform")
49
- true
50
- end
51
- end
52
- end
53
- end