Skip to content

fix: add input type validation to isHtml utility#4523

Merged
fb55 merged 3 commits intocheeriojs:mainfrom
Mallikarjun-0:type-validation
Jan 21, 2026
Merged

fix: add input type validation to isHtml utility#4523
fb55 merged 3 commits intocheeriojs:mainfrom
Mallikarjun-0:type-validation

Conversation

@Mallikarjun-0
Copy link
Copy Markdown
Contributor

@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.

@fb55 fb55 enabled auto-merge (squash) January 21, 2026 11:38
@fb55 fb55 merged commit 4fc2bef into cheeriojs:main Jan 21, 2026
9 checks passed
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.

2 participants