UNPKG

3.12 kBJavaScriptView Raw
1/*
2 * EJS Embedded JavaScript templates
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17*/
18
19/**
20 * Private utility functions
21 * @module utils
22 * @private
23 */
24
25'use strict';
26
27var regExpChars = /[|\\{}()[\]^$+*?.]/g;
28
29/**
30 * Escape characters reserved in regular expressions.
31 *
32 * If `string` is `undefined` or `null`, the empty string is returned.
33 *
34 * @param {String} string Input string
35 * @return {String} Escaped string
36 * @static
37 * @private
38 */
39exports.escapeRegExpChars = function (string) {
40 // istanbul ignore if
41 if (!string) {
42 return '';
43 }
44 return String(string).replace(regExpChars, '\\$&');
45};
46
47var _ENCODE_HTML_RULES = {
48 '&': '&',
49 '<': '&lt;',
50 '>': '&gt;',
51 '"': '&#34;',
52 "'": '&#39;'
53};
54var _MATCH_HTML = /[&<>\'"]/g;
55
56function encode_char(c) {
57 return _ENCODE_HTML_RULES[c] || c;
58}
59
60/**
61 * Stringified version of constants used by {@link module:utils.escapeXML}.
62 *
63 * It is used in the process of generating {@link ClientFunction}s.
64 *
65 * @readonly
66 * @type {String}
67 */
68
69var escapeFuncStr =
70 'var _ENCODE_HTML_RULES = {\n'
71+ ' "&": "&amp;"\n'
72+ ' , "<": "&lt;"\n'
73+ ' , ">": "&gt;"\n'
74+ ' , \'"\': "&#34;"\n'
75+ ' , "\'": "&#39;"\n'
76+ ' }\n'
77+ ' , _MATCH_HTML = /[&<>\'"]/g;\n'
78+ 'function encode_char(c) {\n'
79+ ' return _ENCODE_HTML_RULES[c] || c;\n'
80+ '};\n';
81
82/**
83 * Escape characters reserved in XML.
84 *
85 * If `markup` is `undefined` or `null`, the empty string is returned.
86 *
87 * @implements {EscapeCallback}
88 * @param {String} markup Input string
89 * @return {String} Escaped string
90 * @static
91 * @private
92 */
93
94exports.escapeXML = function (markup) {
95 return markup == undefined
96 ? ''
97 : String(markup)
98 .replace(_MATCH_HTML, encode_char);
99};
100exports.escapeXML.toString = function () {
101 return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
102};
103
104/**
105 * Copy all properties from one object to another, in a shallow fashion.
106 *
107 * @param {Object} to Destination object
108 * @param {Object} from Source object
109 * @return {Object} Destination object
110 * @static
111 * @private
112 */
113exports.shallowCopy = function (to, from) {
114 from = from || {};
115 for (var p in from) {
116 to[p] = from[p];
117 }
118 return to;
119};
120
121/**
122 * Simple in-process cache implementation. Does not implement limits of any
123 * sort.
124 *
125 * @implements Cache
126 * @static
127 * @private
128 */
129exports.cache = {
130 _data: {},
131 set: function (key, val) {
132 this._data[key] = val;
133 },
134 get: function (key) {
135 return this._data[key];
136 },
137 reset: function () {
138 this._data = {};
139 }
140};