Skip to content

Commit

Permalink
Merge pull request #212 from gleroi/feature/handle_atom_entry_xml_bas…
Browse files Browse the repository at this point in the history
…e_attribute

parser/atom: handle xml:base attribute on content
  • Loading branch information
markpritchard committed Mar 2, 2024
2 parents 890aead + 8e38c81 commit bd1e06e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions feed-rs/fixture/atom/atom_xml_base.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" >
<updated>2022-04-27T09:26:27+00:00</updated>
<id>https://numi.st/feed.xml</id>
<title type="html">my cool website title</title>
<entry>
<title type="html">my cool entry title</title>
<updated>2022-04-21T00:00:00+00:00</updated>
<id>https://numi.st/post/2022/travel-uke</id>
<content type="html" xml:base="https://numi.st/post/2022/travel-uke/">
<![CDATA[<p><img src="IMG_1232.jpeg" /></p>]]>
</content>
<author><name>Not Blank</name></author>
</entry>
</feed>
9 changes: 9 additions & 0 deletions feed-rs/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ pub struct Entry {

/// Atom (optional): The language specified on the item
pub language: Option<String>,
/// Atom (optional): The base url specified on the item to resolve any relative
/// references found within the scope on the item
pub base: Option<String>,
}

impl Default for Entry {
Expand All @@ -318,6 +321,7 @@ impl Default for Entry {
rights: None,
media: Vec::new(),
language: None,
base: None,
}
}
}
Expand Down Expand Up @@ -393,6 +397,11 @@ impl Entry {
self.language = Some(language.to_owned());
self
}

pub fn base(mut self, url: &str) -> Self {
self.base = Some(url.to_owned());
self
}
}

/// Represents the category of a feed or entry
Expand Down
1 change: 1 addition & 0 deletions feed-rs/src/parser/atom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn handle_entry<R: BufRead>(parser: &Parser, element: Element<R>) -> ParseFeedRe
(NS::Atom, "author") => if_some_then(handle_person(child)?, |person| entry.authors.push(person)),

(NS::Atom, "content") => {
entry.base = util::handle_base_attr(&child);
entry.language = util::handle_language_attr(&child);
entry.content = handle_content(child)?;
}
Expand Down
10 changes: 10 additions & 0 deletions feed-rs/src/parser/atom/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn test_example_1() {
.title(Text::new("Atom draft-07 snapshot".into()))
.updated_parsed("2005-07-31T12:29:29Z")
.language("en")
.base("http://diveintomark.org/")
.author(Person::new("Mark Pilgrim").uri("http://example.org/").email("f8dy@example.com"))
.link(Link::new("http://example.org/2005/04/02/atom", None).rel("alternate").media_type("text/html"))
.link(
Expand Down Expand Up @@ -125,6 +126,7 @@ fn test_example_3() {
.entry(Entry::default()
.title(Text::new("Time to Transfer Risk: Why Security Complexity & VPNs Are No Longer Sustainable".into()))
.language("en-us")
.base("https://blogs.akamai.com/")
.link(Link::new("http://feedproxy.google.com/~r/TheAkamaiBlog/~3/NnQEuqRSyug/time-to-transfer-risk-why-security-complexity-vpns-are-no-longer-sustainable.html", None)
.rel("alternate")
.media_type("text/html"))
Expand Down Expand Up @@ -561,3 +563,11 @@ fn test_scattered() {
.unwrap()
.contains("there are no strings on me"));
}

// Handle xml:base attribute on content
#[test]
fn test_atom_content_xml_base() {
let test_data = test::fixture_as_string("atom/atom_xml_base.xml");
let actual = parser::parse(test_data.as_bytes()).unwrap();
assert!(actual.entries[0].base.as_ref().unwrap().eq("https://numi.st/post/2022/travel-uke/"));
}
5 changes: 5 additions & 0 deletions feed-rs/src/parser/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub(crate) fn handle_language_attr<R: BufRead>(element: &Element<R>) -> Option<S
element.attr_value("xml:lang")
}

// Handles "xml:base" as an attribute (e.g. in Atom feeds)
pub(crate) fn handle_base_attr<R: BufRead>(element: &Element<R>) -> Option<String> {
element.attr_value("xml:base")
}

// Handles <link>
pub(crate) fn handle_link<R: BufRead>(element: Element<R>) -> Option<Link> {
element.child_as_text().map(|s| Link::new(s, element.xml_base.as_ref()))
Expand Down

0 comments on commit bd1e06e

Please sign in to comment.