From d81ae09f6700693b1971b971980debca5ac5fbd6 Mon Sep 17 00:00:00 2001 From: Niels Geens Date: Thu, 10 Apr 2025 18:37:00 +0200 Subject: [PATCH] Fix nested elements --- lib/xml_schema_validator/src/lib.rs | 11 +++++++++++ .../src/token/element_close_start.rs | 14 ++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/xml_schema_validator/src/lib.rs b/lib/xml_schema_validator/src/lib.rs index dfe4bcd..e508981 100644 --- a/lib/xml_schema_validator/src/lib.rs +++ b/lib/xml_schema_validator/src/lib.rs @@ -162,6 +162,17 @@ mod tests { "Error should mention missing required attribute or invalid continuation"); } } + + #[test] + fn test_invalid_nesting() { + let schema_text = std::fs::read_to_string("../../action_schema.xsd").unwrap(); + let validator = XmlSchemaValidator::new(&schema_text).unwrap(); + validator.clone().append("").expect_err("Should fail because of nested tag"); + validator.clone().append(" ").expect_err("Should fail because of nested tag, even with whitespace"); + validator.clone().append("foo").expect_err("Should fail because of nested tag, even with text"); + validator.clone().append(" foo ").expect_err("Should fail because of nested tag, even with text and whitespace"); + validator.clone().append(" foo ").expect_err("Should fail because of nested tag, even with self closing tag"); + } #[test] fn test_stop_self_closing() { diff --git a/lib/xml_schema_validator/src/token/element_close_start.rs b/lib/xml_schema_validator/src/token/element_close_start.rs index ac77966..01828f9 100644 --- a/lib/xml_schema_validator/src/token/element_close_start.rs +++ b/lib/xml_schema_validator/src/token/element_close_start.rs @@ -23,15 +23,13 @@ impl ElementCloseStart { element: Arc::clone(&self.element), has_slash: true, })] + } else if self.has_slash && self.element.name.starts_with(c) { + vec![Token::ElementCloseName(ElementCloseName::new( + Arc::clone(&self.element), + c.to_string(), + ))] } else { - if self.element.name.starts_with(c) { - vec![Token::ElementCloseName(ElementCloseName::new( - Arc::clone(&self.element), - c.to_string(), - ))] - } else { - vec![] - } + vec![] } } }