html-crush 4.1.10 → 5.0.2

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.
@@ -1,600 +0,0 @@
1
- /**
2
- * @name html-crush
3
- * @fileoverview Minifies HTML/CSS: valid or broken, pure or mixed with other languages
4
- * @version 4.1.10
5
- * @author Roy Revelt, Codsen Ltd
6
- * @license MIT
7
- * {@link https://codsen.com/os/html-crush/}
8
- */
9
-
10
- 'use strict';
11
-
12
- Object.defineProperty(exports, '__esModule', { value: true });
13
-
14
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
15
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
16
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
17
- var _typeof = require('@babel/runtime/helpers/typeof');
18
- var rangesApply = require('ranges-apply');
19
- var rangesPush = require('ranges-push');
20
- var stringMatchLeftRight = require('string-match-left-right');
21
- var stringRangeExpander = require('string-range-expander');
22
- var stringLeftRight = require('string-left-right');
23
-
24
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
25
-
26
- var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
27
- var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
28
- var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
29
- var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
30
-
31
- var version$1 = "4.1.10";
32
-
33
- var version = version$1;
34
- var finalIndexesToDelete = new rangesPush.Ranges({
35
- limitToBeAddedWhitespace: true
36
- });
37
- var defaults = {
38
- lineLengthLimit: 500,
39
- removeIndentations: true,
40
- removeLineBreaks: false,
41
- removeHTMLComments: false,
42
- removeCSSComments: true,
43
- reportProgressFunc: null,
44
- reportProgressFuncFrom: 0,
45
- reportProgressFuncTo: 100,
46
- breakToTheLeftOf: ["</td", "<html", "</html", "<head", "</head", "<meta", "<link", "<table", "<script", "</script", "<!DOCTYPE", "<style", "</style", "<title", "<body", "@media", "</body", "<!--[if", "<!--<![endif", "<![endif]"],
47
- mindTheInlineTags: ["a", "abbr", "acronym", "audio", "b", "bdi", "bdo", "big", "br", "button", "canvas", "cite", "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img", "input", "ins", "kbd", "label", "map", "mark", "meter", "noscript", "object", "output", "picture", "progress", "q", "ruby", "s", "samp", "script", "select", "slot", "small", "span", "strong", "sub", "sup", "svg", "template", "textarea", "time", "u", "tt", "var", "video", "wbr"]
48
- };
49
- var applicableOpts = {
50
- removeHTMLComments: false,
51
- removeCSSComments: false
52
- };
53
- function isStr(something) {
54
- return typeof something === "string";
55
- }
56
- function isLetter(something) {
57
- return typeof something === "string" && something.toUpperCase() !== something.toLowerCase();
58
- }
59
- function crush(str, originalOpts) {
60
- var start = Date.now();
61
- if (!isStr(str)) {
62
- if (str === undefined) {
63
- throw new Error("html-crush: [THROW_ID_01] the first input argument is completely missing! It should be given as string.");
64
- } else {
65
- throw new Error("html-crush: [THROW_ID_02] the first input argument must be string! It was given as \"".concat(_typeof__default['default'](str), "\", equal to:\n").concat(JSON.stringify(str, null, 4)));
66
- }
67
- }
68
- if (originalOpts && _typeof__default['default'](originalOpts) !== "object") {
69
- throw new Error("html-crush: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ".concat(_typeof__default['default'](originalOpts), ", equal to ").concat(JSON.stringify(originalOpts, null, 4)));
70
- }
71
- if (originalOpts && Array.isArray(originalOpts.breakToTheLeftOf) && originalOpts.breakToTheLeftOf.length) {
72
- for (var z = 0, _len = originalOpts.breakToTheLeftOf.length; z < _len; z++) {
73
- if (!isStr(originalOpts.breakToTheLeftOf[z])) {
74
- throw new TypeError("html-crush: [THROW_ID_05] the opts.breakToTheLeftOf array contains non-string elements! For example, element at index ".concat(z, " is of a type \"").concat(_typeof__default['default'](originalOpts.breakToTheLeftOf[z]), "\" and is equal to:\n").concat(JSON.stringify(originalOpts.breakToTheLeftOf[z], null, 4)));
75
- }
76
- }
77
- }
78
- var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOpts);
79
- if (typeof opts.removeHTMLComments === "boolean") {
80
- opts.removeHTMLComments = opts.removeHTMLComments ? 1 : 0;
81
- }
82
- var breakToTheLeftOfFirstLetters = "";
83
- if (Array.isArray(opts.breakToTheLeftOf) && opts.breakToTheLeftOf.length) {
84
- breakToTheLeftOfFirstLetters = _toConsumableArray__default['default'](new Set(opts.breakToTheLeftOf.map(function (val) {
85
- return val[0];
86
- }))).join("");
87
- }
88
- var lastLinebreak = null;
89
- var whitespaceStartedAt = null;
90
- var nonWhitespaceCharMet = false;
91
- var countCharactersPerLine = 0;
92
- var cpl = 0;
93
- var withinStyleTag = false;
94
- var withinHTMLConditional = false;
95
- var withinInlineStyle = null;
96
- var styleCommentStartedAt = null;
97
- var htmlCommentStartedAt = null;
98
- var scriptStartedAt = null;
99
- var doNothing;
100
- var stageFrom = null;
101
- var stageTo = null;
102
- var stageAdd = null;
103
- var tagName = null;
104
- var tagNameStartsAt = null;
105
- var leftTagName = null;
106
- var CHARS_BREAK_ON_THE_RIGHT_OF_THEM = ">};";
107
- var CHARS_BREAK_ON_THE_LEFT_OF_THEM = "<";
108
- var CHARS_DONT_BREAK_ON_THE_LEFT_OF_THEM = "!";
109
- var DELETE_TIGHTLY_IF_ON_LEFT_IS = ">";
110
- var DELETE_TIGHTLY_IF_ON_RIGHT_IS = "<";
111
- var set = "{},:;<>~+";
112
- var DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS = set;
113
- var DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS = set;
114
- var beginningOfAFile = true;
115
- var len = str.length;
116
- var midLen = Math.floor(len / 2);
117
- var leavePercForLastStage = 0.01;
118
- var ceil;
119
- if (opts.reportProgressFunc) {
120
- ceil = Math.floor(opts.reportProgressFuncTo - (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) * leavePercForLastStage - opts.reportProgressFuncFrom);
121
- }
122
- var currentPercentageDone;
123
- var lastPercentage = 0;
124
- var lineEnding = "\n";
125
- if (str.includes("\r\n")) {
126
- lineEnding = "\r\n";
127
- } else if (str.includes("\r")) {
128
- lineEnding = "\r";
129
- }
130
- if (len) {
131
- for (var i = 0; i < len; i++) {
132
- if (opts.reportProgressFunc) {
133
- if (len > 1000 && len < 2000) {
134
- if (i === midLen) {
135
- opts.reportProgressFunc(Math.floor((opts.reportProgressFuncTo - opts.reportProgressFuncFrom) / 2));
136
- }
137
- } else if (len >= 2000) {
138
- currentPercentageDone = opts.reportProgressFuncFrom + Math.floor(i / len * (ceil || 1));
139
- if (currentPercentageDone !== lastPercentage) {
140
- lastPercentage = currentPercentageDone;
141
- opts.reportProgressFunc(currentPercentageDone);
142
- }
143
- }
144
- }
145
- cpl++;
146
- if (doNothing && typeof doNothing === "number" && i >= doNothing) {
147
- doNothing = undefined;
148
- }
149
- if (scriptStartedAt !== null && str.startsWith("</script", i) && !isLetter(str[i + 8])) {
150
- if ((opts.removeIndentations || opts.removeLineBreaks) && i > 0 && str[~-i] && !str[~-i].trim()) {
151
- for (var y = i; y--;) {
152
- if (str[y] === "\n" || str[y] === "\r" || str[y].trim()) {
153
- if (y + 1 < i) {
154
- finalIndexesToDelete.push(y + 1, i);
155
- }
156
- break;
157
- }
158
- }
159
- }
160
- scriptStartedAt = null;
161
- doNothing = false;
162
- i += 8;
163
- continue;
164
- }
165
- if (!doNothing && !withinStyleTag && str.startsWith("<script", i) && !isLetter(str[i + 7])) {
166
- scriptStartedAt = i;
167
- doNothing = true;
168
- var whatToInsert = "";
169
- if ((opts.removeLineBreaks || opts.removeIndentations) && whitespaceStartedAt !== null) {
170
- if (whitespaceStartedAt > 0) {
171
- whatToInsert = lineEnding;
172
- }
173
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToInsert);
174
- }
175
- whitespaceStartedAt = null;
176
- lastLinebreak = null;
177
- }
178
- if (tagNameStartsAt !== null && tagName === null && !/\w/.test(str[i])
179
- ) {
180
- tagName = str.slice(tagNameStartsAt, i);
181
- var idxOnTheRight = stringLeftRight.right(str, ~-i);
182
- if (typeof idxOnTheRight === "number" && str[idxOnTheRight] === ">" && !str[i].trim() && stringLeftRight.right(str, i)) {
183
- finalIndexesToDelete.push(i, stringLeftRight.right(str, i));
184
- } else if (idxOnTheRight && str[idxOnTheRight] === "/" && str[stringLeftRight.right(str, idxOnTheRight)] === ">") {
185
- if (!str[i].trim() && stringLeftRight.right(str, i)) {
186
- finalIndexesToDelete.push(i, stringLeftRight.right(str, i));
187
- }
188
- if (str[idxOnTheRight + 1] !== ">" && stringLeftRight.right(str, idxOnTheRight + 1)) {
189
- finalIndexesToDelete.push(idxOnTheRight + 1, stringLeftRight.right(str, idxOnTheRight + 1));
190
- }
191
- }
192
- }
193
- if (!doNothing && !withinStyleTag && !withinInlineStyle && str[~-i] === "<" && tagNameStartsAt === null) {
194
- if (/\w/.test(str[i])) {
195
- tagNameStartsAt = i;
196
- } else if (str[stringLeftRight.right(str, ~-i)] === "/" && /\w/.test(str[stringLeftRight.right(str, stringLeftRight.right(str, ~-i))] || "")) {
197
- tagNameStartsAt = stringLeftRight.right(str, stringLeftRight.right(str, ~-i));
198
- }
199
- }
200
- if (!doNothing && (withinStyleTag || withinInlineStyle) && styleCommentStartedAt !== null && str[i] === "*" && str[i + 1] === "/") {
201
- var _expander = stringRangeExpander.expander({
202
- str: str,
203
- from: styleCommentStartedAt,
204
- to: i + 2,
205
- ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
206
- ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS
207
- });
208
- var _expander2 = _slicedToArray__default['default'](_expander, 2);
209
- stageFrom = _expander2[0];
210
- stageTo = _expander2[1];
211
- styleCommentStartedAt = null;
212
- if (stageFrom != null) {
213
- finalIndexesToDelete.push(stageFrom, stageTo);
214
- } else {
215
- countCharactersPerLine += 1;
216
- i += 1;
217
- }
218
- doNothing = i + 2;
219
- }
220
- if (!doNothing && (withinStyleTag || withinInlineStyle) && styleCommentStartedAt === null && str[i] === "/" && str[i + 1] === "*") {
221
- if (!applicableOpts.removeCSSComments) {
222
- applicableOpts.removeCSSComments = true;
223
- }
224
- if (opts.removeCSSComments) {
225
- styleCommentStartedAt = i;
226
- }
227
- }
228
- if (withinHTMLConditional && str.startsWith("![endif", i + 1)) {
229
- withinHTMLConditional = false;
230
- }
231
- if (!doNothing && !withinStyleTag && !withinInlineStyle && htmlCommentStartedAt !== null) {
232
- var distanceFromHereToCommentEnding = void 0;
233
- if (str.startsWith("-->", i)) {
234
- distanceFromHereToCommentEnding = 3;
235
- } else if (str[i] === ">" && str[i - 1] === "]") {
236
- distanceFromHereToCommentEnding = 1;
237
- }
238
- if (distanceFromHereToCommentEnding) {
239
- var _expander3 = stringRangeExpander.expander({
240
- str: str,
241
- from: htmlCommentStartedAt,
242
- to: i + distanceFromHereToCommentEnding
243
- });
244
- var _expander4 = _slicedToArray__default['default'](_expander3, 2);
245
- stageFrom = _expander4[0];
246
- stageTo = _expander4[1];
247
- htmlCommentStartedAt = null;
248
- if (stageFrom != null) {
249
- if (opts.lineLengthLimit && cpl - (stageTo - stageFrom) >= opts.lineLengthLimit) {
250
- finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
251
- cpl = -distanceFromHereToCommentEnding;
252
- } else {
253
- finalIndexesToDelete.push(stageFrom, stageTo);
254
- cpl -= stageTo - stageFrom;
255
- }
256
- } else {
257
- countCharactersPerLine += distanceFromHereToCommentEnding - 1;
258
- i += distanceFromHereToCommentEnding - 1;
259
- }
260
- doNothing = i + distanceFromHereToCommentEnding;
261
- }
262
- }
263
- if (!doNothing && !withinStyleTag && !withinInlineStyle && str.startsWith("<!--", i) && htmlCommentStartedAt === null) {
264
- if (str.startsWith("[if", i + 4)) {
265
- if (!withinHTMLConditional) {
266
- withinHTMLConditional = true;
267
- }
268
- if (opts.removeHTMLComments === 2) {
269
- htmlCommentStartedAt = i;
270
- }
271
- } else if (
272
- opts.removeHTMLComments && (
273
- !withinHTMLConditional || opts.removeHTMLComments === 2)) {
274
- htmlCommentStartedAt = i;
275
- }
276
- if (!applicableOpts.removeHTMLComments) {
277
- applicableOpts.removeHTMLComments = true;
278
- }
279
- }
280
- if (!doNothing && withinStyleTag && styleCommentStartedAt === null && str.startsWith("</style", i) && !isLetter(str[i + 7])) {
281
- withinStyleTag = false;
282
- } else if (!doNothing && !withinStyleTag && styleCommentStartedAt === null && str.startsWith("<style", i) && !isLetter(str[i + 6])) {
283
- withinStyleTag = true;
284
- if ((opts.removeLineBreaks || opts.removeIndentations) && opts.breakToTheLeftOf.includes("<style") && str.startsWith(" type=\"text/css\">", i + 6) && str[i + 24]) {
285
- finalIndexesToDelete.push(i + 23, i + 23, lineEnding);
286
- }
287
- }
288
- if (!doNothing && !withinInlineStyle && "\"'".includes(str[i]) && str.endsWith("style=", i)) {
289
- withinInlineStyle = i;
290
- }
291
- if (!doNothing && !str[i].trim()) {
292
- if (whitespaceStartedAt === null) {
293
- whitespaceStartedAt = i;
294
- }
295
- } else if (!doNothing && !((withinStyleTag || withinInlineStyle) && styleCommentStartedAt !== null)) {
296
- if (whitespaceStartedAt !== null) {
297
- if (opts.removeLineBreaks) {
298
- countCharactersPerLine += 1;
299
- }
300
- if (beginningOfAFile) {
301
- beginningOfAFile = false;
302
- if (opts.removeIndentations || opts.removeLineBreaks) {
303
- finalIndexesToDelete.push(0, i);
304
- }
305
- } else {
306
- if (opts.removeIndentations && !opts.removeLineBreaks) {
307
- if (!nonWhitespaceCharMet && lastLinebreak !== null && i > lastLinebreak) {
308
- finalIndexesToDelete.push(lastLinebreak + 1, i);
309
- } else if (whitespaceStartedAt + 1 < i) {
310
- if (
311
- str.endsWith("]>", whitespaceStartedAt) ||
312
- str.endsWith("-->", whitespaceStartedAt) ||
313
- str.startsWith("<![", i) ||
314
- str.startsWith("<!--<![", i)) {
315
- finalIndexesToDelete.push(whitespaceStartedAt, i);
316
- } else if (str[whitespaceStartedAt] === " ") {
317
- finalIndexesToDelete.push(whitespaceStartedAt + 1, i);
318
- } else if (str[~-i] === " ") {
319
- finalIndexesToDelete.push(whitespaceStartedAt, ~-i);
320
- } else {
321
- finalIndexesToDelete.push(whitespaceStartedAt, i, " ");
322
- }
323
- }
324
- }
325
- if (opts.removeLineBreaks || withinInlineStyle) {
326
- if (breakToTheLeftOfFirstLetters.includes(str[i]) && stringMatchLeftRight.matchRightIncl(str, i, opts.breakToTheLeftOf)) {
327
- if (
328
- !("\r\n".includes(str[~-i]) && whitespaceStartedAt === ~-i) &&
329
- !(str[~-i] === "\n" && str[i - 2] === "\r" && whitespaceStartedAt === i - 2)) {
330
- finalIndexesToDelete.push(whitespaceStartedAt, i, lineEnding);
331
- }
332
- stageFrom = null;
333
- stageTo = null;
334
- stageAdd = null;
335
- whitespaceStartedAt = null;
336
- countCharactersPerLine = 1;
337
- continue;
338
- }
339
- var whatToAdd = " ";
340
- if (
341
- str[i] === "<" && stringMatchLeftRight.matchRight(str, i, opts.mindTheInlineTags, {
342
- cb: function cb(nextChar) {
343
- return !nextChar || !/\w/.test(nextChar);
344
- }
345
- })
346
- ) ; else if (str[~-whitespaceStartedAt] && DELETE_TIGHTLY_IF_ON_LEFT_IS.includes(str[~-whitespaceStartedAt]) && DELETE_TIGHTLY_IF_ON_RIGHT_IS.includes(str[i]) || (withinStyleTag || withinInlineStyle) && styleCommentStartedAt === null && (DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS.includes(str[~-whitespaceStartedAt]) || DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS.includes(str[i])) || str.startsWith("!important", i) && !withinHTMLConditional || withinInlineStyle && (str[~-whitespaceStartedAt] === "'" || str[~-whitespaceStartedAt] === '"') || str[~-whitespaceStartedAt] === "}" && str.startsWith("</style", i) || str[i] === ">" && ("'\"".includes(str[stringLeftRight.left(str, i)]) || str[stringLeftRight.right(str, i)] === "<") || str[i] === "/" && str[stringLeftRight.right(str, i)] === ">") {
347
- whatToAdd = "";
348
- if (str[i] === "/" && str[i + 1] === ">" && stringLeftRight.right(str, i) && stringLeftRight.right(str, i) > i + 1) {
349
- finalIndexesToDelete.push(i + 1, stringLeftRight.right(str, i));
350
- countCharactersPerLine -= stringLeftRight.right(str, i) - i + 1;
351
- }
352
- }
353
- if (whatToAdd && whatToAdd.length) {
354
- countCharactersPerLine += 1;
355
- }
356
- if (!opts.lineLengthLimit) {
357
- if (!(i === whitespaceStartedAt + 1 &&
358
- whatToAdd === " ")) {
359
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
360
- }
361
- } else {
362
- if (countCharactersPerLine >= opts.lineLengthLimit || !str[i + 1] || str[i] === ">" || str[i] === "/" && str[i + 1] === ">") {
363
- if (countCharactersPerLine > opts.lineLengthLimit || countCharactersPerLine === opts.lineLengthLimit && str[i + 1] && str[i + 1].trim() && !CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) && !CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i + 1])) {
364
- whatToAdd = lineEnding;
365
- countCharactersPerLine = 1;
366
- }
367
- if (countCharactersPerLine > opts.lineLengthLimit || !(whatToAdd === " " && i === whitespaceStartedAt + 1)) {
368
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
369
- lastLinebreak = null;
370
- }
371
- stageFrom = null;
372
- stageTo = null;
373
- stageAdd = null;
374
- } else if (stageFrom === null || whitespaceStartedAt < stageFrom) {
375
- stageFrom = whitespaceStartedAt;
376
- stageTo = i;
377
- stageAdd = whatToAdd;
378
- }
379
- }
380
- }
381
- }
382
- whitespaceStartedAt = null;
383
- if (!nonWhitespaceCharMet) {
384
- nonWhitespaceCharMet = true;
385
- }
386
- } else {
387
- if (beginningOfAFile) {
388
- beginningOfAFile = false;
389
- }
390
- if (opts.removeLineBreaks) {
391
- countCharactersPerLine += 1;
392
- }
393
- }
394
- if (!nonWhitespaceCharMet) {
395
- nonWhitespaceCharMet = true;
396
- }
397
- }
398
- if (!doNothing && !beginningOfAFile && i !== 0 && opts.removeLineBreaks && (opts.lineLengthLimit || breakToTheLeftOfFirstLetters) && !str.startsWith("</a", i)) {
399
- if (breakToTheLeftOfFirstLetters && stringMatchLeftRight.matchRightIncl(str, i, opts.breakToTheLeftOf) && str.slice(0, i).trim() && (!str.startsWith("<![endif]", i) || !stringMatchLeftRight.matchLeft(str, i, "<!--"))) {
400
- finalIndexesToDelete.push(i, i, lineEnding);
401
- stageFrom = null;
402
- stageTo = null;
403
- stageAdd = null;
404
- countCharactersPerLine = 1;
405
- continue;
406
- } else if (opts.lineLengthLimit && countCharactersPerLine <= opts.lineLengthLimit) {
407
- if (!str[i + 1] || CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) && !CHARS_DONT_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) || CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) || !str[i].trim()) {
408
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
409
- var _whatToAdd = stageAdd;
410
- if (str[i].trim() && str[i + 1] && str[i + 1].trim() && countCharactersPerLine + (stageAdd ? stageAdd.length : 0) > opts.lineLengthLimit) {
411
- _whatToAdd = lineEnding;
412
- }
413
- if (countCharactersPerLine + (_whatToAdd ? _whatToAdd.length : 0) > opts.lineLengthLimit || !(_whatToAdd === " " && stageTo === stageFrom + 1 && str[stageFrom] === " ")) {
414
- if (!(str[~-stageFrom] === "}" && str[stageTo] === "{")) {
415
- finalIndexesToDelete.push(stageFrom, stageTo, _whatToAdd);
416
- lastLinebreak = null;
417
- }
418
- } else {
419
- countCharactersPerLine -= lastLinebreak || 0;
420
- }
421
- }
422
- if (str[i].trim() && (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) || str[~-i] && CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[~-i])) && isStr(leftTagName) && (!tagName || !opts.mindTheInlineTags.includes(tagName)) && !(str[i] === "<" && stringMatchLeftRight.matchRight(str, i, opts.mindTheInlineTags, {
423
- cb: function cb(nextChar) {
424
- return !nextChar || !/\w/.test(nextChar);
425
- }
426
- })) && !(str[i] === "<" && stringMatchLeftRight.matchRight(str, i, opts.mindTheInlineTags, {
427
- trimCharsBeforeMatching: "/",
428
- cb: function cb(nextChar) {
429
- return !nextChar || !/\w/.test(nextChar);
430
- }
431
- }))) {
432
- stageFrom = i;
433
- stageTo = i;
434
- stageAdd = null;
435
- } else if (styleCommentStartedAt === null && stageFrom !== null && (withinInlineStyle || !opts.mindTheInlineTags || !Array.isArray(opts.mindTheInlineTags) || Array.isArray(opts.mindTheInlineTags.length) && !opts.mindTheInlineTags.length || !isStr(tagName) || Array.isArray(opts.mindTheInlineTags) && opts.mindTheInlineTags.length && isStr(tagName) && !opts.mindTheInlineTags.includes(tagName)) && !(str[i] === "<" && stringMatchLeftRight.matchRight(str, i, opts.mindTheInlineTags, {
436
- trimCharsBeforeMatching: "/",
437
- cb: function cb(nextChar) {
438
- return !nextChar || !/\w/.test(nextChar);
439
- }
440
- }))) {
441
- stageFrom = null;
442
- stageTo = null;
443
- stageAdd = null;
444
- }
445
- }
446
- } else if (opts.lineLengthLimit) {
447
- if (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) && !(str[i] === "<" && stringMatchLeftRight.matchRight(str, i, opts.mindTheInlineTags, {
448
- trimCharsBeforeMatching: "/",
449
- cb: function cb(nextChar) {
450
- return !nextChar || !/\w/.test(nextChar);
451
- }
452
- }))) {
453
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
454
- var whatToAddLength = stageAdd && stageAdd.length ? stageAdd.length : 0;
455
- if (countCharactersPerLine - (stageTo - stageFrom - whatToAddLength) - 1 > opts.lineLengthLimit) ; else {
456
- finalIndexesToDelete.push(stageFrom, stageTo, stageAdd);
457
- if (countCharactersPerLine - (stageTo - stageFrom - whatToAddLength) - 1 === opts.lineLengthLimit) {
458
- finalIndexesToDelete.push(i, i, lineEnding);
459
- countCharactersPerLine = 0;
460
- }
461
- stageFrom = null;
462
- stageTo = null;
463
- stageAdd = null;
464
- }
465
- } else {
466
- finalIndexesToDelete.push(i, i, lineEnding);
467
- countCharactersPerLine = 0;
468
- }
469
- } else if (str[i + 1] && CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) && isStr(tagName) && Array.isArray(opts.mindTheInlineTags) && opts.mindTheInlineTags.length && !opts.mindTheInlineTags.includes(tagName)) {
470
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) ; else {
471
- finalIndexesToDelete.push(i + 1, i + 1, lineEnding);
472
- countCharactersPerLine = 0;
473
- }
474
- } else if (!str[i].trim()) ; else if (!str[i + 1]) {
475
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
476
- finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
477
- }
478
- }
479
- }
480
- }
481
- if (!doNothing && !beginningOfAFile && opts.removeLineBreaks && opts.lineLengthLimit && countCharactersPerLine >= opts.lineLengthLimit && stageFrom !== null && stageTo !== null && !CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) && !CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) && !"/".includes(str[i])) {
482
- if (!(countCharactersPerLine === opts.lineLengthLimit && str[i + 1] && !str[i + 1].trim())) {
483
- var _whatToAdd2 = lineEnding;
484
- if (str[i + 1] && !str[i + 1].trim() && countCharactersPerLine === opts.lineLengthLimit) {
485
- _whatToAdd2 = stageAdd;
486
- }
487
- if (_whatToAdd2 === lineEnding && !str[~-stageFrom].trim() && stringLeftRight.left(str, stageFrom)) {
488
- stageFrom = stringLeftRight.left(str, stageFrom) + 1;
489
- }
490
- finalIndexesToDelete.push(stageFrom, stageTo, _whatToAdd2);
491
- countCharactersPerLine = i - stageTo;
492
- if (str[i].length) {
493
- countCharactersPerLine += 1;
494
- }
495
- stageFrom = null;
496
- stageTo = null;
497
- stageAdd = null;
498
- }
499
- }
500
- if (!doNothing && str[i] === "\n" || str[i] === "\r" && (!str[i + 1] || str[i + 1] && str[i + 1] !== "\n")) {
501
- lastLinebreak = i;
502
- if (nonWhitespaceCharMet) {
503
- nonWhitespaceCharMet = false;
504
- }
505
- if (!opts.removeLineBreaks && whitespaceStartedAt !== null && whitespaceStartedAt < i && str[i + 1] && str[i + 1] !== "\r" && str[i + 1] !== "\n") {
506
- finalIndexesToDelete.push(whitespaceStartedAt, i);
507
- }
508
- }
509
- if (!str[i + 1]) {
510
- if (withinStyleTag && styleCommentStartedAt !== null) {
511
- finalIndexesToDelete.push.apply(finalIndexesToDelete, _toConsumableArray__default['default'](stringRangeExpander.expander({
512
- str: str,
513
- from: styleCommentStartedAt,
514
- to: i,
515
- ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
516
- ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS
517
- })));
518
- } else if (whitespaceStartedAt && str[i] !== "\n" && str[i] !== "\r") {
519
- finalIndexesToDelete.push(whitespaceStartedAt, i + 1);
520
- } else if (whitespaceStartedAt && (str[i] === "\r" && str[i + 1] === "\n" || str[i] === "\n" && str[i - 1] !== "\r")) {
521
- finalIndexesToDelete.push(whitespaceStartedAt, i);
522
- }
523
- }
524
- if (!doNothing && withinInlineStyle && withinInlineStyle < i && str[withinInlineStyle] === str[i]) {
525
- withinInlineStyle = null;
526
- }
527
- if (!doNothing && !withinStyleTag && str.startsWith("<pre", i) && !isLetter(str[i + 4])) {
528
- var locationOfClosingPre = str.indexOf("</pre", i + 5);
529
- if (locationOfClosingPre > 0) {
530
- doNothing = locationOfClosingPre;
531
- }
532
- }
533
- if (!doNothing && !withinStyleTag && str.startsWith("<code", i) && !isLetter(str[i + 5])) {
534
- var locationOfClosingCode = str.indexOf("</code", i + 5);
535
- if (locationOfClosingCode > 0) {
536
- doNothing = locationOfClosingCode;
537
- }
538
- }
539
- if (!doNothing && str.startsWith("<![CDATA[", i)) {
540
- var locationOfClosingCData = str.indexOf("]]>", i + 9);
541
- if (locationOfClosingCData > 0) {
542
- doNothing = locationOfClosingCData;
543
- }
544
- }
545
- if (!doNothing && !withinStyleTag && !withinInlineStyle && tagNameStartsAt !== null && str[i] === ">") {
546
- if (str[stringLeftRight.right(str, i)] === "<") {
547
- leftTagName = tagName;
548
- }
549
- tagNameStartsAt = null;
550
- tagName = null;
551
- }
552
- if (str[i] === "<" && leftTagName !== null) {
553
- leftTagName = null;
554
- }
555
- }
556
- if (finalIndexesToDelete.current()) {
557
- var ranges = finalIndexesToDelete.current();
558
- finalIndexesToDelete.wipe();
559
- var startingPercentageDone = opts.reportProgressFuncTo - (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) * leavePercForLastStage;
560
- var res = rangesApply.rApply(str, ranges, function (applyPercDone) {
561
- if (opts.reportProgressFunc && len >= 2000) {
562
- currentPercentageDone = Math.floor(startingPercentageDone + (opts.reportProgressFuncTo - startingPercentageDone) * (applyPercDone / 100));
563
- if (currentPercentageDone !== lastPercentage) {
564
- lastPercentage = currentPercentageDone;
565
- opts.reportProgressFunc(currentPercentageDone);
566
- }
567
- }
568
- });
569
- var resLen = res.length;
570
- return {
571
- log: {
572
- timeTakenInMilliseconds: Date.now() - start,
573
- originalLength: len,
574
- cleanedLength: resLen,
575
- bytesSaved: Math.max(len - resLen, 0),
576
- percentageReducedOfOriginal: len ? Math.round(Math.max(len - resLen, 0) * 100 / len) : 0
577
- },
578
- ranges: ranges,
579
- applicableOpts: applicableOpts,
580
- result: res
581
- };
582
- }
583
- }
584
- return {
585
- log: {
586
- timeTakenInMilliseconds: Date.now() - start,
587
- originalLength: len,
588
- cleanedLength: len,
589
- bytesSaved: 0,
590
- percentageReducedOfOriginal: 0
591
- },
592
- applicableOpts: applicableOpts,
593
- ranges: null,
594
- result: str
595
- };
596
- }
597
-
598
- exports.crush = crush;
599
- exports.defaults = defaults;
600
- exports.version = version;