Ignore item tags that are not inside a channel

This commit is contained in:
ByteHamster 2021-10-30 21:58:17 +02:00
parent 345aad4148
commit bb9936f696
3 changed files with 25 additions and 6 deletions

View File

@ -39,14 +39,12 @@ public class Rss20 extends Namespace {
private static final String ENC_TYPE = "type";
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
if (ITEM.equals(localName)) {
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
if (ITEM.equals(localName) && CHANNEL.equals(state.getTagstack().lastElement().getName())) {
state.setCurrentItem(new FeedItem());
state.getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
} else if (ENCLOSURE.equals(localName)) {
} else if (ENCLOSURE.equals(localName) && ITEM.equals(state.getTagstack().peek().getName())) {
String type = attributes.getValue(ENC_TYPE);
String url = attributes.getValue(ENC_URL);
@ -72,7 +70,6 @@ public class Rss20 extends Namespace {
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
state.getCurrentItem().setMedia(media);
}
}
return new SyndElement(localName, this);
}

View File

@ -96,4 +96,12 @@ public class RssParserTest {
assertTrue(TextUtils.isEmpty(feed.getPaymentLinks().get(2).content));
assertEquals("https://example.com/funding3", feed.getPaymentLinks().get(2).url);
}
@Test
public void testUnsupportedElements() throws Exception {
File feedFile = FeedParserTestHelper.getFeedFile("feed-rss-testUnsupportedElements.xml");
Feed feed = FeedParserTestHelper.runFeedParser(feedFile);
assertEquals(1, feed.getItems().size());
assertEquals("item-0", feed.getItems().get(0).getTitle());
}
}

View File

@ -0,0 +1,14 @@
<?xml version='1.0' encoding='UTF-8' ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>title</title>
<item>
<title>item-0</title>
</item>
<unsupported-element>
<item>
<title>item-1</title>
</item>
</unsupported-element>
</channel>
</rss>