RSSyl: Strip leading and trailing whitespace from XML tag contents.
[claws.git] / src / plugins / rssyl / libfeed / parser_atom10.c
index 64a4ffeaadafd60a5a5154a48736d752b296227b..9cf4ffa8ce5a5a46ef26506e80c4e1d6e58ed53b 100644 (file)
@@ -47,14 +47,24 @@ void feed_parser_atom10_start(void *data, const gchar *el, const gchar **attr)
                        /* Start of author info for the feed found.
                         * Set correct location. */
                        ctx->location = FEED_LOC_ATOM10_AUTHOR;
+               } else if( !strcmp(el, "link") ) {
+                       if (!feed_parser_get_attribute_value(attr, "rel")) {
+                               /* Link tag for the feed */
+                               g_free(ctx->feed->link);
+                               ctx->feed->link =
+                                       g_strdup(feed_parser_get_attribute_value(attr, "href"));
+                       }
                } else ctx->location = FEED_LOC_ATOM10_NONE;
 
        } else if( ctx->depth == 2 ) {
 
-               /* This should only happen with malformed atom feeds - we're in
-                * XML depth 2, but not inside an <entry> block. */
-               if (ctx->curitem == NULL)
+               /* Make sure we are in one of known locations within the XML structure.
+                * This condition should never be true on a valid Atom feed. */
+               if (ctx->location != FEED_LOC_ATOM10_AUTHOR &&
+                               ctx->location != FEED_LOC_ATOM10_ENTRY) {
+                       ctx->depth++;
                        return;
+               }
 
                if( !strcmp(el, "author") ) {
                        /* Start of author info for current feed item.
@@ -94,18 +104,20 @@ void feed_parser_atom10_end(void *data, const gchar *el)
 {
        FeedParserCtx *ctx = (FeedParserCtx *)data;
        Feed *feed = ctx->feed;
-       gchar *text = NULL;
+       gchar *text = NULL, *tmp;
 
        if( ctx->str != NULL )
-               text = ctx->str->str;
+               text = g_strstrip(g_strdup(ctx->str->str));
        else
                text = "";
 
-       ctx->depth--;
-
        switch( ctx->depth ) {
 
                case 0:
+                       /* Just in case. */
+                       break;
+
+               case 1:
 
                        if( !strcmp(el, "feed") ) {
                                /* We have finished parsing the feed, reverse the list
@@ -115,12 +127,23 @@ void feed_parser_atom10_end(void *data, const gchar *el)
 
                        break;
 
-               case 1:
+               case 2:
 
                        /* decide if we just received </entry>, so we can
                         * add a complete item to feed */
                        if( !strcmp(el, "entry") ) {
 
+                               /* Fix up URL, if it is relative */
+                               if (ctx->curitem->url != NULL &&
+                                               !strstr(ctx->curitem->url, "://") &&
+                                               ctx->feed->link != NULL) {
+                                       tmp = g_strconcat(ctx->feed->link,
+                                                       (ctx->curitem->url[0] == '/' ? "" : "/"),
+                                                       ctx->curitem->url, NULL);
+                                       feed_item_set_url(ctx->curitem, tmp);
+                                       g_free(tmp);
+                               }
+
                                /* append the complete feed item */
                                if( ctx->curitem->id && ctx->curitem->title
                                                && ctx->curitem->date_modified ) {
@@ -142,7 +165,7 @@ void feed_parser_atom10_end(void *data, const gchar *el)
 
                        break;
 
-               case 2:
+               case 3:
 
                        if( ctx->curitem == NULL )
                                break;
@@ -189,7 +212,7 @@ void feed_parser_atom10_end(void *data, const gchar *el)
 
                        break;
 
-               case 3:
+               case 4:
 
                        if( ctx->curitem == NULL )
                                break;
@@ -230,8 +253,11 @@ void feed_parser_atom10_end(void *data, const gchar *el)
        }
 
        if( ctx->str != NULL ) {
+               g_free(text);
                g_string_free(ctx->str, TRUE);
                ctx->str = NULL;
        }
        ctx->str = NULL;
+
+       ctx->depth--;
 }