Transformer: Remove empty elements

Pavel Shpak edited this page Nov 25, 2013 · 3 revisions

This transformer will remove any element that doesn't contain any other nodes or non-whitespace text.

lambda {|env|
  node = env[:node]
  return unless node.elem?

  unless node.children.any?{|c| !c.text? || c.content.strip.length > 0 }
    node.unlink
  end
}