When you try to parse Aurelia Templates, they always have tag.
Cheerio put this tag in the instead in the
I cannot make sense of this. Please try again
github remove my html tags from the comment (dull)
when you load aurelia template as . load('<template>......</template>')
and then console.log($.html())
you have <html><head><template>...........</template></head><body></body></html>
and nothing is working, because the body is empty.
If i remove <template> tag - everything is working fine, without Aurelia.
<template> tag is required for Aurelia.
@telbiyski what happens when you open that template in a web browser? Same thing, I'd imagine. This is the new default behaviour for cheerio.
if you can't fix it, i have to use the old version 0.22.0, but i think you have to make some fix for this. maybe option to wrap the content in <html><head><body>.... or not, like: .load(html, false) to work as the old cheerio.
Duplicate of https://github.com/cheeriojs/cheerio/issues/1024
This caught me by surprise too, so I did some digging, and here's what's going on:
const $ = cheerio.load("test");
console.log($.html());
This logged test in 0.22.0, now it logs <html><head></head><body>test</body></html>.
const $ = cheerio.load("<div>test</div>");
console.log($.html());
This logged test in 0.22.0, now it logs <html><head></head><body><div>test</div></body></html>.
const $ = cheerio.load("<template>test</template>");
console.log($.html());
This logged test in 0.22.0, now it logs <html><head><template>test</template></head><body></body></html>.
Now, comparing this to Google Chrome, this is indeed aligned with how a browser handles those cases.
That being said though, it makes it _much_ harder to use this library for it's intended purpose - how am I supposed to reliably find the node containing the content test?
Also, this actually breaks the example at the very top of the cheerio readme.md:
const cheerio = require('cheerio')
const $ = cheerio.load('<h2 class="title">Hello world</h2>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
In my opinion, this change undermines the entire premise of what this library is supposed to do, breaking a whole lot of dependent code as a consequence.
Also, just last month, the readme.md was updated in a commit titled Clarify project scope in README file - to quote the commit message for that:
The project frequently receives bug reports from new users who expect
full web browser behavior. The project documentation does not make it
particularly clear that such functionality is out-of-scope for the
project.
And the actual change introduced in that commit included this:
Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. It does not interpret the result as a web browser does.
So in conclusion, this change seems very, very wrong.
@stevenvachon Can you explain how I am supposed to parse a string that may be _either_ <template>test</template> or <div>test</div>, and then reliably replace the content test with foo, and finally get the result as a string - i.e. <template>foo</template> or <div>foo</div>?
const $ = cheerio.load("");
const body = $("body");
body.html("<template>test</template>");
body.html();
//-> <template>test</template>
or use fragments:
const $ = cheerio.load("");
const template = $("<template>test</template>");
template.html();
//-> test
template.toString();
//-> <template>test</template>
The only line here that differs from jQuery is the load().
The readme likely needs to be updated, as this is a release candidate. And while this is a breaking change, cheerio is certainly not broken. You're always welcome to continue using one of the pre-release versions.
ok, this is fine.
thanks
Most helpful comment
or use fragments:
The only line here that differs from jQuery is the
load().The readme likely needs to be updated, as this is a release candidate. And while this is a breaking change, cheerio is certainly not broken. You're always welcome to continue using one of the pre-release versions.