Debian Codegraph @

lib/compiler/js.js

raw symbols SCIP
1 "use strict";
2
3 function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); }
4
5 /* JavaScript code generation helpers. */
6 var js = {
7 stringEscape: function(s) {
8 /*
9 * ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a string
10 * literal except for the closing quote character, backslash, carriage
11 * return, line separator, paragraph separator, and line feed. Any character
12 * may appear in the form of an escape sequence.
13 *
14 * For portability, we also escape all control and non-ASCII characters.
15 * Note that the "\v" escape sequence is not used because IE does not like
16 * it.
17 */
18 return s
19 .replace(/\\/g, '\\\\') // backslash
20 .replace(/"/g, '\\"') // closing double quote
21 .replace(/\0/g, '\\0') // null
22 .replace(/\x08/g, '\\b') // backspace
23 .replace(/\t/g, '\\t') // horizontal tab
24 .replace(/\n/g, '\\n') // line feed
25 .replace(/\f/g, '\\f') // form feed
26 .replace(/\r/g, '\\r') // carriage return
27 .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
28 .replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
29 .replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
30 .replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
31 },
32
33 regexpClassEscape: function(s) {
34 /*
35 * Based on ECMA-262, 5th ed., 7.8.5 & 15.10.1.
36 *
37 * For portability, we also escape all control and non-ASCII characters.
38 */
39 return s
40 .replace(/\\/g, '\\\\') // backslash
41 .replace(/\//g, '\\/') // closing slash
42 .replace(/\]/g, '\\]') // closing bracket
43 .replace(/\^/g, '\\^') // caret
44 .replace(/-/g, '\\-') // dash
45 .replace(/\0/g, '\\0') // null
46 .replace(/\t/g, '\\t') // horizontal tab
47 .replace(/\n/g, '\\n') // line feed
48 .replace(/\v/g, '\\x0B') // vertical tab
49 .replace(/\f/g, '\\f') // form feed
50 .replace(/\r/g, '\\r') // carriage return
51 .replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); })
52 .replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); })
53 .replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); })
54 .replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); });
55 }
56 };
57
58 module.exports = js;