Skip to content

Commit 2fc40c5

Browse files
author
Kris Reeves
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: package.json
2 parents a126b18 + f707bd7 commit 2fc40c5

File tree

12 files changed

+166
-44
lines changed

12 files changed

+166
-44
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- 0.4
43
- 0.6
5-
- 0.7
4+
- 0.8
5+
- 0.9

lib/FeedHandler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ FeedHandler.prototype.onend = function() {
7979
}
8080
}
8181
this.dom = feed;
82-
DomHandler.prototype._handleCallback.call(this);
82+
DomHandler.prototype._handleCallback.call(
83+
this, feedRoot ? null : Error("couldn't find root of feed")
84+
);
8385
};
8486

8587
module.exports = FeedHandler;

lib/Parser.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Parser(cbs, options){
44
this._options = options || defaultOpts;
55
this._cbs = cbs || defaultCbs;
66
this._buffer = "";
7-
this._tagSep = "";
7+
this._tagSep = ">";
88
this._stack = [];
99
this._wroteSpecial = false;
1010
this._contentFlags = 0;
@@ -248,9 +248,9 @@ Parser.prototype._parseTags = function(force){
248248
if(this._contentFlags !== 0){
249249
this._writeSpecial(rawData, ">");
250250
}
251-
else if(rawData !== "" && this._cbs.ontext){
251+
else if(this._cbs.ontext){
252252
if(this._tagSep === ">") rawData += ">"; //it's the second > in a row
253-
this._cbs.ontext(rawData);
253+
if(rawData !== "") this._cbs.ontext(rawData);
254254
}
255255
}
256256
}
@@ -266,6 +266,7 @@ Parser.prototype._writeCDATA = function(data){
266266
}
267267
this._contentFlags ^= SpecialTags[ElementType.CDATA];
268268
if(this._cbs.oncdataend) this._cbs.oncdataend();
269+
this._wroteSpecial = false;
269270
}
270271
else if(this._cbs.ontext) this._cbs.ontext(data + this._tagSep);
271272
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "htmlparser2",
33
"description": "Performance-optimized forgiving HTML/XML/RSS parser",
4-
"version": "2.5.1",
4+
"version": "2.5.2",
55
"author": "Felix Boehm <[email protected]>",
66
"keywords": ["html", "parser", "streams", "xml", "dom", "rss", "feed", "atom"],
77
"contributors": ["Chris Winberry <[email protected]>"],
@@ -29,4 +29,4 @@
2929
"type": "MIT",
3030
"url": "http://github.com/tautologistics/node-htmlparser/raw/master/LICENSE"
3131
}]
32-
}
32+
}

tests/00-runtests.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
var fs = require("fs"),
2-
assert = require("assert");
2+
path = require("path"),
3+
assert = require("assert");
34

