diff --git a/src/dom/mod.rs b/src/dom/mod.rs index 8d7e4b2..a777391 100644 --- a/src/dom/mod.rs +++ b/src/dom/mod.rs @@ -130,8 +130,12 @@ impl Dom { if dom.tree_type == DomVariant::Empty { dom.tree_type = DomVariant::DocumentFragment; } + let text = pair.as_str().to_string(); - if !text.trim().is_empty() { + if !text + .trim_matches(|c: char| c.is_whitespace() && c != ' ') + .is_empty() + { dom.children.push(Node::Text(text)); } } diff --git a/tests/snapshots/text__it_can_parse_text_with_only_spaces_between_elements.snap b/tests/snapshots/text__it_can_parse_text_with_only_spaces_between_elements.snap new file mode 100644 index 0000000..e4bd47c --- /dev/null +++ b/tests/snapshots/text__it_can_parse_text_with_only_spaces_between_elements.snap @@ -0,0 +1,34 @@ +--- +source: tests/text.rs +expression: dom +--- +{ + "treeType": "documentFragment", + "children": [ + "This is a ", + { + "name": "b", + "variant": "normal", + "children": [ + "para" + ] + }, + " ", + { + "name": "b", + "variant": "normal", + "children": [ + "ph" + ] + }, + " with some", + { + "name": "i", + "variant": "normal", + "children": [ + " weird " + ] + }, + " formatting." + ] +} diff --git a/tests/text.rs b/tests/text.rs index 8776369..1d02dc6 100644 --- a/tests/text.rs +++ b/tests/text.rs @@ -49,11 +49,21 @@ fn it_can_parse_text_with_chevron() -> Result<()> { #[test] fn it_can_parse_text_in_paragraph_with_weird_formatting() -> Result<()> { - let html = indoc!(r" + let html = indoc!( + r"

This is a paragraph with some weird formatting.

- "); + " + ); + let dom = Dom::parse(html)?; + assert_json_snapshot!(dom); + Ok(()) +} + +#[test] +fn it_can_parse_text_with_only_spaces_between_elements() -> Result<()> { + let html = indoc!(r"This is a para ph with some weird formatting."); let dom = Dom::parse(html)?; assert_json_snapshot!(dom); Ok(())