Class: SvgOptimizer::Plugins::RemoveEditorNamespace

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

Constant Summary collapse

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

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

#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
  xml.namespaces.each do |full_name, href|
    _, name = full_name.split(":")
    next unless NAMESPACES.include?(href)

    remove_namespaced_attributes(name, href)
  end

  xml.root.namespace_definitions.each do |namespace|
    remove_namespace(namespace) if NAMESPACES.include?(namespace.href)
  end
end

#remove_matching_attribute(node, name) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/svg_optimizer/plugins/remove_editor_namespace.rb', line 50

def remove_matching_attribute(node, name)
  node.attributes.each do |_, attr|
    next unless attr.namespace

    attr.remove if attr.namespace.prefix == name
  end
end

#remove_namespace(namespace) ⇒ Object



45
46
47
48
# File 'lib/svg_optimizer/plugins/remove_editor_namespace.rb', line 45

def remove_namespace(namespace)
  source = xml.root.to_s.gsub(/ *xmlns:#{namespace.prefix}=".*?"/, "")
  xml.root = Nokogiri::XML(source).root
end

#remove_namespaced_attributes(name, href) ⇒ Object



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

def remove_namespaced_attributes(name, href)
  xml.xpath("//@*[namespace-uri()='#{href}']").each do |node|
    remove_matching_attribute(node, name)
  end

  xml.xpath("//*[@#{name}:*]").each do |node|
    remove_matching_attribute(node, name)
  end
end