unescapeHTML.js 664 B

1234567891011121314151617181920
  1. var makeString = require('./helper/makeString');
  2. var htmlEntities = require('./helper/htmlEntities');
  3. module.exports = function unescapeHTML(str) {
  4. return makeString(str).replace(/\&([^;]{1,10});/g, function(entity, entityCode) {
  5. var match;
  6. if (entityCode in htmlEntities) {
  7. return htmlEntities[entityCode];
  8. /*eslint no-cond-assign: 0*/
  9. } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
  10. return String.fromCharCode(parseInt(match[1], 16));
  11. /*eslint no-cond-assign: 0*/
  12. } else if (match = entityCode.match(/^#(\d+)$/)) {
  13. return String.fromCharCode(~~match[1]);
  14. } else {
  15. return entity;
  16. }
  17. });
  18. };