Tools like JSDOM, Cheerio, HtmlParser2, Puppeteer etc. all correctly mimic the browsers and correct malformed html. e.g. a div inside head ends up
div
head