Skip to content

Commit

Permalink
refactor: Use domhandler nodes directly
Browse files Browse the repository at this point in the history
Avoids unnecessary parser calls. Fixes #1166.

Allows us to drop lodash. Fixes #1506.
  • Loading branch information
fb55 committed Dec 15, 2020
1 parent fc4e4fa commit 034ac5b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
6 changes: 2 additions & 4 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var domEach = utils.domEach;
var cloneDom = utils.cloneDom;
var isHtml = utils.isHtml;
var slice = Array.prototype.slice;
var domhandler = require('domhandler');

/**
* Create an array of nodes, recursing into arrays and parsing strings if
Expand Down Expand Up @@ -803,16 +804,13 @@ exports.text = function (str) {
});
}

var opts = this.options;

// Append text node to each selected elements
domEach(this, function (i, el) {
el.children.forEach(function (child) {
child.next = child.prev = child.parent = null;
});

var textNode = parse(' ', opts).children[0];
textNode.data = str;
var textNode = new domhandler.Text(str);

updateDOM(textNode, el);
});
Expand Down
10 changes: 5 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var htmlparser = require('htmlparser2');
var parse5 = require('parse5');
var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
var domhandler = require('domhandler');

/*
Parser
Expand Down Expand Up @@ -32,11 +33,10 @@ exports = module.exports = function parse(content, options, isDocument) {
dom = content;
} else {
// Generic root element
var root = parse('', options, false);
root.children.length = 0;

// Update the dom using the root
exports.update(content, root);
var root = new domhandler.Document(content);
content.forEach(function (node) {
node.parent = root;
});

dom = root;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var cloneDeepWith = require('lodash/cloneDeepWith');
var domhandler = require('domhandler');

// HTML Tags
var tags = { tag: true, script: true, style: true };
Expand Down Expand Up @@ -64,20 +64,20 @@ exports.domEach = function (cheerio, fn) {
* @private
*/
exports.cloneDom = function (dom) {
var parents =
var clone =
'length' in dom
? Array.prototype.map.call(dom, function (el) {
return el.parent && el.parent.type === 'root' ? null : el.parent;
return domhandler.cloneNode(el, true);
})
: [dom.parent];
: [domhandler.cloneNode(dom, true)];

function filterOutParent(node) {
if (parents.indexOf(node) > -1) {
return null;
}
}
// Add a root node around the cloned nodes
var root = new domhandler.Document(clone);
clone.forEach(function (node) {
node.parent = root;
});

return cloneDeepWith(dom, filterOutParent);
return clone;
};

/*
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"dependencies": {
"css-select": "^3.1.2",
"dom-serializer": "~1.2.0",
"domhandler": "^4.0.0",
"entities": "~2.1.0",
"htmlparser2": "^6.0.0",
"lodash": "^4.17.20",
"parse5": "^6.0.0",
"parse5-htmlparser2-tree-adapter": "^6.0.0"
},
Expand Down

0 comments on commit 034ac5b

Please sign in to comment.