use markup5ever_rcdom::{RcDom, SerializableHandle}; use xml5ever::driver; use xml5ever::serialize; use xml5ever::tendril::TendrilSink; #[test] fn el_ns_serialize() { assert_eq_serialization( "Test", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("Test".as_bytes()), ); } #[test] fn nested_ns_serialize() { assert_eq_serialization("", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("".as_bytes())); } #[test] fn def_ns_serialize() { assert_eq_serialization( "
", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("
".as_bytes()), ); } #[test] fn undefine_ns_serialize() { assert_eq_serialization( "", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one( "" .as_bytes(), ), ); } #[test] fn redefine_default_ns_serialize() { assert_eq_serialization( "", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("".as_bytes()), ); } #[test] fn attr_serialize() { assert_serialization( "Test", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("Test".as_bytes()), ); } #[test] fn from_utf8() { assert_serialization( "<title>Test", driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one("Test".as_bytes()), ); } fn assert_eq_serialization(text: &'static str, dom: RcDom) { let mut serialized = Vec::new(); let document: SerializableHandle = dom.document.clone().into(); serialize::serialize(&mut serialized, &document, Default::default()).unwrap(); let dom_from_text = driver::parse_document(RcDom::default(), Default::default()) .from_utf8() .one(text.as_bytes()); let mut reserialized = Vec::new(); let document: SerializableHandle = dom_from_text.document.clone().into(); serialize::serialize(&mut reserialized, &document, Default::default()).unwrap(); assert_eq!( String::from_utf8(serialized).unwrap(), String::from_utf8(reserialized).unwrap() ); } fn assert_serialization(text: &'static str, dom: RcDom) { let mut serialized = Vec::new(); let document: SerializableHandle = dom.document.clone().into(); serialize::serialize(&mut serialized, &document, Default::default()).unwrap(); assert_eq!(String::from_utf8(serialized).unwrap(), text); }