Skip to content

add_text should escape "'<>& #12

Description

@kitanic

This code generate bad XML

use xml_builder::{XMLBuilder, XMLElement, XMLVersion};

fn main() {
    let mut xml = XMLBuilder::new()
        .version(XMLVersion::XML1_1)
        .encoding("utf-8".into())
        .build();

    let mut text = XMLElement::new("text");
    text.add_text(r#"&"'<>"#.into()).unwrap();

    xml.set_root_element(text);
    let mut writer = Vec::new();
    xml.generate(&mut writer).unwrap();

    let _str = String::from_utf8(writer).unwrap();
    print!("{}", _str)
}

result:

<?xml version="1.1" encoding="utf-8"?>
<text>&"'<></text>

expect:

<?xml version="1.1" encoding="utf-8"?>
<text>&amp;&quot;&apos;&lt;&gt;</text>

I think it is not reasonable behavior for users to have to perform escaping manually.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions