From 1d449bb5ef1783583ec27f4ca2fa0ecd4bf5de49 Mon Sep 17 00:00:00 2001 From: Maurus Decimus <11444311+mdecimus@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:18:47 +0100 Subject: [PATCH] html_to_text: keep the space after a leading element, skip quoted attribute values (fixes #168, fixes #169) --- CHANGELOG.md | 2 + Cargo.toml | 4 ++ benches/html.rs | 74 ++++++++++++++++++++++++++++ resources/decoders/html_to_text.json | 36 ++++++++++++++ src/decoders/html/mod.rs | 2 +- src/decoders/html/text.rs | 23 +++++++-- src/decoders/preview.rs | 2 +- 7 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 benches/html.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index fbbfa94..ea55c03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file. This projec ### Fixed - An address without angle brackets whose local part is a quoted string, such as `"john smith"@example.com`, was read as the display name `john smith` and the address `@example.com` (#166). - A quoted local part inside angle brackets lost its backslashes, so `<"a\"b"@example.com>` gave the address `"a"b"@example.com`. A quoted-pair now stays in the address as written (#167). +- `html_to_text` dropped the space after an element that started a line, so `Hello world` gave `Helloworld`, and put a stray space at the start of the next paragraph (#168). +- A `>` or `<` inside a quoted attribute value ended the tag, so `a > b` leaked `b">` into the text (#169). ## [1.0.1] - 2026-11-02 diff --git a/Cargo.toml b/Cargo.toml index 268172a..022e2ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,10 @@ harness = false name = "text_prefix" harness = false +[[bench]] +name = "html" +harness = false + [lints.rust] rust_2018_idioms = { level = "deny", priority = -1 } unsafe_code = "deny" diff --git a/benches/html.rs b/benches/html.rs new file mode 100644 index 0000000..9b07b04 --- /dev/null +++ b/benches/html.rs @@ -0,0 +1,74 @@ +/* + * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use mail_parser::html_to_text; +use std::{hint::black_box, time::Duration}; + +const SIZES: [(&str, usize); 3] = [("4k", 4 << 10), ("64k", 64 << 10), ("1m", 1 << 20)]; + +const HEAD: &str = "Weekly\ +\ +"; +const TAIL: &str = ""; + +const NEWSLETTER: &[&str] = &[ + "\ +
New arrivals this week for you & your team
\n", + "

Hello Jane, here is what changed since \ +your last visit.
See the full list online.

\n", + "\"Logo brand\" width=\"120\" height=\"40\">\n", + "
Prices start at 10€ and ship in 2–3 days.
\n", + "

Questions? Reply to this message.

\n", +]; + +const PLAIN: &[&str] = &[ + "

The quick brown fox jumps over the lazy dog, and then keeps running down the road.

\n", + "

Plain paragraphs with few tags and no attributes at all, mostly text.
Second line.

\n", +]; + +fn document(blocks: &[&str], size: usize) -> String { + let mut html = String::with_capacity(size + HEAD.len() + TAIL.len() + 512); + html.push_str(HEAD); + for block in blocks.iter().cycle() { + if html.len() >= size { + break; + } + html.push_str(block); + } + html.push_str(TAIL); + html +} + +fn html(c: &mut Criterion) { + let mut group = c.benchmark_group("html_to_text"); + for (kind, blocks) in [("newsletter", NEWSLETTER), ("plain", PLAIN)] { + for (size_name, size) in SIZES { + let input = document(blocks, size); + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_function(BenchmarkId::new(kind, size_name), |b| { + b.iter(|| html_to_text(black_box(&input))) + }); + } + } + group.finish(); +} + +fn config() -> Criterion { + Criterion::default() + .warm_up_time(Duration::from_secs(1)) + .measurement_time(Duration::from_secs(3)) + .sample_size(50) + .noise_threshold(0.02) +} + +criterion_group! { + name = benches; + config = config(); + targets = html +} +criterion_main!(benches); diff --git a/resources/decoders/html_to_text.json b/resources/decoders/html_to_text.json index 593d5db..9b331e6 100644 --- a/resources/decoders/html_to_text.json +++ b/resources/decoders/html_to_text.json @@ -94,5 +94,41 @@ { "input": "before