fatihpense / rust_xml_oxide

Rust XML parser implementation for SAX interface: xml_sax
MIT License
10 stars 1 forks source link

.is_empty not set on empty elements #7

Open gregbaker opened 1 year ago

gregbaker commented 1 year ago

Unless I'm missing some intent of the attribute, the .is_empty attribute isn't be set to true for empty XML elements. I believe the given XML and the commented-out XML here are indistinguishable in xml_oxide as a result.

use xml_oxide::{sax::parser::Parser, sax::Event};

fn main() {
    let doc = "<doc><e/><e></e></doc>";
    //let doc = "<doc><e><e/></e></doc>";
    let mut p = Parser::from_reader(doc.as_bytes());

    loop {
        let res = p.read_event();

        match res {
            Ok(event) => match event {
                Event::EndDocument => {
                    break;
                }
                Event::StartElement(el) => {
                    println!("+ {:?} {:?}", el.name, el.is_empty);
                }
                Event::EndElement(el) => {
                    println!("- {:?}", el.name);
                }
                _ => {}
            },
            Err(_) => {
                break;
            }
        }
    }
}

That produces this output, but I think the second line should have a "true".

+ "doc" false
+ "e" false
+ "e" false
- "e"
- "doc"
gregbaker commented 1 year ago

Upon further investigation, the released 0.3.0 code has is_empty: false in the ContentRelaxed::EmptyElemTag case: https://docs.rs/xml_oxide/latest/src/xml_oxide/sax/parser.rs.html#520

But the master branch has is_empty: true: https://github.com/fatihpense/rust_xml_oxide/blob/master/src/sax/parser.rs#L545