Skip to content

Transformer: Remove empty elements

rgrove edited this page Feb 3, 2011 · 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 || !c.text? }
    node.unlink
  end
}

Clone this wiki locally