single-file-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +43 -0
- package/LICENSE +661 -0
- package/index.js +86 -0
- package/modules/css-fonts-alt-minifier.js +340 -0
- package/modules/css-fonts-minifier.js +371 -0
- package/modules/css-matched-rules.js +350 -0
- package/modules/css-medias-alt-minifier.js +91 -0
- package/modules/css-rules-minifier.js +146 -0
- package/modules/html-images-alt-minifier.js +109 -0
- package/modules/html-minifier.js +262 -0
- package/modules/html-serializer.js +180 -0
- package/modules/index.js +42 -0
- package/package.json +18 -0
- package/processors/frame-tree/content/content-frame-tree.js +418 -0
- package/processors/hooks/content/content-hooks-frames-web.js +404 -0
- package/processors/hooks/content/content-hooks-frames.js +214 -0
- package/processors/hooks/content/content-hooks-web.js +48 -0
- package/processors/hooks/content/content-hooks.js +39 -0
- package/processors/index.js +34 -0
- package/processors/lazy/content/content-lazy-loader.js +229 -0
- package/single-file-bootstrap.js +33 -0
- package/single-file-core.js +2471 -0
- package/single-file-frames.js +1 -0
- package/single-file-helper.js +557 -0
- package/single-file-util.js +412 -0
- package/vendor/css-font-property-parser.js +343 -0
- package/vendor/css-media-query-parser.js +474 -0
- package/vendor/css-minifier.js +789 -0
- package/vendor/css-tree.js +35 -0
- package/vendor/css-unescape.js +72 -0
- package/vendor/html-srcset-parser.js +339 -0
- package/vendor/index.js +40 -0
- package/vendor/mime-type-parser.js +446 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Author: Gildas Lormeau
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// derived from https://github.com/jsdom/whatwg-mimetype
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Copyright © 2017–2018 Domenic Denicola <d@domenic.me>
|
|
29
|
+
*
|
|
30
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
31
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
32
|
+
* in the Software without restriction, including without limitation the rights
|
|
33
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
34
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
35
|
+
* furnished to do so, subject to the following conditions:
|
|
36
|
+
|
|
37
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
38
|
+
* copies or substantial portions of the Software.
|
|
39
|
+
|
|
40
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
41
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
42
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
43
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
44
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
45
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
46
|
+
* SOFTWARE.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
let utils, parser, serializer, MIMEType;
|
|
50
|
+
|
|
51
|
+
// lib/utils.js
|
|
52
|
+
{
|
|
53
|
+
utils = {};
|
|
54
|
+
utils.removeLeadingAndTrailingHTTPWhitespace = string => {
|
|
55
|
+
return string.replace(/^[ \t\n\r]+/, "").replace(/[ \t\n\r]+$/, "");
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
utils.removeTrailingHTTPWhitespace = string => {
|
|
59
|
+
return string.replace(/[ \t\n\r]+$/, "");
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
utils.isHTTPWhitespaceChar = char => {
|
|
63
|
+
return char === " " || char === "\t" || char === "\n" || char === "\r";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
utils.solelyContainsHTTPTokenCodePoints = string => {
|
|
67
|
+
return /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/.test(string);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
utils.soleyContainsHTTPQuotedStringTokenCodePoints = string => {
|
|
71
|
+
return /^[\t\u0020-\u007E\u0080-\u00FF]*$/.test(string);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
utils.asciiLowercase = string => {
|
|
75
|
+
return string.replace(/[A-Z]/g, l => l.toLowerCase());
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// This variant only implements it with the extract-value flag set.
|
|
79
|
+
utils.collectAnHTTPQuotedString = (input, position) => {
|
|
80
|
+
let value = "";
|
|
81
|
+
|
|
82
|
+
position++;
|
|
83
|
+
|
|
84
|
+
// eslint-disable-next-line no-constant-condition
|
|
85
|
+
while (true) {
|
|
86
|
+
while (position < input.length && input[position] !== "\"" && input[position] !== "\\") {
|
|
87
|
+
value += input[position];
|
|
88
|
+
++position;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (position >= input.length) {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const quoteOrBackslash = input[position];
|
|
96
|
+
++position;
|
|
97
|
+
|
|
98
|
+
if (quoteOrBackslash === "\\") {
|
|
99
|
+
if (position >= input.length) {
|
|
100
|
+
value += "\\";
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
value += input[position];
|
|
105
|
+
++position;
|
|
106
|
+
} else {
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return [value, position];
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// lib/serializer.js
|
|
116
|
+
{
|
|
117
|
+
const { solelyContainsHTTPTokenCodePoints } = utils;
|
|
118
|
+
serializer = mimeType => {
|
|
119
|
+
let serialization = `${mimeType.type}/${mimeType.subtype}`;
|
|
120
|
+
|
|
121
|
+
if (mimeType.parameters.size === 0) {
|
|
122
|
+
return serialization;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (let [name, value] of mimeType.parameters) {
|
|
126
|
+
serialization += ";";
|
|
127
|
+
serialization += name;
|
|
128
|
+
serialization += "=";
|
|
129
|
+
|
|
130
|
+
if (!solelyContainsHTTPTokenCodePoints(value) || value.length === 0) {
|
|
131
|
+
value = value.replace(/(["\\])/g, "\\$1");
|
|
132
|
+
value = `"${value}"`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
serialization += value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return serialization;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// lib/parser.js
|
|
143
|
+
{
|
|
144
|
+
const {
|
|
145
|
+
removeLeadingAndTrailingHTTPWhitespace,
|
|
146
|
+
removeTrailingHTTPWhitespace,
|
|
147
|
+
isHTTPWhitespaceChar,
|
|
148
|
+
solelyContainsHTTPTokenCodePoints,
|
|
149
|
+
soleyContainsHTTPQuotedStringTokenCodePoints,
|
|
150
|
+
asciiLowercase,
|
|
151
|
+
collectAnHTTPQuotedString
|
|
152
|
+
} = utils;
|
|
153
|
+
|
|
154
|
+
parser = input => {
|
|
155
|
+
input = removeLeadingAndTrailingHTTPWhitespace(input);
|
|
156
|
+
|
|
157
|
+
let position = 0;
|
|
158
|
+
let type = "";
|
|
159
|
+
while (position < input.length && input[position] !== "/") {
|
|
160
|
+
type += input[position];
|
|
161
|
+
++position;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (position >= input.length) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Skips past "/"
|
|
173
|
+
++position;
|
|
174
|
+
|
|
175
|
+
let subtype = "";
|
|
176
|
+
while (position < input.length && input[position] !== ";") {
|
|
177
|
+
subtype += input[position];
|
|
178
|
+
++position;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
subtype = removeTrailingHTTPWhitespace(subtype);
|
|
182
|
+
|
|
183
|
+
if (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const mimeType = {
|
|
188
|
+
type: asciiLowercase(type),
|
|
189
|
+
subtype: asciiLowercase(subtype),
|
|
190
|
+
parameters: new Map()
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
while (position < input.length) {
|
|
194
|
+
// Skip past ";"
|
|
195
|
+
++position;
|
|
196
|
+
|
|
197
|
+
while (isHTTPWhitespaceChar(input[position])) {
|
|
198
|
+
++position;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let parameterName = "";
|
|
202
|
+
while (position < input.length && input[position] !== ";" && input[position] !== "=") {
|
|
203
|
+
parameterName += input[position];
|
|
204
|
+
++position;
|
|
205
|
+
}
|
|
206
|
+
parameterName = asciiLowercase(parameterName);
|
|
207
|
+
|
|
208
|
+
if (position < input.length) {
|
|
209
|
+
if (input[position] === ";") {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Skip past "="
|
|
214
|
+
++position;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
let parameterValue = null;
|
|
218
|
+
if (input[position] === "\"") {
|
|
219
|
+
[parameterValue, position] = collectAnHTTPQuotedString(input, position);
|
|
220
|
+
|
|
221
|
+
while (position < input.length && input[position] !== ";") {
|
|
222
|
+
++position;
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
parameterValue = "";
|
|
226
|
+
while (position < input.length && input[position] !== ";") {
|
|
227
|
+
parameterValue += input[position];
|
|
228
|
+
++position;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
parameterValue = removeTrailingHTTPWhitespace(parameterValue);
|
|
232
|
+
|
|
233
|
+
if (parameterValue === "") {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (parameterName.length > 0 &&
|
|
239
|
+
solelyContainsHTTPTokenCodePoints(parameterName) &&
|
|
240
|
+
soleyContainsHTTPQuotedStringTokenCodePoints(parameterValue) &&
|
|
241
|
+
!mimeType.parameters.has(parameterName)) {
|
|
242
|
+
mimeType.parameters.set(parameterName, parameterValue);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return mimeType;
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// lib/mime-type.js
|
|
251
|
+
{
|
|
252
|
+
const parse = parser;
|
|
253
|
+
const serialize = serializer;
|
|
254
|
+
const {
|
|
255
|
+
asciiLowercase,
|
|
256
|
+
solelyContainsHTTPTokenCodePoints,
|
|
257
|
+
soleyContainsHTTPQuotedStringTokenCodePoints
|
|
258
|
+
} = utils;
|
|
259
|
+
|
|
260
|
+
MIMEType = class MIMEType {
|
|
261
|
+
constructor(string) {
|
|
262
|
+
string = String(string);
|
|
263
|
+
const result = parse(string);
|
|
264
|
+
if (result === null) {
|
|
265
|
+
throw new Error(`Could not parse MIME type string "${string}"`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
this._type = result.type;
|
|
269
|
+
this._subtype = result.subtype;
|
|
270
|
+
this._parameters = new MIMETypeParameters(result.parameters);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
static parse(string) {
|
|
274
|
+
try {
|
|
275
|
+
return new this(string);
|
|
276
|
+
} catch (e) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
get essence() {
|
|
282
|
+
return `${this.type}/${this.subtype}`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
get type() {
|
|
286
|
+
return this._type;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
set type(value) {
|
|
290
|
+
value = asciiLowercase(String(value));
|
|
291
|
+
|
|
292
|
+
if (value.length === 0) {
|
|
293
|
+
throw new Error("Invalid type: must be a non-empty string");
|
|
294
|
+
}
|
|
295
|
+
if (!solelyContainsHTTPTokenCodePoints(value)) {
|
|
296
|
+
throw new Error(`Invalid type ${value}: must contain only HTTP token code points`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
this._type = value;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
get subtype() {
|
|
303
|
+
return this._subtype;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
set subtype(value) {
|
|
307
|
+
value = asciiLowercase(String(value));
|
|
308
|
+
|
|
309
|
+
if (value.length === 0) {
|
|
310
|
+
throw new Error("Invalid subtype: must be a non-empty string");
|
|
311
|
+
}
|
|
312
|
+
if (!solelyContainsHTTPTokenCodePoints(value)) {
|
|
313
|
+
throw new Error(`Invalid subtype ${value}: must contain only HTTP token code points`);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
this._subtype = value;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
get parameters() {
|
|
320
|
+
return this._parameters;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
toString() {
|
|
324
|
+
// The serialize function works on both "MIME type records" (i.e. the results of parse) and on this class, since
|
|
325
|
+
// this class's interface is identical.
|
|
326
|
+
return serialize(this);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
isJavaScript({ allowParameters = false } = {}) {
|
|
330
|
+
switch (this._type) {
|
|
331
|
+
case "text": {
|
|
332
|
+
switch (this._subtype) {
|
|
333
|
+
case "ecmascript":
|
|
334
|
+
case "javascript":
|
|
335
|
+
case "javascript1.0":
|
|
336
|
+
case "javascript1.1":
|
|
337
|
+
case "javascript1.2":
|
|
338
|
+
case "javascript1.3":
|
|
339
|
+
case "javascript1.4":
|
|
340
|
+
case "javascript1.5":
|
|
341
|
+
case "jscript":
|
|
342
|
+
case "livescript":
|
|
343
|
+
case "x-ecmascript":
|
|
344
|
+
case "x-javascript": {
|
|
345
|
+
return allowParameters || this._parameters.size === 0;
|
|
346
|
+
}
|
|
347
|
+
default: {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
case "application": {
|
|
353
|
+
switch (this._subtype) {
|
|
354
|
+
case "ecmascript":
|
|
355
|
+
case "javascript":
|
|
356
|
+
case "x-ecmascript":
|
|
357
|
+
case "x-javascript": {
|
|
358
|
+
return allowParameters || this._parameters.size === 0;
|
|
359
|
+
}
|
|
360
|
+
default: {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
default: {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
isXML() {
|
|
371
|
+
return (this._subtype === "xml" && (this._type === "text" || this._type === "application")) ||
|
|
372
|
+
this._subtype.endsWith("+xml");
|
|
373
|
+
}
|
|
374
|
+
isHTML() {
|
|
375
|
+
return this._subtype === "html" && this._type === "text";
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
class MIMETypeParameters {
|
|
380
|
+
constructor(map) {
|
|
381
|
+
this._map = map;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
get size() {
|
|
385
|
+
return this._map.size;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
get(name) {
|
|
389
|
+
name = asciiLowercase(String(name));
|
|
390
|
+
return this._map.get(name);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
has(name) {
|
|
394
|
+
name = asciiLowercase(String(name));
|
|
395
|
+
return this._map.has(name);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
set(name, value) {
|
|
399
|
+
name = asciiLowercase(String(name));
|
|
400
|
+
value = String(value);
|
|
401
|
+
|
|
402
|
+
if (!solelyContainsHTTPTokenCodePoints(name)) {
|
|
403
|
+
throw new Error(`Invalid MIME type parameter name "${name}": only HTTP token code points are valid.`);
|
|
404
|
+
}
|
|
405
|
+
if (!soleyContainsHTTPQuotedStringTokenCodePoints(value)) {
|
|
406
|
+
throw new Error(`Invalid MIME type parameter value "${value}": only HTTP quoted-string token code points are valid.`);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return this._map.set(name, value);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
clear() {
|
|
413
|
+
this._map.clear();
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
delete(name) {
|
|
417
|
+
name = asciiLowercase(String(name));
|
|
418
|
+
return this._map.delete(name);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
forEach(callbackFn, thisArg) {
|
|
422
|
+
this._map.forEach(callbackFn, thisArg);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
keys() {
|
|
426
|
+
return this._map.keys();
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
values() {
|
|
430
|
+
return this._map.values();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
entries() {
|
|
434
|
+
return this._map.entries();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
[Symbol.iterator]() {
|
|
438
|
+
return this._map[Symbol.iterator]();
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export {
|
|
445
|
+
MIMEType
|
|
446
|
+
};
|