WIP cdata and comment tokens
This commit is contained in:
@@ -14,9 +14,25 @@ impl TextContent {
|
||||
|
||||
pub fn append(self, c: char) -> Vec<Token> {
|
||||
if c == '<' {
|
||||
vec![Token::ElementCloseStart(ElementCloseStart::new(
|
||||
let is_mixed = self
|
||||
.element
|
||||
.xs_complex_type
|
||||
.mixed
|
||||
.as_ref()
|
||||
.map(|val| val == "true")
|
||||
.unwrap_or(false);
|
||||
|
||||
let mut tokens = vec![Token::ElementCloseStart(ElementCloseStart::new(
|
||||
Arc::clone(&self.element),
|
||||
))]
|
||||
))];
|
||||
|
||||
// If mixed content is allowed, also add Comment and CData as possible transitions
|
||||
if is_mixed {
|
||||
tokens.push(Token::Comment(Comment::new(Arc::clone(&self.element))));
|
||||
tokens.push(Token::CData(CData::new(Arc::clone(&self.element))));
|
||||
}
|
||||
|
||||
tokens
|
||||
} else {
|
||||
vec![Token::TextContent(TextContent::new(Arc::clone(
|
||||
&self.element,
|
||||
@@ -126,4 +142,81 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New tests for transitions to comment and CDATA
|
||||
|
||||
#[test]
|
||||
fn test_text_to_comment_transition() {
|
||||
let values = [
|
||||
"<reasoning>text<",
|
||||
"<reasoning>text<!",
|
||||
"<reasoning>text<!--",
|
||||
"<reasoning>text<!-- comment",
|
||||
];
|
||||
|
||||
for value in values {
|
||||
let mut validator = crate::tests::example_validator();
|
||||
validator.append(value).expect(value);
|
||||
assert!(!validator.current_tokens.is_empty(), "{value}");
|
||||
|
||||
let is_valid = validator
|
||||
.current_tokens
|
||||
.iter()
|
||||
.any(|token| matches!(token, Token::Comment(_)));
|
||||
|
||||
assert!(is_valid, "{value}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_to_cdata_transition() {
|
||||
let values = [
|
||||
"<reasoning>text<",
|
||||
"<reasoning>text<!",
|
||||
"<reasoning>text<![",
|
||||
"<reasoning>text<![C",
|
||||
"<reasoning>text<![CD",
|
||||
"<reasoning>text<![CDA",
|
||||
"<reasoning>text<![CDAT",
|
||||
"<reasoning>text<![CDATA",
|
||||
"<reasoning>text<![CDATA[",
|
||||
];
|
||||
|
||||
for value in values {
|
||||
let mut validator = crate::tests::example_validator();
|
||||
validator.append(value).expect(value);
|
||||
assert!(!validator.current_tokens.is_empty(), "{value}");
|
||||
|
||||
// Should transition to CDATA state or remain in TextContent for partial prefix
|
||||
let is_valid = validator
|
||||
.current_tokens
|
||||
.iter()
|
||||
.any(|token| matches!(token, Token::CData(_)));
|
||||
|
||||
assert!(is_valid, "{value}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_text_with_mixed_content() {
|
||||
let values = [
|
||||
"<reasoning>text<!-- comment -->more text</reasoning>",
|
||||
"<reasoning>text<![CDATA[cdata content]]>more text</reasoning>",
|
||||
"<reasoning>text<!-- comment --><![CDATA[cdata]]>more</reasoning>",
|
||||
"<reasoning><!-- c1 -->text<![CDATA[cd]]><!-- c2 --></reasoning>",
|
||||
];
|
||||
|
||||
for value in values {
|
||||
let mut validator = crate::tests::example_validator();
|
||||
validator.append(value).expect(value);
|
||||
assert!(!validator.current_tokens.is_empty(), "{value}");
|
||||
assert!(
|
||||
validator
|
||||
.current_tokens
|
||||
.iter()
|
||||
.any(|token| matches!(token, Token::EndOfFile(_))),
|
||||
"{value}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user