mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 16:01:27 +00:00
html_to_text: line breaks for block elements, spaces between table cells (fixes #172)
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/).
|
||||
|
||||
## [1.0.3] - Unreleased
|
||||
|
||||
### Fixed
|
||||
- `html_to_text` and HTML previews ran words together across block elements, so `<div>One</div><div>Two</div>` gave `OneTwo`. Block elements such as `<div>`, `<li>`, `<tr>` and `<h1>` now end the line and table cells are separated by a space, while a `<br>` or `<p>` inside `<template>`, `<style>` or `<script>` no longer adds a line break (#172).
|
||||
|
||||
## [1.0.2] - 2026-11-23
|
||||
|
||||
### Fixed
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "mail-parser"
|
||||
description = "Fast and robust e-mail parsing library for Rust"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
edition = "2024"
|
||||
rust-version = "1.98"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
},
|
||||
{
|
||||
"input": "<head><title>ignore head</title><not head>xyz</not head></head><h1><body></h1>",
|
||||
"expected": "<body>"
|
||||
"expected": "<body>\n"
|
||||
},
|
||||
{
|
||||
"input": "<p>what is ♥?</p><p>ßĂΒγ don't hurt me.</p>",
|
||||
@@ -37,15 +37,15 @@
|
||||
},
|
||||
{
|
||||
"input": "<style>body{color:red}</style><div>Hello</div>",
|
||||
"expected": "Hello"
|
||||
"expected": "Hello\n"
|
||||
},
|
||||
{
|
||||
"input": "<style>body{color:red}</style><div>Hello</div><script>alert('x')</script><div>World</div>",
|
||||
"expected": "HelloWorld"
|
||||
"expected": "Hello\nWorld\n"
|
||||
},
|
||||
{
|
||||
"input": "<div>Hello</div><template><div>Hidden</div><style>.x{}</style></template><div>World</div>",
|
||||
"expected": "HelloWorld"
|
||||
"expected": "Hello\nWorld\n"
|
||||
},
|
||||
{
|
||||
"input": "<style type=\"text/css\">p { color: red; }</style><p>Hello world</p>",
|
||||
@@ -65,7 +65,7 @@
|
||||
},
|
||||
{
|
||||
"input": "<div>Hello</div><template id=\"row\"><div>Hidden</div></template><div>World</div>",
|
||||
"expected": "HelloWorld"
|
||||
"expected": "Hello\nWorld\n"
|
||||
},
|
||||
{
|
||||
"input": "line one<br class=\"x\">line two<br style=\"a\" />end",
|
||||
@@ -77,7 +77,7 @@
|
||||
},
|
||||
{
|
||||
"input": "<!--[if mso]><style type=\"text/css\">td{}</style><![endif]--><style type=\"text/css\">@media{.a{b:c}}</style><table role=\"presentation\"><tr><td style=\"padding:0\">Sale & more</td></tr></table>",
|
||||
"expected": "Sale & more"
|
||||
"expected": "Sale & more\n"
|
||||
},
|
||||
{
|
||||
"input": "<p>Before</p><!----><p>Hello world</p>",
|
||||
@@ -130,5 +130,37 @@
|
||||
{
|
||||
"input": "a<img alt=\"x>y",
|
||||
"expected": "a"
|
||||
},
|
||||
{
|
||||
"input": "<div>One</div><div>Two</div>",
|
||||
"expected": "One\nTwo\n"
|
||||
},
|
||||
{
|
||||
"input": "<ul><li>One</li><li>Two</li></ul>",
|
||||
"expected": "One\nTwo\n"
|
||||
},
|
||||
{
|
||||
"input": "<table><tr><td>One</td><td>Two</td></tr><tr><td>Three</td></tr></table>",
|
||||
"expected": "One Two\nThree\n"
|
||||
},
|
||||
{
|
||||
"input": "<h1>Title</h1><p>Body</p>",
|
||||
"expected": "Title\nBody\n"
|
||||
},
|
||||
{
|
||||
"input": "Intro<DIV class=\"a\"><Div>nested</div>tail</DIV>end",
|
||||
"expected": "Intro\nnested\ntail\nend"
|
||||
},
|
||||
{
|
||||
"input": "<p>a</p><p></p><hr><p>b<br></p>",
|
||||
"expected": "a\nb\n"
|
||||
},
|
||||
{
|
||||
"input": "<table>\n <tr>\n <th>Item</th>\n <th>Price</th>\n </tr>\n</table>",
|
||||
"expected": "Item Price\n"
|
||||
},
|
||||
{
|
||||
"input": "Hello<template><div>a</div><br><td>b</td></template> <style>p{}</style>World<script>\"<br>\"</script>!",
|
||||
"expected": "Hello World!"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -22,8 +22,11 @@ const HTML_SUFFIX: &str = "</body></html>";
|
||||
const LINE_BREAK: &str = "<br/>";
|
||||
|
||||
/// Converts HTML to plain text: tags are removed, entities decoded and
|
||||
/// white space collapsed; `<br>` and `</p>` become line breaks; the content
|
||||
/// of `<head>`, `<style>`, `<script>` and `<template>` is skipped.
|
||||
/// white space collapsed; `<br>` becomes a line break, the start and end of
|
||||
/// a block element (`<p>`, `<div>`, `<li>`, `<tr>`, `<h1>`, ...) end the
|
||||
/// current line unless it is empty, and table cells are separated by a
|
||||
/// space; the content of `<head>`, `<style>`, `<script>` and `<template>` is
|
||||
/// skipped.
|
||||
pub fn html_to_text(input: &str) -> String {
|
||||
text::html_to_text(input)
|
||||
}
|
||||
@@ -123,7 +126,7 @@ mod tests {
|
||||
#[test]
|
||||
fn html_to_text_fixtures() {
|
||||
let tests = load_text_pairs("decoders/html_to_text.json");
|
||||
assert_eq!(tests.len(), 33);
|
||||
assert_eq!(tests.len(), 41);
|
||||
for (input, expected) in tests {
|
||||
assert_eq!(html_to_text(&input), expected, "{input:?}");
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ const fn delimiters(bytes: &[u8]) -> [bool; 256] {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Tag {
|
||||
Break,
|
||||
Paragraph,
|
||||
Block,
|
||||
Cell,
|
||||
Head,
|
||||
Style,
|
||||
Script,
|
||||
@@ -41,7 +42,43 @@ impl Tag {
|
||||
fn parse(name: &[u8]) -> Option<Tag> {
|
||||
hashify::map_ignore_case!(name, Tag,
|
||||
"br" => Tag::Break,
|
||||
"p" => Tag::Paragraph,
|
||||
"address" => Tag::Block,
|
||||
"article" => Tag::Block,
|
||||
"aside" => Tag::Block,
|
||||
"blockquote" => Tag::Block,
|
||||
"caption" => Tag::Block,
|
||||
"center" => Tag::Block,
|
||||
"dd" => Tag::Block,
|
||||
"details" => Tag::Block,
|
||||
"div" => Tag::Block,
|
||||
"dl" => Tag::Block,
|
||||
"dt" => Tag::Block,
|
||||
"fieldset" => Tag::Block,
|
||||
"figcaption" => Tag::Block,
|
||||
"figure" => Tag::Block,
|
||||
"footer" => Tag::Block,
|
||||
"form" => Tag::Block,
|
||||
"h1" => Tag::Block,
|
||||
"h2" => Tag::Block,
|
||||
"h3" => Tag::Block,
|
||||
"h4" => Tag::Block,
|
||||
"h5" => Tag::Block,
|
||||
"h6" => Tag::Block,
|
||||
"header" => Tag::Block,
|
||||
"hr" => Tag::Block,
|
||||
"li" => Tag::Block,
|
||||
"main" => Tag::Block,
|
||||
"nav" => Tag::Block,
|
||||
"ol" => Tag::Block,
|
||||
"p" => Tag::Block,
|
||||
"pre" => Tag::Block,
|
||||
"section" => Tag::Block,
|
||||
"summary" => Tag::Block,
|
||||
"table" => Tag::Block,
|
||||
"tr" => Tag::Block,
|
||||
"ul" => Tag::Block,
|
||||
"td" => Tag::Cell,
|
||||
"th" => Tag::Cell,
|
||||
"head" => Tag::Head,
|
||||
"style" => Tag::Style,
|
||||
"script" => Tag::Script,
|
||||
@@ -140,8 +177,8 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
|
||||
&& let Some(tag) = bytes.get(token_start..token_end + 1)
|
||||
{
|
||||
match Tag::parse(tag) {
|
||||
Some(found @ (Tag::Break | Tag::Paragraph))
|
||||
if found == Tag::Break || is_tag_close =>
|
||||
Some(found @ (Tag::Break | Tag::Block))
|
||||
if !hidden && (found == Tag::Break || !is_new_line) =>
|
||||
{
|
||||
if sink.newline() {
|
||||
return true;
|
||||
@@ -153,7 +190,8 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
|
||||
Some(Tag::Style) => in_style = !is_tag_close,
|
||||
Some(Tag::Script) => in_script = !is_tag_close,
|
||||
Some(Tag::Template) => in_template = !is_tag_close,
|
||||
Some(Tag::Break | Tag::Paragraph) | None => {}
|
||||
Some(Tag::Cell) if !hidden => is_after_space = true,
|
||||
Some(Tag::Break | Tag::Block | Tag::Cell) | None => {}
|
||||
}
|
||||
}
|
||||
in_tag = false;
|
||||
|
||||
Reference in New Issue
Block a user