Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use the parser's DOM without modifications #1559

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ The options in the `xml` object are taken directly from [htmlparser2](https://gi

```js
{
withDomLvl1: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option no longer exists in htmlparser2.

normalizeWhitespace: false,
xmlMode: true,
decodeEntities: true
Expand Down
28 changes: 13 additions & 15 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var parse = require('../parse');
var html = require('../static').html;
var text = require('../static').text;
var updateDOM = parse.update;
var evaluate = parse.evaluate;
var utils = require('../utils');
var domEach = utils.domEach;
var cloneDom = utils.cloneDom;
Expand Down Expand Up @@ -39,7 +38,7 @@ exports._makeDomArray = function makeDomArray(elem, clone) {
[]
);
} else if (typeof elem === 'string') {
return evaluate(elem, this.options, false);
return parse(elem, this.options, false).children;
}
return clone ? cloneDom([elem]) : [elem];
};
Expand Down Expand Up @@ -90,7 +89,7 @@ var uniqueSplice = function (array, spliceIdx, spliceCount, newElems, parent) {
// current array.
for (idx = 0, len = newElems.length; idx < len; ++idx) {
node = newElems[idx];
oldParent = node.parent || node.root;
oldParent = node.parent;
prevIdx = oldParent && oldParent.children.indexOf(newElems[idx]);

if (oldParent && prevIdx > -1) {
Expand All @@ -100,7 +99,6 @@ var uniqueSplice = function (array, spliceIdx, spliceCount, newElems, parent) {
}
}

node.root = null;
node.parent = parent;

if (node.prev) {
Expand Down Expand Up @@ -282,7 +280,7 @@ exports.wrap = function (wrapper) {

for (var i = 0; i < this.length; i++) {
var el = this[i];
var parent = el.parent || el.root;
var parent = el.parent;
var siblings = parent.children;
var wrapperDom;
var elInsertLocation;
Expand Down Expand Up @@ -436,7 +434,7 @@ exports.after = function () {
var lastIdx = this.length - 1;

domEach(this, function (i, el) {
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand Down Expand Up @@ -496,7 +494,7 @@ exports.insertAfter = function (target) {
self.remove();
domEach(target, function (i, el) {
var clonedSelf = self._makeDomArray(self.clone());
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand Down Expand Up @@ -535,7 +533,7 @@ exports.before = function () {
var lastIdx = this.length - 1;

domEach(this, function (i, el) {
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand Down Expand Up @@ -596,7 +594,7 @@ exports.insertBefore = function (target) {
self.remove();
domEach(target, function (i, el) {
var clonedSelf = self._makeDomArray(self.clone());
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand Down Expand Up @@ -638,7 +636,7 @@ exports.remove = function (selector) {
if (selector) elems = elems.filter(selector);

domEach(elems, function (i, el) {
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand All @@ -655,7 +653,7 @@ exports.remove = function (selector) {
if (el.next) {
el.next.prev = el.prev;
}
el.prev = el.next = el.parent = el.root = null;
el.prev = el.next = el.parent = null;
});

return this;
Expand Down Expand Up @@ -683,7 +681,7 @@ exports.replaceWith = function (content) {
var self = this;

domEach(this, function (i, el) {
var parent = el.parent || el.root;
var parent = el.parent;
if (!parent) {
return;
}
Expand All @@ -702,7 +700,7 @@ exports.replaceWith = function (content) {

// Completely remove old element
uniqueSplice(siblings, index, 1, dom, parent);
el.parent = el.prev = el.next = el.root = null;
el.parent = el.prev = el.next = null;
});

return this;
Expand Down Expand Up @@ -762,7 +760,7 @@ exports.html = function (str) {

var content = str.cheerio
? str.clone().get()
: evaluate('' + str, opts, false);
: parse('' + str, opts, false).children;

updateDOM(content, el);
});
Expand Down Expand Up @@ -813,7 +811,7 @@ exports.text = function (str) {
child.next = child.prev = child.parent = null;
});

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

updateDOM(textNode, el);
Expand Down
8 changes: 6 additions & 2 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ exports.parent = function (selector) {

domEach(this, function (idx, elem) {
var parentElem = elem.parent;
if (parentElem && set.indexOf(parentElem) < 0) {
if (
parentElem &&
parentElem.type !== 'root' &&
set.indexOf(parentElem) < 0
) {
set.push(parentElem);
}
});
Expand Down Expand Up @@ -848,7 +852,7 @@ exports.slice = function () {

function traverseParents(self, elem, selector, limit) {
var elems = [];
while (elem && elems.length < limit) {
while (elem && elems.length < limit && elem.type !== 'root') {
if (!selector || exports.filter.call([elem], selector, self).length) {
elems.push(elem);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,10 @@ api.forEach(function (mod) {
});

var isNode = function (obj) {
return obj.name || obj.type === 'text' || obj.type === 'comment';
return (
obj.name ||
obj.type === 'root' ||
obj.type === 'text' ||
obj.type === 'comment'
);
};
1 change: 0 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

exports.default = {
withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true,
Expand Down
64 changes: 29 additions & 35 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,7 @@ var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
/*
Parser
*/
exports = module.exports = function (content, options, isDocument) {
var dom = exports.evaluate(content, options, isDocument);
// Generic root element
var root = exports.evaluate('<root></root>', options, false)[0];

root.type = 'root';
root.parent = null;

// Update the dom using the root
exports.update(dom, root);

return root;
};

function parseWithParse5(content, options, isDocument) {
var parse = isDocument ? parse5.parse : parse5.parseFragment;
var root = parse(content, {
treeAdapter: htmlparser2Adapter,
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
});

return root.children;
}

exports.evaluate = function (content, options, isDocument) {
exports = module.exports = function parse(content, options, isDocument) {
// options = options || $.fn.options;

var dom;
Expand All @@ -45,15 +21,39 @@ exports.evaluate = function (content, options, isDocument) {
var useHtmlParser2 = options.xmlMode || options._useHtmlParser2;

dom = useHtmlParser2
? htmlparser.parseDOM(content, options)
? htmlparser.parseDocument(content, options)
: parseWithParse5(content, options, isDocument);
} else {
dom = content;
if (
typeof content === 'object' &&
content != null &&
content.type === 'root'
) {
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);

dom = root;
}
}

return dom;
};

function parseWithParse5(content, options, isDocument) {
var parse = isDocument ? parse5.parse : parse5.parseFragment;

return parse(content, {
treeAdapter: htmlparser2Adapter,
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
});
}

/*
Update the dom structure, for one changed layer
*/
Expand All @@ -73,7 +73,7 @@ exports.update = function (arr, parent) {
var node = arr[i];

// Cleanly remove existing nodes from their previous structures.
var oldParent = node.parent || node.root;
var oldParent = node.parent;
var oldSiblings = oldParent && oldParent.children;
if (oldSiblings && oldSiblings !== arr) {
oldSiblings.splice(oldSiblings.indexOf(node), 1);
Expand All @@ -92,13 +92,7 @@ exports.update = function (arr, parent) {
node.prev = node.next = null;
}

if (parent && parent.type === 'root') {
node.root = parent;
node.parent = null;
} else {
node.root = null;
node.parent = parent;
}
node.parent = parent;
}

return parent;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports.cloneDom = function (dom) {
var parents =
'length' in dom
? Array.prototype.map.call(dom, function (el) {
return el.parent;
return el.parent && el.parent.type === 'root' ? null : el.parent;
})
: [dom.parent];

Expand Down
6 changes: 3 additions & 3 deletions 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 @@ -27,7 +27,7 @@
"node": ">= 0.6"
},
"dependencies": {
"css-select": "~3.1.1",
"css-select": "^3.1.2",
"dom-serializer": "~1.2.0",
"entities": "~2.1.0",
"htmlparser2": "^6.0.0",
Expand Down
Loading