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

Introduce Config struct that holds parser configuration and implement #513 #677

Merged
merged 11 commits into from
Nov 5, 2023
68 changes: 68 additions & 0 deletions tests/reader-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! Please keep tests sorted (exceptions are allowed if options are tightly related).

use quick_xml::errors::{Error, IllFormedError};
use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::reader::Reader;

Expand Down Expand Up @@ -43,6 +44,73 @@ mod expand_empty_elements {
}
}

mod trim_markup_names_in_closing_tags {
use super::*;
use pretty_assertions::assert_eq;

mod false_ {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn check_end_names_false() {
let mut reader = Reader::from_str("<root></root \t\r\n>");
reader.trim_markup_names_in_closing_tags(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it the reason that we only have this for closing tags, attributes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, that this is optimization option. Usually end tags does not contain spaces before >, so if we will assume that the name ends immediately before the >, we could save some time. Such optimisation make sense only for the closing tags -- for opening tags we in any case should check if it has attributes and find the actual end of tag name.

// We need to disable checks, otherwise the error will be returned when read end
reader.check_end_names(false);

assert_eq!(
reader.read_event().unwrap(),
Event::Start(BytesStart::new("root"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::End(BytesEnd::new("root \t\r\n"))
);
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}

#[test]
fn check_end_names_true() {
let mut reader = Reader::from_str("<root></root \t\r\n>");
reader.trim_markup_names_in_closing_tags(false);
reader.check_end_names(true);

assert_eq!(
reader.read_event().unwrap(),
Event::Start(BytesStart::new("root"))
);
match reader.read_event() {
Err(Error::IllFormed(cause)) => assert_eq!(
cause,
IllFormedError::MismatchedEnd {
expected: "root".into(),
found: "root \t\r\n".into(),
}
),
x => panic!("Expected `Err(IllFormed(_))`, but got `{:?}`", x),
}
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}

#[test]
fn true_() {
let mut reader = Reader::from_str("<root></root \t\r\n>");
reader.trim_markup_names_in_closing_tags(true);

assert_eq!(
reader.read_event().unwrap(),
Event::Start(BytesStart::new("root"))
);
assert_eq!(
reader.read_event().unwrap(),
Event::End(BytesEnd::new("root"))
);
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}

const XML: &str = " \t\r\n\
<!doctype root \t\r\n> \t\r\n\
<root \t\r\n> \t\r\n\
Expand Down