forked from indexeddbshim/IndexedDBShim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrap-w3c-tests.js
157 lines (143 loc) · 5.51 KB
/
wrap-w3c-tests.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
const fs = require('fs');
const path = require('path');
const indexedDBDir = path.join(__dirname, '../web-platform-tests/IndexedDB/');
const loaderFile = '_indexeddbshim-loader.html';
const shimLoaderPath = path.join(indexedDBDir, loaderFile);
if (process.argv[2] === 'remove') {
fs.unlinkSync(shimLoaderPath);
fs.readdir(indexedDBDir, (err, files) => {
if (err) {
console.log('err', err);
return;
}
const anyJSFiles = files.filter((f) => (/\.html?\.any\.js$/).test(f));
anyJSFiles.forEach((anyJS) => {
const anyJSPath = path.join(indexedDBDir, anyJS);
fs.unlinkSync(anyJSPath);
});
});
} else if (process.argv[2] === 'add') {
fs.writeFileSync(shimLoaderPath,
`<!DOCTYPE html>
<meta charset="utf-8" />
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
<script src="http://localhost:9999/node_modules/babel-polyfill/dist/polyfill.js"></script>
<script src="http://localhost:9999/dist/indexeddbshim-noninvasive.min.js"></script>
`);
fs.readdir(indexedDBDir, (err, files) => {
if (err) {
console.log('err', err);
return;
}
const htmlFiles = files.filter((f) => (/\.html?$/).test(f) && !(/indexeddbshim-loader\.html$/).test(f));
htmlFiles.forEach((htmlFile) => {
const anyJS = /* '_' + */ htmlFile + '.any.js';
const anyJSPath = path.join(indexedDBDir, anyJS);
if (fs.existsSync(anyJSPath)) {
console.log('file already exists, overwriting ' + anyJS);
// return;
}
const content = `
function createIframe (src, w) {
// Ensure page can allow iframe height to be set to fill page
w.document.documentElement.style.height = '98.5%';
w.document.body.style.height = '98.5%';
var ifr = w.document.createElement('iframe');
ifr.src = src;
ifr.style.width = '100%';
ifr.style.height = '100%';
ifr.style.border = 'none';
w.document.body.appendChild(ifr);
return ifr;
}
var ifr = createIframe('${loaderFile}', window);
// Avoid extra scrollbars: http://stackoverflow.com/a/15494969/271577
ifr.style.overflow = 'hidden';
ifr.setAttribute('scrolling', 'no');
var loaderWin = ifr.contentWindow;
loaderWin.addEventListener('DOMContentLoaded', function () {
var ifr = createIframe('${htmlFile.replace(/'/g, "\\'")}', loaderWin);
var testWin = ifr.contentWindow;
var style = document.createElement('style');
style.textContent = '#log {display: none;}'; // Avoid extra display of log; another way?
document.head.appendChild(style);
['Date', 'Array'].forEach(function (prop) { // For instanceof checks expecting those in test to match those in IndexedDBShim
testWin[prop] = loaderWin[prop];
});
// Override to better ensure transaction has expired
const _setTimeout = testWin.setTimeout;
testWin.setTimeout = function (cb, ms) {
return _setTimeout(cb, ms + 500);
};
loaderWin.setGlobalVars(testWin, {
fullIDLSupport: true,
replaceNonIDBGlobals: true,
useSQLiteIndexes: true
});
testWin.shimIndexedDB.__useShim();
/*
// We can adapt this to apply source maps once we may get sourcemap-transformer working in the browser
testWin.addEventListener('DOMContentLoaded', function () {
testWin.add_completion_callback(function () {
var pres = Array.from(testWin.document.querySelectorAll('pre'));
pres.forEach(function (pre) {
loaderWin.console.log(pre.textContent);
});
});
});
*/
});
`;
fs.writeFileSync(anyJSPath, content);
});
});
} else {
fs.readdir(indexedDBDir, (err, files) => {
if (err) {
console.log('err', err);
return;
}
const htmlFiles = files.filter((f) => (/\.html?$/).test(f));
const polyfillScript = `
<script src="http://localhost:9999/node_modules/babel-polyfill/dist/polyfill.js"></script>
<script src="http://localhost:9999/dist/indexeddbshim-noninvasive.js"></script>
<script>
'use strict';
// Override to better ensure transaction has expired
const _setTimeout = setTimeout;
setTimeout = function () {
arguments[1] = (arguments[1] || 0) + 500;
return _setTimeout.apply(window, arguments);
};
setGlobalVars(null, {
fullIDLSupport: true,
replaceNonIDBGlobals: true,
useSQLiteIndexes: true
});
shimIndexedDB.__useShim();
</script>
`;
/*
// We can adapt this to apply source maps once we may get sourcemap-transformer working in the browser
window.addEventListener('DOMContentLoaded', function () {
add_completion_callback(function () {
var pres = Array.from(document.querySelectorAll('pre'));
pres.forEach(function (pre) {
loaderWin.console.log(pre.textContent);
});
});
});
*/
htmlFiles.forEach((htmlFile) => {
// Could add these too to avoid warnings
// <meta charset="utf-8" />
// <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
const htmlPath = path.join(indexedDBDir, htmlFile);
fs.writeFileSync(
htmlPath,
String(fs.readFileSync(htmlPath))
.replace(polyfillScript, '') // Replace any preexisting polyfill tag
.replace(/<script/, polyfillScript + '$&'));
});
});
}