123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict';
- var extend = require('extend-shallow');
- var regexCache = {};
- var all;
- var charSets = {
- default: {
- '"': '"',
- '"': '"',
- ''': '\'',
- ''': '\'',
- '&': '&',
- '&': '&',
- '>': '>',
- '>': '>',
- '<': '<',
- '<': '<'
- },
- extras: {
- '¢': '¢',
- '¢': '¢',
- '©': '©',
- '©': '©',
- '€': '€',
- '€': '€',
- '£': '£',
- '£': '£',
- '®': '®',
- '®': '®',
- '¥': '¥',
- '¥': '¥'
- }
- };
- // don't merge char sets unless "all" is explicitly called
- Object.defineProperty(charSets, 'all', {
- get: function() {
- return all || (all = extend({}, charSets.default, charSets.extras));
- }
- });
- /**
- * Convert HTML entities to HTML characters.
- *
- * @param {String} `str` String with HTML entities to un-escape.
- * @return {String}
- */
- function unescape(str, type) {
- if (!isString(str)) return '';
- var chars = charSets[type || 'default'];
- var regex = toRegex(type, chars);
- return str.replace(regex, function(m) {
- return chars[m];
- });
- }
- function toRegex(type, chars) {
- if (regexCache[type]) {
- return regexCache[type];
- }
- var keys = Object.keys(chars).join('|');
- var regex = new RegExp('(?=(' + keys + '))\\1', 'g');
- regexCache[type] = regex;
- return regex;
- }
- /**
- * Returns true if str is a non-empty string
- */
- function isString(str) {
- return str && typeof str === 'string';
- }
- /**
- * Expose charSets
- */
- unescape.chars = charSets.default;
- unescape.extras = charSets.extras;
- // don't trip the "charSets" getter unless it's explicitly called
- Object.defineProperty(unescape, 'all', {
- get: function() {
- return charSets.all;
- }
- });
- /**
- * Expose `unescape`
- */
- module.exports = unescape;
|