-
Notifications
You must be signed in to change notification settings - Fork 4
/
useHTMLLogger.js
292 lines (258 loc) · 9.04 KB
/
useHTMLLogger.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Exports a {@code ses.logger} which logs to elements
* on an HTML page.
*
* <p>To use the file, before loading <code>logger.js</code> or
* <code>initSES*.js</code> which includes <code>logger.js</code>, you
* must load and initialize this file. <code>logger.js</code> will
* then detect that there is already a logger in place and not
* overwrite it. For example, the beginning of your html file might
* read
* <pre> <div id="reports"></div>
* <div id="console"></div>
* <script src="useHTMLLogger.js"></script>
* <script>
* function gebi(id) { return document.getElementById(id); };
* useHTMLLogger(gebi("reports"), gebi("console"));
* </script>
* <script src="initSES.js"></script>
* </pre>
*
* <p>Assumes only ES3. Compatible with ES5, ES5-strict, or
* anticipated ES6.
*
* @author Mark S. Miller
* @requires document
* @overrides ses, window
* @provides useHTMLLogger
*/
var ses;
if (!ses) { ses = {}; }
function useHTMLLogger(reportsElement, consoleElement) {
"use strict";
var slice = [].slice;
var maxElement = void 0;
/**
* Needs to work on ES3
*/
function forEach(list, callback) {
for (var i = 0, len = list.length; i < len; i++) {
callback(list[i], i);
}
}
function appendNew(parent, tagName) {
var result = document.createElement(tagName);
parent.appendChild(result);
return result;
}
function prependNew(parent, tagName) {
var result = document.createElement(tagName);
parent.insertBefore(result, parent.firstChild);
return result;
}
function appendText(parent, text) {
var result = document.createTextNode(text);
parent.appendChild(result);
return result;
}
function textAdder(parent, style) {
return function(text) {
var p = appendNew(parent, 'p');
appendText(p, text);
p.className = style;
return p;
};
}
var INFLATE = '[+]';
var DEFLATE = '[-]';
function deflate(toggler, inflatables, opt_sep) {
var sep = opt_sep !== void 0 ? opt_sep : ' ';
var toggle = prependNew(toggler, 'tt');
var icon = appendText(toggle, INFLATE);
appendText(toggle, sep);
forEach(inflatables, function(inflatable) {
inflatable.style.display = 'none';
});
toggler.addEventListener('click', function(event) {
if (icon.data === INFLATE) {
forEach(inflatables, function(inflatable) {
inflatable.style.removeProperty('display');
});
icon.data = DEFLATE;
} else {
forEach(inflatables, function(inflatable) {
inflatable.style.display = 'none';
});
icon.data = INFLATE;
}
}, false);
toggler.style.cursor = 'pointer';
}
/** modeled on textAdder */
function makeLogFunc(parent, style) {
return function logFunc(var_args) {
var p = appendNew(parent, 'p');
var args = slice.call(arguments, 0);
// See debug.js
var getStack = ses.getStack;
for (var i = 0, len = args.length; i < len; i++) {
var span = appendNew(p, 'span');
appendText(span, '' + args[i]);
if (getStack) {
var stack = getStack(args[i]);
if (stack) {
var stackNode = appendNew(p, 'pre');
appendText(stackNode, stack);
deflate(span, [stackNode], '');
}
}
}
p.className = style;
return p;
};
}
var logger = {
log: makeLogFunc(consoleElement, 'log'),
info: makeLogFunc(consoleElement, 'info'),
warn: makeLogFunc(consoleElement, 'warn'),
error: makeLogFunc(consoleElement, 'error')
};
var TestIDPattern = /^(Sbp|S)?([\d\.]*)/;
/**
*
*/
function linkToTest(test) {
var match = TestIDPattern.exec(test);
if (match) {
var parts = match[2].split('.');
var result = 'http://hg.ecmascript.org/tests/test262/file/' +
'c84161250e66/' + // TODO(erights): How do I get the tip automatically?
'test/suite/';
if (match[1] === void 0) {
result += 'chapter';
} else if (match[1] === 'S') {
result += 'ch';
} else if (match[1] === 'Sbp') {
result += 'bestPractice';
}
var len = parts.length;
if (len === 0) {
result += '/';
} else {
result += (parts[0].length === 1 ? '0' : '') + parts[0] + '/';
for (var i = 1; i < len; i++) {
result += parts.slice(0, i+1).join('.') + '/';
}
}
result += test + '.js';
return result;
}
var site = test.charAt(0) === 'S' ?
'+site%3Acode.google.com' : '+site%3Aes5conform.svn.codeplex.com';
return 'http://www.google.com/search?btnI=&q=' +
encodeURIComponent(test) + site;
}
/**
* Logs a report suitable for display on a web page.
*/
logger.reportRepairs = function reportRepairs(reports) {
var numFineElement = appendNew(reportsElement, 'p');
var ul = appendNew(reportsElement, 'ul');
var fineElements = [];
forEach(reports, function(report, i) {
var li = appendNew(ul, 'li');
if (report.status === ses.statuses.ALL_FINE) {
fineElements.push(li);
li.style.listStyleType = 'none';
}
var reportElement = appendNew(li, 'p');
var classification = ses.logger.classify(report.postSeverity);
reportElement.className = classification.consoleLevel;
appendText(reportElement, i + ') ' + report.status + ': ' +
report.description + '. ' + classification.note);
if (typeof report.beforeFailure === 'string') {
var beforeElement = appendNew(reportElement, 'p');
appendText(beforeElement, 'New pre symptom: ' + report.beforeFailure);
}
if (typeof report.afterFailure === 'string') {
var afterElement = appendNew(reportElement, 'p');
appendText(afterElement, 'New post symptom: ' + report.afterFailure);
}
var linksBlock = appendNew(li, 'blockquote');
deflate(reportElement, [linksBlock]);
// TODO(erights): sort by URL relevance based on platform
forEach(report.urls, function(url, i) {
var linkElement = appendNew(linksBlock, 'p');
if (i === 0) { appendText(linkElement, 'See '); }
var link = appendNew(linkElement, 'a');
link.href = url;
link.target = '_blank';
appendText(link, url);
// TODO(erights): spawn a task to fetch the title of the bug
// and use it to replace the link text.
});
forEach(report.sections, function(section, i) {
var linkElement = appendNew(linksBlock, 'p');
if (i === 0) { appendText(linkElement, 'See '); }
var link = appendNew(linkElement, 'a');
link.href = 'http://es5.github.com/#x' + encodeURIComponent(section);
link.target = '_blank';
appendText(link, 'Section ' + section);
});
forEach(report.tests, function(test, i) {
var linkElement = appendNew(linksBlock, 'p');
if (i === 0) { appendText(linkElement, 'See '); }
var link = appendNew(linkElement, 'a');
link.href = linkToTest(test);
link.target = '_blank';
appendText(link, 'Test ' + test);
});
});
if (fineElements.length >= 1) {
appendText(numFineElement, fineElements.length + ' Fine.');
deflate(numFineElement, fineElements);
}
};
logger.reportMax = function reportMax() {
if (!maxElement) {
maxElement = appendNew(reportsElement, 'p');
} else {
maxElement.textContent = '';
}
if (ses.maxSeverity.level > ses.severities.SAFE.level) {
var maxClassification = ses.logger.classify(ses.maxSeverity);
maxElement.className = maxClassification.consoleLevel;
appendText(maxElement, 'Max Severity: ' + maxClassification.note);
}
};
logger.reportDiagnosis = function reportDiagnosis(severity,
status,
problemList) {
var diagnosisElement = appendNew(reportsElement, 'p');
var classification = ses.logger.classify(severity);
var head = textAdder(diagnosisElement, classification.consoleLevel)(
problemList.length + ' ' + status + '. ' + classification.note);
var tail = appendNew(diagnosisElement, 'blockquote');
textAdder(tail, classification.consoleLevel)(
problemList.sort().join(' '));
deflate(head, [tail]);
};
ses.logger = logger;
}
// Exports for closure compiler.
if (typeof window !== 'undefined') {
window['useHTMLLogger'] = useHTMLLogger;
}