45
var runCount = 0,
5-
testCount = 0;
6+
testCount = 0,
7+
done = false;
8+
9+
[
10+
"./01-events.js",
11+
"./02-stream.js",
12+
"./03-feed.js"
13+
]
14+
.map(require)
15+
.forEach(function (test){
16+
console.log("\nStarting", test.dir, "\n----");
17+
18+
var dir = path.resolve(__dirname, test.dir);
619

7-
function runTests(test){
820
//read files, load them, run them
9-
fs.readdirSync(__dirname + test.dir
10-
).map(function(file){
11-
if(file[0] === ".") return false;
12-
if(file.substr(-5) === ".json") return JSON.parse(
13-
fs.readFileSync(__dirname + test.dir + file)
14-
);
15-
return require(__dirname + test.dir + file);
16-
}).forEach(function(file){
17-
if(!file) return;
18-
var second = false;
19-
21+
var f = fs
22+
.readdirSync(dir)
23+
.filter(RegExp.prototype.test, /^[^\._]/) //ignore all files with a leading dot or underscore
24+
.map(function(name){
25+
return path.resolve(dir, name);
26+
})
27+
.map(require)
28+
.forEach(function(file){
2029
runCount++;
2130

2231
console.log("Testing:", file.name);
2332

33+
var second = false; //every test runs twice
2434
test.test(file, function(err, dom){
2535
assert.ifError(err);
2636
assert.deepEqual(file.expected, dom, "didn't get expected output");
2737

2838
if(second){
29-
runCount--;
3039
testCount++;
40+
if(!--runCount && done){
41+
console.log("Total tests:", testCount);
42+
}
3143
}
3244
else second = true;
3345
});
3446
});
35-
console.log("->", test.dir.slice(1, -1), "started");
36-
}
37-
38-
//run all tests
39-
[
40-
"./02-feed.js",
41-
"./03-events.js",
42-
"./05-stream.js"
43-
].map(require).forEach(runTests);
47+
});
4448

45-
//log the results
46-
(function check(){
47-
if(runCount !== 0) return process.nextTick(check);
48-
console.log("Total tests:", testCount);
49-
}());
49+
var done = true; //started all tests

tests/03-events.js renamed to tests/01-events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var helper = require("./test-helper.js"),
22
sliceArr = Array.prototype.slice;
33

4-
exports.dir = "/Events/";
4+
exports.dir = "Events";
55

66
exports.test = function(test, cb){
77
var tokens = [], cbs;

tests/05-stream.js renamed to tests/02-stream.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var helper = require("./test-helper.js"),
33
sliceArr = Array.prototype.slice,
44
fs = require("fs");
55

6-
exports.dir = "/Stream/";
6+
exports.dir = "Stream";
77

88
exports.test = function(test, cb){
99
var tokens = [],
@@ -26,7 +26,7 @@ exports.test = function(test, cb){
2626
event: name,
2727
data: sliceArr.apply(arguments)
2828
});
29-
}
29+
};
3030
}});
3131
}
3232
else {
@@ -41,12 +41,12 @@ exports.test = function(test, cb){
4141
}
4242
};
4343
helper.EVENTS.forEach(function(name){
44-
stream._events[name] = function(){
44+
stream.on(name, function(){
4545
tokens.push({
4646
event: name,
4747
data: sliceArr.apply(arguments)
4848
});
49-
}
49+
});
5050
});
5151
}
5252
fs.createReadStream(__dirname + test.file).pipe(stream);

tests/02-feed.js renamed to tests/03-feed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var helper = require("./test-helper.js"),
77
xmlMode: true
88
};
99

10-
exports.dir = "/Feeds/";
10+
exports.dir = "Feeds";
1111

1212
exports.test = function(test, cb){
1313
var handler = new FeedHandler(function(err, dom){

tests/Events/05-cdata-special.json

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"name": "CDATA",
3+
"options": {
4+
"handler": {},
5+
"parser": {}
6+
},
7+
"html": "<script>/*<![CDATA[*/ asdf ><asdf></adsf><> fo/*]]>*/</script>",
8+
"expected": [
9+
{
10+
"event": "opentagname",
11+
"data": [
12+
"script"
13+
]
14+
},
15+
{
16+
"event": "opentag",
17+
"data": [
18+
"script",
19+
{}
20+
]
21+
},
22+
{
23+
"event": "text",
24+
"data": [
25+
"/*"
26+
]
27+
},
28+
{
29+
"event": "cdatastart",
30+
"data": []
31+
},
32+
{
33+
"event": "text",
34+
"data": [
35+
"*/ asdf >"
36+
]
37+
},
38+
{
39+
"event": "text",
40+
"data": [
41+
"<"
42+
]
43+
},
44+
{
45+
"event": "text",
46+
"data": [
47+
"asdf>"
48+
]
49+
},
50+
{
51+
"event": "text",
52+
"data": [
53+
"<"
54+
]
55+
},
56+
{
57+
"event": "text",
58+
"data": [
59+
"/adsf>"
60+
]
61+
},
62+
{
63+
"event": "text",
64+
"data": [
65+
"<"
66+
]
67+
},
68+
{
69+
"event": "text",
70+
"data": [
71+
">"
72+
]
73+
},
74+
{
75+
"event": "text",
76+
"data": [
77+
" fo/*"
78+
]
79+
},
80+
{
81+
"event": "cdataend",
82+
"data": []
83+
},
84+
{
85+
"event": "text",
86+
"data": [
87+
"*/"
88+
]
89+
},
90+
{
91+
"event": "closetag",
92+
"data": [
93+
"script"
94+
]
95+
}
96+
]
97+
}

tests/Events/06-leading-lt.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "leading lt",
3+
"options": {
4+
"handler": {},
5+
"parser": {}
6+
},
7+
"html": ">a>",
8+
"expected": [
9+
{
10+
"event": "text",
11+
"data": [
12+
">"
13+
]
14+
},
15+
{
16+
"event": "text",
17+
"data": [
18+
"a>"
19+
]
20+
}
21+
]
22+
}

0 commit comments

Comments
 (0)