mathiasbynens / he

A robust HTML entity encoder/decoder written in JavaScript.
https://mths.be/he
MIT License
3.45k stars 254 forks source link

Uncaught TypeError: Cannot read property 'replace' of undefined #61

Closed bauer-git closed 6 years ago

bauer-git commented 6 years ago

When I use this function I get this error... he.js:202 Uncaught TypeError: Cannot read property 'replace' of undefined

Where is 'regexEscape' being initialized?

mathiasbynens commented 6 years ago

Do you have a reproducible test case?

bauer-git commented 6 years ago

I saw 10 seconds after I wrote this exactly where you initialize 'regexEscape'.

All I can tell you is I'm using this in Joomla - with the Fabrik extension. If no one else has ever reported this problem, then I assume it's got to be either Joomla, Fabrik, or my browser (latest version of Chrome) that causes/throws the error.

Maybe there is something in either that doesn't allow variables such as what is created when you initialize regexEscape - and all the others done similar fashion? (What var type is that anyhow - is it considered a string?)

Anyhow, I couldn't use your code. Thank you.

mathiasbynens commented 6 years ago

Again, do you have a test case that reproduces the problem? Otherwise, there’s nothing I can do.

nico2che commented 5 years ago

This can happen with he.encode(undefined) or he.decode(undefined)

IMHO, if the choice was to not handle undefined or null values, an appropriate error could be throw to prevent this usage instead of an Uncaught TypeError

krizpoon commented 5 years ago

This gave me a surprise too. I think the library should handle the undefined and null cases otherwise we have to write code like he.encode(string || '') all over the place. Or we'll need a wrapper function like const htmlEncode = (string, options) => typeof string === 'string' ? he.encode(string, options) : '';