class SvgOptimizer::Plugins::RemoveUselessStrokeAndFill

Constants

FILL_ATTRS
SELECTOR
STROKE_ATTRS

Public Instance Methods

process() click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 10
def process
  return if xml.css("style, script").any?

  xml.css(SELECTOR).each do |node|
    remove_useless_attributes(node)
  end
end

Private Instance Methods

decline_inherited_fill?(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 40
def decline_inherited_fill?(node)
  !inherited_attribute(node, "fill") || node.has_attribute?("fill")
end
decline_inherited_stroke?(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 36
def decline_inherited_stroke?(node)
  (inherited_attribute(node.parent, "stroke") || "none") != "none"
end
inherited_attribute(node, attr) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 60
def inherited_attribute(node, attr)
  return if node.nil? || node.document?
  return node[attr] if node.has_attribute?(attr)

  inherited_attribute(node.parent, attr)
end
remove_fill(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 31
def remove_fill(node)
  FILL_ATTRS.each {|attr| node.delete(attr) }
  node["fill"] = "none" if decline_inherited_fill?(node)
end
remove_fill?(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 51
def remove_fill?(node)
  fill = inherited_attribute(node, "fill")

  return true if fill == "none"
  return true if inherited_attribute(node, "fill-opacity") == "0"

  !fill
end
remove_stroke(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 26
def remove_stroke(node)
  STROKE_ATTRS.each {|attr| node.delete(attr) }
  node["stroke"] = "none" if decline_inherited_stroke?(node)
end
remove_stroke?(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 44
def remove_stroke?(node)
  return true if (inherited_attribute(node, "stroke") || "none") == "none"
  return true if inherited_attribute(node, "stroke-opacity") == "0"

  inherited_attribute(node, "stroke-width") == "0"
end
remove_useless_attributes(node) click to toggle source
# File lib/svg_optimizer/plugins/remove_useless_stroke_and_fill.rb, line 20
def remove_useless_attributes(node)
  return if inherited_attribute(node, "id")
  remove_stroke(node) if remove_stroke?(node)
  remove_fill(node) if remove_fill?(node)
end