From 171e7f1f344b6ff0b936b25da09f4d45b688b290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 28 Mar 2020 16:22:37 +0100 Subject: [PATCH] minidom: ensure prefix is extracted out of provided name when creating Element MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I would have liked to handle all of this in `Element::new` only, but I also have to do it in `Element::builder` unfortunately because then element builder then pushes prefixes it gathered itself. Signed-off-by: Maxime “pep” Buquet --- minidom-rs/src/element.rs | 26 +++++++++++++++++--------- minidom-rs/src/prefixes.rs | 4 ++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/minidom-rs/src/element.rs b/minidom-rs/src/element.rs index 19ff0b6..639218e 100644 --- a/minidom-rs/src/element.rs +++ b/minidom-rs/src/element.rs @@ -129,7 +129,16 @@ impl Element { attributes: BTreeMap, children: Vec, ) -> Element { - // TODO split name and possible prefix. + let (prefix, name) = split_element_name(name).unwrap(); + let namespace: String = namespace.into(); + let prefixes: Prefixes = match prefix { + None => prefixes.into(), + Some(_) => { + let mut p = prefixes.into(); + p.insert(namespace.clone(), prefix); + p + }, + }; Element { name, namespace, @@ -189,14 +198,13 @@ impl Element { /// assert_eq!(bare.text(), ""); /// ``` pub fn bare, NS: Into>(name: S, namespace: NS) -> Element { - // TODO split name and possible prefix. - Element { - name: name.into(), - namespace: namespace.into(), - prefixes: Rc::new(Prefixes::default()), - attributes: BTreeMap::new(), - children: Vec::new(), - } + Element::new( + name.into(), + namespace.into(), + BTreeMap::new(), + BTreeMap::new(), + Vec::new(), + ) } /// Returns a reference to the name of this element. diff --git a/minidom-rs/src/prefixes.rs b/minidom-rs/src/prefixes.rs index c0508b9..7625abd 100644 --- a/minidom-rs/src/prefixes.rs +++ b/minidom-rs/src/prefixes.rs @@ -52,6 +52,10 @@ impl Prefixes { None => None, } } + + pub(crate) fn insert>(&mut self, namespace: S, prefix: Option) { + self.prefixes.insert(namespace.into(), prefix); + } } impl From>> for Prefixes {