Skip to content

Instantly share code, notes, and snippets.

@jugglinmike
Created March 5, 2017 18:35
Show Gist options
  • Save jugglinmike/3661b713ffb0393c5dba16375255389d to your computer and use it in GitHub Desktop.
Save jugglinmike/3661b713ffb0393c5dba16375255389d to your computer and use it in GitHub Desktop.
Cheerio/parse5 research
'use strict';
var cheerio = require('../cheerio');
var doc = [
'<!DOCTYPE html>',
'<html>',
'<body>',
' <h1>Hello, world.</h1>',
'</body>',
'</html>'
].join('\n');
var $1 = cheerio.load(doc);
var $2 = cheerio.load(doc, { useHtmlParser2: true });
function inspect(node) {
var props = [
'tagName', 'childNodes', 'parentNode', 'previousSibling',
'nextSibling', 'nodeValue'
];
var lines = [];
var prop, idx, length;
for (idx = 0, length = props.length; idx < length; ++idx) {
prop = props[idx];
lines.push(prop + ': ' + typeof node[prop]);
}
return ' ' + lines.join('\n ');
}
console.log('parse5\n' + inspect($1('h1')[0]));
console.log('htmlParser2\n' + inspect($2('h1')[0]));
/**
* Output:
*
* parse5
* tagName: string
* childNodes: object
* parentNode: object
* previousSibling: object
* nextSibling: object
* nodeValue: object
* htmlParser2
* tagName: string
* childNodes: object
* parentNode: object
* previousSibling: object
* nextSibling: object
* nodeValue: object
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment