Skip to content

Commit

Permalink
feat(node-reader): now treats the empty tag case
Browse files Browse the repository at this point in the history
  • Loading branch information
nephi-dev committed Nov 19, 2024
1 parent 58cb682 commit 3a38ec6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxml"
version = "2.1.0"
version = "2.1.1"
edition = "2021"

[lib]
Expand Down
17 changes: 16 additions & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ fn read_node(root_tag: String, reader: &mut Reader<&[u8]>) -> Node {
root.children.push(child);
}
},
Ok(Event::Empty(e)) => {
let node = Node {
name: f_utf!(e.name().as_ref()),
attrs: get_attrs(e.attributes()),
children: Vec::new(),
text: None,
};
root.children.push(node);
}
Ok(Event::Text(e)) => {
root.text = Some(f_str!(e.unescape().unwrap()));
}
Expand Down Expand Up @@ -75,7 +84,7 @@ pub fn read_string(xml_string: String, root_tag: String) -> Node {
#[cfg(test)]
mod tests {
use crate::f_str;
use crate::read::read_file;
use crate::read::{read_file, read_string};
use std::fs::{remove_file, File};
use std::io::prelude::*;
#[test]
Expand All @@ -95,4 +104,10 @@ mod tests {
assert_eq!(node.children[0].text.as_ref().unwrap(), "test");
assert_eq!(node.children[0].children.len(), 0);
}
#[test]
fn test_read_self_closing_tag() {
let xml_string = f_str!("<?xml version=\"1.0\" encoding=\"utf-8\"?><tag><wrapper><inner1>value</inner1><inner2 attr=\"attr\"/><inner3>value</inner3></wrapper></tag>");
let node = read_string(xml_string, f_str!("tag"));
assert_eq!(node.children[0].children.len(), 3);
}
}

0 comments on commit 3a38ec6

Please sign in to comment.