Skip to content
Browse files

handle bad XML

  • Loading branch information...
1 parent 5cb3585 commit 92cdb1ba4700f2792eb5d12f2b95fee393c457e7 @sentientwaffle committed
Showing with 119 additions and 43 deletions.
  1. +7 −6 index.js
  2. +62 −0 test/fixtures/atom-invalid.xml
  3. +50 −37 test/index.test.js
View
13 index.js
@@ -113,8 +113,9 @@ FeedRead.atom = function(xml, source, callback) {
};
parser.onend = function() {
- callback(null, _.map(articles,
+ callback(null, _.filter(_.map(articles,
function(art) {
+ if (!art.children.length) return false;
var author = child_by_name(art, "author");
if (author) author = child_data(author, "name");
@@ -130,7 +131,7 @@ FeedRead.atom = function(xml, source, callback) {
if (obj.published) obj.published = new Date(obj.published);
return obj;
}
- ));
+ ), function(art) { return !!art; }));
};
parser.write(xml);
@@ -170,8 +171,9 @@ FeedRead.rss = function(xml, source, callback) {
};
parser.onend = function() {
- callback(null, _.map(articles,
+ callback(null, _.filter(_.map(articles,
function(art) {
+ if (!art.children.length) return false;
var obj = {
title: child_data(art, "title")
, content: scrub_html(child_data(art, "content:encoded"))
@@ -185,7 +187,7 @@ FeedRead.rss = function(xml, source, callback) {
if (obj.published) obj.published = new Date(obj.published);
return obj;
}
- ));
+ ), function(art) { return !!art; }));
};
parser.write(xml);
@@ -215,11 +217,10 @@ var FeedParser = (function() {
parser.onopentag = function(tag) { _this.open(tag); };
parser.onclosetag = function(tag) { _this.close(tag); };
+ parser.onerror = function() { this.error = undefined; }
parser.ontext = function(text) { _this.ontext(text); };
parser.oncdata = function(text) { _this.ontext(text); };
parser.onend = function() { _this.onend(); };
-
- parser.onerror = console.error;
}
View
62 test/fixtures/atom-invalid.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <title>DJG</title>
+ <link href="http://sentientwaffle.github.com/atom.xml" rel="self"/>
+ <link href="http://sentientwaffle.github.com/"/>
+ <updated>2012-03-01T12:01:58-08:00</updated>
+ <id>http://sentientwaffle.github.com/</id>
+ <author>
+ <name>DJG</name>
+ </author>
+
+
+ <entry
+ <title>Save file on blur</title>
+ <link href="http://sentientwaffle.github.com/save-file-on-blur"/>
+ <updated>2012-02-09T00:00:00-08:00</updated>
+ <id>http://sentientwaffle.github.com/save-file-on-blur</id>
+ <content type="html">&lt;h2&gt;Intro&lt;/h2&gt;
+&lt;p&gt;When doing web development, I frequently need to switch back and
+forth between the editor and a browser to see the updates.
+This simple plugin will save the file whenever it loses focus.&lt;/p&gt;
+
+&lt;h2&gt;&lt;a href=&quot;https://github.com/sentientwaffle/save-on-blur&quot;&gt;The Code&lt;/a&gt;&lt;/h2&gt;
+
+&lt;pre&gt;&lt;code&gt;stratus = require 'stratus'
+stratus.on &quot;fractus.blur&quot;, (editor) -&amp;gt;
+ editor.save()
+&lt;/code&gt;&lt;/pre&gt;
+
+&lt;p&gt;&lt;em&gt;3 whole lines.&lt;/em&gt; Not bad.&lt;/p&gt;
+
+&lt;p&gt;The &lt;code&gt;fractus.blur&lt;/code&gt; event is called whenever an editor loses focus, and
+the callback receives that editor.&lt;/p&gt;
+
+&lt;h3&gt;By Bundle&lt;/h3&gt;
+
+&lt;p&gt;If you want to apply the bundle to only CSS, Sass, and Stylus files:&lt;/p&gt;
+
+&lt;pre&gt;&lt;code&gt;stratus.on &quot;fractus.blur&quot;, (editor) -&amp;gt;
+ syntax = editor.syntax?.name
+ return unless syntax &amp;amp;&amp;amp; /^(CSS|Sass|Stylus)\b/.test(syntax)
+ editor.save()
+&lt;/code&gt;&lt;/pre&gt;
+
+&lt;h2&gt;Installing the plugin&lt;/h2&gt;
+
+&lt;pre&gt;&lt;code&gt;$ stratus plugin:install &amp;lt;username&amp;gt; path/to/your/plugin.coffee
+&lt;/code&gt;&lt;/pre&gt;
+
+&lt;h2&gt;More info&lt;/h2&gt;
+
+&lt;p&gt;For more information on writing plugins for Stratus,
+see the &lt;a href=&quot;http://stratuseditor.com/plugins&quot;&gt;plugin API&lt;/a&gt;.&lt;/p&gt;
+
+&lt;p&gt;A configurable version of the save-on-blur plugin is available
+on &lt;a href=&quot;https://github.com/sentientwaffle/save-on-blur&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
+
+&lt;p&gt;There are &lt;a href=&quot;http://stratuseditor.com/plugins#Events&quot;&gt;plenty of other useful events&lt;/a&gt; you can use.&lt;/p&gt;
+</content>
+ </entry>
+
+</feed>
View
87 test/index.test.js
@@ -24,10 +24,11 @@ function load_fixture(name) {
}
var fixtures =
- { atom: load_fixture("atom.xml")
- , rss: load_fixture("rss.xml")
- , google_news: load_fixture("google-news.rss")
- , techcrunch: load_fixture("techcrunch.rss")
+ { atom: load_fixture("atom.xml")
+ , atom_invalid: load_fixture("atom-invalid.xml")
+ , rss: load_fixture("rss.xml")
+ , google_news: load_fixture("google-news.rss")
+ , techcrunch: load_fixture("techcrunch.rss")
};
@@ -84,42 +85,54 @@ describe("feed", function() {
describe(".atom", function() {
- var articles;
- before(function(done) {
- feed.atom(fixtures.atom, function(err, arts) {
- articles = arts;
- done(err);
+ describe("valid XML", function() {
+ var articles;
+ before(function(done) {
+ feed.atom(fixtures.atom, function(err, arts) {
+ articles = arts;
+ done(err);
+ });
+ });
+
+ it("is an Array of articles", function() {
+ articles.should.be.an.instanceof(Array);
+ articles[0].should.be.an.instanceof(Object);
+ });
+
+ it("has a title", function() {
+ articles[0].title.should.eql("Save file on blur");
+ });
+
+ it("has an author", function() {
+ articles[0].author.should.eql("DJG");
+ });
+
+ it("has a link", function() {
+ articles[0].link.should.eql("http://sentientwaffle.github.com/save-file-on-blur");
+ });
+
+ it("has content", function() {
+ articles[0].content.should.include("Installing the plugin");
+ });
+
+ it("has a published date", function() {
+ articles[0].published.should.be.an.instanceof(Date);
+ });
+
+ it("has a feed", function() {
+ articles[0].feed.name.should.eql("DJG");
+ articles[0].feed.link.should.eql("http://sentientwaffle.github.com/");
});
});
- it("is an Array of articles", function() {
- articles.should.be.an.instanceof(Array);
- articles[0].should.be.an.instanceof(Object);
- });
-
- it("has a title", function() {
- articles[0].title.should.eql("Save file on blur");
- });
-
- it("has an author", function() {
- articles[0].author.should.eql("DJG");
- });
-
- it("has a link", function() {
- articles[0].link.should.eql("http://sentientwaffle.github.com/save-file-on-blur");
- });
-
- it("has content", function() {
- articles[0].content.should.include("Installing the plugin");
- });
-
- it("has a published date", function() {
- articles[0].published.should.be.an.instanceof(Date);
- });
-
- it("has a feed", function() {
- articles[0].feed.name.should.eql("DJG");
- articles[0].feed.link.should.eql("http://sentientwaffle.github.com/");
+ describe("invalid XML", function() {
+ it("doesn't crash on invalid XML", function(done) {
+ feed.atom(fixtures.atom_invalid, function(err, arts) {
+ should.not.exist(err);
+ arts.should.be.an.instanceof(Array);
+ done();
+ });
+ });
});
});

0 comments on commit 92cdb1b

Please sign in to comment.
Something went wrong with that request. Please try again.