2020-02-14 23:40:38 +00:00
// Copyright (c) 2020 lumi <lumi@pew.im>
// Copyright (c) 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
// Copyright (c) 2020 Bastien Orivel <eijebong+minidom@bananium.fr>
// Copyright (c) 2020 Maxime “pep” Buquet <pep@bouah.net>
// Copyright (c) 2020 Yue Liu <amznyue@amazon.com>
// Copyright (c) 2020 Matt Bilker <me@mbilker.us>
// Copyright (c) 2020 Xidorn Quan <me@upsuper.org>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
2017-03-08 19:34:17 +00:00
//! Provides an `Element` type, which represents DOM nodes, and a builder to create them with.
2019-09-13 01:07:02 +00:00
use crate ::convert ::IntoAttributeValue ;
use crate ::error ::{ Error , Result } ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
use crate ::namespaces ::NSChoice ;
2020-03-29 16:32:16 +00:00
use crate ::prefixes ::{ Prefix , Namespace , Prefixes } ;
2019-09-13 01:07:02 +00:00
use crate ::node ::Node ;
2017-06-07 20:40:53 +00:00
use std ::collections ::{ btree_map , BTreeMap } ;
2019-10-22 23:32:41 +00:00
use std ::io ::Write ;
2017-03-08 19:34:17 +00:00
2017-08-12 00:13:32 +00:00
use std ::borrow ::Cow ;
2019-10-22 23:32:41 +00:00
use std ::str ;
2017-03-08 19:34:17 +00:00
2019-10-22 23:32:41 +00:00
use quick_xml ::events ::{ BytesDecl , BytesEnd , BytesStart , Event } ;
2018-04-11 01:01:18 +00:00
use quick_xml ::Reader as EventReader ;
use quick_xml ::Writer as EventWriter ;
2017-06-07 20:40:53 +00:00
use std ::io ::BufRead ;
2017-03-08 19:34:17 +00:00
use std ::str ::FromStr ;
use std ::slice ;
2018-01-06 04:20:12 +00:00
/// helper function to escape a `&[u8]` and replace all
/// xml special characters (<, >, &, ', ") with their corresponding
/// xml escaped value.
pub fn escape ( raw : & [ u8 ] ) -> Cow < [ u8 ] > {
let mut escapes : Vec < ( usize , & 'static [ u8 ] ) > = Vec ::new ( ) ;
let mut bytes = raw . iter ( ) ;
fn to_escape ( b : u8 ) -> bool {
match b {
b '<' | b '>' | b '\'' | b '&' | b '"' = > true ,
_ = > false ,
2017-08-12 00:05:18 +00:00
}
}
2018-01-06 04:20:12 +00:00
let mut loc = 0 ;
while let Some ( i ) = bytes . position ( | & b | to_escape ( b ) ) {
loc + = i ;
match raw [ loc ] {
b '<' = > escapes . push ( ( loc , b " < " ) ) ,
b '>' = > escapes . push ( ( loc , b " > " ) ) ,
b '\'' = > escapes . push ( ( loc , b " ' " ) ) ,
b '&' = > escapes . push ( ( loc , b " & " ) ) ,
b '"' = > escapes . push ( ( loc , b " " " ) ) ,
_ = > unreachable! ( " Only '<', '>',' \' , '&' and ' \" ' are escaped " ) ,
}
loc + = 1 ;
}
if escapes . is_empty ( ) {
Cow ::Borrowed ( raw )
} else {
let len = raw . len ( ) ;
let mut v = Vec ::with_capacity ( len ) ;
let mut start = 0 ;
for ( i , r ) in escapes {
v . extend_from_slice ( & raw [ start .. i ] ) ;
v . extend_from_slice ( r ) ;
start = i + 1 ;
}
if start < len {
v . extend_from_slice ( & raw [ start .. ] ) ;
}
Cow ::Owned ( v )
}
2017-08-12 00:05:18 +00:00
}
2019-11-29 13:33:17 +00:00
#[ derive(Clone, Eq, Debug) ]
2017-03-08 19:34:17 +00:00
/// A struct representing a DOM Element.
pub struct Element {
name : String ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
namespace : String ,
2020-03-29 16:32:16 +00:00
/// This is only used when deserializing. If you have to use a custom prefix use
/// `ElementBuilder::prefix`.
prefix : Option < Prefix > ,
2020-03-29 15:41:21 +00:00
prefixes : Prefixes ,
2017-04-26 21:44:58 +00:00
attributes : BTreeMap < String , String > ,
2017-03-08 19:34:17 +00:00
children : Vec < Node > ,
}
2017-05-02 15:30:31 +00:00
impl < ' a > From < & ' a Element > for String {
fn from ( elem : & ' a Element ) -> String {
2017-06-07 20:40:53 +00:00
let mut writer = Vec ::new ( ) ;
elem . write_to ( & mut writer ) . unwrap ( ) ;
String ::from_utf8 ( writer ) . unwrap ( )
2017-05-02 15:30:31 +00:00
}
}
2017-03-08 19:34:17 +00:00
impl FromStr for Element {
type Err = Error ;
2017-05-22 17:20:01 +00:00
fn from_str ( s : & str ) -> Result < Element > {
2017-06-07 20:40:53 +00:00
let mut reader = EventReader ::from_str ( s ) ;
2017-03-08 19:34:17 +00:00
Element ::from_reader ( & mut reader )
}
}
2019-11-29 13:33:17 +00:00
impl PartialEq for Element {
fn eq ( & self , other : & Self ) -> bool {
if self . name ( ) = = other . name ( ) & & self . ns ( ) = = other . ns ( ) & & self . attrs ( ) . eq ( other . attrs ( ) )
{
2019-12-07 16:39:49 +00:00
self . nodes ( )
. zip ( other . nodes ( ) )
. all ( | ( node1 , node2 ) | node1 = = node2 )
2019-11-29 13:33:17 +00:00
} else {
false
}
}
}
2017-03-08 19:34:17 +00:00
impl Element {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
fn new < P : Into < Prefixes > > (
2019-10-22 23:32:41 +00:00
name : String ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
namespace : String ,
2020-03-29 16:32:16 +00:00
prefix : Option < Prefix > ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
prefixes : P ,
2019-10-22 23:32:41 +00:00
attributes : BTreeMap < String , String > ,
children : Vec < Node > ,
) -> Element {
2020-03-29 16:32:16 +00:00
// TODO: Check that "name" doesn't contain ":". We've stopped accepting the "prefix:local"
// format.
2017-03-08 19:34:17 +00:00
Element {
2019-10-22 23:32:41 +00:00
name ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
namespace ,
2020-03-29 16:32:16 +00:00
prefix ,
prefixes : prefixes . into ( ) ,
2019-02-21 20:06:23 +00:00
attributes ,
children ,
2017-03-08 19:34:17 +00:00
}
}
/// Return a builder for an `Element` with the given `name`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem = Element::builder("name", "namespace")
2017-03-08 19:34:17 +00:00
/// .attr("name", "value")
/// .append("inner")
/// .build();
///
/// assert_eq!(elem.name(), "name");
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// assert_eq!(elem.ns(), "namespace".to_owned());
2017-03-08 19:34:17 +00:00
/// assert_eq!(elem.attr("name"), Some("value"));
/// assert_eq!(elem.attr("inexistent"), None);
/// assert_eq!(elem.text(), "inner");
/// ```
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
pub fn builder < S : AsRef < str > , NS : Into < String > > ( name : S , namespace : NS ) -> ElementBuilder {
2017-03-08 19:34:17 +00:00
ElementBuilder {
2020-03-29 16:32:16 +00:00
root : Element ::new (
name . as_ref ( ) . to_string ( ) ,
namespace . into ( ) ,
None ,
None ,
BTreeMap ::new ( ) ,
Vec ::new ( )
) ,
2017-03-08 19:34:17 +00:00
}
}
/// Returns a bare minimum `Element` with this name.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let bare = Element::bare("name", "namespace");
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(bare.name(), "name");
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// assert_eq!(bare.ns(), "namespace");
2017-03-08 19:34:17 +00:00
/// assert_eq!(bare.attr("name"), None);
/// assert_eq!(bare.text(), "");
/// ```
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
pub fn bare < S : Into < String > , NS : Into < String > > ( name : S , namespace : NS ) -> Element {
2020-03-28 15:22:37 +00:00
Element ::new (
name . into ( ) ,
namespace . into ( ) ,
2020-03-29 16:32:16 +00:00
None ,
2020-03-28 15:22:37 +00:00
BTreeMap ::new ( ) ,
BTreeMap ::new ( ) ,
Vec ::new ( ) ,
)
2017-03-08 19:34:17 +00:00
}
2020-03-28 15:41:48 +00:00
/// Returns a reference to the local name of this element (that is, without a possible prefix).
2017-03-08 19:34:17 +00:00
pub fn name ( & self ) -> & str {
& self . name
}
/// Returns a reference to the namespace of this element, if it has one, else `None`.
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
pub fn ns ( & self ) -> String {
self . namespace . clone ( )
2017-03-08 19:34:17 +00:00
}
/// Returns a reference to the value of the given attribute, if it exists, else `None`.
pub fn attr ( & self , name : & str ) -> Option < & str > {
2017-04-26 21:44:58 +00:00
if let Some ( value ) = self . attributes . get ( name ) {
2019-10-22 23:32:41 +00:00
return Some ( value ) ;
2017-03-08 19:34:17 +00:00
}
None
}
2017-05-14 14:38:56 +00:00
/// Returns an iterator over the attributes of this element.
///
/// # Example
///
/// ```rust
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elm: Element = "<elem xmlns=\"ns1\" a=\"b\" />".parse().unwrap();
2017-05-14 14:38:56 +00:00
///
/// let mut iter = elm.attrs();
///
/// assert_eq!(iter.next().unwrap(), ("a", "b"));
/// assert_eq!(iter.next(), None);
/// ```
2017-05-22 16:45:43 +00:00
pub fn attrs ( & self ) -> Attrs {
2017-05-14 14:38:56 +00:00
Attrs {
iter : self . attributes . iter ( ) ,
}
}
/// Returns an iterator over the attributes of this element, with the value being a mutable
/// reference.
2017-05-22 16:45:43 +00:00
pub fn attrs_mut ( & mut self ) -> AttrsMut {
2017-05-14 14:38:56 +00:00
AttrsMut {
iter : self . attributes . iter_mut ( ) ,
}
}
2017-03-08 19:34:17 +00:00
/// Modifies the value of an attribute.
pub fn set_attr < S : Into < String > , V : IntoAttributeValue > ( & mut self , name : S , val : V ) {
let name = name . into ( ) ;
let val = val . into_attribute_value ( ) ;
2017-04-26 21:44:58 +00:00
if let Some ( value ) = self . attributes . get_mut ( & name ) {
2019-10-22 23:32:41 +00:00
* value = val
. expect ( " removing existing value via set_attr, this is not yet supported (TODO) " ) ; // TODO
2017-04-26 21:44:58 +00:00
return ;
2017-03-08 19:34:17 +00:00
}
2017-04-26 21:44:58 +00:00
2017-03-08 19:34:17 +00:00
if let Some ( val ) = val {
2017-04-26 21:44:58 +00:00
self . attributes . insert ( name , val ) ;
2017-03-08 19:34:17 +00:00
}
}
/// Returns whether the element has the given name and namespace.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2019-11-09 00:26:48 +00:00
/// use minidom::{Element, NSChoice};
2017-03-08 19:34:17 +00:00
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem = Element::builder("name", "namespace").build();
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(elem.is("name", "namespace"), true);
/// assert_eq!(elem.is("name", "wrong"), false);
/// assert_eq!(elem.is("wrong", "namespace"), false);
/// assert_eq!(elem.is("wrong", "wrong"), false);
2019-11-09 00:26:48 +00:00
///
/// assert_eq!(elem.is("name", NSChoice::OneOf("namespace")), true);
/// assert_eq!(elem.is("name", NSChoice::OneOf("foo")), false);
2019-11-26 11:47:35 +00:00
/// assert_eq!(elem.is("name", NSChoice::AnyOf(&["foo", "namespace"])), true);
2019-11-09 00:26:48 +00:00
/// assert_eq!(elem.is("name", NSChoice::Any), true);
2017-03-08 19:34:17 +00:00
/// ```
2019-11-09 00:26:48 +00:00
pub fn is < ' a , N : AsRef < str > , NS : Into < NSChoice < ' a > > > ( & self , name : N , namespace : NS ) -> bool {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
self . name = = name . as_ref ( ) & & namespace . into ( ) . compare ( self . namespace . as_ref ( ) )
2017-08-18 23:17:45 +00:00
}
/// Returns whether the element has the given namespace.
///
/// # Examples
///
/// ```rust
2019-11-09 00:26:48 +00:00
/// use minidom::{Element, NSChoice};
2017-08-18 23:17:45 +00:00
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem = Element::builder("name", "namespace").build();
2017-08-18 23:17:45 +00:00
///
/// assert_eq!(elem.has_ns("namespace"), true);
/// assert_eq!(elem.has_ns("wrong"), false);
2019-11-09 00:26:48 +00:00
///
/// assert_eq!(elem.has_ns(NSChoice::OneOf("namespace")), true);
/// assert_eq!(elem.has_ns(NSChoice::OneOf("foo")), false);
2019-11-26 11:47:35 +00:00
/// assert_eq!(elem.has_ns(NSChoice::AnyOf(&["foo", "namespace"])), true);
2019-11-09 00:26:48 +00:00
/// assert_eq!(elem.has_ns(NSChoice::Any), true);
2017-08-18 23:17:45 +00:00
/// ```
2019-11-09 00:26:48 +00:00
pub fn has_ns < ' a , NS : Into < NSChoice < ' a > > > ( & self , namespace : NS ) -> bool {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
namespace . into ( ) . compare ( self . namespace . as_ref ( ) )
2017-03-08 19:34:17 +00:00
}
/// Parse a document from an `EventReader`.
2017-06-07 20:40:53 +00:00
pub fn from_reader < R : BufRead > ( reader : & mut EventReader < R > ) -> Result < Element > {
let mut buf = Vec ::new ( ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let mut prefixes = BTreeMap ::new ( ) ;
2017-08-18 23:34:44 +00:00
let root : Element = loop {
2017-06-07 20:40:53 +00:00
let e = reader . read_event ( & mut buf ) ? ;
2017-03-08 19:34:17 +00:00
match e {
2017-06-07 20:40:53 +00:00
Event ::Empty ( ref e ) | Event ::Start ( ref e ) = > {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
break build_element ( reader , e , & mut prefixes ) ? ;
2019-10-22 23:32:41 +00:00
}
2017-06-07 20:40:53 +00:00
Event ::Eof = > {
2018-02-18 18:54:09 +00:00
return Err ( Error ::EndOfDocument ) ;
2019-10-22 23:32:41 +00:00
}
2019-08-22 16:04:47 +00:00
Event ::Comment { .. } = > {
2020-03-26 18:50:34 +00:00
return Err ( Error ::NoComments ) ;
2019-08-22 16:04:47 +00:00
}
2019-10-22 23:32:41 +00:00
Event ::Text { .. }
| Event ::End { .. }
| Event ::CData { .. }
| Event ::Decl { .. }
| Event ::PI { .. }
| Event ::DocType { .. } = > ( ) , // TODO: may need more errors
2017-03-08 19:34:17 +00:00
}
2017-06-07 20:40:53 +00:00
} ;
let mut stack = vec! [ root ] ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let mut prefix_stack = vec! [ prefixes ] ;
2017-03-08 19:34:17 +00:00
loop {
2017-06-07 20:40:53 +00:00
match reader . read_event ( & mut buf ) ? {
Event ::Empty ( ref e ) = > {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let mut prefixes = prefix_stack . last ( ) . unwrap ( ) . clone ( ) ;
let elem = build_element ( reader , e , & mut prefixes ) ? ;
2017-06-07 20:40:53 +00:00
// Since there is no Event::End after, directly append it to the current node
stack . last_mut ( ) . unwrap ( ) . append_child ( elem ) ;
2019-10-22 23:32:41 +00:00
}
2017-06-07 20:40:53 +00:00
Event ::Start ( ref e ) = > {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let mut prefixes = prefix_stack . last ( ) . unwrap ( ) . clone ( ) ;
let elem = build_element ( reader , e , & mut prefixes ) ? ;
2017-06-07 20:40:53 +00:00
stack . push ( elem ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
prefix_stack . push ( prefixes ) ;
2019-10-22 23:32:41 +00:00
}
2017-06-07 20:40:53 +00:00
Event ::End ( ref e ) = > {
if stack . len ( ) < = 1 {
break ;
}
2020-03-29 16:32:16 +00:00
let prefixes = prefix_stack . pop ( ) . unwrap ( ) ;
2017-06-07 20:40:53 +00:00
let elem = stack . pop ( ) . unwrap ( ) ;
if let Some ( to ) = stack . last_mut ( ) {
2018-01-01 13:01:03 +00:00
// TODO: check whether this is correct, we are comparing &[u8]s, not &strs
let elem_name = e . name ( ) ;
let mut split_iter = elem_name . splitn ( 2 , | u | * u = = 0x3A ) ;
let possible_prefix = split_iter . next ( ) . unwrap ( ) ; // Can't be empty.
2020-03-29 16:32:16 +00:00
let opening_prefix = {
let mut tmp : Option < Option < String > > = None ;
for ( prefix , ns ) in prefixes {
if ns = = elem . namespace {
tmp = Some ( prefix . clone ( ) ) ;
break ;
}
}
match tmp {
Some ( prefix ) = > prefix ,
None = > return Err ( Error ::InvalidPrefix ) ,
}
} ;
2018-01-01 13:01:03 +00:00
match split_iter . next ( ) {
2020-03-29 16:32:16 +00:00
// There is a prefix on the closing tag
2018-01-01 13:01:03 +00:00
Some ( name ) = > {
2020-03-29 16:32:16 +00:00
// Does the closing prefix match the opening prefix?
match opening_prefix {
Some ( prefix ) if possible_prefix = = prefix . as_bytes ( ) = > ( ) ,
_ = > return Err ( Error ::InvalidElementClosed ) ,
2018-01-01 13:01:03 +00:00
}
2020-03-29 16:32:16 +00:00
// Does the closing tag name match the opening tag name?
2018-01-01 13:01:03 +00:00
if name ! = elem . name ( ) . as_bytes ( ) {
2018-02-18 18:54:09 +00:00
return Err ( Error ::InvalidElementClosed ) ;
2018-01-01 13:01:03 +00:00
}
2019-10-22 23:32:41 +00:00
}
2020-03-29 16:32:16 +00:00
// There was no prefix on the closing tag
2018-01-01 13:01:03 +00:00
None = > {
2020-03-29 16:32:16 +00:00
// Is there a prefix on the opening tag?
match opening_prefix {
Some ( _ ) = > return Err ( Error ::InvalidElementClosed ) ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
_ = > ( ) ,
2018-01-01 13:01:03 +00:00
}
2020-03-29 16:32:16 +00:00
// Does the opening tag name match the closing one?
2018-01-01 13:01:03 +00:00
if possible_prefix ! = elem . name ( ) . as_bytes ( ) {
2018-02-18 18:54:09 +00:00
return Err ( Error ::InvalidElementClosed ) ;
2018-01-01 13:01:03 +00:00
}
2019-10-22 23:32:41 +00:00
}
2017-06-07 20:40:53 +00:00
}
to . append_child ( elem ) ;
}
2019-10-22 23:32:41 +00:00
}
2018-05-29 13:59:36 +00:00
Event ::Text ( s ) = > {
2017-06-07 20:40:53 +00:00
let text = s . unescape_and_decode ( reader ) ? ;
if text ! = " " {
2019-06-12 16:22:51 +00:00
let current_elem = stack . last_mut ( ) . unwrap ( ) ;
2017-06-07 20:40:53 +00:00
current_elem . append_text_node ( text ) ;
}
2019-10-22 23:32:41 +00:00
}
2018-05-29 13:59:36 +00:00
Event ::CData ( s ) = > {
2019-09-05 18:25:04 +00:00
let text = reader . decode ( & s ) ? . to_owned ( ) ;
2018-05-29 13:59:36 +00:00
if text ! = " " {
2019-06-12 16:22:51 +00:00
let current_elem = stack . last_mut ( ) . unwrap ( ) ;
2018-05-29 13:59:36 +00:00
current_elem . append_text_node ( text ) ;
}
2019-10-22 23:32:41 +00:00
}
2017-06-07 20:40:53 +00:00
Event ::Eof = > {
break ;
2019-10-22 23:32:41 +00:00
}
2020-03-26 18:50:34 +00:00
Event ::Comment ( _ ) = > return Err ( Error ::NoComments ) ,
2019-10-22 23:32:41 +00:00
Event ::Decl { .. } | Event ::PI { .. } | Event ::DocType { .. } = > ( ) ,
2017-03-08 19:34:17 +00:00
}
}
2017-06-07 20:40:53 +00:00
Ok ( stack . pop ( ) . unwrap ( ) )
2017-03-08 19:34:17 +00:00
}
2017-06-07 20:40:53 +00:00
/// Output a document to a `Writer`.
pub fn write_to < W : Write > ( & self , writer : & mut W ) -> Result < ( ) > {
2018-01-06 04:20:12 +00:00
self . to_writer ( & mut EventWriter ::new ( writer ) )
}
2019-11-10 00:01:34 +00:00
/// Output a document to a `Writer`.
pub fn write_to_decl < W : Write > ( & self , writer : & mut W ) -> Result < ( ) > {
self . to_writer_decl ( & mut EventWriter ::new ( writer ) )
}
2018-01-06 04:20:12 +00:00
/// Output the document to quick-xml `Writer`
pub fn to_writer < W : Write > ( & self , writer : & mut EventWriter < W > ) -> Result < ( ) > {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
self . write_to_inner ( writer , & mut BTreeMap ::new ( ) )
2019-11-10 00:01:34 +00:00
}
/// Output the document to quick-xml `Writer`
pub fn to_writer_decl < W : Write > ( & self , writer : & mut EventWriter < W > ) -> Result < ( ) > {
2018-01-06 04:20:12 +00:00
writer . write_event ( Event ::Decl ( BytesDecl ::new ( b " 1.0 " , Some ( b " utf-8 " ) , None ) ) ) ? ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
self . write_to_inner ( writer , & mut BTreeMap ::new ( ) )
2017-06-07 20:40:53 +00:00
}
2017-07-28 21:58:03 +00:00
/// Like `write_to()` but without the `<?xml?>` prelude
2020-03-29 16:32:16 +00:00
pub fn write_to_inner < W : Write > ( & self , writer : & mut EventWriter < W > , all_prefixes : & mut BTreeMap < Prefix , Namespace > ) -> Result < ( ) > {
let local_prefixes : & BTreeMap < Option < String > , String > = self . prefixes . declared_prefixes ( ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
// Element namespace
// If the element prefix hasn't been set yet via a custom prefix, add it.
let mut existing_self_prefix : Option < Option < String > > = None ;
2020-03-29 16:32:16 +00:00
for ( prefix , ns ) in local_prefixes . iter ( ) . chain ( all_prefixes . iter ( ) ) {
if ns = = & self . namespace {
existing_self_prefix = Some ( prefix . clone ( ) ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
}
}
let mut all_keys = all_prefixes . keys ( ) . cloned ( ) ;
2020-03-29 16:32:16 +00:00
let mut local_keys = local_prefixes . keys ( ) . cloned ( ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let self_prefix : ( Option < String > , bool ) = match existing_self_prefix {
// No prefix exists already for our namespace
None = > {
if local_keys . find ( | p | p = = & None ) . is_none ( ) {
// Use the None prefix if available
( None , true )
} else {
// Otherwise generate one. Check if it isn't already used, if so increase the
// number until we find a suitable one.
let mut prefix_n = 0 u8 ;
while let Some ( _ ) = all_keys . find ( | p | p = = & Some ( format! ( " ns {} " , prefix_n ) ) ) {
prefix_n + = 1 ;
}
( Some ( format! ( " ns {} " , prefix_n ) ) , true )
}
} ,
// Some prefix has already been declared (or is going to be) for our namespace. We
// don't need to declare a new one. We do however need to remember which one to use in
// the tag name.
Some ( prefix ) = > ( prefix , false ) ,
2017-07-28 21:58:03 +00:00
} ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let name = match self_prefix {
( Some ( ref prefix ) , _ ) = > Cow ::Owned ( format! ( " {} : {} " , prefix , self . name ) ) ,
_ = > Cow ::Borrowed ( & self . name ) ,
} ;
2018-01-06 04:20:12 +00:00
let mut start = BytesStart ::borrowed ( name . as_bytes ( ) , name . len ( ) ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
// Write self prefix if necessary
match self_prefix {
( Some ( ref p ) , true ) = > {
let key = format! ( " xmlns: {} " , p ) ;
start . push_attribute ( ( key . as_bytes ( ) , self . namespace . as_bytes ( ) ) ) ;
all_prefixes . insert ( self_prefix . 0 , self . namespace . clone ( ) ) ;
}
( None , true ) = > {
let key = format! ( " xmlns " ) ;
start . push_attribute ( ( key . as_bytes ( ) , self . namespace . as_bytes ( ) ) ) ;
all_prefixes . insert ( self_prefix . 0 , self . namespace . clone ( ) ) ;
} ,
_ = > ( ) ,
} ;
// Custom prefixes/namespace sets
2020-03-29 16:32:16 +00:00
for ( prefix , ns ) in local_prefixes {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
match all_prefixes . get ( prefix ) {
p @ Some ( _ ) if p = = prefix . as_ref ( ) = > ( ) ,
_ = > {
let key = match prefix {
None = > String ::from ( " xmlns " ) ,
Some ( p ) = > format! ( " xmlns: {} " , p ) ,
} ;
start . push_attribute ( ( key . as_bytes ( ) , ns . as_ref ( ) ) ) ;
all_prefixes . insert ( prefix . clone ( ) , ns . clone ( ) ) ;
} ,
2017-06-07 20:40:53 +00:00
}
}
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
2017-06-07 20:40:53 +00:00
for ( key , value ) in & self . attributes {
2018-01-06 04:20:12 +00:00
start . push_attribute ( ( key . as_bytes ( ) , escape ( value . as_bytes ( ) ) . as_ref ( ) ) ) ;
2017-03-08 19:34:17 +00:00
}
2017-06-07 20:40:53 +00:00
if self . children . is_empty ( ) {
2018-01-06 04:20:12 +00:00
writer . write_event ( Event ::Empty ( start ) ) ? ;
2019-10-22 23:32:41 +00:00
return Ok ( ( ) ) ;
2017-03-08 19:34:17 +00:00
}
2017-06-07 20:40:53 +00:00
2018-01-06 04:20:12 +00:00
writer . write_event ( Event ::Start ( start ) ) ? ;
2017-06-07 20:40:53 +00:00
2017-03-08 19:34:17 +00:00
for child in & self . children {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
child . write_to_inner ( writer , all_prefixes ) ? ;
2017-03-08 19:34:17 +00:00
}
2017-06-07 20:40:53 +00:00
2018-01-06 04:20:12 +00:00
writer . write_event ( Event ::End ( BytesEnd ::borrowed ( name . as_bytes ( ) ) ) ) ? ;
2017-03-08 19:34:17 +00:00
Ok ( ( ) )
}
2017-05-14 14:38:56 +00:00
/// Returns an iterator over references to every child node of this element.
2017-03-08 19:34:17 +00:00
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-06-07 20:40:53 +00:00
/// use minidom::Element;
2017-05-14 14:38:56 +00:00
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem: Element = "<root xmlns=\"ns1\">a<c1 />b<c2 />c</root>".parse().unwrap();
2017-05-14 14:38:56 +00:00
///
/// let mut iter = elem.nodes();
///
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "a");
/// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c1");
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "b");
/// assert_eq!(iter.next().unwrap().as_element().unwrap().name(), "c2");
/// assert_eq!(iter.next().unwrap().as_text().unwrap(), "c");
/// assert_eq!(iter.next(), None);
2017-03-08 19:34:17 +00:00
/// ```
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn nodes ( & self ) -> Nodes {
2017-05-14 14:38:56 +00:00
self . children . iter ( )
}
/// Returns an iterator over mutable references to every child node of this element.
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn nodes_mut ( & mut self ) -> NodesMut {
2017-05-14 14:38:56 +00:00
self . children . iter_mut ( )
}
/// Returns an iterator over references to every child element of this element.
///
/// # Examples
///
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem: Element = "<root xmlns=\"ns1\">hello<child1 xmlns=\"ns1\"/>this<child2 xmlns=\"ns1\"/>is<child3 xmlns=\"ns1\"/>ignored</root>".parse().unwrap();
2017-03-08 19:34:17 +00:00
///
/// let mut iter = elem.children();
/// assert_eq!(iter.next().unwrap().name(), "child1");
/// assert_eq!(iter.next().unwrap().name(), "child2");
/// assert_eq!(iter.next().unwrap().name(), "child3");
/// assert_eq!(iter.next(), None);
/// ```
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn children ( & self ) -> Children {
2017-03-08 19:34:17 +00:00
Children {
iter : self . children . iter ( ) ,
}
}
2017-05-14 14:38:56 +00:00
/// Returns an iterator over mutable references to every child element of this element.
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn children_mut ( & mut self ) -> ChildrenMut {
2017-03-08 19:34:17 +00:00
ChildrenMut {
iter : self . children . iter_mut ( ) ,
}
}
2017-05-14 14:38:56 +00:00
/// Returns an iterator over references to every text node of this element.
///
/// # Examples
///
/// ```rust
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem: Element = "<root xmlns=\"ns1\">hello<c /> world!</root>".parse().unwrap();
2017-05-14 14:38:56 +00:00
///
/// let mut iter = elem.texts();
/// assert_eq!(iter.next().unwrap(), "hello");
/// assert_eq!(iter.next().unwrap(), " world!");
/// assert_eq!(iter.next(), None);
/// ```
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn texts ( & self ) -> Texts {
2017-05-14 14:38:56 +00:00
Texts {
iter : self . children . iter ( ) ,
}
}
/// Returns an iterator over mutable references to every text node of this element.
2019-10-22 23:32:41 +00:00
#[ inline ]
pub fn texts_mut ( & mut self ) -> TextsMut {
2017-05-14 14:38:56 +00:00
TextsMut {
iter : self . children . iter_mut ( ) ,
2017-03-08 19:34:17 +00:00
}
}
/// Appends a child node to the `Element`, returning the appended node.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let mut elem = Element::bare("root", "ns1");
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(elem.children().count(), 0);
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// elem.append_child(Element::bare("child", "ns1"));
2017-03-08 19:34:17 +00:00
///
/// {
/// let mut iter = elem.children();
/// assert_eq!(iter.next().unwrap().name(), "child");
/// assert_eq!(iter.next(), None);
/// }
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let child = elem.append_child(Element::bare("new", "ns1"));
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(child.name(), "new");
/// ```
2017-07-28 21:58:03 +00:00
pub fn append_child ( & mut self , child : Element ) -> & mut Element {
2017-03-08 19:34:17 +00:00
self . children . push ( Node ::Element ( child ) ) ;
if let Node ::Element ( ref mut cld ) = * self . children . last_mut ( ) . unwrap ( ) {
cld
2017-07-28 21:58:03 +00:00
} else {
2017-03-08 19:34:17 +00:00
unreachable! ( )
}
}
/// Appends a text node to an `Element`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let mut elem = Element::bare("node", "ns1");
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(elem.text(), "");
///
/// elem.append_text_node("text");
///
/// assert_eq!(elem.text(), "text");
/// ```
pub fn append_text_node < S : Into < String > > ( & mut self , child : S ) {
self . children . push ( Node ::Text ( child . into ( ) ) ) ;
}
/// Appends a node to an `Element`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::{Element, Node};
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let mut elem = Element::bare("node", "ns1");
2017-03-08 19:34:17 +00:00
///
/// elem.append_node(Node::Text("hello".to_owned()));
///
/// assert_eq!(elem.text(), "hello");
/// ```
pub fn append_node ( & mut self , node : Node ) {
self . children . push ( node ) ;
}
/// Returns the concatenation of all text nodes in the `Element`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2017-03-08 19:34:17 +00:00
/// use minidom::Element;
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem: Element = "<node xmlns=\"ns1\">hello,<split /> world!</node>".parse().unwrap();
2017-03-08 19:34:17 +00:00
///
/// assert_eq!(elem.text(), "hello, world!");
/// ```
pub fn text ( & self ) -> String {
2017-05-14 14:38:56 +00:00
self . texts ( ) . fold ( String ::new ( ) , | ret , new | ret + new )
2017-03-08 19:34:17 +00:00
}
/// Returns a reference to the first child element with the specific name and namespace, if it
/// exists in the direct descendants of this `Element`, else returns `None`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2019-11-26 11:47:35 +00:00
/// use minidom::{Element, NSChoice};
2017-03-08 19:34:17 +00:00
///
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
/// let elem: Element = r#"<node xmlns="ns"><a/><a xmlns="other_ns" /><b/></node>"#.parse().unwrap();
2017-03-08 19:34:17 +00:00
/// assert!(elem.get_child("a", "ns").unwrap().is("a", "ns"));
/// assert!(elem.get_child("a", "other_ns").unwrap().is("a", "other_ns"));
/// assert!(elem.get_child("b", "ns").unwrap().is("b", "ns"));
/// assert_eq!(elem.get_child("c", "ns"), None);
/// assert_eq!(elem.get_child("b", "other_ns"), None);
/// assert_eq!(elem.get_child("a", "inexistent_ns"), None);
/// ```
2019-11-26 11:47:35 +00:00
pub fn get_child < ' a , N : AsRef < str > , NS : Into < NSChoice < ' a > > > (
2019-10-22 23:32:41 +00:00
& self ,
name : N ,
namespace : NS ,
) -> Option < & Element > {
2019-11-26 11:47:35 +00:00
let namespace = namespace . into ( ) ;
2017-03-08 19:34:17 +00:00
for fork in & self . children {
if let Node ::Element ( ref e ) = * fork {
2019-11-26 11:47:35 +00:00
if e . is ( name . as_ref ( ) , namespace ) {
2017-03-08 19:34:17 +00:00
return Some ( e ) ;
}
}
}
None
}
/// Returns a mutable reference to the first child element with the specific name and namespace,
/// if it exists in the direct descendants of this `Element`, else returns `None`.
2019-11-26 11:47:35 +00:00
pub fn get_child_mut < ' a , N : AsRef < str > , NS : Into < NSChoice < ' a > > > (
2019-10-22 23:32:41 +00:00
& mut self ,
name : N ,
namespace : NS ,
) -> Option < & mut Element > {
2019-11-26 11:47:35 +00:00
let namespace = namespace . into ( ) ;
2017-03-08 19:34:17 +00:00
for fork in & mut self . children {
if let Node ::Element ( ref mut e ) = * fork {
2019-11-26 11:47:35 +00:00
if e . is ( name . as_ref ( ) , namespace ) {
2017-03-08 19:34:17 +00:00
return Some ( e ) ;
}
}
}
None
}
/// Returns whether a specific child with this name and namespace exists in the direct
/// descendants of the `Element`.
///
/// # Examples
///
2017-05-14 14:38:56 +00:00
/// ```rust
2019-11-26 11:47:35 +00:00
/// use minidom::{Element, NSChoice};
2017-03-08 19:34:17 +00:00
///
/// let elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
/// assert_eq!(elem.has_child("a", "other_ns"), true);
/// assert_eq!(elem.has_child("a", "ns"), true);
/// assert_eq!(elem.has_child("a", "inexistent_ns"), false);
/// assert_eq!(elem.has_child("b", "ns"), true);
/// assert_eq!(elem.has_child("b", "other_ns"), false);
/// assert_eq!(elem.has_child("b", "inexistent_ns"), false);
/// ```
2019-11-26 11:47:35 +00:00
pub fn has_child < ' a , N : AsRef < str > , NS : Into < NSChoice < ' a > > > (
& self ,
name : N ,
namespace : NS ,
) -> bool {
2017-03-08 19:34:17 +00:00
self . get_child ( name , namespace ) . is_some ( )
}
2018-12-23 14:40:23 +00:00
/// Removes the first child with this name and namespace, if it exists, and returns an
/// `Option<Element>` containing this child if it succeeds.
/// Returns `None` if no child matches this name and namespace.
///
/// # Examples
///
/// ```rust
2019-11-26 11:47:35 +00:00
/// use minidom::{Element, NSChoice};
2018-12-23 14:40:23 +00:00
///
/// let mut elem: Element = r#"<node xmlns="ns"><a /><a xmlns="other_ns" /><b /></node>"#.parse().unwrap();
/// assert!(elem.remove_child("a", "ns").unwrap().is("a", "ns"));
/// assert!(elem.remove_child("a", "ns").is_none());
/// assert!(elem.remove_child("inexistent", "inexistent").is_none());
/// ```
2019-11-26 11:47:35 +00:00
pub fn remove_child < ' a , N : AsRef < str > , NS : Into < NSChoice < ' a > > > (
2019-10-22 23:32:41 +00:00
& mut self ,
name : N ,
namespace : NS ,
) -> Option < Element > {
2018-12-23 14:40:23 +00:00
let name = name . as_ref ( ) ;
2019-11-26 11:47:35 +00:00
let namespace = namespace . into ( ) ;
2018-12-23 14:40:23 +00:00
let idx = self . children . iter ( ) . position ( | x | {
if let Node ::Element ( ref elm ) = x {
elm . is ( name , namespace )
} else {
false
}
} ) ? ;
self . children . remove ( idx ) . into_element ( )
}
2017-03-08 19:34:17 +00:00
}
2017-07-28 21:58:03 +00:00
fn split_element_name < S : AsRef < str > > ( s : S ) -> Result < ( Option < String > , String ) > {
let name_parts = s . as_ref ( ) . split ( ':' ) . collect ::< Vec < & str > > ( ) ;
match name_parts . len ( ) {
2 = > Ok ( ( Some ( name_parts [ 0 ] . to_owned ( ) ) , name_parts [ 1 ] . to_owned ( ) ) ) ,
1 = > Ok ( ( None , name_parts [ 0 ] . to_owned ( ) ) ) ,
2018-02-18 18:54:09 +00:00
_ = > Err ( Error ::InvalidElement ) ,
2017-07-28 21:58:03 +00:00
}
}
2020-03-29 16:32:16 +00:00
fn build_element < R : BufRead > ( reader : & EventReader < R > , event : & BytesStart , prefixes : & mut BTreeMap < Prefix , Namespace > ) -> Result < Element > {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let ( prefix , name ) = split_element_name ( str ::from_utf8 ( event . name ( ) ) ? ) ? ;
let mut local_prefixes = BTreeMap ::new ( ) ;
2019-10-22 23:32:41 +00:00
let attributes = event
. attributes ( )
2017-07-28 21:58:03 +00:00
. map ( | o | {
let o = o ? ;
let key = str ::from_utf8 ( o . key ) ? . to_owned ( ) ;
2017-11-08 17:19:05 +00:00
let value = o . unescape_and_decode_value ( reader ) ? ;
2017-07-28 21:58:03 +00:00
Ok ( ( key , value ) )
} )
2019-10-22 23:32:41 +00:00
. filter ( | o | match * o {
Ok ( ( ref key , ref value ) ) if key = = " xmlns " = > {
2020-03-29 16:32:16 +00:00
local_prefixes . insert ( None , value . clone ( ) ) ;
prefixes . insert ( None , value . clone ( ) ) ;
2019-10-22 23:32:41 +00:00
false
}
Ok ( ( ref key , ref value ) ) if key . starts_with ( " xmlns: " ) = > {
2020-03-29 16:32:16 +00:00
local_prefixes . insert ( Some ( key [ 6 .. ] . to_owned ( ) ) , value . to_owned ( ) ) ;
prefixes . insert ( Some ( key [ 6 .. ] . to_owned ( ) ) , value . to_owned ( ) ) ;
2019-10-22 23:32:41 +00:00
false
2017-06-07 20:40:53 +00:00
}
2019-10-22 23:32:41 +00:00
_ = > true ,
2017-07-28 21:58:03 +00:00
} )
. collect ::< Result < BTreeMap < String , String > > > ( ) ? ;
2017-06-07 20:40:53 +00:00
2020-03-29 16:32:16 +00:00
let namespace : & String = {
if let Some ( namespace ) = local_prefixes . get ( & prefix ) {
namespace
} else if let Some ( namespace ) = prefixes . get ( & prefix ) {
namespace
} else {
return Err ( Error ::MissingNamespace ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
}
} ;
2020-03-29 16:32:16 +00:00
let element = Element ::new (
name ,
namespace . clone ( ) ,
// Note that this will always be Some(_) as we can't distinguish between the None case and
// Some(None). At least we make sure the prefix has a namespace associated.
Some ( prefix ) ,
local_prefixes ,
attributes ,
Vec ::new ( )
) ;
2017-07-28 21:58:03 +00:00
Ok ( element )
2017-06-07 20:40:53 +00:00
}
2017-05-14 14:38:56 +00:00
/// An iterator over references to child elements of an `Element`.
2017-03-08 19:34:17 +00:00
pub struct Children < ' a > {
iter : slice ::Iter < ' a , Node > ,
}
impl < ' a > Iterator for Children < ' a > {
type Item = & ' a Element ;
fn next ( & mut self ) -> Option < & ' a Element > {
2017-05-22 16:49:24 +00:00
for item in & mut self . iter {
2017-03-08 19:34:17 +00:00
if let Node ::Element ( ref child ) = * item {
return Some ( child ) ;
}
}
None
}
}
2017-05-14 14:38:56 +00:00
/// An iterator over mutable references to child elements of an `Element`.
2017-03-08 19:34:17 +00:00
pub struct ChildrenMut < ' a > {
iter : slice ::IterMut < ' a , Node > ,
}
impl < ' a > Iterator for ChildrenMut < ' a > {
type Item = & ' a mut Element ;
fn next ( & mut self ) -> Option < & ' a mut Element > {
2017-05-22 16:49:24 +00:00
for item in & mut self . iter {
2017-03-08 19:34:17 +00:00
if let Node ::Element ( ref mut child ) = * item {
return Some ( child ) ;
}
}
None
}
}
2017-05-14 14:38:56 +00:00
/// An iterator over references to child text nodes of an `Element`.
pub struct Texts < ' a > {
iter : slice ::Iter < ' a , Node > ,
}
impl < ' a > Iterator for Texts < ' a > {
type Item = & ' a str ;
fn next ( & mut self ) -> Option < & ' a str > {
2017-05-22 16:49:24 +00:00
for item in & mut self . iter {
2017-05-14 14:38:56 +00:00
if let Node ::Text ( ref child ) = * item {
return Some ( child ) ;
}
}
None
}
}
/// An iterator over mutable references to child text nodes of an `Element`.
pub struct TextsMut < ' a > {
iter : slice ::IterMut < ' a , Node > ,
}
impl < ' a > Iterator for TextsMut < ' a > {
type Item = & ' a mut String ;
fn next ( & mut self ) -> Option < & ' a mut String > {
2017-05-22 16:49:24 +00:00
for item in & mut self . iter {
2017-05-14 14:38:56 +00:00
if let Node ::Text ( ref mut child ) = * item {
return Some ( child ) ;
}
}
None
}
}
/// An iterator over references to all child nodes of an `Element`.
pub type Nodes < ' a > = slice ::Iter < ' a , Node > ;
/// An iterator over mutable references to all child nodes of an `Element`.
pub type NodesMut < ' a > = slice ::IterMut < ' a , Node > ;
/// An iterator over the attributes of an `Element`.
pub struct Attrs < ' a > {
iter : btree_map ::Iter < ' a , String , String > ,
}
impl < ' a > Iterator for Attrs < ' a > {
type Item = ( & ' a str , & ' a str ) ;
fn next ( & mut self ) -> Option < Self ::Item > {
self . iter . next ( ) . map ( | ( x , y ) | ( x . as_ref ( ) , y . as_ref ( ) ) )
}
}
/// An iterator over the attributes of an `Element`, with the values mutable.
pub struct AttrsMut < ' a > {
iter : btree_map ::IterMut < ' a , String , String > ,
}
impl < ' a > Iterator for AttrsMut < ' a > {
type Item = ( & ' a str , & ' a mut String ) ;
fn next ( & mut self ) -> Option < Self ::Item > {
self . iter . next ( ) . map ( | ( x , y ) | ( x . as_ref ( ) , y ) )
}
}
2017-03-08 19:34:17 +00:00
/// A builder for `Element`s.
pub struct ElementBuilder {
root : Element ,
}
impl ElementBuilder {
2020-03-29 16:32:16 +00:00
/// Sets a custom prefix. It is not possible to set the same prefix twice.
pub fn prefix < S : Into < Namespace > > ( mut self , prefix : Prefix , namespace : S ) -> ElementBuilder {
// TODO: Make this actually send back an error when a duplicate prefix is added.
self . root . prefixes . insert ( prefix , namespace . into ( ) ) ;
2017-03-08 19:34:17 +00:00
self
}
/// Sets an attribute.
2019-10-22 23:32:41 +00:00
pub fn attr < S : Into < String > , V : IntoAttributeValue > (
mut self ,
name : S ,
value : V ,
) -> ElementBuilder {
2017-03-08 19:34:17 +00:00
self . root . set_attr ( name , value ) ;
self
}
2018-12-23 14:40:23 +00:00
/// Appends anything implementing `Into<Node>` into the tree.
pub fn append < T : Into < Node > > ( mut self , node : T ) -> ElementBuilder {
self . root . append_node ( node . into ( ) ) ;
self
}
/// Appends an iterator of things implementing `Into<Node>` into the tree.
2019-10-22 23:32:41 +00:00
pub fn append_all < T : Into < Node > , I : IntoIterator < Item = T > > (
mut self ,
iter : I ,
) -> ElementBuilder {
2018-12-23 14:40:23 +00:00
for node in iter {
self . root . append_node ( node . into ( ) ) ;
2017-03-08 19:34:17 +00:00
}
self
}
/// Builds the `Element`.
pub fn build ( self ) -> Element {
2020-03-29 16:32:16 +00:00
self . root
2017-03-08 19:34:17 +00:00
}
}
2017-07-28 21:58:03 +00:00
#[ cfg(test) ]
2018-05-29 14:22:22 +00:00
mod tests {
use super ::* ;
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
#[ test ]
fn test_element_new ( ) {
use std ::iter ::FromIterator ;
2017-12-31 06:19:24 +00:00
2019-10-22 23:32:41 +00:00
let elem = Element ::new (
" name " . to_owned ( ) ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
" namespace " . to_owned ( ) ,
2020-03-29 16:32:16 +00:00
None ,
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
( None , " namespace " . to_owned ( ) ) ,
2019-10-22 23:32:41 +00:00
BTreeMap ::from_iter ( vec! [ ( " name " . to_string ( ) , " value " . to_string ( ) ) ] . into_iter ( ) ) ,
Vec ::new ( ) ,
) ;
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
assert_eq! ( elem . name ( ) , " name " ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
assert_eq! ( elem . ns ( ) , " namespace " . to_owned ( ) ) ;
2018-05-29 14:22:22 +00:00
assert_eq! ( elem . attr ( " name " ) , Some ( " value " ) ) ;
assert_eq! ( elem . attr ( " inexistent " ) , None ) ;
}
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
#[ test ]
fn test_from_reader_simple ( ) {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let xml = " <foo xmlns='ns1'></foo> " ;
2018-05-29 14:22:22 +00:00
let mut reader = EventReader ::from_str ( xml ) ;
let elem = Element ::from_reader ( & mut reader ) ;
2017-12-31 06:19:24 +00:00
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let elem2 = Element ::builder ( " foo " , " ns1 " ) . build ( ) ;
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
assert_eq! ( elem . unwrap ( ) , elem2 ) ;
}
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
#[ test ]
fn test_from_reader_nested ( ) {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let xml = " <foo xmlns='ns1'><bar xmlns='ns1' baz='qxx' /></foo> " ;
2018-05-29 14:22:22 +00:00
let mut reader = EventReader ::from_str ( xml ) ;
let elem = Element ::from_reader ( & mut reader ) ;
2017-12-31 06:19:24 +00:00
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let nested = Element ::builder ( " bar " , " ns1 " ) . attr ( " baz " , " qxx " ) . build ( ) ;
let elem2 = Element ::builder ( " foo " , " ns1 " ) . append ( nested ) . build ( ) ;
2017-12-31 06:19:24 +00:00
2018-05-29 14:22:22 +00:00
assert_eq! ( elem . unwrap ( ) , elem2 ) ;
}
2018-01-01 13:28:17 +00:00
2018-05-29 14:22:22 +00:00
#[ test ]
fn test_from_reader_with_prefix ( ) {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let xml = " <foo xmlns='ns1'><prefix:bar xmlns:prefix='ns1' baz='qxx' /></foo> " ;
2018-05-29 14:22:22 +00:00
let mut reader = EventReader ::from_str ( xml ) ;
let elem = Element ::from_reader ( & mut reader ) ;
2020-03-29 16:32:16 +00:00
let nested = Element ::builder ( " bar " , " ns1 " ) . attr ( " baz " , " qxx " ) . build ( ) ;
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let elem2 = Element ::builder ( " foo " , " ns1 " ) . append ( nested ) . build ( ) ;
2018-05-29 14:22:22 +00:00
assert_eq! ( elem . unwrap ( ) , elem2 ) ;
}
2018-05-29 13:59:36 +00:00
2020-03-28 15:41:48 +00:00
#[ test ]
fn test_from_reader_split_prefix ( ) {
let xml = " <foo:bar xmlns:foo='ns1'/> " ;
let mut reader = EventReader ::from_str ( xml ) ;
let elem = Element ::from_reader ( & mut reader ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , String ::from ( " bar " ) ) ;
assert_eq! ( elem . ns ( ) , String ::from ( " ns1 " ) ) ;
// Ensure the prefix is properly added to the store
2020-03-29 19:25:18 +00:00
assert_eq! ( elem . prefixes . get ( & Some ( String ::from ( " foo " ) ) ) , Some ( & String ::from ( " ns1 " ) ) ) ;
2020-03-28 15:41:48 +00:00
}
2018-05-29 14:22:22 +00:00
#[ test ]
2019-10-22 23:32:41 +00:00
fn parses_spectest_xml ( ) {
// From: https://gitlab.com/lumi/minidom-rs/issues/8
2018-05-29 14:22:22 +00:00
let xml = r #"
< rng :grammar xmlns :rng = " http://relaxng.org/ns/structure/1.0 " >
< rng :name xmlns :rng = " http://relaxng.org/ns/structure/1.0 " > < / rng :name >
< / rng :grammar >
" #;
let mut reader = EventReader ::from_str ( xml ) ;
let _ = Element ::from_reader ( & mut reader ) . unwrap ( ) ;
}
#[ test ]
fn does_not_unescape_cdata ( ) {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let xml = " <test xmlns='test'><![CDATA['>blah<blah>]]></test> " ;
2018-05-29 14:22:22 +00:00
let mut reader = EventReader ::from_str ( xml ) ;
let elem = Element ::from_reader ( & mut reader ) . unwrap ( ) ;
assert_eq! ( elem . text ( ) , " '>blah<blah> " ) ;
}
2019-12-07 16:39:49 +00:00
#[ test ]
fn test_compare_all_ns ( ) {
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.
More specifically this means `Element::new` has changed to require
`Element`'s new 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:
```rust
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: Fix inconsistencies wrt. "prefix:name" format provided as a name
when creating an Element with `Element::new` or `Element::bare`.
`Element::builder` already handles this as it should, splitting name and
prefix.
TODO: Change `Element::name` method to `Element::local_name` to make it
more explicit.
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2020-03-27 12:45:22 +00:00
let xml = " <foo xmlns='foo' xmlns:bar='baz'><bar:meh xmlns:bar='baz' /></foo> " ;
2019-12-07 16:39:49 +00:00
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 ) ;
}
2018-05-29 13:59:36 +00:00
}