From 1c3a701d2e5847586cdadaebc18f6482c721878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 16:48:07 +0100 Subject: [PATCH 1/5] element: Fix element name comparison in from_reader quick-xml's Events seem to always return prefixed names, and the from_reader function, when comparing name of the pop-ed element, and received event element, was using the unprefixed name to compare. --- src/element.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/element.rs b/src/element.rs index e398d40f..4494d411 100644 --- a/src/element.rs +++ b/src/element.rs @@ -191,6 +191,11 @@ impl Element { &self.name } + /// Returns a reference to the prefix of this element. + pub fn prefix(&self) -> Option { + self.prefix.clone() + } + /// Returns a reference to the namespace of this element, if it has one, else `None`. pub fn ns(&self) -> Option { self.namespaces.get(&self.prefix) @@ -318,7 +323,11 @@ impl Element { } let elem = stack.pop().unwrap(); if let Some(to) = stack.last_mut() { - if elem.name().as_bytes() != e.name() { + let name = match elem.prefix() { + Some(ref prefix) => format!("{}:", prefix.clone()), + None => String::from(""), + } + elem.name(); + if name.as_bytes() != e.name() { bail!(ErrorKind::InvalidElementClosed); } to.append_child(elem); From 7d009923b71e3c30185e7872ca59bfe292ca2375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 16:54:03 +0100 Subject: [PATCH 2/5] Update author list --- Cargo.toml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e04d8b04..9a29a616 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,13 @@ [package] name = "minidom" version = "0.7.0" -authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel ", "Astro "] +authors = [ + "lumi ", + "Emmanuel Gil Peyrot ", + "Bastien Orivel ", + "Astro ", + "Maxime “pep” Buquet ", +] description = "A small, simple DOM implementation on top of quick-xml" homepage = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs" From e250e9cf2c9e261ce0011713b1881c44dcde882a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 30 Dec 2017 17:24:54 +0100 Subject: [PATCH 3/5] Add doctest for prefix fn --- src/element.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/element.rs b/src/element.rs index 4494d411..b2cde3a3 100644 --- a/src/element.rs +++ b/src/element.rs @@ -192,6 +192,17 @@ impl Element { } /// Returns a reference to the prefix of this element. + /// + /// # Examples + /// ```rust + /// use minidom::Element; + /// + /// let elem = Element::builder("prefix:name") + /// .build(); + /// + /// assert_eq!(elem.name(), "name"); + /// assert_eq!(elem.prefix(), Some("prefix".to_owned())); + /// ``` pub fn prefix(&self) -> Option { self.prefix.clone() } From 9c498044ee15ee0afc8df7a7c8d32f5d49aea073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 31 Dec 2017 07:19:24 +0100 Subject: [PATCH 4/5] element: Add tests for Element::from_reader --- src/element.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/element.rs b/src/element.rs index b2cde3a3..e363cbb6 100644 --- a/src/element.rs +++ b/src/element.rs @@ -843,3 +843,46 @@ fn test_element_new() { assert_eq!(elem.attr("name"), Some("value")); assert_eq!(elem.attr("inexistent"), None); } + +#[test] +fn test_from_reader_simple() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let elem2 = Element::builder("foo").build(); + + assert_eq!(elem.unwrap(), elem2); +} + +#[test] +fn test_from_reader_nested() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let nested = Element::builder("bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); + + assert_eq!(elem.unwrap(), elem2); +} + +#[test] +fn test_from_reader_with_prefix() { + let xml = ""; + let mut reader = EventReader::from_str(xml); + let elem = Element::from_reader(&mut reader); + + let nested = Element::builder("prefix:bar") + .attr("baz", "qxx") + .build(); + let elem2 = Element::builder("foo") + .append(nested) + .build(); + + assert_eq!(elem.unwrap(), elem2); +} From db70487a31e290ea04f44010f7b765406e271ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 31 Dec 2017 15:24:28 +0000 Subject: [PATCH 5/5] Change prefix fn to return Option<&str> --- src/element.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index e363cbb6..c96424b6 100644 --- a/src/element.rs +++ b/src/element.rs @@ -201,10 +201,10 @@ impl Element { /// .build(); /// /// assert_eq!(elem.name(), "name"); - /// assert_eq!(elem.prefix(), Some("prefix".to_owned())); + /// assert_eq!(elem.prefix(), Some("prefix")); /// ``` - pub fn prefix(&self) -> Option { - self.prefix.clone() + pub fn prefix(&self) -> Option<&str> { + self.prefix.as_ref().map(String::as_ref) } /// Returns a reference to the namespace of this element, if it has one, else `None`. @@ -335,7 +335,7 @@ impl Element { let elem = stack.pop().unwrap(); if let Some(to) = stack.last_mut() { let name = match elem.prefix() { - Some(ref prefix) => format!("{}:", prefix.clone()), + Some(ref prefix) => format!("{}:", prefix), None => String::from(""), } + elem.name(); if name.as_bytes() != e.name() {