Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/soundasleep/html2text_ruby/compare/v0.5.0...master)
### Changed
- Convert documents with many sibling `<div>`s in linear instead of quadratic time: the stripped text lengths of a nested div and its parent are compared first, so the parent's text is no longer computed for every div (e.g. 625 KB of sibling divs: 13 s → 0.2 s)

## [0.5.0](https://github.com/soundasleep/html2text_ruby/compare/v0.4.0...v0.5.0) - 2026-07-08
### Added
Expand Down
18 changes: 17 additions & 1 deletion lib/html2text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def prefix_whitespace(node)
"\n"

when 'div'
if node.parent.name == 'div' && (node.parent.text.strip == node.text.strip)
if node.parent.name == 'div' && same_text_as_parent?(node)
''
else
"\n"
Expand Down Expand Up @@ -216,6 +216,22 @@ def image_text(node)
end
end

# Comparing the lengths first avoids computing the parent's text for every
# child div, which made converting many sibling divs quadratic. If a
# sibling has text, the parent's text is longer than the div's, so the texts
# are only compared for at most one child per parent (and not at all when
# both are empty).
def same_text_as_parent?(node)
text = node.text.strip
length = stripped_text_length(node.parent)
text.length == length && (length.zero? || node.parent.text.strip == text)
end

def stripped_text_length(node)
@stripped_text_lengths ||= {}.compare_by_identity

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably should reset this at the start of convert calls?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually on second thought, I don't think we need to

@stripped_text_lengths[node] ||= node.text.strip.length
end

def next_node_name(node)
next_node = node.next_sibling
until next_node.nil?
Expand Down
41 changes: 41 additions & 0 deletions spec/nested_divs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

describe Html2Text, 'nested divs' do
it 'adds no line break for a div that has all the text of its parent div' do
expect(Html2Text.convert('x<div><div>a</div></div>')).to eq("x\na")
expect(Html2Text.convert('<b>x</b><div> <div>a</div> </div>')).to eq("x\na")
end

it 'ignores comments next to the div' do
expect(Html2Text.convert('<span>x</span><div><div>a</div><!-- c --></div>')).to eq("x\na")
end

it 'adds a line break for a div with text next to it' do
expect(Html2Text.convert('<span>x</span><div><div>a</div>b</div>')).to eq("x\n\na\nb")
end

it 'converts many sibling divs in linear time' do
html = "<div>#{'<div>line</div>' * 50_000}</div>"

started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
text = Html2Text.convert(html)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started

expect(text.lines.size).to eq(50_000)
# Took tens of seconds when the parent's text was computed for every div
expect(elapsed).to be < 5
end

it 'converts many empty sibling divs in linear time' do
html = "<div>#{'<div> </div>' * 50_000}x</div>"

started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
text = Html2Text.convert(html)
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started

expect(text).to eq('x')
expect(elapsed).to be < 5
end
end
Loading