Merge pull request #5509 from ByteHamster/ignore-non-channel-items

Ignore item tags that are not inside a channel
This commit is contained in:
ByteHamster 2021-10-30 23:02:52 +02:00 committed by GitHub
commit 035d09da77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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"; private static final String ENC_TYPE = "type";
@Override @Override
public SyndElement handleElementStart(String localName, HandlerState state, public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
Attributes attributes) { if (ITEM.equals(localName) && CHANNEL.equals(state.getTagstack().lastElement().getName())) {
if (ITEM.equals(localName)) {
state.setCurrentItem(new FeedItem()); state.setCurrentItem(new FeedItem());
state.getItems().add(state.getCurrentItem()); state.getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed()); state.getCurrentItem().setFeed(state.getFeed());
} else if (ENCLOSURE.equals(localName) && ITEM.equals(state.getTagstack().peek().getName())) {
} else if (ENCLOSURE.equals(localName)) {
String type = attributes.getValue(ENC_TYPE); String type = attributes.getValue(ENC_TYPE);
String url = attributes.getValue(ENC_URL); String url = attributes.getValue(ENC_URL);
@ -72,7 +70,6 @@ public class Rss20 extends Namespace {
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type); FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
state.getCurrentItem().setMedia(media); state.getCurrentItem().setMedia(media);
} }
} }
return new SyndElement(localName, this); return new SyndElement(localName, this);
} }

View File

@ -96,4 +96,12 @@ public class RssParserTest {
assertTrue(TextUtils.isEmpty(feed.getPaymentLinks().get(2).content)); assertTrue(TextUtils.isEmpty(feed.getPaymentLinks().get(2).content));
assertEquals("https://example.com/funding3", feed.getPaymentLinks().get(2).url); 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>