summaryrefslogtreecommitdiff
path: root/js/lib/common.js
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-11 23:01:14 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-11 23:01:14 -0800
commitffbca7b85198c2d0efd71b95ef4a1fa693578af0 (patch)
treec08e532e69dbf4640121931a76c53ba63847ab69 /js/lib/common.js
parenta0cbcefe82a6bff0a9b44550e22244d6d5d727c0 (diff)
Factored out unescapeString into new module, js/common.js.
This is used in both blocks.js and inlines.js.
Diffstat (limited to 'js/lib/common.js')
-rw-r--r--js/lib/common.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/js/lib/common.js b/js/lib/common.js
new file mode 100644
index 0000000..e7cc13b
--- /dev/null
+++ b/js/lib/common.js
@@ -0,0 +1,30 @@
+var entityToChar = require('./html5-entities.js').entityToChar;
+
+var ENTITY = "&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});";
+
+var reBackslashOrAmp = /[\\&]/;
+
+var ESCAPABLE = '[!"#$%&\'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]';
+
+var reEntityOrEscapedChar = new RegExp('\\\\' + ESCAPABLE + '|' + ENTITY, 'gi');
+
+var unescapeChar = function(s) {
+ "use strict";
+ if (s[0] === '\\') {
+ return s[1];
+ } else {
+ return entityToChar(s);
+ }
+};
+
+// Replace entities and backslash escapes with literal characters.
+var unescapeString = function(s) {
+ "use strict";
+ if (reBackslashOrAmp.test(s)) {
+ return s.replace(reEntityOrEscapedChar, unescapeChar);
+ } else {
+ return s;
+ }
+};
+
+module.exports = { unescapeString: unescapeString };