Merge branch 'compare-with-prefix' into 'master'

Compare with prefix

See merge request lumi/minidom-rs!23
This commit is contained in:
lumi 2018-01-01 12:26:19 +00:00
commit 0e76e5211d
2 changed files with 71 additions and 2 deletions

View file

@ -1,7 +1,13 @@
[package] [package]
name = "minidom" name = "minidom"
version = "0.7.0" version = "0.7.0"
authors = ["lumi <lumi@pew.im>", "Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>", "Bastien Orivel <eijebong+minidom@bananium.fr>", "Astro <astro@spaceboyz.net>"] authors = [
"lumi <lumi@pew.im>",
"Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>",
"Bastien Orivel <eijebong+minidom@bananium.fr>",
"Astro <astro@spaceboyz.net>",
"Maxime “pep” Buquet <pep@bouah.net>",
]
description = "A small, simple DOM implementation on top of quick-xml" description = "A small, simple DOM implementation on top of quick-xml"
homepage = "https://gitlab.com/lumi/minidom-rs" homepage = "https://gitlab.com/lumi/minidom-rs"
repository = "https://gitlab.com/lumi/minidom-rs" repository = "https://gitlab.com/lumi/minidom-rs"

View file

@ -191,6 +191,22 @@ impl Element {
&self.name &self.name
} }
/// 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"));
/// ```
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`. /// Returns a reference to the namespace of this element, if it has one, else `None`.
pub fn ns(&self) -> Option<String> { pub fn ns(&self) -> Option<String> {
self.namespaces.get(&self.prefix) self.namespaces.get(&self.prefix)
@ -324,7 +340,11 @@ impl Element {
} }
let elem = stack.pop().unwrap(); let elem = stack.pop().unwrap();
if let Some(to) = stack.last_mut() { if let Some(to) = stack.last_mut() {
if elem.name().as_bytes() != e.name() { let name = match elem.prefix() {
Some(ref prefix) => format!("{}:", prefix),
None => String::from(""),
} + elem.name();
if name.as_bytes() != e.name() {
bail!(ErrorKind::InvalidElementClosed); bail!(ErrorKind::InvalidElementClosed);
} }
to.append_child(elem); to.append_child(elem);
@ -832,3 +852,46 @@ fn test_element_new() {
assert_eq!(elem.attr("name"), Some("value")); assert_eq!(elem.attr("name"), Some("value"));
assert_eq!(elem.attr("inexistent"), None); assert_eq!(elem.attr("inexistent"), None);
} }
#[test]
fn test_from_reader_simple() {
let xml = "<foo></foo>";
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 = "<foo><bar baz='qxx' /></foo>";
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 = "<foo><prefix:bar baz='qxx' /></foo>";
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);
}