index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = '', className, padding = '', escapeEnabled = Array.isArray(escaping);
  59. for (var i = 0; i < val.length; i++) {
  60. className = pug_classes(val[i]);
  61. if (!className) continue;
  62. escapeEnabled && escaping[i] && (className = pug_escape(className));
  63. classString = classString + padding + className;
  64. padding = ' ';
  65. }
  66. return classString;
  67. }
  68. function pug_classes_object(val) {
  69. var classString = '', padding = '';
  70. for (var key in val) {
  71. if (key && val[key] && pug_has_own_property.call(val, key)) {
  72. classString = classString + padding + key;
  73. padding = ' ';
  74. }
  75. }
  76. return classString;
  77. }
  78. function pug_classes(val, escaping) {
  79. if (Array.isArray(val)) {
  80. return pug_classes_array(val, escaping);
  81. } else if (val && typeof val === 'object') {
  82. return pug_classes_object(val);
  83. } else {
  84. return val || '';
  85. }
  86. }
  87. /**
  88. * Convert object or string to a string of CSS styles delimited by a semicolon.
  89. *
  90. * @param {(Object.<string, string>|string)} val
  91. * @return {String}
  92. */
  93. exports.style = pug_style;
  94. function pug_style(val) {
  95. if (!val) return '';
  96. if (typeof val === 'object') {
  97. var out = '';
  98. for (var style in val) {
  99. /* istanbul ignore else */
  100. if (pug_has_own_property.call(val, style)) {
  101. out = out + style + ':' + val[style] + ';';
  102. }
  103. }
  104. return out;
  105. } else {
  106. return val + '';
  107. }
  108. };
  109. /**
  110. * Render the given attribute.
  111. *
  112. * @param {String} key
  113. * @param {String} val
  114. * @param {Boolean} escaped
  115. * @param {Boolean} terse
  116. * @return {String}
  117. */
  118. exports.attr = pug_attr;
  119. function pug_attr(key, val, escaped, terse) {
  120. if (val === false || val == null || !val && (key === 'class' || key === 'style')) {
  121. return '';
  122. }
  123. if (val === true) {
  124. return ' ' + (terse ? key : key + '="' + key + '"');
  125. }
  126. if (typeof val.toJSON === 'function') {
  127. val = val.toJSON();
  128. }
  129. if (typeof val !== 'string') {
  130. val = JSON.stringify(val);
  131. if (!escaped && val.indexOf('"') !== -1) {
  132. return ' ' + key + '=\'' + val.replace(/'/g, '&#39;') + '\'';
  133. }
  134. }
  135. if (escaped) val = pug_escape(val);
  136. return ' ' + key + '="' + val + '"';
  137. };
  138. /**
  139. * Render the given attributes object.
  140. *
  141. * @param {Object} obj
  142. * @param {Object} terse whether to use HTML5 terse boolean attributes
  143. * @return {String}
  144. */
  145. exports.attrs = pug_attrs;
  146. function pug_attrs(obj, terse){
  147. var attrs = '';
  148. for (var key in obj) {
  149. if (pug_has_own_property.call(obj, key)) {
  150. var val = obj[key];
  151. if ('class' === key) {
  152. val = pug_classes(val);
  153. attrs = pug_attr(key, val, false, terse) + attrs;
  154. continue;
  155. }
  156. if ('style' === key) {
  157. val = pug_style(val);
  158. }
  159. attrs += pug_attr(key, val, false, terse);
  160. }
  161. }
  162. return attrs;
  163. };
  164. /**
  165. * Escape the given string of `html`.
  166. *
  167. * @param {String} html
  168. * @return {String}
  169. * @api private
  170. */
  171. var pug_match_html = /["&<>]/;
  172. exports.escape = pug_escape;
  173. function pug_escape(_html){
  174. var html = '' + _html;
  175. var regexResult = pug_match_html.exec(html);
  176. if (!regexResult) return _html;
  177. var result = '';
  178. var i, lastIndex, escape;
  179. for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
  180. switch (html.charCodeAt(i)) {
  181. case 34: escape = '&quot;'; break;
  182. case 38: escape = '&amp;'; break;
  183. case 60: escape = '&lt;'; break;
  184. case 62: escape = '&gt;'; break;
  185. default: continue;
  186. }
  187. if (lastIndex !== i) result += html.substring(lastIndex, i);
  188. lastIndex = i + 1;
  189. result += escape;
  190. }
  191. if (lastIndex !== i) return result + html.substring(lastIndex, i);
  192. else return result;
  193. };
  194. /**
  195. * Re-throw the given `err` in context to the
  196. * the pug in `filename` at the given `lineno`.
  197. *
  198. * @param {Error} err
  199. * @param {String} filename
  200. * @param {String} lineno
  201. * @param {String} str original source
  202. * @api private
  203. */
  204. exports.rethrow = pug_rethrow;
  205. function pug_rethrow(err, filename, lineno, str){
  206. if (!(err instanceof Error)) throw err;
  207. if ((typeof window != 'undefined' || !filename) && !str) {
  208. err.message += ' on line ' + lineno;
  209. throw err;
  210. }
  211. try {
  212. str = str || require('fs').readFileSync(filename, 'utf8')
  213. } catch (ex) {
  214. pug_rethrow(err, null, lineno)
  215. }
  216. var context = 3
  217. , lines = str.split('\n')
  218. , start = Math.max(lineno - context, 0)
  219. , end = Math.min(lines.length, lineno + context);
  220. // Error context
  221. var context = lines.slice(start, end).map(function(line, i){
  222. var curr = i + start + 1;
  223. return (curr == lineno ? ' > ' : ' ')
  224. + curr
  225. + '| '
  226. + line;
  227. }).join('\n');
  228. // Alter exception message
  229. err.path = filename;
  230. err.message = (filename || 'Pug') + ':' + lineno
  231. + '\n' + context + '\n\n' + err.message;
  232. throw err;
  233. };