i am searching for a node html parser which make it possible to get not only DOM nodes but html comments too in any way. is this possible`?
The DOM tree contains comment nodes, but there is no explicit way to query them from cheerio.
Does this not work:
var $comments = $( "body" ).contents().filter(function() {
return this.nodeType === 8;
});
I haven't tested with nodeType 8 (comments), but I've used it plenty with nodeType 3.
I can confirm that you can access the comments like so in older versions of Cheerio:
var $comments = $("*").contents().filter(function () {
return this.nodeType === 8;
});
$comments.each(function(){
// read the existing comment
console.log(this.data);
// change it to something else
this.data = "something else";
});
I can confirm that you can access the comments like so in older versions of Cheerio:
var $comments = $("*").contents().filter(function () { return this.nodeType === 8; }); $comments.each(function(){ // read the existing comment console.log(this.data); // change it to something else this.data = "something else"; });
What versions?
What versions?
I'm not sure. What version is it not working in?
I can confirm that you can access the comments like so in older versions of Cheerio:
var $comments = $("*").contents().filter(function () { return this.nodeType === 8; }); $comments.each(function(){ // read the existing comment console.log(this.data); // change it to something else this.data = "something else"; });What versions?
Seems to be working for me in "cheerio": "^1.0.0-rc.2"
Seems to be working for me in "cheerio": "^1.0.0-rc.2"
Nice.
I'm using "cheerio": "^1.0.0-rc.3",, and I find out this works.
const $ = cheerio.load(htmlData, { decodeEntities: false });
$('body').contents().map((i, el) => {
if (el.type === 'comment') {
console.log(el.data) // You can get the contents of html comment
console.log($(el).html()) // This is null
console.log($(el).text()) // This is ' '
}
})
CheerioElement type has .type property, which is defined as string, and it looks like the possible strings are 3, 'tag' | 'text' | 'comment'.
And I tried 3 ways and find that the contents of comment is assigned in el.data.
Most helpful comment
Seems to be working for me in
"cheerio": "^1.0.0-rc.2"