-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
buildDemo.js
192 lines (169 loc) · 3.59 KB
/
buildDemo.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
/* eslint-disable import/unambiguous */
const {
getUserAgentRegExps,
getUserAgentRegExp
} = require('../lib');
function renderStyles() {
return `<style>
body {
font-family:
/* Safari for OS X and iOS (San Francisco) */
-apple-system,
/* Chrome < 56 for OS X (San Francisco) */
BlinkMacSystemFont,
/* Windows */
'Segoe UI',
/* Android */
Roboto,
/* Basic web fallback */
'Helvetica Neue',
Arial,
sans-serif,
/* Emoji fonts */
'Apple Color Emoji',
'Segoe UI Emoji',
'Segoe UI Symbol';
}
pre {
font-family:
SFMono-Regular,
Menlo,
Monaco,
Consolas,
'Liberation Mono',
'Courier New',
monospace;
}
h2 > pre {
display: inline;
}
h2 > input {
vertical-align: middle;
}
table {
width: 100%;
}
th, td {
text-align: left;
}
</style>`;
}
function renderScript() {
const modernBrowsers = getUserAgentRegExp({
browsers: 'last 2 versions and last 1 year',
allowHigherVersions: true,
allowZeroSubverions: true
});
const actualBrowsers = getUserAgentRegExp({
browsers: 'last 2 years and not last 2 versions',
allowHigherVersions: true,
allowZeroSubverions: true
});
return `<script>
var modernBrowsers = ${modernBrowsers};
var actualBrowsers = ${actualBrowsers};
var script = document.createElement('script');
script.type = 'text/javascript';
script.defer = true;
switch (true) {
case modernBrowsers.test(navigator.userAgent):
script.src = 'demojs/index.modern.js';
break;
case actualBrowsers.test(navigator.userAgent):
script.src = 'demojs/index.actual.js';
break;
default:
script.src = 'demojs/index.old.js';
}
document.getElementsByTagName('head')[0].appendChild(script);
</script>`;
}
function renderHtml(body) {
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, minimal-ui">
<meta name="format-detection" content="telephone=no">
<title>DEMO</title>
${renderScript()}
${renderStyles()}
</head>
<body>
${body}
</body>
</html>`;
}
function renderUserAgentRegExp({
family,
sourceRegExpString,
regExpString,
resultVersion,
requestVersionsStrings
}, query) {
return `<li>
<input type="checkbox" onclick="return false" readonly data-for-query="${query}" data-regexp="${regExpString.replace(/([^\\])"/g, '$1\\"')}">
<table>
<tr>
<th>Family:</th><td>${family}</td>
</tr>
<tr>
<th>Versions:</th><td>${requestVersionsStrings.join(' ')}</td>
</tr>
<tr>
<th>Source RegExp:</th><td><pre>${sourceRegExpString}</pre></td>
</tr>
${!resultVersion ? '' : `
<tr>
<th>Source RegExp version:</th><td>${resultVersion.join('.')}</td>
</tr>
`}
${resultVersion ? '' : `
<tr>
<th>Versioned RegExp:</th><td><pre>${regExpString}</pre></td>
</tr>
`}
</table>
</li>`;
}
function renderQuery(query) {
const result = getUserAgentRegExps({
browsers: query,
allowHigherVersions: true,
allowZeroSubverions: true
});
return `<div>
<h2>
<input type="checkbox" onclick="return false" readonly data-query="${query}">
<pre>${query}</pre>
</h2>
<ul>
${result.map(_ => renderUserAgentRegExp(_, query)).join('\n')}
</ul>
</div>`;
}
function render(queries) {
return renderHtml(`<h1>browserslist-useragent-regexp demo</h1>
<table>
<tr>
<th>UserAgent:</th>
<td id="useragent"></td>
<tr>
<th>Options:</th>
<td>
<pre>{
allowHigherVersions: true,
allowZeroSubverions: true
}</pre>
</td>
</tr>
</table>
<div>
${queries.map(_ => renderQuery(_)).join('\n')}
</div>`);
}
console.log(render([
'defaults',
'dead'
]));