aboutsummaryrefslogtreecommitdiff
path: root/js/lib/common.js
blob: e7cc13bed8783278a84b73a1b2d189554b3f79bd (plain)
  1. var entityToChar = require('./html5-entities.js').entityToChar;
  2. var ENTITY = "&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});";
  3. var reBackslashOrAmp = /[\\&]/;
  4. var ESCAPABLE = '[!"#$%&\'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]';
  5. var reEntityOrEscapedChar = new RegExp('\\\\' + ESCAPABLE + '|' + ENTITY, 'gi');
  6. var unescapeChar = function(s) {
  7. "use strict";
  8. if (s[0] === '\\') {
  9. return s[1];
  10. } else {
  11. return entityToChar(s);
  12. }
  13. };
  14. // Replace entities and backslash escapes with literal characters.
  15. var unescapeString = function(s) {
  16. "use strict";
  17. if (reBackslashOrAmp.test(s)) {
  18. return s.replace(reEntityOrEscapedChar, unescapeChar);
  19. } else {
  20. return s;
  21. }
  22. };
  23. module.exports = { unescapeString: unescapeString };