From 251871a96cdca1a8f71df0ec4e8e77a92ebdf123 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Mon, 26 Oct 2026 21:43:43 +0800 Subject: [PATCH] fix(html2md): retain boundaries of expanded block elements --- moli-html2md/src/html_fallback.rs | 12 +++------ moli-html2md/tests/review_semantics.rs | 34 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/moli-html2md/src/html_fallback.rs b/moli-html2md/src/html_fallback.rs index a1918a5b964..5c1e57db46d 100644 --- a/moli-html2md/src/html_fallback.rs +++ b/moli-html2md/src/html_fallback.rs @@ -216,17 +216,11 @@ pub(crate) fn render( continue; } if let NodeKind::Element(tag) = dom.node_kind(node) { - if !code - && dom.has_block_layout(node) - && !crate::structure::is_block(tag) - && !crate::structure::owns_boundaries(tag) - { + let owns_boundaries = allowed(tag) && crate::structure::owns_boundaries(tag); + if !code && dom.has_block_layout(node) && !owns_boundaries { output.push_str("
"); tasks.push(Task::Close("div".to_owned())); - } else if !code - && dom.has_text_boundary(node) - && !crate::structure::owns_boundaries(tag) - { + } else if !code && dom.has_text_boundary(node) && !owns_boundaries { output.push(' '); tasks.push(Task::Literal(" ")); } diff --git a/moli-html2md/tests/review_semantics.rs b/moli-html2md/tests/review_semantics.rs index 604a0832ba3..ab6fff1d9b4 100644 --- a/moli-html2md/tests/review_semantics.rs +++ b/moli-html2md/tests/review_semantics.rs @@ -1084,6 +1084,40 @@ fn fallback_keeps_code_boxes_and_ignores_column_boxes() { } } +#[test] +fn stripped_block_elements_keep_their_computed_boundaries_in_tables() { + let content = + "AB"; + for source in [ + format!("
{content}
"), + format!("
{content}Other
"), + format!("
{content}
"), + ] { + let dom = Counted { + tree: Tree::parse(&source), + visits: Cell::new(0), + }; + let markdown = convert(&dom, dom.tree.root); + let output = Tree::parse(&rendered_html(&markdown)); + let mut text = String::new(); + for node in &output.nodes { + if matches!(node.tag.as_deref(), Some("p" | "div" | "td" | "br")) { + text.push(' '); + } + if let Some(value) = &node.text { + text.push_str(value); + } + } + assert!( + text.split_whitespace() + .collect::>() + .windows(2) + .any(|words| words == ["A", "B"]), + "{source}: {markdown}\n{text}" + ); + } +} + #[test] fn content_after_a_nested_list_can_make_the_outer_list_loose() { assert_eq!(