Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix comment handling - xml comments may contain '>' #16

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion autosar-data/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,13 @@ impl Element {
/// Set or delete the comment attached to the element
///
/// Set None to remove the comment.
pub fn set_comment(&self, opt_comment: Option<String>) {
pub fn set_comment(&self, mut opt_comment: Option<String>) {
if let Some(comment) = &mut opt_comment {
// make sure the comment we store never contains "--" as this is forbidden by the w3 xml specification
if comment.contains("--") {
*comment = comment.replace("--", "__");
}
}
self.0.lock().comment = opt_comment;
}

Expand Down
23 changes: 22 additions & 1 deletion autosar-data/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,20 @@ impl<'a> ArxmlLexer<'a> {
}
b'!' => {
// second char is '!' -> parse a comment
return self.read_comment(endpos).map(|res| (self.line, res));
// we found a '>' character, but comments are allowed to contain unquoted '<' and '>'
// this means we need to make sure the end is actually '-->', not just '>'
let mut comment_endpos = endpos;
while comment_endpos < self.buffer.len()
&& !self.buffer[comment_endpos - 2..].starts_with(b"-->")
{
comment_endpos += 1;
}
if comment_endpos < self.buffer.len() {
return self.read_comment(comment_endpos).map(|res| (self.line, res));
} else {
// hit the end of the input -> unclosed comment
return Err(self.error(ArxmlLexerError::InvalidComment));
}
}
_ => {
// any other second char -> BeginElement
Expand Down Expand Up @@ -363,4 +376,12 @@ mod test {
let event = ArxmlEvent::ArxmlHeader(None);
let _ = format!("{event:#?}");
}

/// github issue #15 - comments can contain '<' and '>'
#[test]
fn test_w3c_comment_example() {
let data = b"<!-- declarations for <head> & <body> -->";
let mut lexer = ArxmlLexer::new(data, PathBuf::from("(buffer)"));
assert!(matches!(lexer.next(), Ok((_, ArxmlEvent::Comment(_)))));
}
}
2 changes: 1 addition & 1 deletion autosar-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ mod lexer;
mod parser;

// allow public access to the error sub-types
pub use parser::ArxmlParserError;
pub use lexer::ArxmlLexerError;
pub use parser::ArxmlParserError;

// reexport some of the info from the specification
pub use autosar_data_specification::AttributeName;
Expand Down
Loading