forked from googlearchive/code-prettify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_base.js
More file actions
246 lines (222 loc) · 7.83 KB
/
Copy pathtest_base.js
File metadata and controls
246 lines (222 loc) · 7.83 KB
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
// get accurate timing.
// This file must be loaded after prettify.js for this to work.
PR_SHOULD_USE_CONTINUATION = false;
var attribToHtml, textToHtml;
var getInnerHtml;
(function () {
/** is the given node's innerHTML normally unescaped? */
function isRawContent(node) {
return 'XMP' === node.tagName;
}
var newlineRe = /[\r\n]/g;
/**
* Are newlines and adjacent spaces significant in the given node's innerHTML?
*/
function isPreformatted(node, content) {
// PRE means preformatted, and is a very common case, so don't create
// unnecessary computed style objects.
if ('PRE' === node.tagName) { return true; }
if (!newlineRe.test(content)) { return true; } // Don't care
var whitespace = '';
// For disconnected nodes, IE has no currentStyle.
if (node.currentStyle) {
whitespace = node.currentStyle.whiteSpace;
} else if (window.getComputedStyle) {
// Firefox makes a best guess if node is disconnected whereas Safari
// returns the empty string.
whitespace = window.getComputedStyle(node, null).whiteSpace;
}
return !whitespace || whitespace === 'pre';
}
// Define regexps here so that the interpreter doesn't have to create an
// object each time the function containing them is called.
// The language spec requires a new object created even if you don't access
// the $1 members.
var pr_amp = /&/g;
var pr_lt = /</g;
var pr_gt = />/g;
var pr_quot = /\"/g;
/** escapest html special characters to html. */
textToHtml = function (str) {
return str.replace(pr_amp, '&')
.replace(pr_lt, '<')
.replace(pr_gt, '>');
};
/** like textToHtml but escapes double quotes to be attribute safe. */
attribToHtml = function (str) {
return str.replace(pr_amp, '&')
.replace(pr_lt, '<')
.replace(pr_gt, '>')
.replace(pr_quot, '"');
};
var PR_innerHtmlWorks = null;
getInnerHtml = function (node) {
// inner html is hopelessly broken in Safari 2.0.4 when the content is
// an html description of well formed XML and the containing tag is a PRE
// tag, so we detect that case and emulate innerHTML.
if (null === PR_innerHtmlWorks) {
var testNode = document.createElement('PRE');
testNode.appendChild(
document.createTextNode('<!DOCTYPE foo PUBLIC "foo bar">\n<foo />'));
PR_innerHtmlWorks = !/</.test(testNode.innerHTML);
}
if (PR_innerHtmlWorks) {
var content = node.innerHTML;
// XMP tags contain unescaped entities so require special handling.
if (isRawContent(node)) {
content = textToHtml(content);
} else if (!isPreformatted(node, content)) {
content = content.replace(/(<br\s*\/?>)[\r\n]+/g, '$1')
.replace(/(?:[\r\n]+[ \t]*)+/g, ' ');
}
return content;
}
var out = [];
for (var child = node.firstChild; child; child = child.nextSibling) {
normalizedHtml(child, out);
}
return out.join('');
};
})();
function normalizedHtml(node, out, opt_sortAttrs) {
switch (node.nodeType) {
case 1: // an element
var name = node.tagName.toLowerCase();
out.push('<', name);
var attrs = node.attributes;
var n = attrs.length;
if (n) {
if (opt_sortAttrs) {
var sortedAttrs = [];
for (var i = n; --i >= 0;) { sortedAttrs[i] = attrs[i]; }
sortedAttrs.sort(function (a, b) {
return (a.name < b.name) ? -1 : a.name === b.name ? 0 : 1;
});
attrs = sortedAttrs;
}
for (var i = 0; i < n; ++i) {
var attr = attrs[i];
if (!attr.specified) { continue; }
out.push(' ', attr.name.toLowerCase(),
'="', attribToHtml(attr.value), '"');
}
}
out.push('>');
for (var child = node.firstChild; child; child = child.nextSibling) {
normalizedHtml(child, out, opt_sortAttrs);
}
if (node.firstChild || !/^(?:br|link|img)$/.test(name)) {
out.push('<\/', name, '>');
}
break;
case 3: case 4: // text
out.push(textToHtml(node.nodeValue));
break;
}
}
/**
* @param golden a mapping from IDs of prettyprinted chunks to an abbreviated
* form of the expected output. See "var goldens" in prettify_test.html
* for an example.
*/
function go(goldens) {
startClock();
prettyPrint(function () { stopClock(); runTests(goldens); });
}
function runTests(goldens) {
/** number of characters in common at the end up to max. */
function commonPrefix(a, b) {
var n = Math.min(a.length, b.length);
var i;
for (i = 0; i < n; ++i) {
if (a.charAt(i) !== b.charAt(i)) { break; }
}
return i;
}
/** number of characters in common at the end up to max. */
function commonSuffix(a, b, max) {
var n = Math.min(a.length - max, b.length - max);
var i;
for (i = 0; i < n; ++i) {
if (a.charAt(a.length - i - 1) !== b.charAt(b.length - i - 1)) { break; }
}
return i;
}
/** convert a plain text string to html by escaping html special chars. */
function html(plainText) {
return attribToHtml(plainText).replace(/\xa0/g, ' ');
}
/**
* get normalized markup. innerHTML varies enough across browsers that we
* can't use it.
*/
function normalizedInnerHtml(node) {
var out = [];
for (var child = node.firstChild; child; child = child.nextSibling) {
normalizedHtml(child, out, true);
}
out = out.join('');
// more normalization to work around problems with non-ascii chars in
// regexps in Safari
for (var i = 0; (i = out.indexOf('\xa0')) >= 0;) {
out = out.substring(0, i) + ' ' + out.substring(i + 1);
}
return out.replace(/\r\n?/g, '\n');
}
var htmlOut = [];
var failures = 0;
document.getElementById('errorReport').innerHTML =
'<h1>Running tests…<\/h1>';
htmlOut.push('<h1>Test results<\/h1>');
for (var lang in goldens) {
var container = document.getElementById(lang);
// Convert abbreviations that start with `.
var golden = goldens[lang].replace(/`([A-Z]{3})/g, function (_, lbl) {
return (lbl == 'END'
? '<\/span>'
: '<span class="' + lbl.toLowerCase() + '">');
})
// Line numbers
.replace(/`#(?![0-9])/, '<li class="L0">')
.replace(/`#([0-9])/g, '</li><li class="L$1">');
var actual = normalizedInnerHtml(container);
if (golden !== actual) { // test failed
// write out
var pre = commonPrefix(golden, actual);
var post = commonSuffix(golden, actual, pre);
++failures;
htmlOut.push(
'<h2><a href="#' + html(lang) + '">'
+ html(lang) + '<\/a> Failed<\/h2>');
htmlOut.push(
'<tt>' + html(golden.substring(0, pre)) +
'»<span class="mismatch">' +
html(golden.substring(pre, golden.length - post)) +
'<\/span>«' +
html(golden.substring(golden.length - post)) +
'<br>!==<br>' +
html(actual.substring(0, pre)) +
'»<span class="mismatch">' +
html(actual.substring(pre, actual.length - post)) +
'<\/span>«' +
html(actual.substring(actual.length - post)) + '<\/tt>');
} else {
htmlOut.push(
'<h2><a href="#' + html(lang) + '">' + html(lang) + '<\/a> OK<\/h2>');
}
}
var summary = (failures ? (failures + ' test(s) failed') : 'Tests Passed');
htmlOut.push('<h2>' + summary + '<\/h2>');
document.title += ' \u2014 ' + summary;
document.getElementById('errorReport').innerHTML =
htmlOut.join('').replace(/<br>/g, '<br>\n');
}
var startTime = null;
function startClock() {
startTime = (new Date).getTime();
}
function stopClock() {
var delta = (new Date).getTime() - startTime;
startTime = null;
document.getElementById('timing').innerHTML = 'Took ' + delta + ' ms';
}