Skip to content

Conversation

@Mallikarjun-0
Copy link

@Mallikarjun-0 Mallikarjun-0 commented Apr 12, 2025

This PR improves the isHtml function in utils by adding a type check to ensure the input is a string. This prevents runtime errors when non-string values (like arrays or objects) are passed to the function.

Example code:

import { isHtml } from 'cheerio/utils';
const str = '<html></html>';

console.log(isHtml(str));    // true

const arr = JSON.parse(' {"html" : ["<", "html", ">", "<", "/", "html", ">"]}');
console.log(isHtml(arr.html));    // Error
  • The first call to isHtml returns true, but in the second call it throws an runtime error as charCodeAt() is not defined for arrays.
  • An explicit type check in the function isHtml() can prevent unexpected runtime crashes.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant