mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Added Element::append_text
This commit is contained in:
parent
2c701038cd
commit
f54776ca0a
1 changed files with 28 additions and 0 deletions
|
@ -587,6 +587,34 @@ impl Element {
|
|||
self.children.push(Node::Text(child.into()));
|
||||
}
|
||||
|
||||
/// Appends a string as plain text to an `Element`.
|
||||
///
|
||||
/// If the last child node of the element is a text node, the string will be appended to it.
|
||||
/// Otherwise, a new text node will be created.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use minidom::Element;
|
||||
///
|
||||
/// let mut elem = Element::bare("node", "ns1");
|
||||
///
|
||||
/// assert_eq!(elem.text(), "");
|
||||
///
|
||||
/// elem.append_text_node("text");
|
||||
///
|
||||
/// elem.append_text(" and more text");
|
||||
///
|
||||
/// assert_eq!(elem.nodes().count(), 1);
|
||||
/// ```
|
||||
pub fn append_text<S: Into<String>>(&mut self, text: S) {
|
||||
if let Some(Node::Text(ref mut child)) = self.children.last_mut() {
|
||||
child.push_str(&text.into());
|
||||
} else {
|
||||
self.append_text_node(text);
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends a node to an `Element`.
|
||||
///
|
||||
/// # Examples
|
||||
|
|
Loading…
Reference in a new issue