minidom: strictly compare whitespace in Element's PartialEq impl

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2019-12-07 17:39:49 +01:00 committed by Maxime Buquet
parent c9e6e3d095
commit e62e870ef0

View file

@ -99,21 +99,9 @@ impl PartialEq for Element {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if self.name() == other.name() && self.ns() == other.ns() && self.attrs().eq(other.attrs()) if self.name() == other.name() && self.ns() == other.ns() && self.attrs().eq(other.attrs())
{ {
let child_elems = self.children().count(); self.nodes()
let text_is_whitespace = self .zip(other.nodes())
.texts() .all(|(node1, node2)| node1 == node2)
.all(|text| text.chars().all(char::is_whitespace));
if child_elems > 0 && text_is_whitespace {
// Ignore all the whitespace text nodes
self.children()
.zip(other.children())
.all(|(node1, node2)| node1 == node2)
} else {
// Compare with text nodes
self.nodes()
.zip(other.nodes())
.all(|(node1, node2)| node1 == node2)
}
} else { } else {
false false
} }
@ -1076,4 +1064,25 @@ mod tests {
let elem = Element::from_reader(&mut reader).unwrap(); let elem = Element::from_reader(&mut reader).unwrap();
assert_eq!(elem.text(), "&apos;&gt;blah<blah>"); assert_eq!(elem.text(), "&apos;&gt;blah<blah>");
} }
#[test]
fn test_compare_all_ns() {
let xml = "<foo xmlns='foo' xmlns:bar='baz'><bar:meh/></foo>";
let mut reader = EventReader::from_str(xml);
let elem = Element::from_reader(&mut reader).unwrap();
let elem2 = elem.clone();
let xml3 = "<foo xmlns='foo'><bar:meh xmlns:bar='baz'/></foo>";
let mut reader3 = EventReader::from_str(xml3);
let elem3 = Element::from_reader(&mut reader3).unwrap();
let xml4 = "<prefix:foo xmlns:prefix='foo'><bar:meh xmlns:bar='baz'/></prefix:foo>";
let mut reader4 = EventReader::from_str(xml4);
let elem4 = Element::from_reader(&mut reader4).unwrap();
assert_eq!(elem, elem2);
assert_eq!(elem, elem3);
assert_eq!(elem, elem4);
}
} }