diff --git a/src/canonicalize.rs b/src/canonicalize.rs index 86ea9094..ffb2151b 100644 --- a/src/canonicalize.rs +++ b/src/canonicalize.rs @@ -1882,7 +1882,7 @@ impl CanonicalizeContext { let close = close_raw.as_deref().unwrap_or(")").replace('>', "⟩"); // debug!("open={}, close={}", open, close); let separators_raw = mfenced.attribute_value("separators"); - let mut separators = separators_raw.as_deref().unwrap_or(",").chars(); + let separators: Vec = separators_raw.as_deref().unwrap_or(",").chars().filter(|ch| !ch.is_whitespace()).collect(); set_mathml_name(mfenced, "mrow"); mfenced.remove_attribute("open"); mfenced.remove_attribute("close"); @@ -1894,9 +1894,10 @@ impl CanonicalizeContext { } if !children.is_empty() { new_children.push(children[0]); - for child in &children[1..] { - let sep = separators.next().unwrap_or(',').to_string(); - new_children.push( ChildOfElement::Element( create_mo(mfenced.document(), &sep, MFENCED_ATTR_VALUE)) ); + for (i, child) in children[1..].iter().enumerate() { + if let Some(&sep) = separators.get(i).or_else(|| separators.last()) { + new_children.push(ChildOfElement::Element( create_mo(mfenced.document(), &sep.to_string(), MFENCED_ATTR_VALUE)) ); + } new_children.push(*child); } } @@ -4991,14 +4992,120 @@ mod canonicalize_tests { y ; + z + ; + a + + ) + + + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_empty_separators() -> Result<()> { + let test_str = "5+3"; + let target_str = " + + ( + + 5 + + + 3 + + ) + + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_repeat_last_separator() -> Result<()> { + let test_str = "xyz"; + let target_str = " + + ( + x + ; + y + ; z - , - a + ) + + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_separators_with_whitespace() -> Result<()> { + let test_str = "xyz"; + let target_str = " + + ( + + x + ; + + y + , + z + + + ) + + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_too_many_separators() -> Result<()> { + let test_str = "xy"; + let target_str = " + + ( + + x + ; + y + + ) + + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_default_separator() -> Result<()> { + let test_str = "xy"; + let target_str = " + + ( + + x + , + y ) + "; + are_strs_canonically_equal_result(test_str, target_str, &[]) + } + + #[test] + fn mfenced_whitespace_only_separators() -> Result<()> { + let test_str = "5+3"; + let target_str = " + + ( + + 5 + + + 3 + + ) "; are_strs_canonically_equal_result(test_str, target_str, &[])