html-crush 5.0.5 → 5.0.6

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,7 +1,7 @@
1
1
  /**
2
2
  * @name html-crush
3
3
  * @fileoverview Minifies HTML/CSS: valid or broken, pure or mixed with other languages
4
- * @version 5.0.5
4
+ * @version 5.0.6
5
5
  * @author Roy Revelt, Codsen Ltd
6
6
  * @license MIT
7
7
  * {@link https://codsen.com/os/html-crush/}
@@ -13,569 +13,889 @@ import { matchRightIncl, matchRight, matchLeft } from 'string-match-left-right';
13
13
  import { expander } from 'string-range-expander';
14
14
  import { right, left } from 'string-left-right';
15
15
 
16
- var version$1 = "5.0.5";
16
+ var version$1 = "5.0.6";
17
17
 
18
18
  const version = version$1;
19
- const finalIndexesToDelete = new Ranges({
20
- limitToBeAddedWhitespace: true
21
- });
19
+ const finalIndexesToDelete = new Ranges({ limitToBeAddedWhitespace: true });
22
20
  const defaults = {
23
- lineLengthLimit: 500,
24
- removeIndentations: true,
25
- removeLineBreaks: false,
26
- removeHTMLComments: false,
27
- removeCSSComments: true,
28
- reportProgressFunc: null,
29
- reportProgressFuncFrom: 0,
30
- reportProgressFuncTo: 100,
31
- breakToTheLeftOf: ["</td", "<html", "</html", "<head", "</head", "<meta", "<link", "<table", "<script", "</script", "<!DOCTYPE", "<style", "</style", "<title", "<body", "@media", "</body", "<!--[if", "<!--<![endif", "<![endif]"],
32
- 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"]
21
+ lineLengthLimit: 500,
22
+ removeIndentations: true,
23
+ removeLineBreaks: false,
24
+ removeHTMLComments: false,
25
+ removeCSSComments: true,
26
+ reportProgressFunc: null,
27
+ reportProgressFuncFrom: 0,
28
+ reportProgressFuncTo: 100,
29
+ breakToTheLeftOf: [
30
+ "</td",
31
+ "<html",
32
+ "</html",
33
+ "<head",
34
+ "</head",
35
+ "<meta",
36
+ "<link",
37
+ "<table",
38
+ "<script",
39
+ "</script",
40
+ "<!DOCTYPE",
41
+ "<style",
42
+ "</style",
43
+ "<title",
44
+ "<body",
45
+ "@media",
46
+ "</body",
47
+ "<!--[if",
48
+ "<!--<![endif",
49
+ "<![endif]",
50
+ ],
51
+ mindTheInlineTags: [
52
+ "a",
53
+ "abbr",
54
+ "acronym",
55
+ "audio",
56
+ "b",
57
+ "bdi",
58
+ "bdo",
59
+ "big",
60
+ "br",
61
+ "button",
62
+ "canvas",
63
+ "cite",
64
+ "code",
65
+ "data",
66
+ "datalist",
67
+ "del",
68
+ "dfn",
69
+ "em",
70
+ "embed",
71
+ "i",
72
+ "iframe",
73
+ "img",
74
+ "input",
75
+ "ins",
76
+ "kbd",
77
+ "label",
78
+ "map",
79
+ "mark",
80
+ "meter",
81
+ "noscript",
82
+ "object",
83
+ "output",
84
+ "picture",
85
+ "progress",
86
+ "q",
87
+ "ruby",
88
+ "s",
89
+ "samp",
90
+ "script",
91
+ "select",
92
+ "slot",
93
+ "small",
94
+ "span",
95
+ "strong",
96
+ "sub",
97
+ "sup",
98
+ "svg",
99
+ "template",
100
+ "textarea",
101
+ "time",
102
+ "u",
103
+ "tt",
104
+ "var",
105
+ "video",
106
+ "wbr",
107
+ ],
33
108
  };
34
109
  const applicableOpts = {
35
- removeHTMLComments: false,
36
- removeCSSComments: false
110
+ removeHTMLComments: false,
111
+ removeCSSComments: false,
37
112
  };
38
113
  function isStr(something) {
39
- return typeof something === "string";
114
+ return typeof something === "string";
40
115
  }
41
116
  function isLetter(something) {
42
- return typeof something === "string" && something.toUpperCase() !== something.toLowerCase();
117
+ return (typeof something === "string" &&
118
+ something.toUpperCase() !== something.toLowerCase());
43
119
  }
44
120
  function crush(str, originalOpts) {
45
- const start = Date.now();
46
- if (!isStr(str)) {
47
- if (str === undefined) {
48
- throw new Error("html-crush: [THROW_ID_01] the first input argument is completely missing! It should be given as string.");
49
- } else {
50
- throw new Error(`html-crush: [THROW_ID_02] the first input argument must be string! It was given as "${typeof str}", equal to:\n${JSON.stringify(str, null, 4)}`);
51
- }
52
- }
53
- if (originalOpts && typeof originalOpts !== "object") {
54
- 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 ${typeof originalOpts}, equal to ${JSON.stringify(originalOpts, null, 4)}`);
55
- }
56
- if (originalOpts && Array.isArray(originalOpts.breakToTheLeftOf) && originalOpts.breakToTheLeftOf.length) {
57
- for (let z = 0, len = originalOpts.breakToTheLeftOf.length; z < len; z++) {
58
- if (!isStr(originalOpts.breakToTheLeftOf[z])) {
59
- throw new TypeError(`html-crush: [THROW_ID_05] the opts.breakToTheLeftOf array contains non-string elements! For example, element at index ${z} is of a type "${typeof originalOpts.breakToTheLeftOf[z]}" and is equal to:\n${JSON.stringify(originalOpts.breakToTheLeftOf[z], null, 4)}`);
60
- }
61
- }
62
- }
63
- const opts = { ...defaults,
64
- ...originalOpts
65
- };
66
- if (typeof opts.removeHTMLComments === "boolean") {
67
- opts.removeHTMLComments = opts.removeHTMLComments ? 1 : 0;
68
- }
69
- let breakToTheLeftOfFirstLetters = "";
70
- if (Array.isArray(opts.breakToTheLeftOf) && opts.breakToTheLeftOf.length) {
71
- breakToTheLeftOfFirstLetters = [...new Set(opts.breakToTheLeftOf.map(val => val[0]))].join("");
72
- }
73
- let lastLinebreak = null;
74
- let whitespaceStartedAt = null;
75
- let nonWhitespaceCharMet = false;
76
- let countCharactersPerLine = 0;
77
- let cpl = 0;
78
- let withinStyleTag = false;
79
- let withinHTMLConditional = false;
80
- let withinInlineStyle = null;
81
- let styleCommentStartedAt = null;
82
- let htmlCommentStartedAt = null;
83
- let scriptStartedAt = null;
84
- let doNothing;
85
- let stageFrom = null;
86
- let stageTo = null;
87
- let stageAdd = null;
88
- let tagName = null;
89
- let tagNameStartsAt = null;
90
- let leftTagName = null;
91
- const CHARS_BREAK_ON_THE_RIGHT_OF_THEM = `>};`;
92
- const CHARS_BREAK_ON_THE_LEFT_OF_THEM = `<`;
93
- const CHARS_DONT_BREAK_ON_THE_LEFT_OF_THEM = `!`;
94
- const DELETE_TIGHTLY_IF_ON_LEFT_IS = `>`;
95
- const DELETE_TIGHTLY_IF_ON_RIGHT_IS = `<`;
96
- const set = `{},:;<>~+`;
97
- const DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS = set;
98
- const DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS = set;
99
- let beginningOfAFile = true;
100
- const len = str.length;
101
- const midLen = Math.floor(len / 2);
102
- const leavePercForLastStage = 0.01;
103
- let ceil;
104
- if (opts.reportProgressFunc) {
105
- ceil = Math.floor(opts.reportProgressFuncTo - (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) * leavePercForLastStage - opts.reportProgressFuncFrom);
106
- }
107
- let currentPercentageDone;
108
- let lastPercentage = 0;
109
- let lineEnding = `\n`;
110
- if (str.includes(`\r\n`)) {
111
- lineEnding = `\r\n`;
112
- } else if (str.includes(`\r`)) {
113
- lineEnding = `\r`;
114
- }
115
- if (len) {
116
- for (let i = 0; i < len; i++) {
117
- if (opts.reportProgressFunc) {
118
- if (len > 1000 && len < 2000) {
119
- if (i === midLen) {
120
- opts.reportProgressFunc(Math.floor((opts.reportProgressFuncTo - opts.reportProgressFuncFrom) / 2));
121
- }
122
- } else if (len >= 2000) {
123
- currentPercentageDone = opts.reportProgressFuncFrom + Math.floor(i / len * (ceil || 1));
124
- if (currentPercentageDone !== lastPercentage) {
125
- lastPercentage = currentPercentageDone;
126
- opts.reportProgressFunc(currentPercentageDone);
127
- }
121
+ const start = Date.now();
122
+ if (!isStr(str)) {
123
+ if (str === undefined) {
124
+ throw new Error("html-crush: [THROW_ID_01] the first input argument is completely missing! It should be given as string.");
128
125
  }
129
- }
130
- cpl++;
131
- if (!doNothing && withinStyleTag && str[i] === "}" && str[i - 1] === "}") {
132
- if (countCharactersPerLine + 1 >= opts.lineLengthLimit) {
133
- finalIndexesToDelete.push(i, i, lineEnding);
134
- countCharactersPerLine = 0;
135
- } else {
136
- stageFrom = i;
137
- stageTo = i;
138
- stageAdd = " ";
126
+ else {
127
+ throw new Error(`html-crush: [THROW_ID_02] the first input argument must be string! It was given as "${typeof str}", equal to:\n${JSON.stringify(str, null, 4)}`);
139
128
  }
140
- }
141
- if (doNothing && typeof doNothing === "number" && i >= doNothing) {
142
- doNothing = undefined;
143
- }
144
- if (scriptStartedAt !== null && str.startsWith("</script", i) && !isLetter(str[i + 8])) {
145
- if ((opts.removeIndentations || opts.removeLineBreaks) && i > 0 && str[~-i] && !str[~-i].trim()) {
146
- for (let y = i; y--;) {
147
- if (str[y] === "\n" || str[y] === "\r" || str[y].trim()) {
148
- if (y + 1 < i) {
149
- finalIndexesToDelete.push(y + 1, i);
150
- }
151
- break;
129
+ }
130
+ if (originalOpts && typeof originalOpts !== "object") {
131
+ 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 ${typeof originalOpts}, equal to ${JSON.stringify(originalOpts, null, 4)}`);
132
+ }
133
+ if (originalOpts &&
134
+ Array.isArray(originalOpts.breakToTheLeftOf) &&
135
+ originalOpts.breakToTheLeftOf.length) {
136
+ for (let z = 0, len = originalOpts.breakToTheLeftOf.length; z < len; z++) {
137
+ if (!isStr(originalOpts.breakToTheLeftOf[z])) {
138
+ throw new TypeError(`html-crush: [THROW_ID_05] the opts.breakToTheLeftOf array contains non-string elements! For example, element at index ${z} is of a type "${typeof originalOpts
139
+ .breakToTheLeftOf[z]}" and is equal to:\n${JSON.stringify(originalOpts.breakToTheLeftOf[z], null, 4)}`);
152
140
  }
153
- }
154
- }
155
- scriptStartedAt = null;
156
- doNothing = false;
157
- i += 8;
158
- continue;
159
- }
160
- if (!doNothing && !withinStyleTag && str.startsWith("<script", i) && !isLetter(str[i + 7])) {
161
- scriptStartedAt = i;
162
- doNothing = true;
163
- let whatToInsert = "";
164
- if ((opts.removeLineBreaks || opts.removeIndentations) && whitespaceStartedAt !== null) {
165
- if (whitespaceStartedAt > 0) {
166
- whatToInsert = lineEnding;
167
- }
168
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToInsert);
169
- }
170
- whitespaceStartedAt = null;
171
- lastLinebreak = null;
172
- }
173
- if (tagNameStartsAt !== null && tagName === null && !/\w/.test(str[i])
174
- ) {
175
- tagName = str.slice(tagNameStartsAt, i);
176
- const idxOnTheRight = right(str, ~-i);
177
- if (typeof idxOnTheRight === "number" && str[idxOnTheRight] === ">" && !str[i].trim() && right(str, i)) {
178
- finalIndexesToDelete.push(i, right(str, i));
179
- } else if (idxOnTheRight && str[idxOnTheRight] === "/" && str[right(str, idxOnTheRight)] === ">") {
180
- if (!str[i].trim() && right(str, i)) {
181
- finalIndexesToDelete.push(i, right(str, i));
182
- }
183
- if (str[idxOnTheRight + 1] !== ">" && right(str, idxOnTheRight + 1)) {
184
- finalIndexesToDelete.push(idxOnTheRight + 1, right(str, idxOnTheRight + 1));
185
- }
186
141
  }
187
- }
188
- if (!doNothing && !withinStyleTag && !withinInlineStyle && str[~-i] === "<" && tagNameStartsAt === null) {
189
- if (/\w/.test(str[i])) {
190
- tagNameStartsAt = i;
191
- } else if (str[right(str, ~-i)] === "/" && /\w/.test(str[right(str, right(str, ~-i))] || "")) {
192
- tagNameStartsAt = right(str, right(str, ~-i));
193
- }
194
- }
195
- if (!doNothing && (withinStyleTag || withinInlineStyle) && styleCommentStartedAt !== null && str[i] === "*" && str[i + 1] === "/") {
196
- [stageFrom, stageTo] = expander({
197
- str,
198
- from: styleCommentStartedAt,
199
- to: i + 2,
200
- ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
201
- ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS
202
- });
203
- styleCommentStartedAt = null;
204
- if (stageFrom != null) {
205
- finalIndexesToDelete.push(stageFrom, stageTo);
206
- } else {
207
- countCharactersPerLine += 1;
208
- i += 1;
209
- }
210
- doNothing = i + 2;
211
- }
212
- if (!doNothing && (withinStyleTag || withinInlineStyle) && styleCommentStartedAt === null && str[i] === "/" && str[i + 1] === "*") {
213
- if (!applicableOpts.removeCSSComments) {
214
- applicableOpts.removeCSSComments = true;
215
- }
216
- if (opts.removeCSSComments) {
217
- styleCommentStartedAt = i;
218
- }
219
- }
220
- if (withinHTMLConditional && str.startsWith("![endif", i + 1)) {
221
- withinHTMLConditional = false;
222
- }
223
- if (!doNothing && !withinStyleTag && !withinInlineStyle && htmlCommentStartedAt !== null) {
224
- let distanceFromHereToCommentEnding;
225
- if (str.startsWith("-->", i)) {
226
- distanceFromHereToCommentEnding = 3;
227
- } else if (str[i] === ">" && str[i - 1] === "]") {
228
- distanceFromHereToCommentEnding = 1;
229
- }
230
- if (distanceFromHereToCommentEnding) {
231
- [stageFrom, stageTo] = expander({
232
- str,
233
- from: htmlCommentStartedAt,
234
- to: i + distanceFromHereToCommentEnding
235
- });
236
- htmlCommentStartedAt = null;
237
- if (stageFrom != null) {
238
- if (opts.lineLengthLimit && cpl - (stageTo - stageFrom) >= opts.lineLengthLimit) {
239
- finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
240
- cpl = -distanceFromHereToCommentEnding;
241
- } else {
242
- finalIndexesToDelete.push(stageFrom, stageTo);
243
- cpl -= stageTo - stageFrom;
142
+ }
143
+ const opts = { ...defaults, ...originalOpts };
144
+ if (typeof opts.removeHTMLComments === "boolean") {
145
+ opts.removeHTMLComments = opts.removeHTMLComments ? 1 : 0;
146
+ }
147
+ let breakToTheLeftOfFirstLetters = "";
148
+ if (Array.isArray(opts.breakToTheLeftOf) && opts.breakToTheLeftOf.length) {
149
+ breakToTheLeftOfFirstLetters = [
150
+ ...new Set(opts.breakToTheLeftOf.map((val) => val[0])),
151
+ ].join("");
152
+ }
153
+ let lastLinebreak = null;
154
+ let whitespaceStartedAt = null;
155
+ let nonWhitespaceCharMet = false;
156
+ let countCharactersPerLine = 0;
157
+ let cpl = 0;
158
+ let withinStyleTag = false;
159
+ let withinHTMLConditional = false;
160
+ let withinInlineStyle = null;
161
+ let styleCommentStartedAt = null;
162
+ let htmlCommentStartedAt = null;
163
+ let scriptStartedAt = null;
164
+ let doNothing;
165
+ let stageFrom = null;
166
+ let stageTo = null;
167
+ let stageAdd = null;
168
+ let tagName = null;
169
+ let tagNameStartsAt = null;
170
+ let leftTagName = null;
171
+ const CHARS_BREAK_ON_THE_RIGHT_OF_THEM = `>};`;
172
+ const CHARS_BREAK_ON_THE_LEFT_OF_THEM = `<`;
173
+ const CHARS_DONT_BREAK_ON_THE_LEFT_OF_THEM = `!`;
174
+ const DELETE_TIGHTLY_IF_ON_LEFT_IS = `>`;
175
+ const DELETE_TIGHTLY_IF_ON_RIGHT_IS = `<`;
176
+ const set = `{},:;<>~+`;
177
+ const DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS = set;
178
+ const DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS = set;
179
+ let beginningOfAFile = true;
180
+ const len = str.length;
181
+ const midLen = Math.floor(len / 2);
182
+ const leavePercForLastStage = 0.01;
183
+ let ceil;
184
+ if (opts.reportProgressFunc) {
185
+ ceil = Math.floor(opts.reportProgressFuncTo -
186
+ (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) *
187
+ leavePercForLastStage -
188
+ opts.reportProgressFuncFrom);
189
+ }
190
+ let currentPercentageDone;
191
+ let lastPercentage = 0;
192
+ let lineEnding = `\n`;
193
+ if (str.includes(`\r\n`)) {
194
+ lineEnding = `\r\n`;
195
+ }
196
+ else if (str.includes(`\r`)) {
197
+ lineEnding = `\r`;
198
+ }
199
+ if (len) {
200
+ for (let i = 0; i < len; i++) {
201
+ if (opts.reportProgressFunc) {
202
+ if (len > 1000 && len < 2000) {
203
+ if (i === midLen) {
204
+ opts.reportProgressFunc(Math.floor((opts.reportProgressFuncTo - opts.reportProgressFuncFrom) / 2));
205
+ }
206
+ }
207
+ else if (len >= 2000) {
208
+ currentPercentageDone =
209
+ opts.reportProgressFuncFrom + Math.floor((i / len) * (ceil || 1));
210
+ if (currentPercentageDone !== lastPercentage) {
211
+ lastPercentage = currentPercentageDone;
212
+ opts.reportProgressFunc(currentPercentageDone);
213
+ }
214
+ }
244
215
  }
245
- } else {
246
- countCharactersPerLine += distanceFromHereToCommentEnding - 1;
247
- i += distanceFromHereToCommentEnding - 1;
248
- }
249
- doNothing = i + distanceFromHereToCommentEnding;
250
- }
251
- }
252
- if (!doNothing && !withinStyleTag && !withinInlineStyle && str.startsWith("<!--", i) && htmlCommentStartedAt === null) {
253
- if (str.startsWith("[if", i + 4)) {
254
- if (!withinHTMLConditional) {
255
- withinHTMLConditional = true;
256
- }
257
- if (opts.removeHTMLComments === 2) {
258
- htmlCommentStartedAt = i;
259
- }
260
- } else if (
261
- opts.removeHTMLComments && (
262
- !withinHTMLConditional || opts.removeHTMLComments === 2)) {
263
- htmlCommentStartedAt = i;
264
- }
265
- if (!applicableOpts.removeHTMLComments) {
266
- applicableOpts.removeHTMLComments = true;
267
- }
268
- }
269
- if (!doNothing && withinStyleTag && styleCommentStartedAt === null && str.startsWith("</style", i) && !isLetter(str[i + 7])) {
270
- withinStyleTag = false;
271
- } else if (!doNothing && !withinStyleTag && styleCommentStartedAt === null && str.startsWith("<style", i) && !isLetter(str[i + 6])) {
272
- withinStyleTag = true;
273
- if ((opts.removeLineBreaks || opts.removeIndentations) && opts.breakToTheLeftOf.includes("<style") && str.startsWith(` type="text/css">`, i + 6) && str[i + 24]) {
274
- finalIndexesToDelete.push(i + 23, i + 23, lineEnding);
275
- }
276
- }
277
- if (!doNothing && !withinInlineStyle && `"'`.includes(str[i]) && str.endsWith("style=", i)) {
278
- withinInlineStyle = i;
279
- }
280
- if (!doNothing && !str[i].trim()) {
281
- if (whitespaceStartedAt === null) {
282
- whitespaceStartedAt = i;
283
- }
284
- } else if (!doNothing && !((withinStyleTag || withinInlineStyle) && styleCommentStartedAt !== null)) {
285
- if (whitespaceStartedAt !== null) {
286
- if (opts.removeLineBreaks) {
287
- countCharactersPerLine += 1;
288
- }
289
- if (beginningOfAFile) {
290
- beginningOfAFile = false;
291
- if (opts.removeIndentations || opts.removeLineBreaks) {
292
- finalIndexesToDelete.push(0, i);
216
+ cpl++;
217
+ if (!doNothing &&
218
+ withinStyleTag &&
219
+ str[i] === "}" &&
220
+ str[i - 1] === "}") {
221
+ if (countCharactersPerLine + 1 >= opts.lineLengthLimit) {
222
+ finalIndexesToDelete.push(i, i, lineEnding);
223
+ countCharactersPerLine = 0;
224
+ }
225
+ else {
226
+ stageFrom = i;
227
+ stageTo = i;
228
+ stageAdd = " ";
229
+ }
293
230
  }
294
- } else {
295
- if (opts.removeIndentations && !opts.removeLineBreaks) {
296
- if (!nonWhitespaceCharMet && lastLinebreak !== null && i > lastLinebreak) {
297
- finalIndexesToDelete.push(lastLinebreak + 1, i);
298
- } else if (whitespaceStartedAt + 1 < i) {
299
- if (
300
- str.endsWith("]>", whitespaceStartedAt) ||
301
- str.endsWith("-->", whitespaceStartedAt) ||
302
- str.startsWith("<![", i) ||
303
- str.startsWith("<!--<![", i)) {
304
- finalIndexesToDelete.push(whitespaceStartedAt, i);
305
- } else if (str[whitespaceStartedAt] === " ") {
306
- finalIndexesToDelete.push(whitespaceStartedAt + 1, i);
307
- } else if (str[~-i] === " ") {
308
- finalIndexesToDelete.push(whitespaceStartedAt, ~-i);
309
- } else {
310
- finalIndexesToDelete.push(whitespaceStartedAt, i, " ");
311
- }
312
- }
231
+ if (doNothing && typeof doNothing === "number" && i >= doNothing) {
232
+ doNothing = undefined;
313
233
  }
314
- if (opts.removeLineBreaks || withinInlineStyle) {
315
- if (breakToTheLeftOfFirstLetters.includes(str[i]) && matchRightIncl(str, i, opts.breakToTheLeftOf)) {
316
- if (
317
- !(`\r\n`.includes(str[~-i]) && whitespaceStartedAt === ~-i) &&
318
- !(str[~-i] === "\n" && str[i - 2] === "\r" && whitespaceStartedAt === i - 2)) {
319
- finalIndexesToDelete.push(whitespaceStartedAt, i, lineEnding);
320
- }
321
- stageFrom = null;
322
- stageTo = null;
323
- stageAdd = null;
324
- whitespaceStartedAt = null;
325
- countCharactersPerLine = 1;
234
+ if (scriptStartedAt !== null &&
235
+ str.startsWith("</script", i) &&
236
+ !isLetter(str[i + 8])) {
237
+ if ((opts.removeIndentations || opts.removeLineBreaks) &&
238
+ i > 0 &&
239
+ str[~-i] &&
240
+ !str[~-i].trim()) {
241
+ for (let y = i; y--;) {
242
+ if (str[y] === "\n" || str[y] === "\r" || str[y].trim()) {
243
+ if (y + 1 < i) {
244
+ finalIndexesToDelete.push(y + 1, i);
245
+ }
246
+ break;
247
+ }
248
+ }
249
+ }
250
+ scriptStartedAt = null;
251
+ doNothing = false;
252
+ i += 8;
326
253
  continue;
327
- }
328
- let whatToAdd = " ";
329
- if (
330
- str[i] === "<" && matchRight(str, i, opts.mindTheInlineTags, {
331
- cb: nextChar => !nextChar || !/\w/.test(nextChar)
332
- })
333
- ) ; 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[left(str, i)]) || str[right(str, i)] === "<") || str[i] === "/" && str[right(str, i)] === ">") {
334
- whatToAdd = "";
335
- if (str[i] === "/" && str[i + 1] === ">" && right(str, i) && right(str, i) > i + 1) {
336
- finalIndexesToDelete.push(i + 1, right(str, i));
337
- countCharactersPerLine -= right(str, i) - i + 1;
338
- }
339
- }
340
- if (withinStyleTag && str[i] === "}" && whitespaceStartedAt && str[whitespaceStartedAt - 1] === "}") {
341
- whatToAdd = " ";
342
- }
343
- if (whatToAdd && whatToAdd.length) {
344
- countCharactersPerLine += 1;
345
- }
346
- if (!opts.lineLengthLimit) {
347
- if (!(i === whitespaceStartedAt + 1 &&
348
- whatToAdd === " ")) {
349
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
350
- }
351
- } else {
352
- if (countCharactersPerLine >= opts.lineLengthLimit || !str[i + 1] || str[i] === ">" || str[i] === "/" && str[i + 1] === ">") {
353
- 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])) {
354
- whatToAdd = lineEnding;
254
+ }
255
+ if (!doNothing &&
256
+ !withinStyleTag &&
257
+ str.startsWith("<script", i) &&
258
+ !isLetter(str[i + 7])) {
259
+ scriptStartedAt = i;
260
+ doNothing = true;
261
+ let whatToInsert = "";
262
+ if ((opts.removeLineBreaks || opts.removeIndentations) &&
263
+ whitespaceStartedAt !== null) {
264
+ if (whitespaceStartedAt > 0) {
265
+ whatToInsert = lineEnding;
266
+ }
267
+ finalIndexesToDelete.push(whitespaceStartedAt, i, whatToInsert);
268
+ }
269
+ whitespaceStartedAt = null;
270
+ lastLinebreak = null;
271
+ }
272
+ if (tagNameStartsAt !== null &&
273
+ tagName === null &&
274
+ !/\w/.test(str[i])
275
+ ) {
276
+ tagName = str.slice(tagNameStartsAt, i);
277
+ const idxOnTheRight = right(str, ~-i);
278
+ if (typeof idxOnTheRight === "number" &&
279
+ str[idxOnTheRight] === ">" &&
280
+ !str[i].trim() &&
281
+ right(str, i)) {
282
+ finalIndexesToDelete.push(i, right(str, i));
283
+ }
284
+ else if (idxOnTheRight &&
285
+ str[idxOnTheRight] === "/" &&
286
+ str[right(str, idxOnTheRight)] === ">") {
287
+ if (!str[i].trim() && right(str, i)) {
288
+ finalIndexesToDelete.push(i, right(str, i));
289
+ }
290
+ if (str[idxOnTheRight + 1] !== ">" && right(str, idxOnTheRight + 1)) {
291
+ finalIndexesToDelete.push(idxOnTheRight + 1, right(str, idxOnTheRight + 1));
292
+ }
293
+ }
294
+ }
295
+ if (!doNothing &&
296
+ !withinStyleTag &&
297
+ !withinInlineStyle &&
298
+ str[~-i] === "<" &&
299
+ tagNameStartsAt === null) {
300
+ if (/\w/.test(str[i])) {
301
+ tagNameStartsAt = i;
302
+ }
303
+ else if (str[right(str, ~-i)] === "/" &&
304
+ /\w/.test(str[right(str, right(str, ~-i))] || "")) {
305
+ tagNameStartsAt = right(str, right(str, ~-i));
306
+ }
307
+ }
308
+ if (!doNothing &&
309
+ (withinStyleTag || withinInlineStyle) &&
310
+ styleCommentStartedAt !== null &&
311
+ str[i] === "*" &&
312
+ str[i + 1] === "/") {
313
+ [stageFrom, stageTo] = expander({
314
+ str,
315
+ from: styleCommentStartedAt,
316
+ to: i + 2,
317
+ ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
318
+ ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS ,
319
+ });
320
+ styleCommentStartedAt = null;
321
+ if (stageFrom != null) {
322
+ finalIndexesToDelete.push(stageFrom, stageTo);
323
+ }
324
+ else {
325
+ countCharactersPerLine += 1;
326
+ i += 1;
327
+ }
328
+ doNothing = i + 2;
329
+ }
330
+ if (!doNothing &&
331
+ (withinStyleTag || withinInlineStyle) &&
332
+ styleCommentStartedAt === null &&
333
+ str[i] === "/" &&
334
+ str[i + 1] === "*") {
335
+ if (!applicableOpts.removeCSSComments) {
336
+ applicableOpts.removeCSSComments = true;
337
+ }
338
+ if (opts.removeCSSComments) {
339
+ styleCommentStartedAt = i;
340
+ }
341
+ }
342
+ if (withinHTMLConditional && str.startsWith("![endif", i + 1)) {
343
+ withinHTMLConditional = false;
344
+ }
345
+ if (!doNothing &&
346
+ !withinStyleTag &&
347
+ !withinInlineStyle &&
348
+ htmlCommentStartedAt !== null) {
349
+ let distanceFromHereToCommentEnding;
350
+ if (str.startsWith("-->", i)) {
351
+ distanceFromHereToCommentEnding = 3;
352
+ }
353
+ else if (str[i] === ">" && str[i - 1] === "]") {
354
+ distanceFromHereToCommentEnding = 1;
355
+ }
356
+ if (distanceFromHereToCommentEnding) {
357
+ [stageFrom, stageTo] = expander({
358
+ str,
359
+ from: htmlCommentStartedAt,
360
+ to: i + distanceFromHereToCommentEnding,
361
+ });
362
+ htmlCommentStartedAt = null;
363
+ if (stageFrom != null) {
364
+ if (opts.lineLengthLimit &&
365
+ cpl - (stageTo - stageFrom) >= opts.lineLengthLimit) {
366
+ finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
367
+ cpl = -distanceFromHereToCommentEnding;
368
+ }
369
+ else {
370
+ finalIndexesToDelete.push(stageFrom, stageTo);
371
+ cpl -= stageTo - stageFrom;
372
+ }
373
+ }
374
+ else {
375
+ countCharactersPerLine += distanceFromHereToCommentEnding - 1;
376
+ i += distanceFromHereToCommentEnding - 1;
377
+ }
378
+ doNothing = i + distanceFromHereToCommentEnding;
379
+ }
380
+ }
381
+ if (!doNothing &&
382
+ !withinStyleTag &&
383
+ !withinInlineStyle &&
384
+ str.startsWith("<!--", i) &&
385
+ htmlCommentStartedAt === null) {
386
+ if (str.startsWith("[if", i + 4)) {
387
+ if (!withinHTMLConditional) {
388
+ withinHTMLConditional = true;
389
+ }
390
+ if (opts.removeHTMLComments === 2) {
391
+ htmlCommentStartedAt = i;
392
+ }
393
+ }
394
+ else if (
395
+ opts.removeHTMLComments &&
396
+ (!withinHTMLConditional || opts.removeHTMLComments === 2)) {
397
+ htmlCommentStartedAt = i;
398
+ }
399
+ if (!applicableOpts.removeHTMLComments) {
400
+ applicableOpts.removeHTMLComments = true;
401
+ }
402
+ }
403
+ if (!doNothing &&
404
+ withinStyleTag &&
405
+ styleCommentStartedAt === null &&
406
+ str.startsWith("</style", i) &&
407
+ !isLetter(str[i + 7])) {
408
+ withinStyleTag = false;
409
+ }
410
+ else if (!doNothing &&
411
+ !withinStyleTag &&
412
+ styleCommentStartedAt === null &&
413
+ str.startsWith("<style", i) &&
414
+ !isLetter(str[i + 6])) {
415
+ withinStyleTag = true;
416
+ if ((opts.removeLineBreaks || opts.removeIndentations) &&
417
+ opts.breakToTheLeftOf.includes("<style") &&
418
+ str.startsWith(` type="text/css">`, i + 6) &&
419
+ str[i + 24]) {
420
+ finalIndexesToDelete.push(i + 23, i + 23, lineEnding);
421
+ }
422
+ }
423
+ if (!doNothing &&
424
+ !withinInlineStyle &&
425
+ `"'`.includes(str[i]) &&
426
+ str.endsWith("style=", i)) {
427
+ withinInlineStyle = i;
428
+ }
429
+ if (!doNothing && !str[i].trim()) {
430
+ if (whitespaceStartedAt === null) {
431
+ whitespaceStartedAt = i;
432
+ }
433
+ }
434
+ else if (!doNothing &&
435
+ !((withinStyleTag || withinInlineStyle) &&
436
+ styleCommentStartedAt !== null)) {
437
+ if (whitespaceStartedAt !== null) {
438
+ if (opts.removeLineBreaks) {
439
+ countCharactersPerLine += 1;
440
+ }
441
+ if (beginningOfAFile) {
442
+ beginningOfAFile = false;
443
+ if (opts.removeIndentations || opts.removeLineBreaks) {
444
+ finalIndexesToDelete.push(0, i);
445
+ }
446
+ }
447
+ else {
448
+ if (opts.removeIndentations && !opts.removeLineBreaks) {
449
+ if (!nonWhitespaceCharMet &&
450
+ lastLinebreak !== null &&
451
+ i > lastLinebreak) {
452
+ finalIndexesToDelete.push(lastLinebreak + 1, i);
453
+ }
454
+ else if (whitespaceStartedAt + 1 < i) {
455
+ if (
456
+ str.endsWith("]>", whitespaceStartedAt) ||
457
+ str.endsWith("-->", whitespaceStartedAt) ||
458
+ str.startsWith("<![", i) ||
459
+ str.startsWith("<!--<![", i)) {
460
+ finalIndexesToDelete.push(whitespaceStartedAt, i);
461
+ }
462
+ else if (str[whitespaceStartedAt] === " ") {
463
+ finalIndexesToDelete.push(whitespaceStartedAt + 1, i);
464
+ }
465
+ else if (str[~-i] === " ") {
466
+ finalIndexesToDelete.push(whitespaceStartedAt, ~-i);
467
+ }
468
+ else {
469
+ finalIndexesToDelete.push(whitespaceStartedAt, i, " ");
470
+ }
471
+ }
472
+ }
473
+ if (opts.removeLineBreaks || withinInlineStyle) {
474
+ if (breakToTheLeftOfFirstLetters.includes(str[i]) &&
475
+ matchRightIncl(str, i, opts.breakToTheLeftOf)) {
476
+ if (
477
+ !(`\r\n`.includes(str[~-i]) && whitespaceStartedAt === ~-i) &&
478
+ !(str[~-i] === "\n" &&
479
+ str[i - 2] === "\r" &&
480
+ whitespaceStartedAt === i - 2)) {
481
+ finalIndexesToDelete.push(whitespaceStartedAt, i, lineEnding);
482
+ }
483
+ stageFrom = null;
484
+ stageTo = null;
485
+ stageAdd = null;
486
+ whitespaceStartedAt = null;
487
+ countCharactersPerLine = 1;
488
+ continue;
489
+ }
490
+ let whatToAdd = " ";
491
+ if (
492
+ str[i] === "<" &&
493
+ matchRight(str, i, opts.mindTheInlineTags, {
494
+ cb: (nextChar) => !nextChar || !/\w/.test(nextChar),
495
+ })
496
+ ) ;
497
+ else if ((str[~-whitespaceStartedAt] &&
498
+ DELETE_TIGHTLY_IF_ON_LEFT_IS.includes(str[~-whitespaceStartedAt]) &&
499
+ DELETE_TIGHTLY_IF_ON_RIGHT_IS.includes(str[i])) ||
500
+ ((withinStyleTag || withinInlineStyle) &&
501
+ styleCommentStartedAt === null &&
502
+ (DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS.includes(str[~-whitespaceStartedAt]) ||
503
+ DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS.includes(str[i]))) ||
504
+ (str.startsWith("!important", i) && !withinHTMLConditional) ||
505
+ (withinInlineStyle &&
506
+ (str[~-whitespaceStartedAt] === "'" ||
507
+ str[~-whitespaceStartedAt] === '"')) ||
508
+ (str[~-whitespaceStartedAt] === "}" &&
509
+ str.startsWith("</style", i)) ||
510
+ (str[i] === ">" &&
511
+ (`'"`.includes(str[left(str, i)]) ||
512
+ str[right(str, i)] === "<")) ||
513
+ (str[i] === "/" && str[right(str, i)] === ">")) {
514
+ whatToAdd = "";
515
+ if (str[i] === "/" &&
516
+ str[i + 1] === ">" &&
517
+ right(str, i) &&
518
+ right(str, i) > i + 1) {
519
+ finalIndexesToDelete.push(i + 1, right(str, i));
520
+ countCharactersPerLine -= right(str, i) - i + 1;
521
+ }
522
+ }
523
+ if (withinStyleTag &&
524
+ str[i] === "}" &&
525
+ whitespaceStartedAt &&
526
+ str[whitespaceStartedAt - 1] === "}") {
527
+ whatToAdd = " ";
528
+ }
529
+ if (whatToAdd && whatToAdd.length) {
530
+ countCharactersPerLine += 1;
531
+ }
532
+ if (!opts.lineLengthLimit) {
533
+ if (!(i === whitespaceStartedAt + 1 &&
534
+ whatToAdd === " ")) {
535
+ finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
536
+ }
537
+ }
538
+ else {
539
+ if (countCharactersPerLine >= opts.lineLengthLimit ||
540
+ !str[i + 1] ||
541
+ str[i] === ">" ||
542
+ (str[i] === "/" && str[i + 1] === ">")) {
543
+ if (countCharactersPerLine > opts.lineLengthLimit ||
544
+ (countCharactersPerLine === opts.lineLengthLimit &&
545
+ str[i + 1] &&
546
+ str[i + 1].trim() &&
547
+ !CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) &&
548
+ !CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i + 1]))) {
549
+ whatToAdd = lineEnding;
550
+ countCharactersPerLine = 1;
551
+ }
552
+ if (countCharactersPerLine > opts.lineLengthLimit ||
553
+ !(whatToAdd === " " && i === whitespaceStartedAt + 1)) {
554
+ finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
555
+ lastLinebreak = null;
556
+ }
557
+ stageFrom = null;
558
+ stageTo = null;
559
+ stageAdd = null;
560
+ }
561
+ else if (stageFrom === null ||
562
+ whitespaceStartedAt < stageFrom) {
563
+ stageFrom = whitespaceStartedAt;
564
+ stageTo = i;
565
+ stageAdd = whatToAdd;
566
+ }
567
+ }
568
+ }
569
+ }
570
+ whitespaceStartedAt = null;
571
+ if (!nonWhitespaceCharMet) {
572
+ nonWhitespaceCharMet = true;
573
+ }
574
+ }
575
+ else {
576
+ if (beginningOfAFile) {
577
+ beginningOfAFile = false;
578
+ }
579
+ if (opts.removeLineBreaks) {
580
+ countCharactersPerLine += 1;
581
+ }
582
+ }
583
+ if (!nonWhitespaceCharMet) {
584
+ nonWhitespaceCharMet = true;
585
+ }
586
+ }
587
+ if (!doNothing &&
588
+ !beginningOfAFile &&
589
+ i !== 0 &&
590
+ opts.removeLineBreaks &&
591
+ (opts.lineLengthLimit || breakToTheLeftOfFirstLetters) &&
592
+ !str.startsWith("</a", i)) {
593
+ if (breakToTheLeftOfFirstLetters &&
594
+ matchRightIncl(str, i, opts.breakToTheLeftOf) &&
595
+ str.slice(0, i).trim() &&
596
+ (!str.startsWith("<![endif]", i) || !matchLeft(str, i, "<!--"))) {
597
+ finalIndexesToDelete.push(i, i, lineEnding);
598
+ stageFrom = null;
599
+ stageTo = null;
600
+ stageAdd = null;
355
601
  countCharactersPerLine = 1;
356
- }
357
- if (countCharactersPerLine > opts.lineLengthLimit || !(whatToAdd === " " && i === whitespaceStartedAt + 1)) {
358
- finalIndexesToDelete.push(whitespaceStartedAt, i, whatToAdd);
359
- lastLinebreak = null;
360
- }
361
- stageFrom = null;
362
- stageTo = null;
363
- stageAdd = null;
364
- } else if (stageFrom === null || whitespaceStartedAt < stageFrom) {
365
- stageFrom = whitespaceStartedAt;
366
- stageTo = i;
367
- stageAdd = whatToAdd;
368
- }
369
- }
602
+ continue;
603
+ }
604
+ else if (opts.lineLengthLimit &&
605
+ countCharactersPerLine <= opts.lineLengthLimit) {
606
+ if (!str[i + 1] ||
607
+ (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) &&
608
+ !CHARS_DONT_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i])) ||
609
+ CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) ||
610
+ !str[i].trim()) {
611
+ if (stageFrom !== null &&
612
+ stageTo !== null &&
613
+ (stageFrom !== stageTo || (stageAdd && stageAdd.length))) {
614
+ let whatToAdd = stageAdd;
615
+ if (str[i].trim() &&
616
+ str[i + 1] &&
617
+ str[i + 1].trim() &&
618
+ countCharactersPerLine + (stageAdd ? stageAdd.length : 0) >
619
+ opts.lineLengthLimit) {
620
+ whatToAdd = lineEnding;
621
+ }
622
+ if (countCharactersPerLine + (whatToAdd ? whatToAdd.length : 0) >
623
+ opts.lineLengthLimit ||
624
+ !(whatToAdd === " " &&
625
+ stageTo === stageFrom + 1 &&
626
+ str[stageFrom] === " ")) {
627
+ if (!(str[~-stageFrom] === "}" && str[stageTo] === "{")) {
628
+ finalIndexesToDelete.push(stageFrom, stageTo, whatToAdd);
629
+ lastLinebreak = null;
630
+ }
631
+ }
632
+ }
633
+ if (str[i].trim() &&
634
+ (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) ||
635
+ (str[~-i] &&
636
+ CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[~-i]))) &&
637
+ isStr(leftTagName) &&
638
+ (!tagName || !opts.mindTheInlineTags.includes(tagName)) &&
639
+ !(str[i] === "<" &&
640
+ matchRight(str, i, opts.mindTheInlineTags, {
641
+ cb: (nextChar) => !nextChar || !/\w/.test(nextChar),
642
+ })) &&
643
+ !(str[i] === "<" &&
644
+ matchRight(str, i, opts.mindTheInlineTags, {
645
+ trimCharsBeforeMatching: "/",
646
+ cb: (nextChar) => !nextChar || !/\w/.test(nextChar),
647
+ }))) {
648
+ stageFrom = i;
649
+ stageTo = i;
650
+ stageAdd = null;
651
+ }
652
+ else if (styleCommentStartedAt === null &&
653
+ stageFrom !== null &&
654
+ (withinInlineStyle ||
655
+ !opts.mindTheInlineTags ||
656
+ !Array.isArray(opts.mindTheInlineTags) ||
657
+ (Array.isArray(opts.mindTheInlineTags.length) &&
658
+ !opts.mindTheInlineTags.length) ||
659
+ !isStr(tagName) ||
660
+ (Array.isArray(opts.mindTheInlineTags) &&
661
+ opts.mindTheInlineTags.length &&
662
+ isStr(tagName) &&
663
+ !opts.mindTheInlineTags.includes(tagName))) &&
664
+ !(str[i] === "<" &&
665
+ matchRight(str, i, opts.mindTheInlineTags, {
666
+ trimCharsBeforeMatching: "/",
667
+ cb: (nextChar) => !nextChar || !/\w/.test(nextChar),
668
+ }))) {
669
+ stageFrom = null;
670
+ stageTo = null;
671
+ stageAdd = null;
672
+ }
673
+ }
674
+ }
675
+ else if (opts.lineLengthLimit) {
676
+ if (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) &&
677
+ !(str[i] === "<" &&
678
+ matchRight(str, i, opts.mindTheInlineTags, {
679
+ trimCharsBeforeMatching: "/",
680
+ cb: (nextChar) => !nextChar || !/\w/.test(nextChar),
681
+ }))) {
682
+ if (stageFrom !== null &&
683
+ stageTo !== null &&
684
+ (stageFrom !== stageTo || (stageAdd && stageAdd.length))) {
685
+ const whatToAddLength = stageAdd && stageAdd.length ? stageAdd.length : 0;
686
+ if (countCharactersPerLine -
687
+ (stageTo - stageFrom - whatToAddLength) -
688
+ 1 >
689
+ opts.lineLengthLimit) ;
690
+ else {
691
+ finalIndexesToDelete.push(stageFrom, stageTo, stageAdd);
692
+ if (countCharactersPerLine -
693
+ (stageTo - stageFrom - whatToAddLength) -
694
+ 1 ===
695
+ opts.lineLengthLimit) {
696
+ finalIndexesToDelete.push(i, i, lineEnding);
697
+ countCharactersPerLine = 0;
698
+ }
699
+ stageFrom = null;
700
+ stageTo = null;
701
+ stageAdd = null;
702
+ }
703
+ }
704
+ else {
705
+ finalIndexesToDelete.push(i, i, lineEnding);
706
+ countCharactersPerLine = 0;
707
+ }
708
+ }
709
+ else if (str[i + 1] &&
710
+ CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) &&
711
+ isStr(tagName) &&
712
+ Array.isArray(opts.mindTheInlineTags) &&
713
+ opts.mindTheInlineTags.length &&
714
+ !opts.mindTheInlineTags.includes(tagName)) {
715
+ if (stageFrom !== null &&
716
+ stageTo !== null &&
717
+ (stageFrom !== stageTo || (stageAdd && stageAdd.length))) ;
718
+ else {
719
+ finalIndexesToDelete.push(i + 1, i + 1, lineEnding);
720
+ countCharactersPerLine = 0;
721
+ }
722
+ }
723
+ else if (!str[i].trim()) ;
724
+ else if (!str[i + 1]) {
725
+ if (stageFrom !== null &&
726
+ stageTo !== null &&
727
+ (stageFrom !== stageTo || (stageAdd && stageAdd.length))) {
728
+ finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
729
+ }
730
+ }
731
+ }
370
732
  }
371
- }
372
- whitespaceStartedAt = null;
373
- if (!nonWhitespaceCharMet) {
374
- nonWhitespaceCharMet = true;
375
- }
376
- } else {
377
- if (beginningOfAFile) {
378
- beginningOfAFile = false;
379
- }
380
- if (opts.removeLineBreaks) {
381
- countCharactersPerLine += 1;
382
- }
383
- }
384
- if (!nonWhitespaceCharMet) {
385
- nonWhitespaceCharMet = true;
386
- }
387
- }
388
- if (!doNothing && !beginningOfAFile && i !== 0 && opts.removeLineBreaks && (opts.lineLengthLimit || breakToTheLeftOfFirstLetters) && !str.startsWith("</a", i)) {
389
- if (breakToTheLeftOfFirstLetters && matchRightIncl(str, i, opts.breakToTheLeftOf) && str.slice(0, i).trim() && (!str.startsWith("<![endif]", i) || !matchLeft(str, i, "<!--"))) {
390
- finalIndexesToDelete.push(i, i, lineEnding);
391
- stageFrom = null;
392
- stageTo = null;
393
- stageAdd = null;
394
- countCharactersPerLine = 1;
395
- continue;
396
- } else if (opts.lineLengthLimit && countCharactersPerLine <= opts.lineLengthLimit) {
397
- 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()) {
398
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
399
- let whatToAdd = stageAdd;
400
- if (str[i].trim() && str[i + 1] && str[i + 1].trim() && countCharactersPerLine + (stageAdd ? stageAdd.length : 0) > opts.lineLengthLimit) {
401
- whatToAdd = lineEnding;
402
- }
403
- if (countCharactersPerLine + (whatToAdd ? whatToAdd.length : 0) > opts.lineLengthLimit || !(whatToAdd === " " && stageTo === stageFrom + 1 && str[stageFrom] === " ")) {
404
- if (!(str[~-stageFrom] === "}" && str[stageTo] === "{")) {
405
- finalIndexesToDelete.push(stageFrom, stageTo, whatToAdd);
406
- lastLinebreak = null;
407
- }
408
- }
733
+ if (!doNothing &&
734
+ !beginningOfAFile &&
735
+ opts.removeLineBreaks &&
736
+ opts.lineLengthLimit &&
737
+ countCharactersPerLine >= opts.lineLengthLimit &&
738
+ stageFrom !== null &&
739
+ stageTo !== null &&
740
+ !CHARS_BREAK_ON_THE_RIGHT_OF_THEM.includes(str[i]) &&
741
+ !CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) &&
742
+ !"/".includes(str[i])) {
743
+ if (!(countCharactersPerLine === opts.lineLengthLimit &&
744
+ str[i + 1] &&
745
+ !str[i + 1].trim())) {
746
+ let whatToAdd = lineEnding;
747
+ if (str[i + 1] &&
748
+ !str[i + 1].trim() &&
749
+ countCharactersPerLine === opts.lineLengthLimit) {
750
+ whatToAdd = stageAdd;
751
+ }
752
+ if (whatToAdd === lineEnding &&
753
+ !str[~-stageFrom].trim() &&
754
+ left(str, stageFrom)) {
755
+ stageFrom = left(str, stageFrom) + 1;
756
+ }
757
+ finalIndexesToDelete.push(stageFrom, stageTo, whatToAdd);
758
+ countCharactersPerLine = i - stageTo;
759
+ if (str[i].length) {
760
+ countCharactersPerLine += 1;
761
+ }
762
+ stageFrom = null;
763
+ stageTo = null;
764
+ stageAdd = null;
765
+ }
766
+ }
767
+ if ((!doNothing && str[i] === "\n") ||
768
+ (str[i] === "\r" &&
769
+ (!str[i + 1] || (str[i + 1] && str[i + 1] !== "\n")))) {
770
+ lastLinebreak = i;
771
+ if (nonWhitespaceCharMet) {
772
+ nonWhitespaceCharMet = false;
773
+ }
774
+ if (!opts.removeLineBreaks &&
775
+ whitespaceStartedAt !== null &&
776
+ whitespaceStartedAt < i &&
777
+ str[i + 1] &&
778
+ str[i + 1] !== "\r" &&
779
+ str[i + 1] !== "\n") {
780
+ finalIndexesToDelete.push(whitespaceStartedAt, i);
781
+ }
409
782
  }
410
- 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] === "<" && matchRight(str, i, opts.mindTheInlineTags, {
411
- cb: nextChar => !nextChar || !/\w/.test(nextChar)
412
- })) && !(str[i] === "<" && matchRight(str, i, opts.mindTheInlineTags, {
413
- trimCharsBeforeMatching: "/",
414
- cb: nextChar => !nextChar || !/\w/.test(nextChar)
415
- }))) {
416
- stageFrom = i;
417
- stageTo = i;
418
- stageAdd = null;
419
- } 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] === "<" && matchRight(str, i, opts.mindTheInlineTags, {
420
- trimCharsBeforeMatching: "/",
421
- cb: nextChar => !nextChar || !/\w/.test(nextChar)
422
- }))) {
423
- stageFrom = null;
424
- stageTo = null;
425
- stageAdd = null;
783
+ if (!str[i + 1]) {
784
+ if (withinStyleTag && styleCommentStartedAt !== null) {
785
+ finalIndexesToDelete.push(...expander({
786
+ str,
787
+ from: styleCommentStartedAt,
788
+ to: i,
789
+ ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
790
+ ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS ,
791
+ }));
792
+ }
793
+ else if (whitespaceStartedAt && str[i] !== "\n" && str[i] !== "\r") {
794
+ finalIndexesToDelete.push(whitespaceStartedAt, i + 1);
795
+ }
796
+ else if (whitespaceStartedAt &&
797
+ ((str[i] === "\r" && str[i + 1] === "\n") ||
798
+ (str[i] === "\n" && str[i - 1] !== "\r"))) {
799
+ finalIndexesToDelete.push(whitespaceStartedAt, i);
800
+ }
426
801
  }
427
- }
428
- } else if (opts.lineLengthLimit) {
429
- if (CHARS_BREAK_ON_THE_LEFT_OF_THEM.includes(str[i]) && !(str[i] === "<" && matchRight(str, i, opts.mindTheInlineTags, {
430
- trimCharsBeforeMatching: "/",
431
- cb: nextChar => !nextChar || !/\w/.test(nextChar)
432
- }))) {
433
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
434
- const whatToAddLength = stageAdd && stageAdd.length ? stageAdd.length : 0;
435
- if (countCharactersPerLine - (stageTo - stageFrom - whatToAddLength) - 1 > opts.lineLengthLimit) ; else {
436
- finalIndexesToDelete.push(stageFrom, stageTo, stageAdd);
437
- if (countCharactersPerLine - (stageTo - stageFrom - whatToAddLength) - 1 === opts.lineLengthLimit) {
438
- finalIndexesToDelete.push(i, i, lineEnding);
439
- countCharactersPerLine = 0;
440
- }
441
- stageFrom = null;
442
- stageTo = null;
443
- stageAdd = null;
444
- }
445
- } else {
446
- finalIndexesToDelete.push(i, i, lineEnding);
447
- countCharactersPerLine = 0;
802
+ if (!doNothing &&
803
+ withinInlineStyle &&
804
+ withinInlineStyle < i &&
805
+ str[withinInlineStyle] === str[i]) {
806
+ withinInlineStyle = null;
448
807
  }
449
- } 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)) {
450
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) ; else {
451
- finalIndexesToDelete.push(i + 1, i + 1, lineEnding);
452
- countCharactersPerLine = 0;
808
+ if (!doNothing &&
809
+ !withinStyleTag &&
810
+ str.startsWith("<pre", i) &&
811
+ !isLetter(str[i + 4])) {
812
+ const locationOfClosingPre = str.indexOf("</pre", i + 5);
813
+ if (locationOfClosingPre > 0) {
814
+ doNothing = locationOfClosingPre;
815
+ }
453
816
  }
454
- } else if (!str[i].trim()) ; else if (!str[i + 1]) {
455
- if (stageFrom !== null && stageTo !== null && (stageFrom !== stageTo || stageAdd && stageAdd.length)) {
456
- finalIndexesToDelete.push(stageFrom, stageTo, lineEnding);
817
+ if (!doNothing &&
818
+ !withinStyleTag &&
819
+ str.startsWith("<code", i) &&
820
+ !isLetter(str[i + 5])) {
821
+ const locationOfClosingCode = str.indexOf("</code", i + 5);
822
+ if (locationOfClosingCode > 0) {
823
+ doNothing = locationOfClosingCode;
824
+ }
825
+ }
826
+ if (!doNothing && str.startsWith("<![CDATA[", i)) {
827
+ const locationOfClosingCData = str.indexOf("]]>", i + 9);
828
+ if (locationOfClosingCData > 0) {
829
+ doNothing = locationOfClosingCData;
830
+ }
831
+ }
832
+ if (!doNothing &&
833
+ !withinStyleTag &&
834
+ !withinInlineStyle &&
835
+ tagNameStartsAt !== null &&
836
+ str[i] === ">") {
837
+ if (str[right(str, i)] === "<") {
838
+ leftTagName = tagName;
839
+ }
840
+ tagNameStartsAt = null;
841
+ tagName = null;
842
+ }
843
+ if (str[i] === "<" && leftTagName !== null) {
844
+ leftTagName = null;
845
+ }
846
+ if (withinStyleTag &&
847
+ str[i] === "{" &&
848
+ str[i + 1] === "{" &&
849
+ str.indexOf("}}") !== -1) {
850
+ doNothing = str.indexOf("}}") + 2;
457
851
  }
458
- }
459
- }
460
- }
461
- 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])) {
462
- if (!(countCharactersPerLine === opts.lineLengthLimit && str[i + 1] && !str[i + 1].trim())) {
463
- let whatToAdd = lineEnding;
464
- if (str[i + 1] && !str[i + 1].trim() && countCharactersPerLine === opts.lineLengthLimit) {
465
- whatToAdd = stageAdd;
466
- }
467
- if (whatToAdd === lineEnding && !str[~-stageFrom].trim() && left(str, stageFrom)) {
468
- stageFrom = left(str, stageFrom) + 1;
469
- }
470
- finalIndexesToDelete.push(stageFrom, stageTo, whatToAdd);
471
- countCharactersPerLine = i - stageTo;
472
- if (str[i].length) {
473
- countCharactersPerLine += 1;
474
- }
475
- stageFrom = null;
476
- stageTo = null;
477
- stageAdd = null;
478
- }
479
- }
480
- if (!doNothing && str[i] === "\n" || str[i] === "\r" && (!str[i + 1] || str[i + 1] && str[i + 1] !== "\n")) {
481
- lastLinebreak = i;
482
- if (nonWhitespaceCharMet) {
483
- nonWhitespaceCharMet = false;
484
- }
485
- if (!opts.removeLineBreaks && whitespaceStartedAt !== null && whitespaceStartedAt < i && str[i + 1] && str[i + 1] !== "\r" && str[i + 1] !== "\n") {
486
- finalIndexesToDelete.push(whitespaceStartedAt, i);
487
- }
488
- }
489
- if (!str[i + 1]) {
490
- if (withinStyleTag && styleCommentStartedAt !== null) {
491
- finalIndexesToDelete.push(...expander({
492
- str,
493
- from: styleCommentStartedAt,
494
- to: i,
495
- ifLeftSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_LEFT_IS ,
496
- ifRightSideIncludesThisThenCropTightly: DELETE_IN_STYLE_TIGHTLY_IF_ON_RIGHT_IS
497
- }));
498
- } else if (whitespaceStartedAt && str[i] !== "\n" && str[i] !== "\r") {
499
- finalIndexesToDelete.push(whitespaceStartedAt, i + 1);
500
- } else if (whitespaceStartedAt && (str[i] === "\r" && str[i + 1] === "\n" || str[i] === "\n" && str[i - 1] !== "\r")) {
501
- finalIndexesToDelete.push(whitespaceStartedAt, i);
502
- }
503
- }
504
- if (!doNothing && withinInlineStyle && withinInlineStyle < i && str[withinInlineStyle] === str[i]) {
505
- withinInlineStyle = null;
506
- }
507
- if (!doNothing && !withinStyleTag && str.startsWith("<pre", i) && !isLetter(str[i + 4])) {
508
- const locationOfClosingPre = str.indexOf("</pre", i + 5);
509
- if (locationOfClosingPre > 0) {
510
- doNothing = locationOfClosingPre;
511
- }
512
- }
513
- if (!doNothing && !withinStyleTag && str.startsWith("<code", i) && !isLetter(str[i + 5])) {
514
- const locationOfClosingCode = str.indexOf("</code", i + 5);
515
- if (locationOfClosingCode > 0) {
516
- doNothing = locationOfClosingCode;
517
- }
518
- }
519
- if (!doNothing && str.startsWith("<![CDATA[", i)) {
520
- const locationOfClosingCData = str.indexOf("]]>", i + 9);
521
- if (locationOfClosingCData > 0) {
522
- doNothing = locationOfClosingCData;
523
852
  }
524
- }
525
- if (!doNothing && !withinStyleTag && !withinInlineStyle && tagNameStartsAt !== null && str[i] === ">") {
526
- if (str[right(str, i)] === "<") {
527
- leftTagName = tagName;
853
+ if (finalIndexesToDelete.current()) {
854
+ const ranges = finalIndexesToDelete.current();
855
+ finalIndexesToDelete.wipe();
856
+ const startingPercentageDone = opts.reportProgressFuncTo -
857
+ (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) *
858
+ leavePercForLastStage;
859
+ const res = rApply(str, ranges, (applyPercDone) => {
860
+ if (opts.reportProgressFunc && len >= 2000) {
861
+ currentPercentageDone = Math.floor(startingPercentageDone +
862
+ (opts.reportProgressFuncTo - startingPercentageDone) *
863
+ (applyPercDone / 100));
864
+ if (currentPercentageDone !== lastPercentage) {
865
+ lastPercentage = currentPercentageDone;
866
+ opts.reportProgressFunc(currentPercentageDone);
867
+ }
868
+ }
869
+ });
870
+ const resLen = res.length;
871
+ return {
872
+ log: {
873
+ timeTakenInMilliseconds: Date.now() - start,
874
+ originalLength: len,
875
+ cleanedLength: resLen,
876
+ bytesSaved: Math.max(len - resLen, 0),
877
+ percentageReducedOfOriginal: len
878
+ ? Math.round((Math.max(len - resLen, 0) * 100) / len)
879
+ : 0,
880
+ },
881
+ ranges,
882
+ applicableOpts,
883
+ result: res,
884
+ };
528
885
  }
529
- tagNameStartsAt = null;
530
- tagName = null;
531
- }
532
- if (str[i] === "<" && leftTagName !== null) {
533
- leftTagName = null;
534
- }
535
- if (withinStyleTag && str[i] === "{" && str[i + 1] === "{" && str.indexOf("}}") !== -1) {
536
- doNothing = str.indexOf("}}") + 2;
537
- }
538
886
  }
539
- if (finalIndexesToDelete.current()) {
540
- const ranges = finalIndexesToDelete.current();
541
- finalIndexesToDelete.wipe();
542
- const startingPercentageDone = opts.reportProgressFuncTo - (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) * leavePercForLastStage;
543
- const res = rApply(str, ranges, applyPercDone => {
544
- if (opts.reportProgressFunc && len >= 2000) {
545
- currentPercentageDone = Math.floor(startingPercentageDone + (opts.reportProgressFuncTo - startingPercentageDone) * (applyPercDone / 100));
546
- if (currentPercentageDone !== lastPercentage) {
547
- lastPercentage = currentPercentageDone;
548
- opts.reportProgressFunc(currentPercentageDone);
549
- }
550
- }
551
- });
552
- const resLen = res.length;
553
- return {
887
+ return {
554
888
  log: {
555
- timeTakenInMilliseconds: Date.now() - start,
556
- originalLength: len,
557
- cleanedLength: resLen,
558
- bytesSaved: Math.max(len - resLen, 0),
559
- percentageReducedOfOriginal: len ? Math.round(Math.max(len - resLen, 0) * 100 / len) : 0
889
+ timeTakenInMilliseconds: Date.now() - start,
890
+ originalLength: len,
891
+ cleanedLength: len,
892
+ bytesSaved: 0,
893
+ percentageReducedOfOriginal: 0,
560
894
  },
561
- ranges,
562
895
  applicableOpts,
563
- result: res
564
- };
565
- }
566
- }
567
- return {
568
- log: {
569
- timeTakenInMilliseconds: Date.now() - start,
570
- originalLength: len,
571
- cleanedLength: len,
572
- bytesSaved: 0,
573
- percentageReducedOfOriginal: 0
574
- },
575
- applicableOpts,
576
- ranges: null,
577
- result: str
578
- };
896
+ ranges: null,
897
+ result: str,
898
+ };
579
899
  }
580
900
 
581
901
  export { crush, defaults, version };