mathiasbynens / he

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

TypeError: html.replace is not a function. #60

Closed lapinskicho closed 6 years ago

lapinskicho commented 6 years ago

When I tested the module with the following code:

// file: decode.js
const Transform = require('stream').Transform;
const he = require('he');

const parser = new Transform();
parser._transform = function(data, encoding, done) {
    this.push(he.decode(data));
    done();
};
process.stdin.pipe(parser).pipe(process.stdout);
process.stdout.on('error', process.exit);

and run it by decoding any text file, say, decoding its own:

$ cat decode.js | node decode.js

I got the following error:

./node_modules/he/he.js:232 return html.replace(regexDecode, function($0, $1, $2, $3, $4, $5, $6, $7) { ^ TypeError: html.replace is not a function at Object.decode (./node_modules/he/he.js:232:15)

and if I changed html.replace to String(html).replace, it fixes the TypeError. Is it a valid bug/fix?

mathiasbynens commented 6 years ago

he.decode only accepts strings by design.

The fix is to change the code that’s calling he.decode to ensure it converts the argument into a string, e.g.

-    this.push(he.decode(data));
+    this.push(he.decode(String(data)));