Fix HTML text parsing issues

This commit is contained in:
Maurus Decimus
2026-10-06 15:45:20 +02:00
parent 79b2f3fd5e
commit f39064dc3c
3 changed files with 63 additions and 13 deletions
+48
View File
@@ -46,5 +46,53 @@
{
"input": "<div>Hello</div><template><div>Hidden</div><style>.x{}</style></template><div>World</div>",
"expected": "HelloWorld"
},
{
"input": "<style type=\"text/css\">p { color: red; }</style><p>Hello world</p>",
"expected": "Hello world\n"
},
{
"input": "<script type=\"text/javascript\">var x = 1;</script><p>Hello world</p>",
"expected": "Hello world\n"
},
{
"input": "<STYLE TYPE=\"text/css\" media=\"screen\">.a{b:c}</STYLE>Hi",
"expected": "Hi"
},
{
"input": "<head lang=\"en\"><title>Title</title></head><body class=\"main\">Body</body>",
"expected": "Body"
},
{
"input": "<div>Hello</div><template id=\"row\"><div>Hidden</div></template><div>World</div>",
"expected": "HelloWorld"
},
{
"input": "line one<br class=\"x\">line two<br style=\"a\" />end",
"expected": "line one\nline two\nend"
},
{
"input": "<p class=\"intro\">first</p><p style=\"margin:0\">second</p>",
"expected": "first\nsecond\n"
},
{
"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 &amp; more</td></tr></table>",
"expected": "Sale & more"
},
{
"input": "<p>Before</p><!----><p>Hello world</p>",
"expected": "Before\nHello world\n"
},
{
"input": "a<!-- x --->b<!---->c<!-->d<!--->e<!-- y --!>f",
"expected": "abcdef"
},
{
"input": "a<!---!>x--><!--!>y--><!-- z- ->w -->b",
"expected": "ab"
},
{
"input": "before<!-- unterminated --",
"expected": "before"
}
]
+1 -1
View File
@@ -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(), 12);
assert_eq!(tests.len(), 24);
for (input, expected) in tests {
assert_eq!(html_to_text(&input), expected, "{input:?}");
}
+14 -12
View File
@@ -9,7 +9,8 @@ use memchr::{memchr, memchr2};
use super::decode_entity;
const COMMENT_OPEN: &[u8] = b"--";
const COMMENT_CLOSE_DASHES: usize = 2;
const COMMENT_CLOSE: &[u8] = b"--";
const COMMENT_BANG_CLOSE: &[u8] = b"--!";
const NAMED_TAG: usize = 1;
static TEXT_DELIMITERS: [bool; 256] = delimiters(b"<&; \t\r\n");
@@ -133,7 +134,7 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
continue;
}
b'>' if in_tag => {
if tag_token_pos == NAMED_TAG
if tag_token_pos >= NAMED_TAG
&& let Some(tag) = bytes.get(token_start..token_end + 1)
{
match Tag::parse(tag) {
@@ -220,11 +221,15 @@ fn convert(input: &str, out: &mut String, mode: Mode) -> bool {
_ => (),
}
if is_token_start {
token_start = pos;
is_token_start = false;
if in_tag {
tag_token_pos += 1;
if tag_token_pos > NAMED_TAG {
pos += 1;
continue;
}
}
token_start = pos;
}
token_end = pos;
pos += 1;
@@ -255,15 +260,12 @@ fn comment_close(bytes: &[u8], comment_start: usize) -> Option<usize> {
let mut pos = comment_start;
loop {
let close = pos + memchr(b'>', bytes.get(pos..)?)?;
let dashes = bytes
.get(comment_start..close)
.unwrap_or_default()
.iter()
.rev()
.take(COMMENT_CLOSE_DASHES + 1)
.take_while(|&&byte| byte == b'-')
.count();
if dashes == COMMENT_CLOSE_DASHES {
let comment = bytes.get(comment_start..close)?;
if comment.ends_with(COMMENT_CLOSE)
|| comment
.strip_prefix(COMMENT_OPEN)
.is_some_and(|body| body.ends_with(COMMENT_BANG_CLOSE))
{
return Some(close);
}
pos = close + 1;