Added tests for bug 73160. And code to make them pass.
Dave Cross [Sun, 11 Dec 2011 12:47:52 +0000 (12:47 +0000)]
MANIFEST
lib/XML/Feed/Format/RSS.pm
t/22-bug73160.t [new file with mode: 0644]
t/samples/rss10-datespaces.xml [new file with mode: 0644]

index a8c3085..46f9e14 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -65,4 +65,5 @@ t/samples/rss10-double2.xml
 t/20-no-enclosures.t
 t/21-rss2-permalinks.t
 t/samples/rss20-p.xml
-
+t/22-bug73160.t
+t/samples/rss10-datespaces.xml
index 07be8a7..a147bc1 100644 (file)
@@ -49,7 +49,12 @@ sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
 
 ## The following elements are the same in all versions of RSS.
 sub title       { shift->{rss}->channel('title', @_) }
-sub link        { shift->{rss}->channel('link', @_) }
+sub link        {
+    my $link = shift->{rss}->channel('link', @_);
+    $link =~ s/^\s+//;
+    $link =~ s/\s+$//;
+    return $link;
+}
 sub description { shift->{rss}->channel('description', @_) }
 sub updated     { shift->modified(@_) }
 
@@ -135,8 +140,12 @@ sub modified {
         my $date;
         eval {
             if (my $ts = $rss->channel('pubDate')) {
+                $ts =~ s/^\s+//;
+                $ts =~ s/\s+$//;
                 $date = DateTime::Format::Mail->parse_datetime($ts);
             } elsif ($ts = $rss->channel->{dc}{date}) {
+                $ts =~ s/^\s+//;
+                $ts =~ s/\s+$//;
                 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
             }
         };
@@ -191,9 +200,12 @@ sub link {
         ## For RSS 2.0 output from XML::RSS. Sigh.
         $entry->{entry}{permaLink} = $_[0];
     } else {
-        $entry->{entry}{link} ||
-        $entry->{entry}{permaLink} ||
-        $entry->{entry}{guid};
+        my $link = $entry->{entry}{link} ||
+            $entry->{entry}{permaLink} ||
+            $entry->{entry}{guid};
+        $link =~ s/^\s+//;
+        $link =~ s/\s+$//;
+        
     }
 }
 
@@ -303,9 +315,13 @@ sub issued {
             if (my $ts = $item->{pubDate}) {
                 my $parser = DateTime::Format::Mail->new;
                 $parser->loose;
+                $ts =~ s/^\s+//;
+                $ts =~ s/\s+$//;
                 $date = $parser->parse_datetime($ts);
             } elsif ($ts = $item->{dc}{date} or $ts = $item->{dcterms}{date}) {
-               $date = DateTime::Format::W3CDTF->parse_datetime($ts);
+                $ts =~ s/^\s+//;
+                $ts =~ s/\s+$//;
+                $date = DateTime::Format::W3CDTF->parse_datetime($ts);
             }
         };
         return $date;
