index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. var pug_has_own_property = Object.prototype.hasOwnProperty;
  3. /**
  4. * Merge two attribute objects giving precedence
  5. * to values in object `b`. Classes are special-cased
  6. * allowing for arrays and merging/joining appropriately
  7. * resulting in a string.
  8. *
  9. * @param {Object} a
  10. * @param {Object} b
  11. * @return {Object} a
  12. * @api private
  13. */
  14. exports.merge = pug_merge;
  15. function pug_merge(a, b) {
  16. if (arguments.length === 1) {
  17. var attrs = a[0];
  18. for (var i = 1; i < a.length; i++) {
  19. attrs = pug_merge(attrs, a[i]);
  20. }
  21. return attrs;
  22. }
  23. for (var key in b) {
  24. if (key === 'class') {
  25. var valA = a[key] || [];
  26. a[key] = (Array.isArray(valA) ? valA : [valA]).concat(b[key] || []);
  27. } else if (key === 'style') {
  28. var valA = pug_style(a[key]);
  29. valA = valA && valA[valA.length - 1] !== ';' ? valA + ';' : valA;
  30. var valB = pug_style(b[key]);
  31. valB = valB && valB[valB.length - 1] !== ';' ? valB + ';' : valB;
  32. a[key] = valA + valB;
  33. } else {
  34. a[key] = b[key];
  35. }
  36. }
  37. return a;
  38. }
  39. /**
  40. * Process array, object, or string as a string of classes delimited by a space.
  41. *
  42. * If `val` is an array, all members of it and its subarrays are counted as
  43. * classes. If `escaping` is an array, then whether or not the item in `val` is
  44. * escaped depends on the corresponding item in `escaping`. If `escaping` is
  45. * not an array, no escaping is done.
  46. *
  47. * If `val` is an object, all the keys whose value is truthy are counted as
  48. * classes. No escaping is done.
  49. *
  50. * If `val` is a string, it is counted as a class. No escaping is done.
  51. *
  52. * @param {(Array.<string>|Object.<string, boolean>|string)} val
  53. * @param {?Array.<string>} escaping
  54. * @return {String}
  55. */
  56. exports.classes = pug_classes;
  57. function pug_classes_array(val, escaping) {
  58. var classString = '',
  59. className,
  60. padding = '',
  61. escapeEnabled = Array.isArray(escaping);
  62. for (var i = 0; i < val.length; i++) {
  63. className = pug_classes(val[i]);
  64. if (!className) continue;
  65. escapeEnabled && escaping[i] && (className = pug_escape(className));
  66. classString = classString + padding + className;
  67. padding = ' ';
  68. }
  69. return classString;
  70. }
  71. function pug_classes_object(val) {
  72. var classString = '',
  73. padding = '';
  74. for (var key in val) {
  75. if (key && val[key] && pug_has_own_property.call(val, key)) {
  76. classString = classString + padding + key;
  77. padding = ' ';
  78. }
  79. }
  80. return classString;
  81. }
  82. function pug_classes(val, escaping) {
  83. if (Array.isArray(val)) {
  84. return pug_classes_array(val, escaping);
  85. } else if (val && typeof val === 'object') {
  86. return pug_classes_object(val);
  87. } else {
  88. return val || '';
  89. }
  90. }
  91. /**
  92. * Convert object or string to a string of CSS styles delimited by a semicolon.
  93. *
  94. * @param {(Object.<string, string>|string)} val
  95. * @return {String}
  96. */
  97. exports.style = pug_style;
  98. function pug_style(val) {
  99. if (!val) return '';
  100. if (typeof val === 'object') {
  101. var out = '';
  102. for (var style in val) {
  103. /* istanbul ignore else */
  104. if (pug_has_own_property.call(val, style)) {
  105. out = out + style + ':' + val[style] + ';';
  106. }
  107. }
  108. return out;
  109. } else {
  110. return val + '';
  111. }
  112. }
  113. /**
  114. * Render the given attribute.
  115. *
  116. * @param {String} key
  117. * @param {String} val
  118. * @param {Boolean} escaped
  119. * @param {Boolean} terse
  120. * @return {String}
  121. */
  122. exports.attr = pug_attr;
  123. function pug_attr(key, val, escaped, terse) {
  124. if (
  125. val === false ||
  126. val == null ||
  127. (!val && (key === 'class' || key === 'style'))
  128. ) {
  129. return '';
  130. }
  131. if (val === true) {
  132. return ' ' + (terse ? key : key + '="' + key + '"');
  133. }
  134. var type = typeof val;
  135. if (
  136. (type === 'object' || type === 'function') &&
  137. typeof val.toJSON === 'function'
  138. ) {
  139. val = val.toJSON();
  140. }
  141. if (typeof val !== 'string') {
  142. val = JSON.stringify(val);
  143. if (!escaped && val.indexOf('"') !== -1) {
  144. return ' ' + key + "='" + val.replace(/'/g, '&#39;') + "'";
  145. }
  146. }
  147. if (escaped) val = pug_escape(val);
  148. return ' ' + key + '="' + val + '"';
  149. }
  150. /**
  151. * Render the given attributes object.
  152. *
  153. * @param {Object} obj
  154. * @param {Object} terse whether to use HTML5 terse boolean attributes
  155. * @return {String}
  156. */
  157. exports.attrs = pug_attrs;
  158. function pug_attrs(obj, terse) {
  159. var attrs = '';
  160. for (var key in obj) {
  161. if (pug_has_own_property.call(obj, key)) {
  162. var val = obj[key];
  163. if ('class' === key) {
  164. val = pug_classes(val);
  165. attrs = pug_attr(key, val, false, terse) + attrs;
  166. continue;
  167. }
  168. if ('style' === key) {
  169. val = pug_style(val);
  170. }
  171. attrs += pug_attr(key, val, false, terse);
  172. }
  173. }
  174. return attrs;
  175. }
  176. /**
  177. * Escape the given string of `html`.
  178. *
  179. * @param {String} html
  180. * @return {String}
  181. * @api private
  182. */
  183. var pug_match_html = /["&<>]/;
  184. exports.escape = pug_escape;
  185. function pug_escape(_html) {
  186. var html = '' + _html;
  187. var regexResult = pug_match_html.exec(html);
  188. if (!regexResult) return _html;
  189. var result = '';
  190. var i, lastIndex, escape;
  191. for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
  192. switch (html.charCodeAt(i)) {
  193. case 34:
  194. escape = '&quot;';
  195. break;
  196. case 38:
  197. escape = '&amp;';
  198. break;
  199. case 60:
  200. escape = '&lt;';
  201. break;
  202. case 62:
  203. escape = '&gt;';
  204. break;
  205. default:
  206. continue;
  207. }
  208. if (lastIndex !== i) result += html.substring(lastIndex, i);
  209. lastIndex = i + 1;
  210. result += escape;
  211. }
  212. if (lastIndex !== i) return result + html.substring(lastIndex, i);
  213. else return result;
  214. }
  215. /**
  216. * Re-throw the given `err` in context to the
  217. * the pug in `filename` at the given `lineno`.
  218. *
  219. * @param {Error} err
  220. * @param {String} filename
  221. * @param {String} lineno
  222. * @param {String} str original source
  223. * @api private
  224. */
  225. exports.rethrow = pug_rethrow;
  226. function pug_rethrow(err, filename, lineno, str) {
  227. if (!(err instanceof Error)) throw err;
  228. if ((typeof window != 'undefined' || !filename) && !str) {
  229. err.message += ' on line ' + lineno;
  230. throw err;
  231. }
  232. var context, lines, start, end;
  233. try {
  234. str = str || require('fs').readFileSync(filename, {encoding: 'utf8'});
  235. context = 3;
  236. lines = str.split('\n');
  237. start = Math.max(lineno - context, 0);
  238. end = Math.min(lines.length, lineno + context);
  239. } catch (ex) {
  240. err.message +=
  241. ' - could not read from ' + filename + ' (' + ex.message + ')';
  242. pug_rethrow(err, null, lineno);
  243. return;
  244. }
  245. // Error context
  246. context = lines
  247. .slice(start, end)
  248. .map(function(line, i) {
  249. var curr = i + start + 1;
  250. return (curr == lineno ? ' > ' : ' ') + curr + '| ' + line;
  251. })
  252. .join('\n');
  253. // Alter exception message
  254. err.path = filename;
  255. try {
  256. err.message =
  257. (filename || 'Pug') +
  258. ':' +
  259. lineno +
  260. '\n' +
  261. context +
  262. '\n\n' +
  263. err.message;
  264. } catch (e) {}
  265. throw err;
  266. }