Module: NokogiriTruncator::NodeWithChildren

Defined in:
app/helpers/effective_truncate_html_helper.rb

Instance Method Summary collapse

Instance Method Details

#truncate_html(max_length) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/effective_truncate_html_helper.rb', line 49

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

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