Skip to content

Commit

Permalink
Implement API to add newlines between attributes when writing XML
Browse files Browse the repository at this point in the history
  • Loading branch information
areleu authored and Mingun committed Mar 31, 2024
1 parent 6e34a73 commit 613b230
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ to get an offset of the error position. For `SyntaxError`s the range
- [#362]: Added `BytesCData::minimal_escape()` which escapes only `&` and `<`.
- [#362]: Added `Serializer::set_quote_level()` which allow to set desired level of escaping.
- [#705]: Added `NsReader::prefixes()` to list all the prefixes currently declared.
- [#275]: Added `ElementWriter::new_line()` which enables pretty printing elements with multiple attributes.

### Bug Fixes

Expand Down Expand Up @@ -61,6 +62,7 @@ to get an offset of the error position. For `SyntaxError`s the range
- [#689]: `buffer_position()` now always report the position the parser last seen.
To get an error position use `error_position()`.

[#275]: https://github.com/tafia/quick-xml/issues/275
[#362]: https://github.com/tafia/quick-xml/issues/362
[#513]: https://github.com/tafia/quick-xml/issues/513
[#622]: https://github.com/tafia/quick-xml/issues/622
Expand Down
29 changes: 22 additions & 7 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,8 @@ impl<'a> BytesStart<'a> {
where
A: Into<Attribute<'b>>,
{
let a = attr.into();
let bytes = self.buf.to_mut();
bytes.push(b' ');
bytes.extend_from_slice(a.key.as_ref());
bytes.extend_from_slice(b"=\"");
bytes.extend_from_slice(a.value.as_ref());
bytes.push(b'"');
self.buf.to_mut().push(b' ');
self.push_attr(attr.into());
}

/// Remove all attributes from the ByteStart
Expand Down Expand Up @@ -287,6 +282,26 @@ impl<'a> BytesStart<'a> {
}
Ok(None)
}

/// Adds an attribute to this element.
pub(crate) fn push_attr<'b>(&mut self, attr: Attribute<'b>) {
let bytes = self.buf.to_mut();
bytes.extend_from_slice(attr.key.as_ref());
bytes.extend_from_slice(b"=\"");
// FIXME: need to escape attribute content
bytes.extend_from_slice(attr.value.as_ref());
bytes.push(b'"');
}

/// Adds new line in existing element
pub(crate) fn push_newline(&mut self) {
self.buf.to_mut().push(b'\n');
}

/// Adds indentation bytes in existing element
pub(crate) fn push_indent(&mut self, indent: &[u8]) {
self.buf.to_mut().extend_from_slice(indent);
}
}

impl<'a> Debug for BytesStart<'a> {
Expand Down
Loading

0 comments on commit 613b230

Please sign in to comment.