@ohos-ports/html-minifier-next 8.4.2-beta.0 → 8.10.8-beta.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/README.md +147 -33
- package/cli.js +70 -54
- package/dist/types/htmlminifier.d.ts +87 -33
- package/dist/types/htmlminifier.d.ts.map +1 -1
- package/dist/types/htmlparser.d.ts +4 -1
- package/dist/types/htmlparser.d.ts.map +1 -1
- package/dist/types/lib/attributes.d.ts +12 -10
- package/dist/types/lib/attributes.d.ts.map +1 -1
- package/dist/types/lib/constants.d.ts +38 -3
- package/dist/types/lib/constants.d.ts.map +1 -1
- package/dist/types/lib/content.d.ts.map +1 -1
- package/dist/types/lib/elements.d.ts.map +1 -1
- package/dist/types/lib/fragments.d.ts +1 -0
- package/dist/types/lib/fragments.d.ts.map +1 -1
- package/dist/types/lib/option-definitions.d.ts.map +1 -1
- package/dist/types/lib/options.d.ts +14 -46
- package/dist/types/lib/options.d.ts.map +1 -1
- package/dist/types/lib/unused-css.d.ts +34 -4
- package/dist/types/lib/unused-css.d.ts.map +1 -1
- package/dist/types/lib/urls.d.ts.map +1 -1
- package/dist/types/lib/utils.d.ts +31 -1
- package/dist/types/lib/utils.d.ts.map +1 -1
- package/dist/types/lib/whitespace.d.ts +57 -5
- package/dist/types/lib/whitespace.d.ts.map +1 -1
- package/dist/types/presets.d.ts +1 -2
- package/dist/types/presets.d.ts.map +1 -1
- package/dist/types/tokenchain.d.ts.map +1 -1
- package/html-minifier-next.schema.json +15 -7
- package/package.json +17 -18
- package/src/htmlminifier.js +1103 -392
- package/src/htmlparser.js +452 -200
- package/src/lib/attributes.js +201 -112
- package/src/lib/constants.js +130 -19
- package/src/lib/content.js +1 -6
- package/src/lib/elements.js +17 -12
- package/src/lib/file-pool.js +18 -2
- package/src/lib/file-write.js +87 -0
- package/src/lib/fragments.js +74 -8
- package/src/lib/option-definitions.js +17 -10
- package/src/lib/options.js +151 -71
- package/src/lib/unused-css.js +145 -27
- package/src/lib/urls.js +0 -9
- package/src/lib/utils.js +210 -45
- package/src/lib/whitespace.js +173 -63
- package/src/presets.js +1 -2
- package/src/tokenchain.js +1 -4
package/src/lib/whitespace.js
CHANGED
|
@@ -9,17 +9,24 @@ import {
|
|
|
9
9
|
RE_NBSP_TRAILING_GROUP,
|
|
10
10
|
RE_NBSP_TRAILING_STRIP,
|
|
11
11
|
inlineElementsToKeepWhitespace,
|
|
12
|
+
inlineElementsToKeepWhitespaceAround,
|
|
12
13
|
inlineElementsToKeepWhitespaceWithin,
|
|
13
|
-
|
|
14
|
+
inlineElementsToKeepWhitespaceBetween,
|
|
15
|
+
inlineElementsToKeepWhitespaceBetweenEnd,
|
|
16
|
+
inlineElementsTransparent,
|
|
17
|
+
inlineElementsTransparentAfter,
|
|
18
|
+
formControlElementsEither,
|
|
19
|
+
toEndTags
|
|
14
20
|
} from './constants.js';
|
|
15
21
|
|
|
16
22
|
const RE_ANY_WS_NBSP = /[ \n\r\t\f\xA0]/;
|
|
17
23
|
const RE_TAB_NBSP = /[\t\xA0]/;
|
|
18
24
|
const RE_ASCII_WS_RUN = /[ \n\r\f]+/g;
|
|
19
|
-
|
|
25
|
+
// Every reason `collapseWhitespaceAllKnown` has to do any work at all, in one scan
|
|
26
|
+
const RE_COLLAPSIBLE = /[\t\xA0\n\r\f]| {2}/;
|
|
20
27
|
const RE_NON_WS = /\S/;
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
// Any no-break space character (the `collapseNoBreakSpaces` early-exit scan)
|
|
29
|
+
const RE_NO_BREAK_SPACE_CHARS = /[\xA0\u202F\u2007]/;
|
|
23
30
|
|
|
24
31
|
/** @param {string} str */
|
|
25
32
|
const trimWhitespace = str => {
|
|
@@ -41,12 +48,30 @@ function isWsOrNbsp(code) {
|
|
|
41
48
|
return code === 32 || code === 10 || code === 13 || code === 9 || code === 12 || code === 160;
|
|
42
49
|
}
|
|
43
50
|
|
|
44
|
-
//
|
|
51
|
+
// Whether the string is empty or holds only what `\s` matches, scanned by code,
|
|
52
|
+
// as text mostly ends the scan on its first character (where a regex costs more)
|
|
53
|
+
/** @param {string} str */
|
|
54
|
+
function isAllWhitespace(str) {
|
|
55
|
+
for (let i = 0; i < str.length; i++) {
|
|
56
|
+
const code = str.charCodeAt(i);
|
|
57
|
+
if (!((code >= 9 && code <= 13) || code === 32 || code === 0xA0 || code === 0x1680 ||
|
|
58
|
+
(code >= 0x2000 && code <= 0x200A) || code === 0x2028 || code === 0x2029 ||
|
|
59
|
+
code === 0x202F || code === 0x205F || code === 0x3000 || code === 0xFEFF)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Whether the last character is whitespace, no-break space included
|
|
67
|
+
/** @param {string} str */
|
|
68
|
+
function endsWithWhitespace(str) {
|
|
69
|
+
return str.length > 0 && isWsOrNbsp(str.charCodeAt(str.length - 1));
|
|
70
|
+
}
|
|
45
71
|
|
|
46
72
|
// Module scope so the hot path doesn’t allocate a closure per call
|
|
47
73
|
/** @param {string} spaces */
|
|
48
74
|
function collapseRun(spaces) {
|
|
49
|
-
// Preserve standalone tabs
|
|
50
75
|
if (spaces === '\t') return '\t';
|
|
51
76
|
if (spaces.indexOf('\xA0') === -1) return ' ';
|
|
52
77
|
return spaces.replace(RE_NBSP_LEADING_GROUP, '$1 ');
|
|
@@ -65,14 +90,58 @@ function collapseWhitespaceAll(str) {
|
|
|
65
90
|
// As `collapseWhitespaceAll`, for callers that already know `str` holds whitespace
|
|
66
91
|
/** @param {string} str */
|
|
67
92
|
function collapseWhitespaceAllKnown(str) {
|
|
93
|
+
// Most text reaching here is already collapsed, so settle that in one scan rather
|
|
94
|
+
// than two—single spaces with no tab and no no-break space leave nothing to do
|
|
95
|
+
if (!RE_COLLAPSIBLE.test(str)) {
|
|
96
|
+
return str;
|
|
97
|
+
}
|
|
68
98
|
// Only a tab or a no-break space makes the replacement depend on the run itself;
|
|
69
99
|
// without either, every run becomes one space and no per-match callback is needed
|
|
70
100
|
if (!RE_TAB_NBSP.test(str)) {
|
|
71
|
-
return
|
|
101
|
+
return str.replace(RE_ASCII_WS_RUN, ' ');
|
|
72
102
|
}
|
|
73
103
|
return str.replace(RE_ALL_WS_NBSP, collapseRun);
|
|
74
104
|
}
|
|
75
105
|
|
|
106
|
+
// Collapse whitespace into adjacent no-break spaces
|
|
107
|
+
|
|
108
|
+
// A no-break space (`U+00A0`), narrow no-break space (`U+202F`), or figure space (`U+2007`),
|
|
109
|
+
// literal or as a character reference; numeric references and ` ` also work without
|
|
110
|
+
// the semicolon in text
|
|
111
|
+
const RE_NO_BREAK_SPACE_WS = /([ \n\r\t\f]*)(\xA0|\u202F|\u2007|&(?:nbsp;?|NonBreakingSpace;|numsp;|#0*(?:160|8239|8199)(?![0-9]);?|#[xX]0*(?:[aA]0|202[fF]|2007)(?![0-9a-fA-F]);?))([ \n\r\t\f]*)/g;
|
|
112
|
+
// Decoded text holds references only as text, so there the characters alone count
|
|
113
|
+
const RE_NO_BREAK_SPACE_CHAR_WS = /([ \n\r\t\f]*)([\xA0\u202F\u2007])([ \n\r\t\f]*)/g;
|
|
114
|
+
const RE_LINE_BREAK = /[\n\r]/;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {string} _match
|
|
118
|
+
* @param {string} before
|
|
119
|
+
* @param {string} noBreakSpace
|
|
120
|
+
* @param {string} after
|
|
121
|
+
*/
|
|
122
|
+
function keepLineBreaks(_match, before, noBreakSpace, after) {
|
|
123
|
+
return (RE_LINE_BREAK.test(before) ? before : '') + noBreakSpace + (RE_LINE_BREAK.test(after) ? after : '');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Callers pass text whose whitespace runs are already collapsed, as each match
|
|
127
|
+
// rescans the run in front of it
|
|
128
|
+
/**
|
|
129
|
+
* @param {string} str
|
|
130
|
+
* @param {{decodeEntities?: boolean | undefined, preserveLineBreaks?: boolean | undefined}} options
|
|
131
|
+
*/
|
|
132
|
+
function collapseNoBreakSpaces(str, options) {
|
|
133
|
+
const decoded = Boolean(options.decodeEntities);
|
|
134
|
+
// One scan for all three no-break space characters, rather than an `indexOf` per character
|
|
135
|
+
if (!str || (!RE_NO_BREAK_SPACE_CHARS.test(str) && (decoded || str.indexOf('&') === -1))) {
|
|
136
|
+
return str;
|
|
137
|
+
}
|
|
138
|
+
const pattern = decoded ? RE_NO_BREAK_SPACE_CHAR_WS : RE_NO_BREAK_SPACE_WS;
|
|
139
|
+
if (options.preserveLineBreaks) {
|
|
140
|
+
return str.replace(pattern, keepLineBreaks);
|
|
141
|
+
}
|
|
142
|
+
return str.replace(pattern, '$2');
|
|
143
|
+
}
|
|
144
|
+
|
|
76
145
|
// Collapse whitespace with options
|
|
77
146
|
|
|
78
147
|
/**
|
|
@@ -81,12 +150,13 @@ function collapseWhitespaceAllKnown(str) {
|
|
|
81
150
|
* @param {boolean} trimLeft
|
|
82
151
|
* @param {boolean} trimRight
|
|
83
152
|
* @param {boolean} [collapseAll]
|
|
153
|
+
* @param {boolean} [keepUntrimmed] - Whether a side not trimmed keeps its line breaks as written
|
|
84
154
|
*/
|
|
85
|
-
function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll = false) {
|
|
155
|
+
function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll = false, keepUntrimmed = false) {
|
|
86
156
|
if (!str) return str;
|
|
87
157
|
|
|
88
158
|
// Fast path: Nothing to do
|
|
89
|
-
if (!trimLeft && !trimRight && !collapseAll && !options.preserveLineBreaks) {
|
|
159
|
+
if (!trimLeft && !trimRight && !collapseAll && (!options.preserveLineBreaks || keepUntrimmed)) {
|
|
90
160
|
return str;
|
|
91
161
|
}
|
|
92
162
|
|
|
@@ -95,7 +165,7 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll = fal
|
|
|
95
165
|
return str;
|
|
96
166
|
}
|
|
97
167
|
|
|
98
|
-
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll);
|
|
168
|
+
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll, keepUntrimmed);
|
|
99
169
|
}
|
|
100
170
|
|
|
101
171
|
// As `collapseWhitespace`, for callers that already know `str` is non-empty, holds
|
|
@@ -106,31 +176,39 @@ function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll = fal
|
|
|
106
176
|
* @param {boolean} trimLeft
|
|
107
177
|
* @param {boolean} trimRight
|
|
108
178
|
* @param {boolean} collapseAll
|
|
179
|
+
* @param {boolean} [keepUntrimmed] - Whether a side not trimmed keeps its line breaks as written
|
|
109
180
|
*/
|
|
110
|
-
function collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll) {
|
|
181
|
+
function collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll, keepUntrimmed = false) {
|
|
111
182
|
let lineBreakBefore = ''; let lineBreakAfter = '';
|
|
112
183
|
|
|
113
184
|
if (options.preserveLineBreaks) {
|
|
114
185
|
// Find leading/trailing whitespace containing line breaks manually
|
|
115
|
-
// (avoids polynomial backtracking with end-anchored lazy quantifiers)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
186
|
+
// (avoids polynomial backtracking with end-anchored lazy quantifiers);
|
|
187
|
+
// the line-break check scans the run’s char codes directly rather than
|
|
188
|
+
// testing a sliced copy, so the common case allocates nothing
|
|
189
|
+
if (trimLeft || !keepUntrimmed) {
|
|
190
|
+
let leadEnd = 0;
|
|
191
|
+
let hasLineBreak = false;
|
|
192
|
+
for (; leadEnd < str.length; leadEnd++) {
|
|
193
|
+
const code = str.charCodeAt(leadEnd);
|
|
194
|
+
if (!isAsciiWs(code)) break;
|
|
195
|
+
// Settling the line break in the same pass keeps the run from being walked twice
|
|
196
|
+
if (code === 10 || code === 13) hasLineBreak = true;
|
|
197
|
+
}
|
|
198
|
+
if (hasLineBreak) {
|
|
123
199
|
lineBreakBefore = '\n';
|
|
124
200
|
str = str.slice(leadEnd);
|
|
125
201
|
}
|
|
126
202
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
203
|
+
if (trimRight || !keepUntrimmed) {
|
|
204
|
+
let trailStart = str.length;
|
|
205
|
+
let hasLineBreak = false;
|
|
206
|
+
for (; trailStart > 0; trailStart--) {
|
|
207
|
+
const code = str.charCodeAt(trailStart - 1);
|
|
208
|
+
if (!isAsciiWs(code)) break;
|
|
209
|
+
if (code === 10 || code === 13) hasLineBreak = true;
|
|
210
|
+
}
|
|
211
|
+
if (hasLineBreak) {
|
|
134
212
|
lineBreakAfter = '\n';
|
|
135
213
|
str = str.slice(0, trailStart);
|
|
136
214
|
}
|
|
@@ -199,6 +277,36 @@ function collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll)
|
|
|
199
277
|
|
|
200
278
|
// Collapse whitespace smartly based on surrounding tags
|
|
201
279
|
|
|
280
|
+
/**
|
|
281
|
+
* @typedef {object} InlineSets
|
|
282
|
+
* @prop {Set<string>} around - Elements whose surrounding whitespace is kept
|
|
283
|
+
* @prop {Set<string>} aroundEnd - The same, spelled as end tags
|
|
284
|
+
* @prop {Set<string>} within - Elements whose inner whitespace is kept
|
|
285
|
+
* @prop {Set<string>} withinEnd - The same, spelled as end tags
|
|
286
|
+
* @prop {Set<string>} withinEither - `within` under both spellings
|
|
287
|
+
*/
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Inline-element membership for the whitespace pass, each set paired with its end-tag
|
|
291
|
+
* spelling so a lookup takes the tag as it comes rather than slicing off the slash
|
|
292
|
+
* @param {Set<string>} around
|
|
293
|
+
* @param {Set<string>} within
|
|
294
|
+
* @returns {InlineSets}
|
|
295
|
+
*/
|
|
296
|
+
function buildInlineSets(around, within) {
|
|
297
|
+
const withinEnd = toEndTags(within);
|
|
298
|
+
return {
|
|
299
|
+
around,
|
|
300
|
+
aroundEnd: toEndTags(around),
|
|
301
|
+
within,
|
|
302
|
+
withinEnd,
|
|
303
|
+
withinEither: new Set([...within, ...withinEnd])
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// The sets for a document without custom inline elements, built once
|
|
308
|
+
const defaultInlineSets = buildInlineSets(inlineElementsToKeepWhitespaceAround, inlineElementsToKeepWhitespaceWithin);
|
|
309
|
+
|
|
202
310
|
// Check if an input element has `type="hidden"`; module-scope so the hot path
|
|
203
311
|
// doesn’t allocate a closure per text node
|
|
204
312
|
/**
|
|
@@ -207,8 +315,9 @@ function collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll)
|
|
|
207
315
|
function isHiddenInput(attrs) {
|
|
208
316
|
if (!attrs || !attrs.length) return false;
|
|
209
317
|
for (const attr of attrs) {
|
|
210
|
-
|
|
211
|
-
|
|
318
|
+
// Attribute names and the `type` keyword are both ASCII case-insensitive
|
|
319
|
+
if (attr.name.toLowerCase() === 'type') {
|
|
320
|
+
return attr.value?.toLowerCase() === 'hidden';
|
|
212
321
|
}
|
|
213
322
|
}
|
|
214
323
|
return false;
|
|
@@ -221,10 +330,10 @@ function isHiddenInput(attrs) {
|
|
|
221
330
|
* @param {Array<{name: string, value?: string}>} prevAttrs
|
|
222
331
|
* @param {Array<{name: string, value?: string}>} nextAttrs
|
|
223
332
|
* @param {{preserveLineBreaks?: boolean, conservativeCollapse?: boolean, collapseInlineTagWhitespace?: boolean}} options
|
|
224
|
-
* @param {
|
|
225
|
-
* @param {
|
|
333
|
+
* @param {InlineSets} inlineSets
|
|
334
|
+
* @param {boolean} [collapseInside] - False where `canCollapseWhitespace` keeps runs inside the text as they are
|
|
226
335
|
*/
|
|
227
|
-
function collapseWhitespaceSmart(str, prevTag, nextTag, prevAttrs, nextAttrs, options,
|
|
336
|
+
function collapseWhitespaceSmart(str, prevTag, nextTag, prevAttrs, nextAttrs, options, inlineSets, collapseInside = true) {
|
|
228
337
|
if (!str) return str;
|
|
229
338
|
|
|
230
339
|
// Fast path: No whitespace at all—every decision below would leave `str`
|
|
@@ -233,14 +342,19 @@ function collapseWhitespaceSmart(str, prevTag, nextTag, prevAttrs, nextAttrs, op
|
|
|
233
342
|
return str;
|
|
234
343
|
}
|
|
235
344
|
|
|
236
|
-
const inlineOption = Boolean(options.collapseInlineTagWhitespace);
|
|
237
|
-
|
|
238
345
|
let trimLeft = Boolean(prevTag) && !inlineElementsToKeepWhitespace.has(prevTag);
|
|
239
346
|
let trimRight = Boolean(nextTag) && !inlineElementsToKeepWhitespace.has(nextTag);
|
|
240
347
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
348
|
+
const prevTransparent = inlineElementsTransparentAfter.has(prevTag);
|
|
349
|
+
const nextTransparent = inlineElementsTransparent.has(nextTag);
|
|
350
|
+
|
|
351
|
+
// Every branch below that consults the text’s content needs a side left untrimmed, an element
|
|
352
|
+
// that doesn’t render, or `collapseInlineTagWhitespace`, so the scan for a non-whitespace
|
|
353
|
+
// character is only worth making then
|
|
354
|
+
const isPureWhitespace = (!trimLeft || !trimRight || prevTransparent || nextTransparent || Boolean(options.collapseInlineTagWhitespace)) && !RE_NON_WS.test(str);
|
|
355
|
+
|
|
356
|
+
// Next to text, spacing is part of the text, so aggressive collapsing only applies between tags
|
|
357
|
+
const inlineOption = Boolean(options.collapseInlineTagWhitespace) && isPureWhitespace;
|
|
244
358
|
|
|
245
359
|
if (isPureWhitespace) {
|
|
246
360
|
// Smart default behavior: Collapse space around non-rendering elements
|
|
@@ -253,43 +367,36 @@ function collapseWhitespaceSmart(str, prevTag, nextTag, prevAttrs, nextAttrs, op
|
|
|
253
367
|
}
|
|
254
368
|
// Aggressive mode: Collapse between all form controls
|
|
255
369
|
if (inlineOption && (!trimLeft || !trimRight) &&
|
|
256
|
-
|
|
370
|
+
formControlElementsEither.has(prevTag) && formControlElementsEither.has(nextTag)) {
|
|
257
371
|
trimLeft = true;
|
|
258
372
|
trimRight = true;
|
|
259
373
|
}
|
|
260
374
|
}
|
|
261
375
|
|
|
376
|
+
// Aggressive mode keeps whitespace around standard text-level elements only, not
|
|
377
|
+
// around all inline elements
|
|
262
378
|
if (trimLeft) {
|
|
263
|
-
|
|
264
|
-
// Still preserve whitespace around inline text elements
|
|
265
|
-
if (inlineElementsToKeepWhitespaceWithin.has(stripSlash(prevTag))) {
|
|
266
|
-
trimLeft = false;
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
trimLeft = prevTag.charCodeAt(0) === 47 /* / */ ? !inlineElements.has(prevTag.slice(1)) : !inlineTextSet.has(prevTag);
|
|
270
|
-
}
|
|
379
|
+
trimLeft = prevTag.charCodeAt(0) === 47 /* / */ ? !(inlineOption ? inlineElementsToKeepWhitespaceBetweenEnd : inlineSets.aroundEnd).has(prevTag) : !inlineSets.within.has(prevTag);
|
|
271
380
|
}
|
|
272
381
|
|
|
273
382
|
if (trimRight) {
|
|
274
|
-
|
|
275
|
-
if (inlineElementsToKeepWhitespaceWithin.has(stripSlash(nextTag))) {
|
|
276
|
-
trimRight = false;
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
trimRight = nextTag.charCodeAt(0) === 47 /* / */ ? !inlineTextSet.has(nextTag.slice(1)) : !inlineElements.has(nextTag);
|
|
280
|
-
}
|
|
383
|
+
trimRight = nextTag.charCodeAt(0) === 47 /* / */ ? !inlineSets.withinEnd.has(nextTag) : !(inlineOption ? inlineElementsToKeepWhitespaceBetween : inlineSets.around).has(nextTag);
|
|
281
384
|
}
|
|
282
385
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
386
|
+
// Next to an element that doesn’t render, whitespace may separate what lies beyond it, so the
|
|
387
|
+
// element leaves the decision to the text or to a tag on the other side that renders
|
|
388
|
+
if (prevTransparent && (!isPureWhitespace || (Boolean(nextTag) && !nextTransparent))) {
|
|
389
|
+
trimLeft = false;
|
|
390
|
+
}
|
|
391
|
+
if (nextTransparent && (!isPureWhitespace || (Boolean(prevTag) && !prevTransparent))) {
|
|
392
|
+
trimRight = false;
|
|
286
393
|
}
|
|
287
|
-
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll);
|
|
288
|
-
}
|
|
289
394
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
395
|
+
const collapseAll = collapseInside && Boolean(prevTag && nextTag);
|
|
396
|
+
if (!trimLeft && !trimRight && !collapseAll && (!options.preserveLineBreaks || !collapseInside)) {
|
|
397
|
+
return str;
|
|
398
|
+
}
|
|
399
|
+
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll, !collapseInside);
|
|
293
400
|
}
|
|
294
401
|
|
|
295
402
|
// Collapse/trim whitespace for given tag
|
|
@@ -309,13 +416,16 @@ function canTrimWhitespace(tag) {
|
|
|
309
416
|
return !noTrimWhitespaceTags.has(tag);
|
|
310
417
|
}
|
|
311
418
|
|
|
312
|
-
// Exports
|
|
313
|
-
|
|
314
419
|
export {
|
|
420
|
+
buildInlineSets,
|
|
421
|
+
defaultInlineSets,
|
|
422
|
+
endsWithWhitespace,
|
|
315
423
|
trimWhitespace,
|
|
316
424
|
collapseWhitespaceAll,
|
|
425
|
+
collapseNoBreakSpaces,
|
|
317
426
|
collapseWhitespace,
|
|
318
427
|
collapseWhitespaceSmart,
|
|
319
428
|
canCollapseWhitespace,
|
|
320
|
-
canTrimWhitespace
|
|
429
|
+
canTrimWhitespace,
|
|
430
|
+
isAllWhitespace
|
|
321
431
|
};
|
package/src/presets.js
CHANGED
|
@@ -20,6 +20,7 @@ export const presets = {
|
|
|
20
20
|
comprehensive: {
|
|
21
21
|
collapseAttributeWhitespace: true,
|
|
22
22
|
collapseBooleanAttributes: true,
|
|
23
|
+
collapseEmptyAttributes: true,
|
|
23
24
|
collapseWhitespace: true,
|
|
24
25
|
decodeEntities: true,
|
|
25
26
|
mergeScripts: true,
|
|
@@ -38,7 +39,6 @@ export const presets = {
|
|
|
38
39
|
};
|
|
39
40
|
|
|
40
41
|
/**
|
|
41
|
-
* Get preset configuration by name.
|
|
42
42
|
* @param {string} name - Preset name (“conservative” or “comprehensive”)
|
|
43
43
|
* @returns {object|null} Preset options object or null if not found
|
|
44
44
|
*/
|
|
@@ -49,7 +49,6 @@ export function getPreset(name) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Get list of available preset names
|
|
53
52
|
* @returns {string[]} Array of preset names
|
|
54
53
|
*/
|
|
55
54
|
export function getPresetNames() {
|
package/src/tokenchain.js
CHANGED
|
@@ -73,14 +73,12 @@ class TokenChain {
|
|
|
73
73
|
createSorter() {
|
|
74
74
|
const sorter = new Sorter();
|
|
75
75
|
|
|
76
|
-
//
|
|
76
|
+
// Sort tokens by usage frequency (descending), then alphabetically
|
|
77
77
|
const entries = Array.from(this.map.entries()).sort((a, b) => {
|
|
78
78
|
const m = a[1].arrays.length;
|
|
79
79
|
const n = b[1].arrays.length;
|
|
80
|
-
// Sort by length descending (larger first)
|
|
81
80
|
const lengthDiff = n - m;
|
|
82
81
|
if (lengthDiff !== 0) return lengthDiff;
|
|
83
|
-
// If lengths equal, sort by key ascending
|
|
84
82
|
return a[0].localeCompare(b[0]);
|
|
85
83
|
});
|
|
86
84
|
|
|
@@ -98,7 +96,6 @@ class TokenChain {
|
|
|
98
96
|
}
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
// Mark remaining tokens as processed
|
|
102
99
|
filtered.forEach((t) => {
|
|
103
100
|
const tData = this.map.get(t);
|
|
104
101
|
if (tData) {
|