mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-28 08:01:24 +00:00
html_to_text: keep the space after a leading element, skip quoted attribute values (fixes #168, fixes #169)
This commit is contained in:
@@ -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 `<b>Hello</b> 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 `<img alt="a > b">` leaked `b">` into the text (#169).
|
||||
|
||||
## [1.0.1] - 2026-11-02
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
|
||||
*
|
||||
* 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 = "<html><head><meta charset=\"utf-8\"><title>Weekly</title>\
|
||||
<style type=\"text/css\">body{margin:0}td{padding:0 8px;font-family:Arial,sans-serif}</style>\
|
||||
</head><body class=\"main\">";
|
||||
const TAIL: &str = "</body></html>";
|
||||
|
||||
const NEWSLETTER: &[&str] = &[
|
||||
"<table role=\"presentation\" width=\"100%\" cellpadding=\"0\"><tr>\
|
||||
<td style=\"padding:0 8px;color:#333\" class=\"c1\"><a href=\"https://example.com/item?id=42&src=mail\" \
|
||||
title='Open item'><b>New</b> arrivals this week</a> for you & your team</td></tr></table>\n",
|
||||
"<p class=\"intro\" style=\"margin:0 0 12px 0\">Hello <b>Jane</b>, here is what changed since \
|
||||
your last visit.<br>See <a href=\"https://example.com/a\">the full list</a> online.</p>\n",
|
||||
"<img src=\"https://example.com/logo.png\" alt=\"Logo > brand\" width=\"120\" height=\"40\">\n",
|
||||
"<div><span style=\"font-size:14px\">Prices start at 10€ and ship in 2–3 days.</span></div>\n",
|
||||
"<!--[if mso]><table><tr><td><![endif]--><p>Questions? Reply to this message.</p>\n",
|
||||
];
|
||||
|
||||
const PLAIN: &[&str] = &[
|
||||
"<p>The quick brown fox jumps over the lazy dog, and then keeps running down the road.</p>\n",
|
||||
"<p>Plain paragraphs with few tags and no attributes at all, mostly text.<br>Second line.</p>\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);
|
||||
@@ -94,5 +94,41 @@
|
||||
{
|
||||
"input": "before<!-- unterminated --",
|
||||
"expected": "before"
|
||||
},
|
||||
{
|
||||
"input": "<b>Hello</b> world",
|
||||
"expected": "Hello world"
|
||||
},
|
||||
{
|
||||
"input": "<a href=u>link</a> text",
|
||||
"expected": "link text"
|
||||
},
|
||||
{
|
||||
"input": "<p>Name: <b>John</b> Smith</p>",
|
||||
"expected": "Name: John Smith\n"
|
||||
},
|
||||
{
|
||||
"input": "<p>a</p>\n<p>b</p>",
|
||||
"expected": "a\nb\n"
|
||||
},
|
||||
{
|
||||
"input": "<img alt=\"a > b\"> after",
|
||||
"expected": "after"
|
||||
},
|
||||
{
|
||||
"input": "<a title='1>0'>t</a>",
|
||||
"expected": "t"
|
||||
},
|
||||
{
|
||||
"input": "<img alt=\"a < b\" src=x>after",
|
||||
"expected": "after"
|
||||
},
|
||||
{
|
||||
"input": "<a href = \"x>y\" class=c>t</a> u",
|
||||
"expected": "t u"
|
||||
},
|
||||
{
|
||||
"input": "a<img alt=\"x>y",
|
||||
"expected": "a"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -123,7 +123,7 @@ mod tests {
|
||||
#[test]
|
||||
fn html_to_text_fixtures() {
|
||||
let tests = load_text_pairs("decoders/html_to_text.json");
|
||||
assert_eq!(tests.len(), 24);
|
||||
assert_eq!(tests.len(), 33);
|
||||
for (input, expected) in tests {
|
||||
assert_eq!(html_to_text(&input), expected, "{input:?}");
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
use memchr::{memchr, memchr2};
|
||||
use memchr::memchr;
|
||||
|
||||
use super::decode_entity;
|
||||
|
||||
@@ -15,6 +15,7 @@ const NAMED_TAG: usize = 1;
|
||||
|
||||
static TEXT_DELIMITERS: [bool; 256] = delimiters(b"<&; \t\r\n");
|
||||
static TAG_DELIMITERS: [bool; 256] = delimiters(b"<>/ \t\r\n");
|
||||
static ATTRIBUTE_DELIMITERS: [bool; 256] = delimiters(b"<>=");
|
||||
|
||||
const fn delimiters(bytes: &[u8]) -> [bool; 256] {
|
||||
let mut table = [false; 256];
|
||||
@@ -94,7 +95,7 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
|
||||
let hidden = in_head || in_style || in_script || in_template;
|
||||
if in_tag {
|
||||
if tag_token_pos > NAMED_TAG {
|
||||
pos = find(bytes, pos, |rest| memchr2(b'<', b'>', rest));
|
||||
pos += run_until(bytes, pos, &ATTRIBUTE_DELIMITERS);
|
||||
} else if !is_token_start {
|
||||
let run = run_until(bytes, pos, &TAG_DELIMITERS);
|
||||
if run > 0 {
|
||||
@@ -121,10 +122,11 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
|
||||
match ch {
|
||||
b'<' => {
|
||||
if !in_tag && !hidden && !is_token_start {
|
||||
if sink.emit(token_start, token_end + 1, is_after_space) {
|
||||
if sink.emit(token_start, token_end + 1, is_after_space && !is_new_line) {
|
||||
return true;
|
||||
}
|
||||
is_after_space = false;
|
||||
is_new_line = false;
|
||||
}
|
||||
tag_token_pos = 0;
|
||||
in_tag = true;
|
||||
@@ -159,6 +161,10 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
|
||||
pos += 1;
|
||||
continue;
|
||||
}
|
||||
b'=' if in_tag && tag_token_pos > NAMED_TAG => {
|
||||
pos = attribute_value_end(bytes, pos + 1);
|
||||
continue;
|
||||
}
|
||||
b'/' if in_tag => {
|
||||
if tag_token_pos == 0 {
|
||||
is_tag_close = true;
|
||||
@@ -256,6 +262,17 @@ fn run_until(bytes: &[u8], pos: usize, delimiters: &[bool; 256]) -> usize {
|
||||
.unwrap_or(rest.len())
|
||||
}
|
||||
|
||||
fn attribute_value_end(bytes: &[u8], pos: usize) -> usize {
|
||||
let value = bytes.get(pos..).unwrap_or_default().trim_ascii_start();
|
||||
let after = match value.split_first() {
|
||||
Some(("e @ (b'"' | b'\''), quoted)) => memchr(quote, quoted)
|
||||
.and_then(|close| quoted.get(close + 1..))
|
||||
.unwrap_or_default(),
|
||||
_ => value,
|
||||
};
|
||||
bytes.len() - after.len()
|
||||
}
|
||||
|
||||
fn comment_close(bytes: &[u8], comment_start: usize) -> Option<usize> {
|
||||
let mut pos = comment_start;
|
||||
loop {
|
||||
|
||||
@@ -253,7 +253,7 @@ mod tests {
|
||||
));
|
||||
assert_eq!(
|
||||
super::preview_html("<p>a b</p>\r\n<p>c</p>", 100),
|
||||
"ab\n c\n"
|
||||
"ab\nc\n"
|
||||
);
|
||||
assert_eq!(super::preview_html("<p>hello world</p>", 8), "hello...");
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user