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

Breaking change in 1.0.0: htmlparser2 mode self-closes empty tags #4034

Open
nwalters512 opened this issue Aug 17, 2024 · 1 comment
Open

Comments

@nwalters512
Copy link

Reproduction: https://github.com/nwalters512/cheerio-self-closing-repro

Code for reference:

import * as oldCheerio from 'cheerio-rc/lib/slim';
import * as newCheerio from 'cheerio-1/slim';

const HTML = '<html><head></head><body><div></div></body></html>';

console.log(oldCheerio.load(HTML).html());
console.log(newCheerio.load(HTML).html());

console.log(oldCheerio.load(HTML, { recognizeSelfClosing: true }).html());
console.log(newCheerio.load(HTML, { xml: { recognizeSelfClosing: true } }).html());

Steps to reproduce:

  • Clone the repository.
  • Run yarn.
  • Run node index.js.

Observe the following output is printed:

<html><head></head><body><div></div></body></html>
<html><head></head><body><div></div></body></html>
<html><head></head><body><div></div></body></html>
<html><head/><body><div/></body></html>

Specifically, note that <head> and <div> were serialized as self-closing tags.

I'm not sure if this should be considered a bug or not, but it appears to be a breaking change and it isn't called out anywhere in the release notes or upgrade guide: https://cheerio.js.org/blog/cheerio-1.0

@nwalters512
Copy link
Author

It seems that things work as expected if I change the last line to the following (adding xmlMode: false:

console.log(newCheerio.load(HTML, { xml: { recognizeSelfClosing: true, xmlMode: false } }).html());

This doesn't make sense given the configuration documentation (https://cheerio.js.org/docs/advanced/configuring-cheerio#using-htmlparser2-for-html) which states:

You can also use Cheerio's slim export, which always uses htmlparser2. This avoids loading parse5, which saves some bytes eg. in browser environments:

That is, I would expect to not have to set xmlMode: false when using the "slim" export. Do I in fact have to set xmlMode: false even in that case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant