From 07de5c2f65d31d079a25913c6574a1dbda095cce Mon Sep 17 00:00:00 2001 From: Fabian Schwahn Date: Fri, 20 Nov 2026 11:29:09 +0100 Subject: [PATCH] Convert many sibling divs in linear time A nested div gets no leading line break if it has all of its parent's text, which was checked by comparing the parent's text with the div's. Computing the parent's text again for every child div made converting documents with many sibling divs quadratic: 625 KB of sibling divs took 13 seconds. The stripped text lengths are now compared first, with the parent's length computed once. 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. --- CHANGELOG.md | 2 ++ lib/html2text.rb | 18 +++++++++++++++++- spec/nested_divs_spec.rb | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 spec/nested_divs_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f09a90..33c10b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `
`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 diff --git a/lib/html2text.rb b/lib/html2text.rb index e6dca85..eb43d39 100644 --- a/lib/html2text.rb +++ b/lib/html2text.rb @@ -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" @@ -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 + @stripped_text_lengths[node] ||= node.text.strip.length + end + def next_node_name(node) next_node = node.next_sibling until next_node.nil? diff --git a/spec/nested_divs_spec.rb b/spec/nested_divs_spec.rb new file mode 100644 index 0000000..20e390c --- /dev/null +++ b/spec/nested_divs_spec.rb @@ -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
a
')).to eq("x\na") + expect(Html2Text.convert('x
a
')).to eq("x\na") + end + + it 'ignores comments next to the div' do + expect(Html2Text.convert('x
a
')).to eq("x\na") + end + + it 'adds a line break for a div with text next to it' do + expect(Html2Text.convert('x
a
b
')).to eq("x\n\na\nb") + end + + it 'converts many sibling divs in linear time' do + html = "
#{'
line
' * 50_000}
" + + 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 = "
#{'
' * 50_000}x
" + + 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