Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added JSDOM/jQuery as an alternative to htmlparser/soupselect

  • Loading branch information...
commit 8be661a27452114a8d7551e80968caa1a96dcdcd 1 parent 90eec24
@chriso authored
Showing with 39 additions and 23 deletions.
  1. +35 −21 lib/node.io/dom.js
  2. +2 −1  lib/node.io/job.js
  3. +2 −1  package.json
View
56 lib/node.io/dom.js
@@ -4,20 +4,19 @@
* MIT Licensed
*/
-var Job = require('./job').JobProto,
- soupselect = require('soupselect').select,
- htmlparser = require('htmlparser');
+var Job = require('./job').JobProto, soupselect;
/**
- * A selector object similar to jQuery's. See the documentation for
- * usage examples.
+ * The CSS selector function. See the documentation for usage examples.
*
* @param {String} selector
* @param {Function} context
- * @param {Object} headers (optional)
* @api public
*/
Job.prototype.$ = function (selector, context) {
+ if (!soupselect) {
+ soupselect = require('soupselect').select;
+ }
var selected = soupselect(context, selector);
if (selected.length === 0) {
throw new Error("No elements matching '" + selector + "'");
@@ -39,21 +38,36 @@ Job.prototype.$ = function (selector, context) {
* @param {Object} headers (optional)
* @api public
*/
-Job.prototype.parseHtml = function (data, callback, headers) {
- var self = this;
- var handler = new htmlparser.DefaultHandler(function (err, dom) {
- if (err) {
- callback(err);
- } else {
- var $ = function (selector, context) {
- //Allow the user to specify a custom context (thanks to github.com/jimbishopp)
- return self.$(selector, context || dom);
- };
- callback(null, $, data, headers);
- }
- }, {verbose: true, ignoreWhitespace: true});
- var parser = new htmlparser.Parser(handler);
- parser.parseComplete(data);
+Job.prototype.parseHtml = function (data, callback, response) {
+ if (this.options.jsdom) {
+ var features = {
+ FetchExternalResources: false,
+ ProcessExternalResources: false,
+ QuerySelector: false
+ };
+ var $, window = require('jsdom').jsdom(data, null, {features:features}).createWindow(),
+ jquery = require('jquery'),
+ default_$ = jquery.create(window);
+ $ = function (selector, context) {
+ return context ? jquery.create(context) : default_$(selector);
+ };
+ callback(null, $, data, response.headers, response);
+ } else {
+ var self = this, handler, parser, $, htmlparser = require('htmlparser');
+ handler = new htmlparser.DefaultHandler(function (err, dom) {
+ if (err) {
+ callback(err);
+ } else {
+ $ = function (selector, context) {
+ //Allow the user to specify a custom context (thanks to github.com/jimbishopp)
+ return self.$(selector, context || dom);
+ };
+ callback(null, $, data, response.headers, response);
+ }
+ }, {verbose: true, ignoreWhitespace: true});
+ parser = new htmlparser.Parser(handler);
+ parser.parseComplete(data);
+ }
};
/**
View
3  lib/node.io/job.js
@@ -28,7 +28,8 @@ var default_options = {
proxy: false,
redirects: 3,
retry_request: false,
- args: []
+ args: [],
+ jsdom: false
};
/**
View
3  package.json
@@ -20,7 +20,8 @@
"validator": ">= 0.1.1",
"expresso": ">= 0.7.0",
"coffee-script": ">= 0.9.5",
- "daemon": ">= 0.1.0"
+ "daemon": ">= 0.1.0",
+ "jquery": ">= 1.4.4"
},
"scripts": {
"test": "make test",
Please sign in to comment.
Something went wrong with that request. Please try again.