Permalink
Browse files

Merge branch 'master' of github.com:tautologistics/node-htmlparser

  • Loading branch information...
2 parents 2267f32 + bf22f13 commit 9613b99096ac1757b81695b1a4bbee7f10afcd7f @tautologistics committed Oct 17, 2013
Showing with 116 additions and 0 deletions.
  1. +6 −0 README.md
  2. +27 −0 lib/htmlparser.js
  3. +41 −0 runtests.js
  4. +42 −0 tests/testutils.js
View
@@ -76,6 +76,12 @@ while (...) {
parser.done();
```
+##Streaming To Parser in Node
+
+```javascript
+fs.createReadStream('./path_to_file.html').pipe(parser);
+```
+
##Parsing RSS/Atom Feeds
```javascript
View
@@ -64,6 +64,33 @@ function Parser (builder, options) {
this.reset();
}
+if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') {
+
+ var Stream = require('stream');
+ inherits(Parser, Stream);
+
+ Parser.prototype.writable = true;
+ Parser.prototype.write = function(data) {
+ if(data instanceof Buffer) {
+ data = data.toString();
+ }
+ this.parseChunk(data);
+ };
+
+ Parser.prototype.end = function(data) {
+ if (arguments.length) {
+ this.write(data);
+ }
+ this.writable = false;
+ this.done();
+ };
+
+ Parser.prototype.destroy = function() {
+ this.writable = false;
+ };
+
+}
+
//**Public**//
Parser.prototype.reset = function Parser$reset () {
this._state = {
View
@@ -204,6 +204,47 @@ testUtils.runParserTests(
};
});
+testUtils.runStreamingParserTests(
+ parserTests
+ , htmlparser.Parser
+ , null
+ , function (testName, testResult, actual, expected) {
+ console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
+ }, function (elapsed, passed, failed) {
+ testResults['StreamingParser'] = {
+ elapsed: elapsed
+ , passed: passed
+ , failed: failed
+ };
+ });
+
+testUtils.runStreamingParserTests(
+ parserTests
+ , htmlparser.Parser
+ , function (test) {
+ var newTest = {};
+ for (var key in test) {
+ if (!test.hasOwnProperty(key)) {
+ continue;
+ }
+ newTest[key] = (key === 'data') ?
+ test.data.join('').split('')
+ :
+ test[key]
+ ;
+ }
+ return newTest;
+ }
+ , function (testName, testResult, actual, expected) {
+ console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
+ }, function (elapsed, passed, failed) {
+ testResults['StreamingParser (streamed)'] = {
+ elapsed: elapsed
+ , passed: passed
+ , failed: failed
+ };
+ });
+
console.log('');
console.log('Test Results');
console.log('------------------');
View
@@ -192,4 +192,46 @@ exports.runParserTests = function (tests, parserCtor, permutator, testHandler, r
resultsHandler(endTime - startTime, passed, failed);
}
+exports.runStreamingParserTests = function (tests, parserCtor, permutator, testHandler, resultsHandler) {
+ var callback = function builderCallback (err) {
+ if (err) {
+ console.log('Builder error', err);
+ }
+ };
+ var builder = new TestBuilder(callback);
+ var parser = new parserCtor(builder);
+
+ var passed = 0;
+ var failed = 0;
+
+ var startTime = Date.now();
+ for (var testName in tests) {
+ if (!tests.hasOwnProperty(testName)) {
+ continue;
+ }
+ var test = permutator ? permutator(tests[testName]) : tests[testName];
+ parser.reset();
+ if (test.data.length === 1) {
+ parser.end(test.data[0]);
+ } else {
+ for (var i = 0, len = test.data.length; i < len; i++) {
+ parser.write(test.data[i]);
+ }
+ parser.end();
+ }
+
+ var testResult = exports.compareObjects(builder.output, test.expected);
+ testHandler(testName, testResult, builder.dom, test.expected);
+
+ if (!testResult) {
+ failed++;
+ } else {
+ passed++;
+ }
+ }
+ var endTime = Date.now();
+
+ resultsHandler(endTime - startTime, passed, failed);
+}
+
})();

0 comments on commit 9613b99

Please sign in to comment.