2 * EJS Embedded JavaScript templates
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Private utility functions
27 var regExpChars = /[|\\{}()[\]^$+*?.]/g;
30 * Escape characters reserved in regular expressions.
32 * If `string` is `undefined` or `null`, the empty string is returned.
34 * @param {String} string Input string
35 * @return {String} Escaped string
39 exports.escapeRegExpChars = function (string) {
44 return String(string).replace(regExpChars, '\\$&');
47 var _ENCODE_HTML_RULES = {
54 var _MATCH_HTML = /[&<>'"]/g;
56 function encode_char(c) {
57 return _ENCODE_HTML_RULES[c] || c;
61 * Stringified version of constants used by {@link module:utils.escapeXML}.
63 * It is used in the process of generating {@link ClientFunction}s.
70 'var _ENCODE_HTML_RULES = {\n'
74 + ' , \'"\': """\n'
75 + ' , "\'": "'"\n'
77 + ' , _MATCH_HTML = /[&<>\'"]/g;\n'
78 + 'function encode_char(c) {\n'
79 + ' return _ENCODE_HTML_RULES[c] || c;\n'
83 * Escape characters reserved in XML.
85 * If `markup` is `undefined` or `null`, the empty string is returned.
87 * @implements {EscapeCallback}
88 * @param {String} markup Input string
89 * @return {String} Escaped string
94 exports.escapeXML = function (markup) {
95 return markup == undefined
98 .replace(_MATCH_HTML, encode_char);
100 exports.escapeXML.toString = function () {
101 return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
105 * Naive copy of properties from one object to another.
106 * Does not recurse into non-scalar properties
107 * Does not check to see if the property has a value before copying
109 * @param {Object} to Destination object
110 * @param {Object} from Source object
111 * @return {Object} Destination object
115 exports.shallowCopy = function (to, from) {
117 for (var p in from) {
124 * Naive copy of a list of key names, from one object to another.
125 * Only copies property if it is actually defined
126 * Does not recurse into non-scalar properties
128 * @param {Object} to Destination object
129 * @param {Object} from Source object
130 * @param {Array} list List of properties to copy
131 * @return {Object} Destination object
135 exports.shallowCopyFromList = function (to, from, list) {
136 for (var i = 0; i < list.length; i++) {
138 if (typeof from[p] != 'undefined') {
146 * Simple in-process cache implementation. Does not implement limits of any
149 * @implements {Cache}
155 set: function (key, val) {
156 this._data[key] = val;
158 get: function (key) {
159 return this._data[key];
161 remove: function (key) {
162 delete this._data[key];
170 * Transforms hyphen case variable into camel case.
172 * @param {String} string Hyphen case string
173 * @return {String} Camel case string
177 exports.hyphenToCamel = function (str) {
178 return str.replace(/-[a-z]/g, function (match) { return match[1].toUpperCase(); });