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