From b74dbb74bb0e7941b0ca0f3e8dd6b0fa576f9f3a Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 10 Apr 2018 06:31:51 +0300 Subject: [PATCH] h-data: Remove `rel` attributes from `` tags when sanitizing html. They are invalid in `pango` markup so theres no reason they should are not needed. Also add some paranoid .trim() calls. It returnes a &str slice so it's cheap. --- hammond-data/src/models/new_episode.rs | 12 +++++++++--- hammond-data/src/models/new_podcast.rs | 12 ++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index 3eba254..9df4229 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -197,9 +197,9 @@ impl NewEpisodeMinimal { let guid = item.guid().map(|s| s.value().trim().to_owned()); let uri = item.enclosure() - .map(|s| url_cleaner(s.url())) + .map(|s| url_cleaner(s.url().trim())) // Fallback to Rss.Item.link if enclosure is None. - .or_else(|| item.link().map(|s| url_cleaner(s))); + .or_else(|| item.link().map(|s| url_cleaner(s.trim()))); // If url is still None return an Error as this behaviour is // compliant with the RSS Spec. @@ -234,7 +234,13 @@ impl NewEpisodeMinimal { // TODO: TryInto is stabilizing in rustc v1.26! pub(crate) fn into_new_episode(self, item: &rss::Item) -> NewEpisode { let length = item.enclosure().and_then(|x| x.length().parse().ok()); - let description = item.description().map(|s| ammonia::clean(s)); + let description = item.description().map(|s| { + ammonia::Builder::new() + // Remove `rel` attributes from `` tags + .link_rel(None) + .clean(chan.description().trim()) + .to_string(); + }); NewEpisodeBuilder::default() .title(self.title) diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index a4faa2a..38f31b7 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -88,16 +88,20 @@ impl NewPodcast { /// Parses a `rss::Channel` into a `NewPodcast` Struct. pub(crate) fn new(chan: &rss::Channel, source_id: i32) -> NewPodcast { let title = chan.title().trim(); + let link = url_cleaner(chan.link().trim()); - let description = ammonia::clean(chan.description().trim()); - let link = url_cleaner(chan.link()); + let description = ammonia::Builder::new() + // Remove `rel` attributes from `` tags + .link_rel(None) + .clean(chan.description().trim()) + .to_string(); // Try to get the itunes img first let itunes_img = chan.itunes_ext() - .and_then(|s| s.image()) + .and_then(|s| s.image().trim()) .map(|s| s.to_owned()); // If itunes is None, try to get the channel.image from the rss spec - let image_uri = itunes_img.or_else(|| chan.image().map(|s| s.url().to_owned())); + let image_uri = itunes_img.or_else(|| chan.image().map(|s| s.url().trim().to_owned())); NewPodcastBuilder::default() .title(title)