class SvgOptimizer::Plugins::RemoveEditorNamespace

Constants

NAMESPACES

Public Instance Methods

process() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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