You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.8 KiB

  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. * Private utility functions
  20. * @module utils
  21. * @private
  22. */
  23. 'use strict';
  24. var regExpChars = /[|\\{}()[\]^$+*?.]/g;
  25. /**
  26. * Escape characters reserved in regular expressions.
  27. *
  28. * If `string` is `undefined` or `null`, the empty string is returned.
  29. *
  30. * @param {String} string Input string
  31. * @return {String} Escaped string
  32. * @static
  33. * @private
  34. */
  35. exports.escapeRegExpChars = function (string) {
  36. // istanbul ignore if
  37. if (!string) {
  38. return '';
  39. }
  40. return String(string).replace(regExpChars, '\\$&');
  41. };
  42. var _ENCODE_HTML_RULES = {
  43. '&': '&',
  44. '<': '&lt;',
  45. '>': '&gt;',
  46. '"': '&#34;',
  47. "'": '&#39;'
  48. };
  49. var _MATCH_HTML = /[&<>'"]/g;
  50. function encode_char(c) {
  51. return _ENCODE_HTML_RULES[c] || c;
  52. }
  53. /**
  54. * Stringified version of constants used by {@link module:utils.escapeXML}.
  55. *
  56. * It is used in the process of generating {@link ClientFunction}s.
  57. *
  58. * @readonly
  59. * @type {String}
  60. */
  61. var escapeFuncStr =
  62. 'var _ENCODE_HTML_RULES = {\n'
  63. + ' "&": "&amp;"\n'
  64. + ' , "<": "&lt;"\n'
  65. + ' , ">": "&gt;"\n'
  66. + ' , \'"\': "&#34;"\n'
  67. + ' , "\'": "&#39;"\n'
  68. + ' }\n'
  69. + ' , _MATCH_HTML = /[&<>\'"]/g;\n'
  70. + 'function encode_char(c) {\n'
  71. + ' return _ENCODE_HTML_RULES[c] || c;\n'
  72. + '};\n';
  73. /**
  74. * Escape characters reserved in XML.
  75. *
  76. * If `markup` is `undefined` or `null`, the empty string is returned.
  77. *
  78. * @implements {EscapeCallback}
  79. * @param {String} markup Input string
  80. * @return {String} Escaped string
  81. * @static
  82. * @private
  83. */
  84. exports.escapeXML = function (markup) {
  85. return markup == undefined
  86. ? ''
  87. : String(markup)
  88. .replace(_MATCH_HTML, encode_char);
  89. };
  90. exports.escapeXML.toString = function () {
  91. return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
  92. };
  93. /**
  94. * Naive copy of properties from one object to another.
  95. * Does not recurse into non-scalar properties
  96. * Does not check to see if the property has a value before copying
  97. *
  98. * @param {Object} to Destination object
  99. * @param {Object} from Source object
  100. * @return {Object} Destination object
  101. * @static
  102. * @private
  103. */
  104. exports.shallowCopy = function (to, from) {
  105. from = from || {};
  106. for (var p in from) {
  107. to[p] = from[p];
  108. }
  109. return to;
  110. };
  111. /**
  112. * Naive copy of a list of key names, from one object to another.
  113. * Only copies property if it is actually defined
  114. * Does not recurse into non-scalar properties
  115. *
  116. * @param {Object} to Destination object
  117. * @param {Object} from Source object
  118. * @param {Array} list List of properties to copy
  119. * @return {Object} Destination object
  120. * @static
  121. * @private
  122. */
  123. exports.shallowCopyFromList = function (to, from, list) {
  124. for (var i = 0; i < list.length; i++) {
  125. var p = list[i];
  126. if (typeof from[p] != 'undefined') {
  127. to[p] = from[p];
  128. }
  129. }
  130. return to;
  131. };
  132. /**
  133. * Simple in-process cache implementation. Does not implement limits of any
  134. * sort.
  135. *
  136. * @implements Cache
  137. * @static
  138. * @private
  139. */
  140. exports.cache = {
  141. _data: {},
  142. set: function (key, val) {
  143. this._data[key] = val;
  144. },
  145. get: function (key) {
  146. return this._data[key];
  147. },
  148. remove: function (key) {
  149. delete this._data[key];
  150. },
  151. reset: function () {
  152. this._data = {};
  153. }
  154. };