Added new (currently failing) test for the double-encoding bug.
[catagits/XML-Feed.git] / lib / XML / Feed / Format / Atom.pm
index 7bd84cd..b0a8554 100644 (file)
@@ -8,6 +8,7 @@ use XML::Atom::Feed;
 use XML::Atom::Util qw( iso2dt );
 use List::Util qw( first );
 use DateTime::Format::W3CDTF;
+use HTML::Entities;
 
 use XML::Atom::Entry;
 XML::Atom::Entry->mk_elem_accessors(qw( lat long ), ['http://www.w3.org/2003/01/geo/wgs84_pos#']);
@@ -196,15 +197,25 @@ sub content {
     if (@_) {
         my %param;
         my $base;
+        my $orig_body;
         if (ref($_[0]) eq 'XML::Feed::Content') {
-                       if (defined $_[0]->type && defined $types{$_[0]->type}) {
-                   %param = (Body => $_[0]->body, Type => $types{$_[0]->type});
-                       } else {
-                   %param = (Body => $_[0]->body);
-                       }
+            $orig_body = $_[0]->body;
+            if (defined $_[0]->type && defined $types{$_[0]->type}) {
+                %param = (Body => $orig_body, Type => $types{$_[0]->type});
+
+                if ($param{Type} eq 'html') {
+                    $param{Body} = HTML::Entities::encode_entities($param{Body});
+                }
+            } else {
+                # %param = (Body => $_[0]->body);
+            }
             $base = $_[0]->base if defined $_[0]->base;
         } else {
-            %param = (Body => $_[0]);
+            $orig_body = $_[0];
+            # %param = (Body => $_[0]);
+        }
+        if (!exists $param{Body}) {
+            $param{Body} = $orig_body;
         }
         $entry->{entry}->content(XML::Atom::Content->new(%param, Version => 1.0));
         $entry->{entry}->content->base($base) if defined $base;
@@ -293,4 +304,24 @@ sub long {
     }
 }
 
+
+sub enclosure {
+    my $entry = shift;
+
+    if (@_) {
+        my $enclosure = shift;
+        my $method    = ($XML::Feed::MULTIPLE_ENCLOSURES)? 'add_link' : 'link';
+        $entry->{entry}->$method({ rel => 'enclosure', href => $enclosure->{url},
+                                length => $enclosure->{length},
+                                type   => $enclosure->{type} });
+        return 1;
+    } else {
+        my @links = grep { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
+        return undef unless @links;
+        my @encs = map { XML::Feed::Enclosure->new({ url => $_->href, length => $_->length, type => $_->type }) } @links ;
+        return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1];
+    }
+}
+
+
 1;