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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 252e3ad0e74992fe85def8ef8d749d2f55975b6a
|
4
|
+
data.tar.gz: 0ab2451d8cf7ee5ec69a0cf6462adcba953575e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfd401c30de6a280c6daddc788678f8635f0e179b7119ddc9ead993dbfbd0d08d70c30cf61153a6a6e7ff224dbdfd8b336c0027c65cbb916b8d115e262d966e6
|
7
|
+
data.tar.gz: 0db71a133efeaa39992777d7a34fd4c1c309afafb4367fa1b9741fa73af26f4755cdb834ee3f2e8f80948794bf03f3190397e0a24e9e34e6e8395fffca9292a2
|
data/lib/svg_optimizer.rb
CHANGED
@@ -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 =
|
7
|
-
http://
|
8
|
-
http://
|
9
|
-
http://ns.adobe.com/
|
10
|
-
http://ns.adobe.com/
|
11
|
-
http://ns.adobe.com/
|
12
|
-
http://ns.adobe.com/
|
13
|
-
http://ns.adobe.com/
|
14
|
-
http://ns.adobe.com/
|
15
|
-
http://ns.adobe.com/
|
16
|
-
http://ns.adobe.com/
|
17
|
-
http://
|
18
|
-
http://
|
19
|
-
http://www.
|
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
|
24
|
-
|
25
|
-
|
23
|
+
xml.namespaces.each do |full_name, href|
|
24
|
+
_, name = full_name.split(":")
|
25
|
+
next unless NAMESPACES.include?(href)
|
26
26
|
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
40
|
+
xml.xpath("//*[@#{name}:*]").each do |node|
|
41
|
+
remove_matching_attribute(node, name)
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
35
|
-
def
|
36
|
-
xml.
|
37
|
-
|
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
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
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
|
+
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
|