question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically adding <html><head><body> ?

See original GitHub issue

OSX 10.12.4 Node 8.0 “cheerio”: “1.0.0-rc.1”

Not sure if this is expected, but:

let $ = cheerio.load('<div>Hello</div>');
console.log($.html());

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

While in my case the auto-wrapping as an <html> document is weirdly useful, I was still surprised to see it. Is this a bug, or a feature? I don’t see a .fragment() method or anything like that. Maybe just missed something.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:42
  • Comments:33 (14 by maintainers)

github_iconTop GitHub Comments

60reactions
musienkoyuriycommented, Feb 25, 2018

Use xmlMode: true to avoid auto-wrapping:

const parsed = cheerio.load(content, {
  xmlMode: true
});
24reactions
fb55commented, Dec 20, 2020

To give an answer here: The load method has a third argument, isDocument. If you set this to false, your document will be parsed in fragment mode and no <html> root tag (or other boilerplate) will be inserted. To use the example from the original issue:

let $ = cheerio.load('<div>Hello</div>', null, false);
console.log($.html());

// "<div>Hello</div>"
Read more comments on GitHub >

github_iconTop Results From Across the Web

don't automatically adding <html><head><body> #1247
basically if you load document as fragment - elements like html, body and head will not be added. All reactions.
Read more >
Don't put html, head and body tags automatically, beautifulsoup
Unlike html5lib, this parser makes no attempt to create a well-formed HTML document by adding a <body> tag. Unlike lxml, it doesn't even ......
Read more >
HTML head Elements - W3Schools
The <head> element is a container for metadata (data about data) and is placed between the <html> tag and the <body> tag. HTML...
Read more >
It tells me that my "</head> tag needs to come before your ...
The HTML-Element has no parent but is a parent to 2 child-Element's the 'head'-Element and the 'body'-Element. The 'head'- and 'body'-Element, both being ......
Read more >
HTML Body Tag: Master The Most Important HTML Element ...
The <body> element contains the entire content of a webpage. It must be the second element inside of the parent <html> element, following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found