Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Bump MSRV from 1.83.0 to 1.88.0 for `encoding_rs`. [#99](https://github.com/rust-syndication/atom/pull/99)
- Replace C0 control characters, which XML 1.0 forbids, with U+FFFD when writing a feed or entry instead of producing a document that conforming parsers reject. [`#101`](https://github.com/rust-syndication/atom/pull/101)

## 0.12.10 - 2026-07-30

Expand Down
8 changes: 4 additions & 4 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::link::Link;
use crate::person::Person;
use crate::source::Source;
use crate::text::Text;
use crate::toxml::{ToXml, WriterExt};
use crate::toxml::{ToXml, WriterExt, XmlSafeWriter};
use crate::util::{
atom_datetime, atom_text, attr_value, decode, default_fixed_datetime, skip, FixedDateTime,
};
Expand Down Expand Up @@ -653,8 +653,8 @@ impl Entry {
write_config: WriteConfig,
) -> Result<W, Error> {
let mut writer = match write_config.indent_size {
Some(indent_size) => Writer::new_with_indent(writer, b' ', indent_size),
None => Writer::new(writer),
Some(indent_size) => Writer::new_with_indent(XmlSafeWriter(writer), b' ', indent_size),
None => Writer::new(XmlSafeWriter(writer)),
};
if write_config.write_document_declaration {
writer
Expand All @@ -665,7 +665,7 @@ impl Entry {
.map_err(XmlError::new)?;
}
self.to_xml_inner(&mut writer, true)?;
Ok(writer.into_inner())
Ok(writer.into_inner().0)
}

/// Serializes the `<entry>` element. `declare_xmlns` controls whether the
Expand Down
8 changes: 4 additions & 4 deletions src/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::generator::Generator;
use crate::link::Link;
use crate::person::Person;
use crate::text::Text;
use crate::toxml::{ToXml, WriterExt};
use crate::toxml::{ToXml, WriterExt, XmlSafeWriter};
use crate::util::{
atom_datetime, atom_text, attr_value, decode, default_fixed_datetime, skip, FixedDateTime,
};
Expand Down Expand Up @@ -194,8 +194,8 @@ impl Feed {
write_config: WriteConfig,
) -> Result<W, Error> {
let mut writer = match write_config.indent_size {
Some(indent_size) => Writer::new_with_indent(writer, b' ', indent_size),
None => Writer::new(writer),
Some(indent_size) => Writer::new_with_indent(XmlSafeWriter(writer), b' ', indent_size),
None => Writer::new(XmlSafeWriter(writer)),
};
if write_config.write_document_declaration {
writer
Expand All @@ -206,7 +206,7 @@ impl Feed {
.map_err(XmlError::new)?;
}
self.to_xml(&mut writer)?;
Ok(writer.into_inner())
Ok(writer.into_inner().0)
}

/// Return the title of this feed.
Expand Down
24 changes: 23 additions & 1 deletion src/toxml.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
use std::io::Write;
use std::io::{self, Write};

use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;

use crate::error::XmlError;

/// Replaces the C0 control characters XML 1.0 forbids with U+FFFD. Dropping them
/// could join their neighbors into a new token.
pub(crate) struct XmlSafeWriter<W>(pub(crate) W);

impl<W: Write> Write for XmlSafeWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut runs = buf.split(|b| matches!(b, 0x00..=0x08 | 0x0B | 0x0C | 0x0E..=0x1F));
if let Some(first) = runs.next() {
self.0.write_all(first)?;
}
for run in runs {
self.0.write_all("\u{FFFD}".as_bytes())?;
Comment thread
andy128k marked this conversation as resolved.
Resolved
self.0.write_all(run)?;
}
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}

pub(crate) trait ToXml {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError>;
}
Expand Down
28 changes: 27 additions & 1 deletion tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate atom_syndication as atom;
use std::fs::File;
use std::io::BufReader;

use crate::atom::{Content, Entry, Feed};
use crate::atom::{Category, Content, Entry, Feed, Text};

macro_rules! feed {
($f:expr) => {{
Expand Down Expand Up @@ -102,3 +102,29 @@ fn write_content_roundtrip() {

assert_eq!(feed.to_string().parse::<Feed>().unwrap(), feed);
}

#[test]
fn write_replaces_control_characters() {
let mut category = Category::default();
category.set_term("ta\u{8}g");

let mut entry = Entry::default();
entry.set_title("Ti\u{1e}tle\tand\nmore");
entry.set_summary(Text::html("<scr\u{1}ipt>"));
entry.set_categories(vec![category]);

let mut feed = Feed::default();
feed.set_entries(vec![entry]);

let out = feed.to_string();
// The parser is lenient, so check the raw output too.
assert!(!out
.bytes()
.any(|b| b < 0x20 && !matches!(b, b'\t' | b'\n' | b'\r')));

let parsed = out.parse::<Feed>().unwrap();
let entry = &parsed.entries()[0];
assert_eq!(entry.title().as_str(), "Ti\u{FFFD}tle\tand\nmore");
assert_eq!(entry.summary().unwrap().as_str(), "<scr\u{FFFD}ipt>");
assert_eq!(entry.categories()[0].term(), "ta\u{FFFD}g");
}
Loading