Class: SvgOptimizer::Plugins::RemoveUnusedNamespace

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

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



6
7
8
9
10
11
12
# File 'lib/svg_optimizer/plugins/remove_unused_namespace.rb', line 6

def process
  xml
    .root
    .namespace_definitions
    .select(&:prefix)
    .each(&method(:remove_unused_ns))
end

#remove_unused_ns(ns) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/svg_optimizer/plugins/remove_unused_namespace.rb', line 14

def remove_unused_ns(ns)
  return if xml.xpath("//#{ns.prefix}:*").any?
  return if xml.xpath("//@*[namespace-uri()='#{ns.href}']").any?

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