jqgram, PQ-Gram, , Node.js, #. , ... . .
https://github.com/hoonto/jqgram
DOM vs cheerio, , , . , . , , , . DOM DOM Cheerio vs Cheerio - HTML, Cheerio, , (Cheerio - jQuery- DOM- ).
, , Node.js javascript , , , #/. NET.
var cheerio = require('./lib/cheerio');
var jq = require("../jqgram").jqgram;
var html = '<body><div id="a"><div class="c d"><span>Irrelevent text</span></div></div></body>';
var cheeriodom = cheerio.load(html, {
ignoreWhitespace: false,
lowerCaseTags: true
});
var realdom = $('body');
jq.distance({
root: cheeriodom,
lfn: function(node){
return node.name;
},
cfn: function(node){
var retarr = [];
if(!! node.attribs && !! node.attribs.class){
retarr = retarr.concat(node.attribs.class.split(' '));
}
if(!! node.attribs && !! node.attribs.id){
retarr.push(node.attribs.id);
}
retarr = retarr.concat(node.children);
return retarr;
}
},{
root: realdom,
lfn: function(node){
return node.nodeName.toLowerCase();
},
cfn: function(node){
var retarr = [];
if(!! node.attributes && !! node.attributes.class && !! node.attributes.class.nodeValue){
retarr = retarr.concat(node.attributes.class.nodeValue.split(' '));
}
if(!! node.attributes && !! node.attributes.id && !! node.attributes.id.nodeValue) {
retarr.push(node.attributes.id.nodeValue);
}
for(var i=0; i<node.children.length; ++i){
retarr.push(node.children[i]);
}
return retarr;
}
},{ p:2, q:3, depth:10 },
function(result) {
console.log(result.distance);
});
source
share