From f9e0cc2509d24e2c8b1e3fdf5296c3ab81091ae9 Mon Sep 17 00:00:00 2001 From: mgros Date: Sat, 26 Sep 2026 03:45:20 +0200 Subject: [PATCH] refactor: simplify `is_simple` and `is_trivially_simple` logic --- src/xpath_functions.rs | 55 +++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/src/xpath_functions.rs b/src/xpath_functions.rs index a713fad8..f3454dae 100644 --- a/src/xpath_functions.rs +++ b/src/xpath_functions.rs @@ -81,30 +81,10 @@ impl IsNode { /// this is fairly detailed, so we define a few local functions (at end) to help out /// Also, it doesn't help that the structure is a bit complicated Elements->ChildOfElement->Element/Text pub fn is_simple(elem: Element) -> bool { - if is_trivially_simple(elem) { - return true; - } - - if is_negative_of_trivially_simple(elem) { - // -3 or -x - return true; - } - - if !is_tag(elem, "mrow") || elem.children().is_empty() { - return false; - } - - // x y or -x or -3 x or -x y or -3 x y or x° or n° or -x° or -n° - #[allow(clippy::if_same_then_else)] - if is_times_mi(elem) { - return true; // x y - } else if is_degrees(elem) { - return true; // x° or n° - } else if is_function(elem) { - return true; - } - - return false; + return is_trivially_simple(elem) + || is_negative_of_trivially_simple(elem) + || (is_tag(elem, "mrow") + && (is_times_mi(elem) || is_degrees(elem) || is_function(elem))); // returns the element's text value @@ -139,19 +119,10 @@ impl IsNode { // checks the single element to see if it is simple (mn, mi that is a single char, common fraction) fn is_trivially_simple(elem: Element) -> bool { - if is_tag(elem, "mn") { - return true; - } - if is_tag(elem, "mi") && is_single_char(&to_str(elem)) { - // "simple" only if it is a single char (which can be multiple bytes) - return true; - } - // FIX: need to consult preference Fraction_Ordinal - if IsNode::is_common_fraction(elem, 10, 19) { - return true; - } - return false; + is_tag(elem, "mn") + || (is_tag(elem, "mi") && is_single_char(&to_str(elem))) + || IsNode::is_common_fraction(elem, 10, 19) } // true if the negative of a single element that is simple @@ -1957,6 +1928,18 @@ mod tests { }); } + /// Degree forms are simple, while longer products and identifiers exceed the limit. + #[test] + fn is_simple_shape_boundaries() -> Result<()> { + xpath_test(|| { + test_is_simple("variable with degrees", "x°")?; + test_is_simple("number with degrees", "30°")?; + test_is_not_simple("longer product", "x⁢yz")?; + test_is_not_simple("non-simple function argument", "f⁡x")?; + Ok(()) + }) + } + fn check_table_dims(mathml: &str, dims: (usize, usize)) -> Result<()> { let package = parser::parse(mathml).map_err(|e| anyhow::anyhow!("failed to parse XML: {e}"))?; let math_elem = get_element(&package);