forked from compat-table/compat-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
236 lines (209 loc) · 6.63 KB
/
build.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
// Copyright 2012 Dan Wolff | danwolff.se
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var fs = require('fs');
// let prototypes declared below in this file be initialized
process.nextTick(function () {
handle(require('./data-es5'));
handle(require('./data-es6'));
handle(require('./data-non-standard'));
});
function handle(options) {
var skeleton = fs.readFileSync(__dirname + '/' + options.skeleton_file, 'utf-8');
var html = dataToHtml(options.browsers, options.tests);
var result = replaceAndIndent(skeleton, [
["<!-- TABLE HEADERS -->", html.tableHeaders],
["<!-- TABLE BODY -->", html.tableBody],
["<!-- FOOTNOTES -->", html.footnotes]
]).replace(/\t/g, ' ');
var target_file = __dirname + '/' + options.target_file;
var old_result;
try {
old_result = fs.readFileSync(target_file, 'utf-8');
} catch (e) {}
if (old_result === result) {
console.log('[' + options.name + '] ' + options.target_file + ' not changed');
} else {
fs.writeFileSync(target_file, result);
console.log('[' + options.name + '] Write to file ' + options.target_file);
}
}
function dataToHtml(browsers, tests) {
var i, j, browserId, footnote;
var footnoter = new Footnoter();
// headers
var b,
headers = [];
for (browserId in browsers) {
b = browsers[browserId];
if (!b) {
throw new Error('No browser with ID ' + browserId);
}
headers.push(
'<th class="' + browserTableClass(browserId, b) + '">' +
('<a href="#' + browserId + '" class="browser-name">') +
(b.short ? '<abbr title="' + b.full + '">' + b.short + '</abbr>' : b.full) +
(b.link ? '</a>' : '') +
footnoter.get(b) +
'</th>'
);
}
// body rows
var val,
body = [],
name, id;
for (i = 0; i < tests.length; i++) {
t = tests[i];
id = t.name.replace(/^[\s<>&"]+|[\s<>&"]+$/g, '').replace(/[\s<>&"]+/g, '_');
name = t.link ? ('<a href="' + t.link + '">' + t.name + '</a>') : t.name;
body.push(
'<tr>',
'\t<td id="' + id + '"><span><a class="anchor" href="#' + id + '">§</a>' + name + footnoter.get(t) + '</span></td>\n' +
testScript(t.exec)
);
// each browser for this test
for (browserId in browsers) {
val = t.res[browserId];
if (val == null) {
body.push('\t<td class="' + browserTableClass(browserId, browsers[browserId]) + '"></td>');
} else {
body.push(
'\t<td class="' + boolToString(val).toLowerCase() + ' ' + browserTableClass(browserId, browsers[browserId]) + '">' +
boolToString(val) +
footnoter.get(val) +
'</td>'
);
}
}
body.push('</tr>');
if (t.separator === 'after') {
body.push(
'<tr>',
'\t<th colspan="' + (Object.keys(browsers).length + 3) + '" class="separator"></th>',
'</tr>'
);
}
}
return {
tableHeaders: headers,
tableBody: body,
footnotes: footnoter.getAll()
};
}
function browserTableClass(browserId, entry) {
return browserId + (entry.obsolete ? ' obsolete' : '');
}
function boolToString(val) {
if (typeof val === 'object') {
val = val.val;
}
return val ? 'Yes' : 'No';
}
// Footnoter
function Footnoter() {
this.indexById = Object.create(null);
this.notes = [];
}
Footnoter.prototype.get = function (val) {
var id;
if (typeof val === 'object' && val.note_id) {
id = val.note_id;
// save if it's new
if (this.indexById[id] == null) {
this.indexById[id] = this.notes.length;
this.notes.push(val.note_html);
}
// save html if such exists (may replace existing)
else if (val.note_html) {
this.notes[ this.indexById[id] ] = val.note_html;
}
// return the index + 1
return '<a href="#' + id + '-note"><sup>[' +
(this.indexById[id] + 1) +
']</sup></a>';
}
return '';
};
Footnoter.prototype.getAll = function() {
var id, index,
html = [];
for (id in this.indexById) {
index = this.indexById[id];
html.push(
'<p id="' + id + '-note">',
'\t<sup>[' + (index + 1) + ']</sup> ' + this.notes[index],
'</p>'
);
}
return html;
};
function replaceAndIndent(str, replacements) {
var i, replacement, indent;
for (i = 0; i < replacements.length; i++) {
replacement = replacements[i];
indent = new RegExp('(\n[ \t]*)' + replacement[0]).exec(str);
if (!indent) {
throw new Error('Could not find indent for ' + replacement[0]);
}
str = str
.split(replacement[0])
.join(replacement[1].join(indent[1]));
}
return str;
}
function deindentFunc(fn) {
fn += '';
var indent = /\n([\t ]*)[^\n]*$/.exec(fn);
if (indent) {
fn = fn.replace(new RegExp('\n' + indent[1], 'g'), '\n');
}
return fn;
}
function testScript(fn) {
if (typeof fn === 'function') {
fn = deindentFunc(fn);
// find the expression if it's there
var expr = /^function \(\) \{\s*return\s+([\s\S]+?);?\s*\}$/.exec(fn);
expr = expr && expr[1];
// if there wasn't an expression, make the function statement into one
if (!expr) {
expr = fn + '()';
}
return '<script>\n' +
'test(' + expr + ');\n' +
'</script>\n';
} else {
// it's an array of objects like the following:
// { type: 'application/javascript;version=1.8', script: function () { ... } }
var i, script,
scripts = [];
for (i = 0; i < fn.length; i++) {
script = fn[i];
scripts.push(
'<script' + (script.type ? ' type="' + script.type + '"' : '') + '>',
deindentFunc(
(script.script+'').replace(/^function \(\) \{\s*|\s*\}$/g, '')
),
'</script>'
);
}
return scripts.join('\n') + '\n';
}
}