Module: HpricotTruncator::NodeWithChildren

Defined in:
vendor/plugins/refinery/lib/refinery/html_truncation_helper.rb

Instance Method Summary collapse

Instance Method Details

#truncate(max_length) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'vendor/plugins/refinery/lib/refinery/html_truncation_helper.rb', line 74

def truncate(max_length)
  return self if inner_text.mb_chars.length <= max_length
  truncated_node = if self.is_a?(Hpricot::Doc)
    self.dup
  else
    # only pass self.attributes if it's able to use map, otherwise nil works. (Fix for Hpricot 0.8.2)
    self.class.send(:new, self.name, self.attributes.respond_to?("map") ? self.attributes : nil)
  end
  truncated_node.children = []
  each_child do |node|
    remaining_length = max_length - truncated_node.inner_text.mb_chars.length
    break if remaining_length <= 0
    truncated_node.children << node.truncate(remaining_length)
  end
  truncated_node
end