diff --git a/benches/xml.rs b/benches/xml.rs index b2d5577..b8c09a7 100644 --- a/benches/xml.rs +++ b/benches/xml.rs @@ -399,12 +399,7 @@ benchmark_group!( large_sdx_document, huge_sdx_document, ); -benchmark_group!( - minidom, - tiny_minidom, - medium_minidom, - large_minidom, -); +benchmark_group!(minidom, tiny_minidom, medium_minidom, large_minidom,); benchmark_group!( xmlparser, tiny_xmlparser, diff --git a/src/parse.rs b/src/parse.rs index b42932d..97146bb 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -80,6 +80,9 @@ pub enum Error { /// The XML document must have at least one element. NoRootNode, + /// The root node was opened but never closed. + UnclosedRootNode, + /// An XML with DTD detected. /// /// This error will be emitted only when `ParsingOptions::allow_dtd` is set to `false`. @@ -194,6 +197,9 @@ impl core::fmt::Display for Error { Error::NoRootNode => { write!(f, "the document does not have a root node") } + Error::UnclosedRootNode => { + write!(f, "the root node was opened but never closed") + } Error::DtdDetected => { write!(f, "XML with DTD detected") } @@ -521,6 +527,10 @@ fn parse(text: &str, opt: ParsingOptions) -> Result { return Err(Error::NoRootNode); } + if pd.parent_prefixes.len() > 1 { + return Err(Error::UnclosedRootNode); + } + doc.nodes.shrink_to_fit(); doc.attrs.shrink_to_fit(); doc.namespaces.shrink_to_fit(); @@ -1002,6 +1012,7 @@ impl<'input, 'temp> BorrowedText<'input, 'temp> { } } +#[allow(clippy::needless_lifetimes)] fn append_text<'input, 'temp>( text: BorrowedText<'input, 'temp>, parent_id: NodeId, @@ -1174,8 +1185,8 @@ fn _normalize_attribute( Ok(()) } -fn get_ns_idx_by_prefix<'input>( - doc: &Document<'input>, +fn get_ns_idx_by_prefix( + doc: &Document, range: ShortRange, prefix: StrSpan, ) -> Result, Error> { diff --git a/tests/api.rs b/tests/api.rs index 22d1bf5..4e5360e 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -1,3 +1,5 @@ +#![allow(clippy::bool_assert_comparison)] + extern crate roxmltree; use roxmltree::*; diff --git a/tests/ast.rs b/tests/ast.rs index dfb99da..90914c9 100644 --- a/tests/ast.rs +++ b/tests/ast.rs @@ -40,7 +40,7 @@ fn actual_test(path: &str) { ..roxmltree::ParsingOptions::default() }; - let input_xml = load_file(&path); + let input_xml = load_file(path); let doc = match Document::parse_with_options(&input_xml, opt) { Ok(v) => v, Err(e) => { @@ -53,7 +53,7 @@ fn actual_test(path: &str) { } fn load_file(path: &path::Path) -> String { - let mut file = fs::File::open(&path).unwrap(); + let mut file = fs::File::open(path).unwrap(); let mut text = String::new(); file.read_to_string(&mut text).unwrap(); text @@ -117,7 +117,7 @@ fn _to_yaml(doc: &Document, s: &mut String) -> Result<(), fmt::Error> { } let attributes = child.attributes(); - if !(attributes.len() == 0) { + if attributes.len() != 0 { let mut attrs = Vec::new(); for attr in attributes { match attr.namespace() { @@ -149,7 +149,7 @@ fn _to_yaml(doc: &Document, s: &mut String) -> Result<(), fmt::Error> { }; ns_list.push((name, uri)); } - ns_list.sort_by(|a, b| a.0.cmp(&b.0)); + ns_list.sort_by(|a, b| a.0.cmp(b.0)); writeln_indented!(depth + 2, s, "namespaces:"); for (name, uri) in ns_list { @@ -288,3 +288,5 @@ test!(tree_001); test!(tree_002); // test!(tree_003); // unsupported test!(tree_err_001); +test!(tree_err_002); +test!(tree_err_003); diff --git a/tests/files/README.md b/tests/files/README.md index a6f30e0..950fc20 100644 --- a/tests/files/README.md +++ b/tests/files/README.md @@ -85,3 +85,5 @@ - tree_002 - BOM - tree_003 - Windows-1251 encoding - tree_err_001 - no elements +- tree_err_002 - root element not closed +- tree_err_003 - child element not closed diff --git a/tests/files/tree_err_002.xml b/tests/files/tree_err_002.xml new file mode 100644 index 0000000..e1856eb --- /dev/null +++ b/tests/files/tree_err_002.xml @@ -0,0 +1,2 @@ + +Text diff --git a/tests/files/tree_err_002.yaml b/tests/files/tree_err_002.yaml new file mode 100644 index 0000000..a80ea7b --- /dev/null +++ b/tests/files/tree_err_002.yaml @@ -0,0 +1 @@ +error: "the root node was opened but never closed" diff --git a/tests/files/tree_err_003.xml b/tests/files/tree_err_003.xml new file mode 100644 index 0000000..7cb568e --- /dev/null +++ b/tests/files/tree_err_003.xml @@ -0,0 +1,2 @@ + +Text diff --git a/tests/files/tree_err_003.yaml b/tests/files/tree_err_003.yaml new file mode 100644 index 0000000..b93f025 --- /dev/null +++ b/tests/files/tree_err_003.yaml @@ -0,0 +1 @@ +error: "expected 'open' tag, not 'root' at 2:17"