minidom: Don't prepend xml prelude in writer. Add new API
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
8042d6ea23
commit
141d11ad38
6 changed files with 29 additions and 11 deletions
|
@ -1,4 +1,6 @@
|
||||||
Version XXX, released YYY:
|
Version XXX, released YYY:
|
||||||
|
* Breaking
|
||||||
|
`Element.write_to` doesn't prepand xml prelude anymore. Use `write_to_decl`.
|
||||||
* Changes
|
* Changes
|
||||||
* Update edition to 2018
|
* Update edition to 2018
|
||||||
* Add NSChoice enum to allow comparing NSs differently
|
* Add NSChoice enum to allow comparing NSs differently
|
||||||
|
|
|
@ -414,8 +414,18 @@ impl Element {
|
||||||
self.to_writer(&mut EventWriter::new(writer))
|
self.to_writer(&mut EventWriter::new(writer))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Output a document to a `Writer`.
|
||||||
|
pub fn write_to_decl<W: Write>(&self, writer: &mut W) -> Result<()> {
|
||||||
|
self.to_writer_decl(&mut EventWriter::new(writer))
|
||||||
|
}
|
||||||
|
|
||||||
/// Output the document to quick-xml `Writer`
|
/// Output the document to quick-xml `Writer`
|
||||||
pub fn to_writer<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()> {
|
pub fn to_writer<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()> {
|
||||||
|
self.write_to_inner(writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Output the document to quick-xml `Writer`
|
||||||
|
pub fn to_writer_decl<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()> {
|
||||||
writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"utf-8"), None)))?;
|
writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"utf-8"), None)))?;
|
||||||
self.write_to_inner(writer)
|
self.write_to_inner(writer)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::element::Element;
|
||||||
|
|
||||||
use quick_xml::Reader;
|
use quick_xml::Reader;
|
||||||
|
|
||||||
const TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root xmlns="root_ns" a="b" xml:lang="en">meow<child c="d"/><child xmlns="child_ns" d="e" xml:lang="fr"/>nya</root>"#;
|
const TEST_STRING: &'static str = r#"<root xmlns="root_ns" a="b" xml:lang="en">meow<child c="d"/><child xmlns="child_ns" d="e" xml:lang="fr"/>nya</root>"#;
|
||||||
|
|
||||||
fn build_test_tree() -> Element {
|
fn build_test_tree() -> Element {
|
||||||
let mut root = Element::builder("root")
|
let mut root = Element::builder("root")
|
||||||
|
@ -24,7 +24,7 @@ fn build_test_tree() -> Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "comments")]
|
#[cfg(feature = "comments")]
|
||||||
const COMMENT_TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root><!--This is a child.--><child attr="val"><!--This is a grandchild.--><grandchild/></child></root>"#;
|
const COMMENT_TEST_STRING: &'static str = r#"<root><!--This is a child.--><child attr="val"><!--This is a grandchild.--><grandchild/></child></root>"#;
|
||||||
|
|
||||||
#[cfg(feature = "comments")]
|
#[cfg(feature = "comments")]
|
||||||
fn build_comment_test_tree() -> Element {
|
fn build_comment_test_tree() -> Element {
|
||||||
|
@ -57,6 +57,17 @@ fn writer_works() {
|
||||||
assert_eq!(String::from_utf8(writer).unwrap(), TEST_STRING);
|
assert_eq!(String::from_utf8(writer).unwrap(), TEST_STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn writer_with_decl_works() {
|
||||||
|
let root = build_test_tree();
|
||||||
|
let mut writer = Vec::new();
|
||||||
|
{
|
||||||
|
root.write_to_decl(&mut writer).unwrap();
|
||||||
|
}
|
||||||
|
let result = format!(r#"<?xml version="1.0" encoding="utf-8"?>{}"#, TEST_STRING);
|
||||||
|
assert_eq!(String::from_utf8(writer).unwrap(), result);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn writer_escapes_attributes() {
|
fn writer_escapes_attributes() {
|
||||||
let root = Element::builder("root").attr("a", "\"Air\" quotes").build();
|
let root = Element::builder("root").attr("a", "\"Air\" quotes").build();
|
||||||
|
@ -66,7 +77,7 @@ fn writer_escapes_attributes() {
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
String::from_utf8(writer).unwrap(),
|
String::from_utf8(writer).unwrap(),
|
||||||
r#"<?xml version="1.0" encoding="utf-8"?><root a=""Air" quotes"/>"#
|
r#"<root a=""Air" quotes"/>"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,10 +88,7 @@ fn writer_escapes_text() {
|
||||||
{
|
{
|
||||||
root.write_to(&mut writer).unwrap();
|
root.write_to(&mut writer).unwrap();
|
||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(String::from_utf8(writer).unwrap(), r#"<root><3</root>"#);
|
||||||
String::from_utf8(writer).unwrap(),
|
|
||||||
r#"<?xml version="1.0" encoding="utf-8"?><root><3</root>"#
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -23,6 +23,5 @@ trust-dns-resolver = "0.12"
|
||||||
trust-dns-proto = "0.8"
|
trust-dns-proto = "0.8"
|
||||||
tokio-io = "0.1"
|
tokio-io = "0.1"
|
||||||
tokio-tls = "0.2"
|
tokio-tls = "0.2"
|
||||||
quick-xml = "0.17"
|
|
||||||
xml5ever = "0.15"
|
xml5ever = "0.15"
|
||||||
xmpp-parsers = "0.16"
|
xmpp-parsers = "0.16"
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use crate::{ParseError, ParserError};
|
use crate::{ParseError, ParserError};
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
use quick_xml::Writer as EventWriter;
|
|
||||||
use std;
|
use std;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
@ -302,7 +301,7 @@ impl Encoder for XMPPCodec {
|
||||||
}
|
}
|
||||||
Packet::Stanza(stanza) => {
|
Packet::Stanza(stanza) => {
|
||||||
stanza
|
stanza
|
||||||
.write_to_inner(&mut EventWriter::new(WriteBytes::new(dst)))
|
.write_to(&mut WriteBytes::new(dst))
|
||||||
.and_then(|_| {
|
.and_then(|_| {
|
||||||
// println!(">> {:?}", dst);
|
// println!(">> {:?}", dst);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -607,7 +607,7 @@ mod tests {
|
||||||
assert_eq!(html, "Hello world!");
|
assert_eq!(html, "Hello world!");
|
||||||
|
|
||||||
let elem = Element::from(parsed2);
|
let elem = Element::from(parsed2);
|
||||||
assert_eq!(String::from(&elem), "<?xml version=\"1.0\" encoding=\"utf-8\"?><html xmlns=\"http://jabber.org/protocol/xhtml-im\"><body xmlns=\"http://www.w3.org/1999/xhtml\">Hello world!</body></html>");
|
assert_eq!(String::from(&elem), "<html xmlns=\"http://jabber.org/protocol/xhtml-im\"><body xmlns=\"http://www.w3.org/1999/xhtml\">Hello world!</body></html>");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue