Module: NokogiriTruncator::NodeWithChildren

Defined in:
app/helpers/postablr/truncate_html_helper.rb

Instance Method Summary collapse

Instance Method Details

#truncate(max_length) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/postablr/truncate_html_helper.rb', line 28

def truncate(max_length)
  return self if inner_text.length <= max_length
  truncated_node = self.dup
  truncated_node.children.remove

  self.children.each do |node|
    remaining_length = max_length - truncated_node.inner_text.length
    break if remaining_length <= 0
    truncated_node.add_child node.truncate(remaining_length)
  end
  truncated_node
end