Skip to content

Commit

Permalink
Disables auto introduction of <head>,<body> tags in Cheerio
Browse files Browse the repository at this point in the history
This removes check for body tag using Regex. Earlier check fails, on
`<body>` tags with special character attribute names. They are common
when using frameworks like Alpine.js.

Fixes #16.
  • Loading branch information
saneef committed Jun 3, 2022
1 parent 2ed953d commit b9825e1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 15 deletions.
12 changes: 0 additions & 12 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,10 @@ function objectToAttributes(obj) {
}, "");
}

/**
* Checks for `<body>` tag in a string.
*
* @param {string} content The content
* @return {boolean} `True` if body tag, `False` otherwise.
*/
function hasBodyTag(content) {
const hasBody = /<\s*body(\w|\s|=|"|-)*>/gm;
return hasBody.test(content);
}

module.exports = {
isUrl,
isAllowedExtension,
parseStringToNumbers,
generateWidths,
objectToAttributes,
hasBodyTag,
};
5 changes: 2 additions & 3 deletions lib/img2picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
generateWidths,
parseStringToNumbers,
objectToAttributes,
hasBodyTag,
} = require("./helpers.js");

/**
Expand Down Expand Up @@ -210,7 +209,7 @@ async function generateImage(attrs, options) {
*/
async function replaceImages(content, options) {
const { extensions, fetchRemote } = options;
const $ = cheerio.load(content);
const $ = cheerio.load(content, null, false);
let images = $("img")
.not("picture img") // Ignore images wrapped in <picture>
.not("[data-img2picture-ignore]") // Ignore excluded images
Expand Down Expand Up @@ -243,7 +242,7 @@ async function replaceImages(content, options) {
$(images[i]).replaceWith(picture);
});

return hasBodyTag(content) ? $.html() : $("body").html();
return $.html();
}

/**
Expand Down

0 comments on commit b9825e1

Please sign in to comment.