//! Escaping, serialising, the writer, and what a round trip keeps. use craft_xml::{El, Node, Options, Writer, is_xml_char, parse}; /// Reads keeping everything the tree can hold. fn full() -> Options<'static> { Options { cdata: true, pis: true, comments: true, ..Options::default() } } #[test] fn every_character_survives_a_round_trip_or_is_dropped() { let all: String = (0..0x3000u32).chain([0xD7FF, 0xE000, 0xFFFD, 0xFFFE, 0xFFFF, 0x1_0000, 0x10_FFFF]).filter_map(char::from_u32).collect(); let kept: String = all.chars().filter(|c| is_xml_char(*c)).collect(); let root = El::new("a").with_attr("v", &all).with_text(&all); let back = parse(root.to_xml().as_bytes()).unwrap(); assert_eq!(back.attr("v"), Some(kept.as_str())); assert_eq!(back.text(), kept); let mut w = Writer::new(); w.tag("a").attr("v", &all).text(&all); assert_eq!(parse(w.finish().as_bytes()).unwrap(), back); } /// A name from outside cannot become markup: the element or attribute is left out. #[test] fn a_name_that_is_not_a_name_is_not_written() { let root = El::new("r") .with_attr("ok", 1) .with_attr("x onload=\"evil()\"", 2) .with_attr("", 3) .with_child(El::new("a>b".into() }) .with_node(Node::Comment("a--b".into())) .with_node(Node::Comment("ends-".into())); assert_eq!(root.to_xml(), "kept"); assert_eq!(root.to_xml_pretty(" "), "kept\n"); assert_eq!(El::new("not a name").to_xml(), ""); // An element whose content was all left out is written the way it reads back. let hollow = El::new("r").with_child(El::new("a").with_child(El::new("1")).with_text("\u{1}")).with_child(El::new("b").with_child(El::new("2"))); assert_eq!(hollow.to_xml(), ""); assert_eq!(hollow.to_xml_pretty(" "), "\n \n \n\n"); } #[test] fn compact_round_trip_keeps_what_the_tree_holds() { let docs = [ "", "x & y", " leading\n\ttrailing mixed text ", "&<> \n\t", " & ]]]]> end]]>t", "deep", "<é:日本 ü=\"ö\">😀", ]; for doc in docs { let tree = full().parse(doc.as_bytes()).unwrap(); assert_eq!(tree.to_xml(), doc, "these are already in the form the writer uses"); assert_eq!(full().parse(tree.to_xml().as_bytes()).unwrap(), tree); } } #[test] fn pretty_output() { let tree = parse(b"textmixed content ").unwrap(); let pretty = tree.to_xml_pretty(" "); assert_eq!(pretty, "\n \n \n text\n mixed content\n \n \n \n\n"); // Character data is unchanged: reading it back without the indentation gives the tree. let back = Options { drop_blank_text: true, ..Options::default() }.parse(pretty.as_bytes()).unwrap(); assert_eq!(back, tree); // Printing what was printed changes nothing. assert_eq!(parse(pretty.as_bytes()).unwrap().to_xml_pretty(" "), pretty); assert_eq!(tree.to_xml_pretty("\t").lines().nth(2), Some("\t\t")); // An element with a comment or an instruction among its children is written on one line. let noted = full().parse(b"").unwrap(); assert_eq!(noted.to_xml_pretty(" "), "\n"); } #[test] fn writer_closes_names_in_order_and_what_is_left_open() { let mut w = Writer::new(); for name in ["a", "w:body", "日本", "d"] { w.open(name, &[]); } w.close(); w.empty("e", &[]); assert_eq!(w.finish(), "<日本>"); }