Skip to content

Minidom force ns

pep requested to merge minidom-force-ns into master

minidom: forcing a namespace on Element. Stop requiring prefixes.

Below is what I think I did.

A few changes:

  • Change prefixes to be something less important in the API.
  • Rework the Element struct to force a namespace. In XMPP everything is namespaced.
  • Remove parent ref on what was previously NamespaceSet and is now Prefixes.
  • Make ElementBuilder::prefix fail on adding duplicate prefix
  • Ensure there is no colon in name when creating elements.
  • Clarify meaning of Element.name (being the local name) in docs

More specifically this means Element::new has changed to require Element's new properties as parameters. Element::builder and Element::bare now require a namespace unconditionally. Element::prefix has been removed.

This new API is based on the fact that prefixes are non-essential (really just an implementation detail) and shouldn't be visible to the user. It is possible nonetheless to set custom prefixes for compatibility reasons with ElementBuilder::prefix. A prefix is firstly mapped to a namespace, and then attached to an element, there cannot be a prefix without a namespace.

Prefix inheritance is used if possible but for the case with no prefix ("xmlns") to be reused, we only check custom prefixes declared on the tag itself and not ascendants. If it's already used then we generate prefixes (ns0, ns1, ..) checking on what has been declared on all ascendants (plus of course those already set on the current tag).

Example API:

let mut elem = ElementBuilder("stream", "http://etherx.jabber.org/streams")
  .prefix(Some(String::from("stream")), "http://etherx.jabber.org/streams")
  .prefix(None, "jabber:client")
  .attr(..)
  .build();

assert_eq!(elem.ns(), String::from("http://etherx.jabber.org/streams"));

See also the few tests added in src/tests.

TODO: Change Element::name method to Element::local_name to make it more explicit.
TODO: Return Result<Element> in Element::new to remove the unwrap call.

Signed-off-by: Maxime “pep” Buquet pep@bouah.net

Edited by pep

Merge request reports