Skip to content

Commit

Permalink
Re-factor $.parseHTML to use $.load
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed Jul 4, 2013
1 parent 7cb293c commit 2e4f73f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 5 additions & 9 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var htmlparser = require('htmlparser2'),
/*
Parser
*/
exports = module.exports = function(content, options, scripts) {
var dom = evaluate(content, options, scripts);
exports = module.exports = function(content, options) {
var dom = evaluate(content, options);

// Generic root element
var root = {
Expand All @@ -27,7 +27,7 @@ exports = module.exports = function(content, options, scripts) {
return root;
};

var evaluate = exports.evaluate = function(content, options, scripts) {
var evaluate = exports.evaluate = function(content, options) {
// options = options || $.fn.options;

var handler = new htmlparser.DomHandler(options),
Expand All @@ -36,10 +36,10 @@ var evaluate = exports.evaluate = function(content, options, scripts) {
parser.write(content);
parser.done();

return connect(handler.dom, null, scripts);
return connect(handler.dom);
};

var connect = exports.connect = function(dom, parent, scripts) {
var connect = exports.connect = function(dom, parent) {
parent = parent || null;

var prevElem = null;
Expand All @@ -49,10 +49,6 @@ var connect = exports.connect = function(dom, parent, scripts) {
if (isTag(elem.type) && elem.attribs === undefined)
elem.attribs = {};

if (scripts && elem.name === 'script') {
scripts.push(elem);
}

// Set parent
elem.parent = parent;

Expand Down
12 changes: 5 additions & 7 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var text = exports.text = function(elems) {
* meaning for Cheerio, but it is maintained for API compatability with jQuery.
*/
var parseHTML = exports.parseHTML = function(data, context, keepScripts) {
var parsed, scripts;
var parsed;

if (!data || typeof data !== 'string') {
return null;
Expand All @@ -96,14 +96,12 @@ var parseHTML = exports.parseHTML = function(data, context, keepScripts) {
keepScripts = context;
}

scripts = !keepScripts && [];
parsed = parse(data, null, scripts);

if (scripts) {
this(scripts).remove();
parsed = this.load(data);
if (!keepScripts) {
parsed('script').remove();
}

return parsed.children;
return parsed.root()[0].children;
};

/**
Expand Down

0 comments on commit 2e4f73f

Please sign in to comment.