@@ -318,7 +334,10 @@ sub modified {
         $item->{dcterms}{modified} =
             DateTime::Format::W3CDTF->format_datetime($_[0]);
     } else {
-        if (my $ts = $item->{dcterms}{modified} || $item->{'http://www.w3.org/2005/Atom'}{updated}) {
+        if (my $ts = $item->{dcterms}{modified} ||
+                $item->{'http://www.w3.org/2005/Atom'}{updated}) {
+            $ts =~ s/^\s+//;
+            $ts =~ s/\s+$//;
             return eval { DateTime::Format::W3CDTF->parse_datetime($ts) } || eval { XML::Atom::Util::iso2dt($ts) };
         } 
     }
diff --git a/t/22-bug73160.t b/t/22-bug73160.t
new file mode 100644 (file)
index 0000000..fb3f513
--- /dev/null
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+
+use Test::More;
+use XML::Feed;
+
+my $file = 't/samples/rss10-datespaces.xml';
+my $feed = XML::Feed->parse($file);
+isa_ok($feed, 'XML::Feed::Format::RSS');
+ok(my $date = $feed->modified, 'Got a feed modified date');
+isa_ok($date, 'DateTime');
+
+my $link = $feed->link;
+unlike($link, qr[^\s], 'No spaces at start of link');
+unlike($link, qr[\s$], 'No spaces at end of link');
+
+my $entry = ($feed->entries)[0];
+ok(my $iss = $entry->issued, 'Got an entry issued date');
+isa_ok($iss, 'DateTime');
+
+$link = $entry->link;
+unlike($link, qr[^\s], 'No spaces at start of link');
+unlike($link, qr[\s$], 'No spaces at end of link');
+
+done_testing;
diff --git a/t/samples/rss10-datespaces.xml b/t/samples/rss10-datespaces.xml
new file mode 100644 (file)
index 0000000..7a9ee18
--- /dev/null
@@ -0,0 +1,80 @@
+<?xml version="1.0"?><!-- name="generator" content="bryar" -->
+<rdf:RDF
+  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+  xmlns:dc="http://purl.org/dc/elements/1.1/"
+  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
+  xmlns:admin="http://webns.net/mvcb/"
+  xmlns:content="http://purl.org/rss/1.0/modules/content/"
+  xmlns="http://purl.org/rss/1.0/">
+
+  <channel rdf:about="http://www.cantrell.org.uk/david/journal">
+    <title>Dave's Free Press</title>
+    <link> http://www.cantrell.org.uk/david/journal </link>
+    <description>the Slightly Less Bad And Wrong Blog Edition</description>
+    <dc:language>en-us</dc:language>
+    <dc:date> 2011-12-06T22:36:07Z </dc:date>
+    <items>
+      <rdf:Seq>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/olympic-costs-yet-again"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/2011-nov-in-books"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/2011-oct-in-books"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/rugby-world-cup-roundup-2011"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/2011-sep-in-books"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/amazon-kindle-wireless"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/olympic-security-costs-2"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/the-tudors"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/2011-aug-in-books"/>      <rdf:li rdf:resource="http://www.cantrell.org.uk/david/journal/id/rum-tasting"/>      </rdf:Seq>
+    </items>
+  </channel>
+  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/olympic-costs-yet-again">
+    <title> Oh look, more Olympic financial incompetence </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/olympic-costs-yet-again </link>
+    <description></description>
+    <dc:date> 2011-12-06T22:36:07Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/2011-nov-in-books">
+    <title> November 2011 in books </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/2011-nov-in-books</link>
+    <description></description>
+    <dc:date> 2011-12-02T18:39:16Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/2011-oct-in-books">
+    <title> October 2011 in books </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/2011-oct-in-books</link>
+    <description></description>
+    <dc:date> 2011-10-30T15:32:43Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/rugby-world-cup-roundup-2011">
+    <title> Rugby World Cup roundup </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/rugby-world-cup-roundup-2011</link>
+    <description></description>
+    <dc:date> 2011-10-23T13:14:23Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/2011-sep-in-books">
+    <title> September 2011 in books </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/2011-sep-in-books</link>
+    <description></description>
+    <dc:date> 2011-10-02T11:30:30Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/amazon-kindle-wireless">
+    <title> Amazon Kindle wireless bug - and a bugfix </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/amazon-kindle-wireless</link>
+    <description></description>
+    <dc:date> 2011-09-29T19:21:32Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/olympic-security-costs-2">
+    <title> Olympic security costs - again </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/olympic-security-costs-2</link>
+    <description></description>
+    <dc:date> 2011-09-09T21:53:28Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/the-tudors">
+    <title> The Tudors </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/the-tudors</link>
+    <description></description>
+    <dc:date> 2011-09-08T21:15:48Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/2011-aug-in-books">
+    <title> August 2011 in books </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/2011-aug-in-books</link>
+    <description></description>
+    <dc:date> 2011-09-03T10:52:56Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item>  <item rdf:about="http://www.cantrell.org.uk/david/journal/id/rum-tasting">
+    <title> Rum tasting notes </title>
+    <link> http://www.cantrell.org.uk/david/journal/id/rum-tasting</link>
+    <description></description>
+    <dc:date> 2011-08-03T22:06:40Z </dc:date>
+    <dc:creator>  </dc:creator>
+  </item></rdf:RDF>