Class: SvgOptimizer::Plugins::RemoveEditorNamespace

Inherits:
Base
  • Object
show all
Defined in:
lib/svg_optimizer/plugins/remove_editor_namespace.rb

Constant Summary collapse

NAMESPACES =
%w[
  http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd
  http://www.inkscape.org/namespaces/inkscape
  http://ns.adobe.com/AdobeIllustrator/10.0/
  http://ns.adobe.com/Graphs/1.0/
  http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/
  http://ns.adobe.com/Variables/1.0/
  http://ns.adobe.com/SaveForWeb/1.0/
  http://ns.adobe.com/Extensibility/1.0/
  http://ns.adobe.com/Flows/1.0/
  http://ns.adobe.com/ImageReplacement/1.0/
  http://ns.adobe.com/GenericCustomNamespace/1.0/
  http://ns.adobe.com/XPath/1.0
  http://www.bohemiancoding.com/sketch/ns
]

Instance Attribute Summary

Attributes inherited from Base

#xml

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SvgOptimizer::Plugins::Base

Instance Method Details

#namespaces_to_be_removedObject



35
36
37
38
39
40
# File 'lib/svg_optimizer/plugins/remove_editor_namespace.rb', line 35

def namespaces_to_be_removed
  xml.namespaces.map do |name, value|
    _, name = name.split(":")
    name if NAMESPACES.include?(value)
  end.compact
end

#processObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/svg_optimizer/plugins/remove_editor_namespace.rb', line 22

def process
  namespaces = xml.namespaces
  remove_namespaced_attributes
  xml.remove_namespaces!

  namespaces.each do |name, value|
    next if NAMESPACES.include?(value)

    _, name = name.split(":")
    xml.root.add_namespace name, value
  end
end

#remove_namespaced_attributesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/svg_optimizer/plugins/remove_editor_namespace.rb', line 42

def remove_namespaced_attributes
  namespaces_to_be_removed.each do |ns|
    xml.xpath("//*[@#{ns}:*]").each do |node|
      node.attributes.each do |_, attr|
        next unless attr.namespace
        attr.remove if attr.namespace.prefix == ns
      end
    end
  end
end