@mgdis/core-ui-helpers 1.3.1 → 1.3.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.
- package/dist/stencil/index.cjs +20 -0
- package/dist/stencil/index.cjs.map +1 -1
- package/dist/stencil/index.js +20 -0
- package/dist/stencil/index.js.map +1 -1
- package/dist/stencil/index.umd.js +379 -359
- package/dist/stencil/index.umd.js.map +1 -1
- package/dist/storybook/index.cjs +534 -389
- package/dist/storybook/index.cjs.map +1 -1
- package/dist/storybook/index.js +534 -389
- package/dist/storybook/index.js.map +1 -1
- package/dist/storybook/index.umd.js +534 -389
- package/dist/storybook/index.umd.js.map +1 -1
- package/package.json +5 -5
package/dist/storybook/index.cjs
CHANGED
|
@@ -1004,7 +1004,7 @@ const VOID_ELEMENTS = [
|
|
|
1004
1004
|
];
|
|
1005
1005
|
|
|
1006
1006
|
/**
|
|
1007
|
-
* Defined
|
|
1007
|
+
* Defined per formatting operation from configuration.
|
|
1008
1008
|
*
|
|
1009
1009
|
* CONTENT_IGNORE_PLACEHOLDER
|
|
1010
1010
|
* SELF_CLOSING_PLACEHOLDER
|
|
@@ -1012,46 +1012,110 @@ const VOID_ELEMENTS = [
|
|
|
1012
1012
|
*/
|
|
1013
1013
|
|
|
1014
1014
|
/**
|
|
1015
|
-
* @typedef {object}
|
|
1015
|
+
* @typedef {object} HtmlfyConstants
|
|
1016
1016
|
* @property {string} CONTENT_IGNORE_PLACEHOLDER
|
|
1017
1017
|
* @property {string} SELF_CLOSING_PLACEHOLDER
|
|
1018
1018
|
* @property {string} ATTRIBUTE_IGNORE_PLACEHOLDER
|
|
1019
1019
|
*/
|
|
1020
|
+
|
|
1020
1021
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1023
|
-
* @
|
|
1024
|
-
* @
|
|
1025
|
-
* @property {Constants} constants - Constant strings, influenced by ignore_with.
|
|
1022
|
+
* Create the placeholders used during one formatting operation.
|
|
1023
|
+
*
|
|
1024
|
+
* @param {string} [ignore_with]
|
|
1025
|
+
* @returns {HtmlfyConstants}
|
|
1026
1026
|
*/
|
|
1027
|
+
const createConstants = (ignore_with = CONFIG.ignore_with) => ({
|
|
1028
|
+
CONTENT_IGNORE_PLACEHOLDER: `${ignore_with}_`,
|
|
1029
|
+
SELF_CLOSING_PLACEHOLDER: `${ignore_with}/_>`,
|
|
1030
|
+
ATTRIBUTE_IGNORE_PLACEHOLDER: `${ignore_with}=_`
|
|
1031
|
+
});
|
|
1032
|
+
|
|
1033
|
+
const DEFAULT_CONSTANTS = createConstants();
|
|
1027
1034
|
|
|
1028
1035
|
/**
|
|
1029
|
-
*
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1036
|
+
* Visit complete tags without treating brackets inside quoted attributes as boundaries.
|
|
1037
|
+
* Returning true from the visitor stops the scan.
|
|
1038
|
+
*
|
|
1039
|
+
* @param {string} content
|
|
1040
|
+
* @param {(tag: string, start: number, end: number) => boolean | void} visitor
|
|
1041
|
+
* @returns {boolean}
|
|
1032
1042
|
*/
|
|
1033
|
-
const
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1043
|
+
const scanTags = (content, visitor) => {
|
|
1044
|
+
let tag_start = -1;
|
|
1045
|
+
let quote = '';
|
|
1046
|
+
|
|
1047
|
+
for (let index = 0; index < content.length; index++) {
|
|
1048
|
+
const character = content[index];
|
|
1049
|
+
|
|
1050
|
+
if (tag_start === -1) {
|
|
1051
|
+
if (character === '<') tag_start = index;
|
|
1052
|
+
continue
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
if (quote) {
|
|
1056
|
+
if (character === quote) quote = '';
|
|
1057
|
+
continue
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
if (character === '"' || character === "'") {
|
|
1061
|
+
quote = character;
|
|
1062
|
+
} else if (character === '<') {
|
|
1063
|
+
tag_start = index;
|
|
1064
|
+
} else if (character === '>') {
|
|
1065
|
+
if (visitor(content.slice(tag_start, index + 1), tag_start, index + 1)) return true
|
|
1066
|
+
tag_start = -1;
|
|
1067
|
+
}
|
|
1041
1068
|
}
|
|
1069
|
+
|
|
1070
|
+
return false
|
|
1042
1071
|
};
|
|
1043
1072
|
|
|
1044
1073
|
/**
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1074
|
+
* Parse the name of an opening tag and return its end offset within the tag.
|
|
1075
|
+
*
|
|
1076
|
+
* @param {string} tag
|
|
1077
|
+
* @returns {{ name: string, name_end: number } | undefined}
|
|
1047
1078
|
*/
|
|
1048
|
-
const
|
|
1079
|
+
const getOpeningTag = (tag) => {
|
|
1080
|
+
let name_start = 1;
|
|
1081
|
+
while (/\s/.test(tag[name_start] || '')) name_start++;
|
|
1082
|
+
if (!/[A-Za-z]/.test(tag[name_start] || '')) return
|
|
1083
|
+
|
|
1084
|
+
let name_end = name_start + 1;
|
|
1085
|
+
while (name_end < tag.length && !/[\s/>]/.test(tag[name_end])) name_end++;
|
|
1086
|
+
|
|
1087
|
+
const name = tag.slice(name_start, name_end);
|
|
1088
|
+
if (!/^[A-Za-z][A-Za-z0-9:._-]*$/.test(name)) return
|
|
1089
|
+
|
|
1090
|
+
return { name, name_end }
|
|
1091
|
+
};
|
|
1049
1092
|
|
|
1050
1093
|
/**
|
|
1051
|
-
*
|
|
1052
|
-
*
|
|
1094
|
+
* Transform complete opening tags while preserving all content between them.
|
|
1095
|
+
*
|
|
1096
|
+
* @param {string} content
|
|
1097
|
+
* @param {(tag: string, name: string, name_end: number) => string} transform
|
|
1098
|
+
* @returns {string}
|
|
1053
1099
|
*/
|
|
1054
|
-
const
|
|
1100
|
+
const transformOpeningTags = (content, transform) => {
|
|
1101
|
+
const chunks = [];
|
|
1102
|
+
let previous_end = 0;
|
|
1103
|
+
|
|
1104
|
+
scanTags(content, (tag, start, end) => {
|
|
1105
|
+
const opening_tag = getOpeningTag(tag);
|
|
1106
|
+
if (!opening_tag) return
|
|
1107
|
+
|
|
1108
|
+
chunks.push(
|
|
1109
|
+
content.slice(previous_end, start),
|
|
1110
|
+
transform(tag, opening_tag.name, opening_tag.name_end)
|
|
1111
|
+
);
|
|
1112
|
+
previous_end = end;
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
if (chunks.length === 0) return content
|
|
1116
|
+
chunks.push(content.slice(previous_end));
|
|
1117
|
+
return chunks.join('')
|
|
1118
|
+
};
|
|
1055
1119
|
|
|
1056
1120
|
/**
|
|
1057
1121
|
* Checks if content contains at least one HTML element or custom HTML element.
|
|
@@ -1076,11 +1140,30 @@ const setState = (new_state) => Object.assign(state, new_state);
|
|
|
1076
1140
|
* @returns {boolean} A boolean.
|
|
1077
1141
|
*/
|
|
1078
1142
|
const isHtml = (content) => {
|
|
1079
|
-
|
|
1143
|
+
const paired_elements = new Set();
|
|
1144
|
+
const standard_element = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
1145
|
+
const namespaced_element = /^(?:[A-Za-z][A-Za-z0-9]*:)[A-Za-z][A-Za-z0-9]*$/;
|
|
1146
|
+
const custom_element = /^(?:[a-z][a-z0-9._]*:)?[a-z][a-z0-9._]*-[a-z0-9._-]+$/;
|
|
1147
|
+
|
|
1148
|
+
return scanTags(content, (tag) => {
|
|
1149
|
+
if (tag.startsWith('</')) {
|
|
1150
|
+
const name = tag.slice(2, -1);
|
|
1151
|
+
return paired_elements.has(name)
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
const opening_tag = getOpeningTag(tag);
|
|
1155
|
+
if (!opening_tag) return false
|
|
1156
|
+
|
|
1157
|
+
const suffix_start = tag[opening_tag.name_end];
|
|
1158
|
+
if (!(suffix_start === '>' || /\s/.test(suffix_start) || (suffix_start === '/' && tag[opening_tag.name_end + 1] === '>')))
|
|
1159
|
+
return false
|
|
1160
|
+
|
|
1161
|
+
if (standard_element.test(opening_tag.name)) return true
|
|
1162
|
+
if (namespaced_element.test(opening_tag.name) || custom_element.test(opening_tag.name))
|
|
1163
|
+
paired_elements.add(opening_tag.name);
|
|
1080
1164
|
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
/<(?<Element>(?:[a-z][a-z0-9._]*:)?[a-z][a-z0-9._]*-[a-z0-9._-]+)(?:\s+.*?)*?>(?:.|\n)*?<\/{1}\k<Element>>/.test(content)
|
|
1165
|
+
return false
|
|
1166
|
+
})
|
|
1084
1167
|
};
|
|
1085
1168
|
|
|
1086
1169
|
/**
|
|
@@ -1124,27 +1207,15 @@ const mergeObjects = (current, updates) => {
|
|
|
1124
1207
|
* @returns {import('htmlfy').Config}
|
|
1125
1208
|
*/
|
|
1126
1209
|
const mergeConfig = (default_config, config) => {
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
/* Below `constants` prefixes and suffixes must be in sync with those in state.js */
|
|
1130
|
-
setState({
|
|
1131
|
-
config: validated_config,
|
|
1132
|
-
constants: {
|
|
1133
|
-
CONTENT_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}_`,
|
|
1134
|
-
SELF_CLOSING_PLACEHOLDER: `${validated_config.ignore_with}/_>`,
|
|
1135
|
-
ATTRIBUTE_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}=_`
|
|
1136
|
-
}
|
|
1137
|
-
});
|
|
1138
|
-
return validated_config
|
|
1210
|
+
return mergeObjects(default_config, config)
|
|
1139
1211
|
};
|
|
1140
1212
|
|
|
1141
1213
|
/**
|
|
1142
1214
|
*
|
|
1143
|
-
* @param {string} html
|
|
1215
|
+
* @param {string} html
|
|
1216
|
+
* @param {HtmlfyConstants} [constants]
|
|
1144
1217
|
*/
|
|
1145
|
-
const protectAttributes = (html) => {
|
|
1146
|
-
const { constants } = getState();
|
|
1147
|
-
|
|
1218
|
+
const protectAttributes = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1148
1219
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1149
1220
|
return match.replace(capture, (match) => {
|
|
1150
1221
|
return match
|
|
@@ -1159,11 +1230,10 @@ const protectAttributes = (html) => {
|
|
|
1159
1230
|
|
|
1160
1231
|
/**
|
|
1161
1232
|
*
|
|
1162
|
-
* @param {string} html
|
|
1233
|
+
* @param {string} html
|
|
1234
|
+
* @param {HtmlfyConstants} [constants]
|
|
1163
1235
|
*/
|
|
1164
|
-
const protectContent = (html) => {
|
|
1165
|
-
const { constants } = getState();
|
|
1166
|
-
|
|
1236
|
+
const protectContent = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1167
1237
|
return html
|
|
1168
1238
|
.replace(/\n/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!')
|
|
1169
1239
|
.replace(/\r/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!')
|
|
@@ -1172,11 +1242,11 @@ const protectContent = (html) => {
|
|
|
1172
1242
|
|
|
1173
1243
|
/**
|
|
1174
1244
|
*
|
|
1175
|
-
* @param {string} html
|
|
1245
|
+
* @param {string} html
|
|
1246
|
+
* @param {HtmlfyConstants} [constants]
|
|
1176
1247
|
*/
|
|
1177
|
-
const finalProtectContent = (html) => {
|
|
1248
|
+
const finalProtectContent = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1178
1249
|
const regex = /\s*<([a-zA-Z0-9:-]+)[^>]*>\n\s*<\/\1>(?=\n[ ]*[^\n]*__!i-£___£%__[^\n]*\n)(\n[ ]*\S[^\n]*\n)|<([a-zA-Z0-9:-]+)[^>]*>(?=\n[ ]*[^\n]*__!i-£___£%__[^\n]*\n)(\n[ ]*\S[^\n]*\n\s*)<\/\3>/g;
|
|
1179
|
-
const { constants } = getState();
|
|
1180
1250
|
|
|
1181
1251
|
return html
|
|
1182
1252
|
.replace(regex, (/** @type {string} */match, p1, p2, p3, p4) => {
|
|
@@ -1197,22 +1267,39 @@ const finalProtectContent = (html) => {
|
|
|
1197
1267
|
/**
|
|
1198
1268
|
* Replace html brackets with ignore string.
|
|
1199
1269
|
*
|
|
1200
|
-
* @param {string} html
|
|
1270
|
+
* @param {string} html
|
|
1271
|
+
* @param {HtmlfyConstants} [constants]
|
|
1201
1272
|
* @returns {string}
|
|
1202
1273
|
*/
|
|
1203
|
-
const setIgnoreAttribute = (html) => {
|
|
1204
|
-
|
|
1205
|
-
|
|
1274
|
+
const setIgnoreAttribute = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1275
|
+
// Most documents do not contain HTML-like brackets inside attribute values.
|
|
1276
|
+
if (!/=\s*(?:"[^"]*[<>][^"]*"|'[^']*[<>][^']*')/.test(html)) return html
|
|
1277
|
+
|
|
1278
|
+
return transformOpeningTags(html, (tag, name, name_end) => {
|
|
1279
|
+
let quote = '';
|
|
1280
|
+
let previous_end = 0;
|
|
1281
|
+
const chunks = [];
|
|
1282
|
+
|
|
1283
|
+
for (let index = name_end; index < tag.length - 1; index++) {
|
|
1284
|
+
const character = tag[index];
|
|
1285
|
+
|
|
1286
|
+
if (!quote && (character === '"' || character === "'")) {
|
|
1287
|
+
quote = character;
|
|
1288
|
+
} else if (quote && character === quote) {
|
|
1289
|
+
quote = '';
|
|
1290
|
+
} else if (quote && (character === '<' || character === '>')) {
|
|
1291
|
+
chunks.push(
|
|
1292
|
+
tag.slice(previous_end, index),
|
|
1293
|
+
constants.ATTRIBUTE_IGNORE_PLACEHOLDER + (character === '<' ? 'lt!' : 'gt!')
|
|
1294
|
+
);
|
|
1295
|
+
previous_end = index + 1;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1206
1298
|
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
.replace(/>/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'gt!')
|
|
1212
|
-
})
|
|
1213
|
-
});
|
|
1214
|
-
|
|
1215
|
-
return html
|
|
1299
|
+
if (chunks.length === 0) return tag
|
|
1300
|
+
chunks.push(tag.slice(previous_end));
|
|
1301
|
+
return chunks.join('')
|
|
1302
|
+
})
|
|
1216
1303
|
};
|
|
1217
1304
|
|
|
1218
1305
|
/**
|
|
@@ -1238,11 +1325,10 @@ const trimify = (html, trim) => {
|
|
|
1238
1325
|
|
|
1239
1326
|
/**
|
|
1240
1327
|
*
|
|
1241
|
-
* @param {string} html
|
|
1328
|
+
* @param {string} html
|
|
1329
|
+
* @param {HtmlfyConstants} [constants]
|
|
1242
1330
|
*/
|
|
1243
|
-
const unprotectAttributes = (html) => {
|
|
1244
|
-
const { constants } = getState();
|
|
1245
|
-
|
|
1331
|
+
const unprotectAttributes = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1246
1332
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1247
1333
|
return match.replace(capture, (match) => {
|
|
1248
1334
|
return match
|
|
@@ -1257,11 +1343,10 @@ const unprotectAttributes = (html) => {
|
|
|
1257
1343
|
|
|
1258
1344
|
/**
|
|
1259
1345
|
*
|
|
1260
|
-
* @param {string} html
|
|
1346
|
+
* @param {string} html
|
|
1347
|
+
* @param {HtmlfyConstants} [constants]
|
|
1261
1348
|
*/
|
|
1262
|
-
const unprotectContent = (html) => {
|
|
1263
|
-
const { constants } = getState();
|
|
1264
|
-
|
|
1349
|
+
const unprotectContent = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1265
1350
|
html = html.replace(new RegExp(`.*${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!.*`, "g"), (/** @type {string} */match) => {
|
|
1266
1351
|
return match.replace(new RegExp(`${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!`, "g"), (match) => {
|
|
1267
1352
|
return match
|
|
@@ -1277,13 +1362,13 @@ const unprotectContent = (html) => {
|
|
|
1277
1362
|
/**
|
|
1278
1363
|
* Replace ignore string with html brackets.
|
|
1279
1364
|
*
|
|
1280
|
-
* @param {string} html
|
|
1365
|
+
* @param {string} html
|
|
1366
|
+
* @param {HtmlfyConstants} [constants]
|
|
1281
1367
|
* @returns {string}
|
|
1282
1368
|
*/
|
|
1283
|
-
const unsetIgnoreAttribute = (html) => {
|
|
1369
|
+
const unsetIgnoreAttribute = (html, constants = DEFAULT_CONSTANTS) => {
|
|
1284
1370
|
/* Regex to find opening tags and capture their attributes. */
|
|
1285
1371
|
const tagRegex = /<([\w:\-]+)([^>]*)>/g;
|
|
1286
|
-
const { constants } = getState();
|
|
1287
1372
|
const escapedIgnoreString = constants.ATTRIBUTE_IGNORE_PLACEHOLDER.replace(
|
|
1288
1373
|
/[-\/\\^$*+?.()|[\]{}]/g,
|
|
1289
1374
|
"\\$&"
|
|
@@ -1316,6 +1401,8 @@ const unsetIgnoreAttribute = (html) => {
|
|
|
1316
1401
|
*/
|
|
1317
1402
|
const validateConfig = (config) => {
|
|
1318
1403
|
if (typeof config !== 'object') throw new Error('Config must be an object.')
|
|
1404
|
+
|
|
1405
|
+
config = { ...config };
|
|
1319
1406
|
|
|
1320
1407
|
const default_config = { ...CONFIG };
|
|
1321
1408
|
|
|
@@ -1330,7 +1417,6 @@ const validateConfig = (config) => {
|
|
|
1330
1417
|
);
|
|
1331
1418
|
|
|
1332
1419
|
if (config_empty) {
|
|
1333
|
-
setState({ config: default_config });
|
|
1334
1420
|
return default_config
|
|
1335
1421
|
}
|
|
1336
1422
|
|
|
@@ -1388,113 +1474,165 @@ const validateConfig = (config) => {
|
|
|
1388
1474
|
* @param {string} text
|
|
1389
1475
|
* @param {number} width
|
|
1390
1476
|
* @param {string} indent
|
|
1477
|
+
* @param {HtmlfyConstants} [constants]
|
|
1391
1478
|
*/
|
|
1392
|
-
const wordWrap = (text, width, indent) => {
|
|
1479
|
+
const wordWrap = (text, width, indent, constants = DEFAULT_CONSTANTS) => {
|
|
1393
1480
|
const words = text.trim().split(/\s+/);
|
|
1394
1481
|
|
|
1395
1482
|
if (words.length === 0 || (words.length === 1 && words[0] === ''))
|
|
1396
1483
|
return ""
|
|
1397
1484
|
|
|
1485
|
+
/** @type {string[]} */
|
|
1398
1486
|
const lines = [];
|
|
1399
|
-
|
|
1400
|
-
const
|
|
1487
|
+
/** @type {string[]} */
|
|
1488
|
+
const current_words = [];
|
|
1489
|
+
let current_length = 0;
|
|
1490
|
+
|
|
1491
|
+
const flushLine = () => {
|
|
1492
|
+
if (current_words.length === 0) return
|
|
1493
|
+
lines.push(indent + current_words.join(' '));
|
|
1494
|
+
current_words.length = 0;
|
|
1495
|
+
current_length = 0;
|
|
1496
|
+
};
|
|
1401
1497
|
|
|
1402
|
-
|
|
1403
|
-
if (word === "")
|
|
1498
|
+
for (const word of words) {
|
|
1499
|
+
if (word === "") continue
|
|
1404
1500
|
|
|
1405
1501
|
if (word.length >= width) {
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
/* Push a long word on its own line with correct padding. */
|
|
1411
|
-
lines.push(lines.length === 0 ? indent + word : padding_string + word);
|
|
1412
|
-
current_line = ""; // Reset current line
|
|
1413
|
-
return // Move to the next word
|
|
1502
|
+
flushLine();
|
|
1503
|
+
lines.push(indent + word);
|
|
1504
|
+
continue
|
|
1414
1505
|
}
|
|
1415
1506
|
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
current_line = test_line;
|
|
1507
|
+
const next_length = current_length + (current_words.length === 0 ? 0 : 1) + word.length;
|
|
1508
|
+
if (next_length <= width) {
|
|
1509
|
+
current_words.push(word);
|
|
1510
|
+
current_length = next_length;
|
|
1421
1511
|
} else {
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
lines.push(lines.length === 0 ? indent + current_line : padding_string + current_line);
|
|
1426
|
-
}
|
|
1427
|
-
/* Start a new line with the current word. */
|
|
1428
|
-
current_line = word;
|
|
1512
|
+
flushLine();
|
|
1513
|
+
current_words.push(word);
|
|
1514
|
+
current_length = word.length;
|
|
1429
1515
|
}
|
|
1430
|
-
}
|
|
1516
|
+
}
|
|
1431
1517
|
|
|
1432
|
-
|
|
1433
|
-
if (current_line !== "")
|
|
1434
|
-
lines.push(lines.length === 0 ? indent + current_line : padding_string + current_line);
|
|
1518
|
+
flushLine();
|
|
1435
1519
|
|
|
1436
1520
|
const result = lines.join("\n");
|
|
1437
1521
|
|
|
1438
|
-
return protectContent(result)
|
|
1522
|
+
return protectContent(result, constants)
|
|
1439
1523
|
};
|
|
1440
1524
|
|
|
1525
|
+
const IGNORE_MARKER_PREFIX = "___HTMLFY_SPECIAL_IGNORE_MARKER_";
|
|
1526
|
+
const IGNORE_MARKER_REGEX = /___HTMLFY_SPECIAL_IGNORE_MARKER_\d+___/g;
|
|
1527
|
+
const TEXTAREA_MARKER_PREFIX = "___HTMLFY_TEXTAREA_MARKER_";
|
|
1528
|
+
const TEXTAREA_MARKER_REGEX = /___HTMLFY_TEXTAREA_MARKER_\d+___/g;
|
|
1529
|
+
|
|
1441
1530
|
/**
|
|
1442
|
-
* Extract
|
|
1443
|
-
*
|
|
1444
|
-
*
|
|
1445
|
-
*
|
|
1446
|
-
* @param {string}
|
|
1447
|
-
* @
|
|
1531
|
+
* Extract the contents of matching elements in one traversal.
|
|
1532
|
+
*
|
|
1533
|
+
* @param {string} html
|
|
1534
|
+
* @param {Set<string>} names
|
|
1535
|
+
* @param {string} marker_prefix
|
|
1536
|
+
* @param {(content: string) => string} [transform]
|
|
1537
|
+
* @returns {{ html_with_markers: string, extracted_map: Map<string,string> }}
|
|
1448
1538
|
*/
|
|
1449
|
-
|
|
1450
|
-
setState({ ignored: true });
|
|
1451
|
-
const config = (getState()).config;
|
|
1452
|
-
let current_html = html;
|
|
1539
|
+
const extractBlocks = (html, names, marker_prefix, transform = content => content) => {
|
|
1453
1540
|
const extracted_blocks = new Map();
|
|
1541
|
+
const chunks = [];
|
|
1454
1542
|
let marker_id = 0;
|
|
1455
|
-
|
|
1543
|
+
let previous_end = 0;
|
|
1456
1544
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1545
|
+
/** @type {{ name: string, content_start: number } | undefined} */
|
|
1546
|
+
let active_block;
|
|
1547
|
+
let tag_start = -1;
|
|
1548
|
+
let quote = '';
|
|
1460
1549
|
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
"gs" // global and dotAll
|
|
1464
|
-
);
|
|
1550
|
+
for (let index = 0; index < html.length; index++) {
|
|
1551
|
+
const character = html[index];
|
|
1465
1552
|
|
|
1466
|
-
|
|
1467
|
-
|
|
1553
|
+
if (active_block) {
|
|
1554
|
+
if (character !== '<') continue
|
|
1468
1555
|
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1556
|
+
let closing_index = index + 1;
|
|
1557
|
+
while (/\s/.test(html[closing_index] || '')) closing_index++;
|
|
1558
|
+
if (html[closing_index] !== '/') continue
|
|
1559
|
+
|
|
1560
|
+
closing_index++;
|
|
1561
|
+
while (/\s/.test(html[closing_index] || '')) closing_index++;
|
|
1562
|
+
if (!html.startsWith(active_block.name, closing_index)) continue
|
|
1563
|
+
|
|
1564
|
+
closing_index += active_block.name.length;
|
|
1565
|
+
while (/\s/.test(html[closing_index] || '')) closing_index++;
|
|
1566
|
+
if (html[closing_index] !== '>') continue
|
|
1473
1567
|
|
|
1474
|
-
|
|
1475
|
-
|
|
1568
|
+
const marker = `${marker_prefix}${marker_id++}___`;
|
|
1569
|
+
chunks.push(html.slice(previous_end, active_block.content_start), marker);
|
|
1570
|
+
extracted_blocks.set(marker, transform(html.slice(active_block.content_start, index)));
|
|
1571
|
+
previous_end = index;
|
|
1572
|
+
active_block = undefined;
|
|
1573
|
+
index = closing_index;
|
|
1574
|
+
continue
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
if (tag_start === -1) {
|
|
1578
|
+
if (character === '<') tag_start = index;
|
|
1579
|
+
continue
|
|
1580
|
+
}
|
|
1476
1581
|
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
replacements.push({
|
|
1481
|
-
start: match.index + match[1].length, // start of content
|
|
1482
|
-
end: match.index + match[1].length + match[2].length, // end of content
|
|
1483
|
-
marker: marker,
|
|
1484
|
-
});
|
|
1582
|
+
if (quote) {
|
|
1583
|
+
if (character === quote) quote = '';
|
|
1584
|
+
continue
|
|
1485
1585
|
}
|
|
1486
1586
|
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1587
|
+
if (character === '"' || character === "'") {
|
|
1588
|
+
quote = character;
|
|
1589
|
+
} else if (character === '<') {
|
|
1590
|
+
tag_start = index;
|
|
1591
|
+
} else if (character === '>') {
|
|
1592
|
+
const opening_tag = getOpeningTag(html.slice(tag_start, index + 1));
|
|
1593
|
+
if (opening_tag && names.has(opening_tag.name)) {
|
|
1594
|
+
active_block = { name: opening_tag.name, content_start: index + 1 };
|
|
1595
|
+
}
|
|
1596
|
+
tag_start = -1;
|
|
1494
1597
|
}
|
|
1495
1598
|
}
|
|
1496
1599
|
|
|
1497
|
-
|
|
1600
|
+
if (extracted_blocks.size === 0)
|
|
1601
|
+
return { html_with_markers: html, extracted_map: extracted_blocks }
|
|
1602
|
+
|
|
1603
|
+
chunks.push(html.slice(previous_end));
|
|
1604
|
+
return { html_with_markers: chunks.join(''), extracted_map: extracted_blocks }
|
|
1605
|
+
};
|
|
1606
|
+
|
|
1607
|
+
/**
|
|
1608
|
+
* Extract any HTML blocks to be ignored,
|
|
1609
|
+
* and replace them with a placeholder for re-insertion later.
|
|
1610
|
+
*
|
|
1611
|
+
* @param {string} html
|
|
1612
|
+
* @param {string[]} ignore
|
|
1613
|
+
* @returns {{ html_with_markers: string, extracted_map: Map<string,string> }}
|
|
1614
|
+
*/
|
|
1615
|
+
function extractIgnoredBlocks(html, ignore) {
|
|
1616
|
+
return extractBlocks(html, new Set(ignore), IGNORE_MARKER_PREFIX)
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
/**
|
|
1620
|
+
* Protect textarea contents without expanding them into entities.
|
|
1621
|
+
*
|
|
1622
|
+
* @param {string} html
|
|
1623
|
+
* @returns {{ html_with_markers: string, extracted_map: Map<string,string> }}
|
|
1624
|
+
*/
|
|
1625
|
+
function extractTextareaBlocks(html) {
|
|
1626
|
+
return extractBlocks(html, new Set(['textarea']), TEXTAREA_MARKER_PREFIX, content => content
|
|
1627
|
+
.replace(/</g, '<')
|
|
1628
|
+
.replace(/>/g, '>')
|
|
1629
|
+
.replace(/"/g, '"')
|
|
1630
|
+
.replace(/'/g, "'")
|
|
1631
|
+
.replace(/ /g, '\n')
|
|
1632
|
+
.replace(/ /g, '\r')
|
|
1633
|
+
.replace(/ /g, ' ')
|
|
1634
|
+
.replace(/\s+/g, ' ')
|
|
1635
|
+
)
|
|
1498
1636
|
}
|
|
1499
1637
|
|
|
1500
1638
|
/**
|
|
@@ -1505,13 +1643,18 @@ function extractIgnoredBlocks(html) {
|
|
|
1505
1643
|
* @returns
|
|
1506
1644
|
*/
|
|
1507
1645
|
function reinsertIgnoredBlocks(html_with_markers, extracted_map) {
|
|
1508
|
-
|
|
1509
|
-
|
|
1646
|
+
return html_with_markers.replace(IGNORE_MARKER_REGEX, marker => extracted_map.get(marker) ?? marker)
|
|
1647
|
+
}
|
|
1510
1648
|
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1649
|
+
/**
|
|
1650
|
+
* Re-insert protected textarea contents in one pass.
|
|
1651
|
+
*
|
|
1652
|
+
* @param {string} html_with_markers
|
|
1653
|
+
* @param {Map<string,string>} extracted_map
|
|
1654
|
+
* @returns {string}
|
|
1655
|
+
*/
|
|
1656
|
+
function reinsertTextareaBlocks(html_with_markers, extracted_map) {
|
|
1657
|
+
return html_with_markers.replace(TEXTAREA_MARKER_REGEX, marker => extracted_map.get(marker) ?? marker)
|
|
1515
1658
|
}
|
|
1516
1659
|
|
|
1517
1660
|
const void_element_regex = new RegExp(`<(${VOID_ELEMENTS.join("|")})(?:\\s(?:[^/>]|/(?!>))*)*>`, 'g');
|
|
@@ -1520,12 +1663,11 @@ const void_element_regex = new RegExp(`<(${VOID_ELEMENTS.join("|")})(?:\\s(?:[^/
|
|
|
1520
1663
|
* Add a placeholder for void elements that are not self-closing.
|
|
1521
1664
|
* This is for internal processing only.
|
|
1522
1665
|
*
|
|
1523
|
-
* @param {string} html
|
|
1666
|
+
* @param {string} html
|
|
1667
|
+
* @param {HtmlfyConstants} [constants]
|
|
1524
1668
|
* @returns
|
|
1525
1669
|
*/
|
|
1526
|
-
function setSelfClosing(html) {
|
|
1527
|
-
const { constants } = getState();
|
|
1528
|
-
|
|
1670
|
+
function setSelfClosing(html, constants = DEFAULT_CONSTANTS) {
|
|
1529
1671
|
return html.replace(
|
|
1530
1672
|
// match only void elements that are not self-closing
|
|
1531
1673
|
void_element_regex,
|
|
@@ -1534,131 +1676,25 @@ function setSelfClosing(html) {
|
|
|
1534
1676
|
}
|
|
1535
1677
|
|
|
1536
1678
|
/**
|
|
1537
|
-
*
|
|
1538
|
-
*
|
|
1539
|
-
* @param {string} html
|
|
1540
|
-
* @
|
|
1541
|
-
|
|
1542
|
-
function unsetSelfClosing(html) {
|
|
1543
|
-
const { constants } = getState();
|
|
1544
|
-
|
|
1545
|
-
return html.replace(constants.SELF_CLOSING_PLACEHOLDER, ">")
|
|
1546
|
-
}
|
|
1547
|
-
|
|
1548
|
-
/**
|
|
1549
|
-
* Enforce entity characters for textarea content.
|
|
1550
|
-
* To also minifiy tags, pass `minify` as `true`.
|
|
1551
|
-
*
|
|
1552
|
-
* @param {string} html The HTML string to evaluate.
|
|
1553
|
-
* @param {boolean} [minify] Minifies the textarea tags themselves.
|
|
1554
|
-
* Defaults to `false`. We recommend a value of `true` if you're running `entify()`
|
|
1555
|
-
* as a standalone function.
|
|
1556
|
-
* @returns {string}
|
|
1557
|
-
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1558
|
-
* @example With minify.
|
|
1559
|
-
* <textarea >3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1560
|
-
*/
|
|
1561
|
-
const entify = (html, minify = false) => {
|
|
1562
|
-
/**
|
|
1563
|
-
* Use entities inside textarea content.
|
|
1564
|
-
*/
|
|
1565
|
-
html = html.replace(/<\s*textarea[^>]*>((.|\n)*?)<s*\/\s*textarea\s*>/g, (match, capture) => {
|
|
1566
|
-
return match.replace(capture, (match) => {
|
|
1567
|
-
return match
|
|
1568
|
-
.replace(/</g, '<')
|
|
1569
|
-
.replace(/>/g, '>')
|
|
1570
|
-
.replace(/"/g, '"')
|
|
1571
|
-
.replace(/'/g, ''')
|
|
1572
|
-
.replace(/\n/g, ' ')
|
|
1573
|
-
.replace(/\r/g, ' ')
|
|
1574
|
-
.replace(/\s/g, ' ')
|
|
1575
|
-
})
|
|
1576
|
-
});
|
|
1577
|
-
|
|
1578
|
-
if (minify) {
|
|
1579
|
-
html = html.replace(/<\s*textarea[^>]*>(.|\n)*?<\s*\/\s*textarea\s*>/g, (match) => {
|
|
1580
|
-
/* This only affects the html tags, since everything else has been entified. */
|
|
1581
|
-
return match
|
|
1582
|
-
.replace(/\s+/g, ' ')
|
|
1583
|
-
.replace(/\s>/g, '>')
|
|
1584
|
-
.replace(/>\s/g, '>')
|
|
1585
|
-
.replace(/\s</g, '<')
|
|
1586
|
-
.replace(/<\s/g, '<')
|
|
1587
|
-
.replace(/<\/\s/g, '<\/')
|
|
1588
|
-
.replace(/class=["']\s/g, (match) => match.replace(/\s/g, ''))
|
|
1589
|
-
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1590
|
-
});
|
|
1591
|
-
}
|
|
1592
|
-
|
|
1593
|
-
return html
|
|
1594
|
-
};
|
|
1595
|
-
|
|
1596
|
-
/**
|
|
1597
|
-
* Remove entity characters for textarea content.
|
|
1598
|
-
* Currently internal use only.
|
|
1599
|
-
*
|
|
1600
|
-
* @param {string} html The HTML string to evaluate.
|
|
1679
|
+
* Minify HTML using configuration already validated by the caller.
|
|
1680
|
+
*
|
|
1681
|
+
* @param {string} html
|
|
1682
|
+
* @param {import('htmlfy').Config} validated_config
|
|
1683
|
+
* @param {boolean} extract_ignored
|
|
1601
1684
|
* @returns {string}
|
|
1602
|
-
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1603
1685
|
*/
|
|
1604
|
-
const
|
|
1605
|
-
/**
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
.replace(/>/g, '>')
|
|
1613
|
-
.replace(/"/g, '"')
|
|
1614
|
-
.replace(/'/g, "'")
|
|
1615
|
-
.replace(/ /g, '\n')
|
|
1616
|
-
.replace(/ /g, '\r')
|
|
1617
|
-
.replace(/ /g, ' ')
|
|
1618
|
-
// Ensure we collapse consecutive spaces, or they'll be completely removed later.
|
|
1619
|
-
.replace(/\s+/g, ' ');
|
|
1620
|
-
|
|
1621
|
-
return match
|
|
1622
|
-
})
|
|
1623
|
-
})
|
|
1624
|
-
};
|
|
1625
|
-
|
|
1626
|
-
/**
|
|
1627
|
-
* @type {Map<any,any>}
|
|
1628
|
-
*/
|
|
1629
|
-
let ignore_map$1;
|
|
1630
|
-
|
|
1631
|
-
/**
|
|
1632
|
-
* Creates a single-line HTML string
|
|
1633
|
-
* by removing line returns, tabs, and relevant spaces.
|
|
1634
|
-
*
|
|
1635
|
-
* @param {string} html The HTML string to minify.
|
|
1636
|
-
* @param {import('htmlfy').UserConfig} [config] A user configuration object.
|
|
1637
|
-
* @returns {string} A minified HTML string.
|
|
1638
|
-
*/
|
|
1639
|
-
const minify = (html, config) => {
|
|
1640
|
-
let reinsert_ignored = false;
|
|
1641
|
-
const { checked_html, ignored, constants } = getState();
|
|
1642
|
-
|
|
1643
|
-
if (!checked_html && !isHtml(html)) return html
|
|
1644
|
-
|
|
1645
|
-
const validated_config = (getState()).config;
|
|
1646
|
-
const ignore = validated_config.ignore.length > 0;
|
|
1647
|
-
|
|
1648
|
-
/* Extract ignored elements. Skipped if prettify has already ignored blocks. */
|
|
1649
|
-
if (!ignored && ignore) {
|
|
1650
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1686
|
+
const minifyHtml = (html, validated_config, extract_ignored) => {
|
|
1687
|
+
/** @type {Map<string,string> | undefined} */
|
|
1688
|
+
let textarea_map;
|
|
1689
|
+
validated_config.ignore.length > 0;
|
|
1690
|
+
|
|
1691
|
+
/* Keep textarea markup out of the general minification passes. */
|
|
1692
|
+
if (!validated_config.ignore.includes('textarea') && html.includes('textarea')) {
|
|
1693
|
+
const { html_with_markers, extracted_map } = extractTextareaBlocks(html);
|
|
1651
1694
|
html = html_with_markers;
|
|
1652
|
-
|
|
1653
|
-
reinsert_ignored = true;
|
|
1695
|
+
textarea_map = extracted_map;
|
|
1654
1696
|
}
|
|
1655
1697
|
|
|
1656
|
-
/**
|
|
1657
|
-
* Ensure textarea content is protected
|
|
1658
|
-
* before general minification.
|
|
1659
|
-
*/
|
|
1660
|
-
html = entify(html, true);
|
|
1661
|
-
|
|
1662
1698
|
/* All other minification. */
|
|
1663
1699
|
// Remove ALL newlines and tabs explicitly.
|
|
1664
1700
|
html = html.replace(/\n|\t/g, '');
|
|
@@ -1697,131 +1733,230 @@ const minify = (html, config) => {
|
|
|
1697
1733
|
html = html.replace(/ = /g, "=");
|
|
1698
1734
|
// Consider safer alternatives if needed (e.g., / = "/g, '="')
|
|
1699
1735
|
|
|
1700
|
-
// Trim whitespace inside attribute values
|
|
1701
|
-
html
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1736
|
+
// Trim whitespace inside quoted attribute values when any are padded.
|
|
1737
|
+
if (/=["']\s|\s["']/.test(html)) {
|
|
1738
|
+
html = transformOpeningTags(html, (tag, name, name_end) => {
|
|
1739
|
+
const chunks = [];
|
|
1740
|
+
let previous_end = 0;
|
|
1741
|
+
let index = name_end;
|
|
1742
|
+
|
|
1743
|
+
while (index < tag.length - 1) {
|
|
1744
|
+
while (/\s/.test(tag[index] || '')) index++;
|
|
1745
|
+
|
|
1746
|
+
const attribute_start = index;
|
|
1747
|
+
while (/[a-zA-Z0-9_-]/.test(tag[index] || '')) index++;
|
|
1748
|
+
if (index === attribute_start || tag[index] !== '=') {
|
|
1749
|
+
index++;
|
|
1750
|
+
continue
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
const quote = tag[index + 1];
|
|
1754
|
+
if (quote !== '"' && quote !== "'") {
|
|
1755
|
+
index++;
|
|
1756
|
+
continue
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
const value_start = index + 2;
|
|
1760
|
+
const value_end = tag.indexOf(quote, value_start);
|
|
1761
|
+
if (value_end === -1) break
|
|
1762
|
+
|
|
1763
|
+
const value = tag.slice(value_start, value_end);
|
|
1764
|
+
const trimmed_value = value.trim();
|
|
1765
|
+
if (trimmed_value !== value) {
|
|
1766
|
+
chunks.push(tag.slice(previous_end, value_start), trimmed_value);
|
|
1767
|
+
previous_end = value_end;
|
|
1768
|
+
}
|
|
1769
|
+
index = value_end + 1;
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
if (chunks.length === 0) return tag
|
|
1773
|
+
chunks.push(tag.slice(previous_end));
|
|
1774
|
+
return chunks.join('')
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1710
1777
|
|
|
1711
1778
|
// Final trim for the whole string
|
|
1712
1779
|
html = html.trim();
|
|
1713
1780
|
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
/* Re-insert ignored elements. Skipped unless minify did the ignore. */
|
|
1718
|
-
if (reinsert_ignored) {
|
|
1719
|
-
html = reinsertIgnoredBlocks(html, ignore_map$1);
|
|
1781
|
+
if (textarea_map) {
|
|
1782
|
+
html = reinsertTextareaBlocks(html, textarea_map);
|
|
1720
1783
|
}
|
|
1721
1784
|
|
|
1722
1785
|
return html
|
|
1723
1786
|
};
|
|
1724
1787
|
|
|
1725
1788
|
/**
|
|
1726
|
-
*
|
|
1789
|
+
* Minify HTML that has already been checked and had ignored blocks extracted.
|
|
1790
|
+
*
|
|
1791
|
+
* @param {string} html
|
|
1792
|
+
* @param {import('htmlfy').Config} config
|
|
1793
|
+
* @returns {string}
|
|
1727
1794
|
*/
|
|
1728
|
-
const
|
|
1729
|
-
|
|
1730
|
-
|
|
1795
|
+
const minifyKnownHtml = (html, config) => minifyHtml(html, config);
|
|
1796
|
+
|
|
1797
|
+
const VOID_ELEMENT_SET = new Set(VOID_ELEMENTS);
|
|
1798
|
+
const TOKEN_CLOSING = 1;
|
|
1799
|
+
const TOKEN_COMMENT = 2;
|
|
1800
|
+
const TOKEN_DOCTYPE = 4;
|
|
1801
|
+
const TOKEN_IGNORED = 8;
|
|
1802
|
+
const TOKEN_SELF_CLOSING = 16;
|
|
1803
|
+
const TOKEN_SYNTHETIC_SELF_CLOSING = 32;
|
|
1804
|
+
const TOKEN_INLINE = 64;
|
|
1731
1805
|
|
|
1732
1806
|
/**
|
|
1733
|
-
* @
|
|
1807
|
+
* @typedef {object} Token
|
|
1808
|
+
* @property {'tag' | 'text'} type
|
|
1809
|
+
* @property {string} value
|
|
1810
|
+
* @property {number} flags
|
|
1734
1811
|
*/
|
|
1735
|
-
let ignore_map;
|
|
1736
1812
|
|
|
1737
1813
|
/**
|
|
1738
1814
|
* Isolate tags, content, and comments.
|
|
1739
1815
|
*
|
|
1740
1816
|
* @param {string} html The HTML string to evaluate.
|
|
1741
|
-
* @
|
|
1742
|
-
*
|
|
1743
|
-
* Hello World!
|
|
1744
|
-
* [#-# : 1 : </div> : #-#]
|
|
1817
|
+
* @param {import('./utils.js').HtmlfyConstants} constants
|
|
1818
|
+
* @returns {Token[]}
|
|
1745
1819
|
*/
|
|
1746
|
-
const enqueue = (html) => {
|
|
1747
|
-
|
|
1748
|
-
|
|
1820
|
+
const enqueue = (html, constants) => {
|
|
1821
|
+
/** @type {Token[]} */
|
|
1822
|
+
const lines = [];
|
|
1749
1823
|
/* Regex to find tags OR text content between tags. */
|
|
1750
|
-
const regex =
|
|
1824
|
+
const regex = /<[^>]+>|[^<]+/g;
|
|
1825
|
+
|
|
1826
|
+
/* Use replace for callback iteration, but avoid constructing the old marker output. */
|
|
1827
|
+
html.replace(regex, (value) => {
|
|
1828
|
+
const type = value.startsWith('<') ? 'tag' : 'text';
|
|
1829
|
+
|
|
1830
|
+
if (type === 'text') {
|
|
1831
|
+
if (value.trim().length > 0) {
|
|
1832
|
+
lines.push({
|
|
1833
|
+
type,
|
|
1834
|
+
value,
|
|
1835
|
+
flags: value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_') ? TOKEN_IGNORED : 0,
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
return ''
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
const trimmed = value.trim();
|
|
1842
|
+
const synthetic_self_closing = trimmed.endsWith(constants.SELF_CLOSING_PLACEHOLDER);
|
|
1843
|
+
let flags = 0;
|
|
1844
|
+
if (trimmed.startsWith('</')) flags |= TOKEN_CLOSING;
|
|
1845
|
+
if (trimmed.startsWith('<!--')) flags |= TOKEN_COMMENT;
|
|
1846
|
+
if (trimmed.startsWith('<!doctype')) flags |= TOKEN_DOCTYPE;
|
|
1847
|
+
if (trimmed.endsWith('/>') || synthetic_self_closing) flags |= TOKEN_SELF_CLOSING;
|
|
1848
|
+
if (synthetic_self_closing) flags |= TOKEN_SYNTHETIC_SELF_CLOSING;
|
|
1849
|
+
|
|
1850
|
+
lines.push({
|
|
1851
|
+
type,
|
|
1852
|
+
value,
|
|
1853
|
+
flags,
|
|
1854
|
+
});
|
|
1855
|
+
return ''
|
|
1856
|
+
});
|
|
1751
1857
|
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1858
|
+
return lines
|
|
1859
|
+
};
|
|
1860
|
+
|
|
1861
|
+
/**
|
|
1862
|
+
* Collapse the same simple element pairs handled by the final output regex.
|
|
1863
|
+
*
|
|
1864
|
+
* @param {Token[]} lines
|
|
1865
|
+
* @returns {Token[]}
|
|
1866
|
+
*/
|
|
1867
|
+
const collapseInlineTokens = (lines) => {
|
|
1868
|
+
/** @type {Token[]} */
|
|
1869
|
+
const collapsed = [];
|
|
1870
|
+
|
|
1871
|
+
for (let index = 0; index < lines.length; index++) {
|
|
1872
|
+
const opening = lines[index];
|
|
1873
|
+
|
|
1874
|
+
if (opening.type === 'tag' && !(opening.flags & (
|
|
1875
|
+
TOKEN_CLOSING |
|
|
1876
|
+
TOKEN_COMMENT |
|
|
1877
|
+
TOKEN_DOCTYPE |
|
|
1878
|
+
TOKEN_SELF_CLOSING
|
|
1879
|
+
))) {
|
|
1880
|
+
const name_match = opening.value.match(/^<([^>\s]+)[^>]*>$/);
|
|
1881
|
+
const name = name_match?.[1];
|
|
1882
|
+
const content = lines[index + 1];
|
|
1883
|
+
const possible_closing = content?.type === 'text' ? lines[index + 2] : content;
|
|
1884
|
+
|
|
1885
|
+
if (name && possible_closing?.value === `</${name}>`) {
|
|
1886
|
+
const empty_pair = content?.type === 'tag' && /^[\w:._-]+$/.test(name);
|
|
1887
|
+
const text_pair = content?.type === 'text' && /[^><\/\s]/.test(content.value);
|
|
1888
|
+
|
|
1889
|
+
if (empty_pair || text_pair) {
|
|
1890
|
+
collapsed.push({
|
|
1891
|
+
type: 'tag',
|
|
1892
|
+
value: opening.value + (text_pair ? content.value.trim() : '') + possible_closing.value,
|
|
1893
|
+
flags: TOKEN_INLINE,
|
|
1894
|
+
});
|
|
1895
|
+
index += text_pair ? 2 : 1;
|
|
1896
|
+
continue
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1758
1899
|
}
|
|
1759
1900
|
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1901
|
+
collapsed.push(opening);
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
return collapsed
|
|
1763
1905
|
};
|
|
1764
1906
|
|
|
1765
1907
|
/**
|
|
1766
1908
|
* Process enqueued content.
|
|
1767
1909
|
*
|
|
1910
|
+
* @param {Token[]} lines
|
|
1911
|
+
* @param {import('htmlfy').Config} config
|
|
1912
|
+
* @param {import('./utils.js').HtmlfyConstants} constants
|
|
1768
1913
|
* @returns {string}
|
|
1769
1914
|
*/
|
|
1770
|
-
const process = () => {
|
|
1771
|
-
const { config, constants } = getState();
|
|
1915
|
+
const process = (lines, config, constants) => {
|
|
1772
1916
|
const step = " ".repeat(config.tab_size);
|
|
1773
1917
|
const tag_wrap = config.tag_wrap;
|
|
1774
1918
|
const content_wrap = config.content_wrap;
|
|
1775
1919
|
const strict = config.strict;
|
|
1776
1920
|
|
|
1777
1921
|
/* Track current number of indentations needed. */
|
|
1778
|
-
let
|
|
1922
|
+
let indent_level = 0;
|
|
1779
1923
|
|
|
1780
1924
|
/** @type string[] */
|
|
1781
1925
|
const output_lines = [];
|
|
1782
|
-
const tag_regex = /<[A-Za-z]+\b[^>]*(?:.|\n)
|
|
1926
|
+
const tag_regex = /<[A-Za-z]+\b[^>]*(?:.|\n)*?\/?>/; /* Is opening tag or void element. */
|
|
1783
1927
|
const attribute_regex = /\s{1}[A-Za-z:@#*?$()\[\].-]+(?:=".*?")?/g; /* Matches all tag/element attributes. */
|
|
1784
1928
|
|
|
1785
1929
|
/* Process lines and indent. */
|
|
1786
|
-
|
|
1930
|
+
lines.forEach((source, index) => {
|
|
1787
1931
|
let current_line_value = source.value;
|
|
1788
1932
|
|
|
1789
|
-
const is_ignored_content =
|
|
1790
|
-
current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
|
|
1791
|
-
|
|
1792
1933
|
let subtrahend = 0;
|
|
1793
|
-
const prev_line_data =
|
|
1794
|
-
const prev_line_value = prev_line_data?.value ?? ""; // Use empty string if no prev line
|
|
1934
|
+
const prev_line_data = lines[index - 1];
|
|
1795
1935
|
|
|
1796
|
-
|
|
1797
|
-
* Arbitratry character, to keep track of the string's length.
|
|
1798
|
-
*/
|
|
1799
|
-
indents += '0';
|
|
1936
|
+
indent_level++;
|
|
1800
1937
|
|
|
1801
1938
|
if (index === 0) subtrahend++;
|
|
1802
1939
|
/* We're processing a closing tag. */
|
|
1803
|
-
if (
|
|
1940
|
+
if (source.flags & TOKEN_CLOSING) subtrahend++;
|
|
1804
1941
|
/* prevLine is a doctype declaration. */
|
|
1805
|
-
if (
|
|
1942
|
+
if (prev_line_data && (prev_line_data.flags & TOKEN_DOCTYPE)) subtrahend++;
|
|
1806
1943
|
/* prevLine is a comment. */
|
|
1807
|
-
if (
|
|
1944
|
+
if (prev_line_data && (prev_line_data.flags & TOKEN_COMMENT)) subtrahend++;
|
|
1808
1945
|
/* prevLine is a void element. */
|
|
1809
|
-
if (
|
|
1810
|
-
prev_line_value.trim().endsWith("/>") // native self-closing
|
|
1811
|
-
||
|
|
1812
|
-
prev_line_value.trim().endsWith(constants.SELF_CLOSING_PLACEHOLDER) // synthetic self-closing
|
|
1813
|
-
) subtrahend++;
|
|
1946
|
+
if (prev_line_data && (prev_line_data.flags & TOKEN_SELF_CLOSING)) subtrahend++;
|
|
1814
1947
|
/* prevLine is a closing tag. */
|
|
1815
|
-
if (
|
|
1948
|
+
if (prev_line_data && (prev_line_data.flags & TOKEN_CLOSING)) subtrahend++;
|
|
1949
|
+
/* prevLine opens and closes on the same line. */
|
|
1950
|
+
if (prev_line_data && (prev_line_data.flags & TOKEN_INLINE)) subtrahend++;
|
|
1816
1951
|
/* prevLine is text. */
|
|
1817
1952
|
if (prev_line_data?.type === "text") subtrahend++;
|
|
1818
1953
|
|
|
1819
1954
|
/* Determine offset for line indentation. */
|
|
1820
|
-
const offset = Math.max(0,
|
|
1955
|
+
const offset = Math.max(0, indent_level - subtrahend);
|
|
1821
1956
|
/* Correct indent level for *this* line's content */
|
|
1822
1957
|
const current_indent_level = offset; // Store the level for this line
|
|
1823
1958
|
|
|
1824
|
-
|
|
1959
|
+
indent_level = current_indent_level;
|
|
1825
1960
|
|
|
1826
1961
|
/**
|
|
1827
1962
|
* Starts with a single punctuation character.
|
|
@@ -1844,25 +1979,26 @@ const process = () => {
|
|
|
1844
1979
|
|
|
1845
1980
|
const padding = step.repeat(current_indent_level);
|
|
1846
1981
|
|
|
1847
|
-
if (
|
|
1982
|
+
if (source.flags & TOKEN_IGNORED) {
|
|
1848
1983
|
/* Stop processing this line, as it's set to be ignored. */
|
|
1849
1984
|
output_lines.push(current_line_value);
|
|
1850
1985
|
} else {
|
|
1851
1986
|
/* Remove comment. */
|
|
1852
|
-
if (strict &&
|
|
1987
|
+
if (strict && (source.flags & TOKEN_COMMENT))
|
|
1853
1988
|
return
|
|
1854
1989
|
|
|
1855
1990
|
let result = current_line_value;
|
|
1856
1991
|
|
|
1857
1992
|
/* Remove self-closing placeholder, if needed. */
|
|
1858
|
-
|
|
1993
|
+
if (source.flags & TOKEN_SYNTHETIC_SELF_CLOSING)
|
|
1994
|
+
result = result.replace(constants.SELF_CLOSING_PLACEHOLDER, '>');
|
|
1859
1995
|
|
|
1860
1996
|
if (
|
|
1861
1997
|
source.type === 'text' &&
|
|
1862
1998
|
content_wrap > 0 &&
|
|
1863
1999
|
result.length >= content_wrap
|
|
1864
2000
|
) {
|
|
1865
|
-
result = wordWrap(result, content_wrap, padding);
|
|
2001
|
+
result = wordWrap(result, content_wrap, padding, constants);
|
|
1866
2002
|
}
|
|
1867
2003
|
/* Wrap the attributes of open tags and void elements. */
|
|
1868
2004
|
else if (
|
|
@@ -1870,30 +2006,36 @@ const process = () => {
|
|
|
1870
2006
|
result.length > tag_wrap &&
|
|
1871
2007
|
tag_regex.test(result)
|
|
1872
2008
|
) {
|
|
1873
|
-
tag_regex.lastIndex = 0; // Reset stateful regex
|
|
1874
2009
|
attribute_regex.lastIndex = 0; // Reset stateful regex
|
|
1875
2010
|
|
|
1876
|
-
const
|
|
2011
|
+
const attributes = [];
|
|
2012
|
+
let first_attribute_start = -1;
|
|
2013
|
+
let last_attribute_end = -1;
|
|
2014
|
+
let attribute_match;
|
|
1877
2015
|
|
|
1878
|
-
|
|
1879
|
-
|
|
2016
|
+
while ((attribute_match = attribute_regex.exec(result)) !== null) {
|
|
2017
|
+
if (first_attribute_start === -1) first_attribute_start = attribute_match.index;
|
|
2018
|
+
last_attribute_end = attribute_regex.lastIndex;
|
|
2019
|
+
attributes.push(attribute_match[0].trim());
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
if (attributes.length > 0) {
|
|
2023
|
+
const opening_part = result.slice(0, first_attribute_start);
|
|
2024
|
+
const closing_part = result.slice(last_attribute_end).trim();
|
|
1880
2025
|
const inner_padding = padding + step;
|
|
1881
|
-
|
|
2026
|
+
const wrapped_tag = [padding + opening_part];
|
|
1882
2027
|
|
|
1883
|
-
for (const
|
|
1884
|
-
|
|
1885
|
-
wrapped_tag += inner_padding + attribute_string + "\n";
|
|
2028
|
+
for (const attribute of attributes) {
|
|
2029
|
+
wrapped_tag.push(inner_padding + attribute);
|
|
1886
2030
|
}
|
|
1887
2031
|
|
|
1888
|
-
const tag_name_match =
|
|
2032
|
+
const tag_name_match = opening_part.match(/<([A-Za-z_:-]+)/);
|
|
1889
2033
|
const tag_name = tag_name_match ? tag_name_match[1] : "";
|
|
1890
|
-
const is_self_closing =
|
|
1891
|
-
const closing_part = tag_parts[1].trim();
|
|
2034
|
+
const is_self_closing = result.endsWith("/>") && VOID_ELEMENT_SET.has(tag_name);
|
|
1892
2035
|
const closing_padding = padding + (strict && is_self_closing ? " " : "");
|
|
1893
2036
|
|
|
1894
|
-
wrapped_tag
|
|
1895
|
-
|
|
1896
|
-
result = wrapped_tag; // Assign the fully wrapped string
|
|
2037
|
+
wrapped_tag.push(closing_padding + closing_part);
|
|
2038
|
+
result = wrapped_tag.join('\n');
|
|
1897
2039
|
} else {
|
|
1898
2040
|
result = padding + result;
|
|
1899
2041
|
}
|
|
@@ -1911,30 +2053,32 @@ const process = () => {
|
|
|
1911
2053
|
let final_html = output_lines.join("\n");
|
|
1912
2054
|
|
|
1913
2055
|
/* Preserve wrapped attributes. */
|
|
1914
|
-
if (tag_wrap > 0) final_html = protectAttributes(final_html);
|
|
2056
|
+
if (tag_wrap > 0) final_html = protectAttributes(final_html, constants);
|
|
1915
2057
|
|
|
1916
2058
|
/* Extra preserve wrapped content. */
|
|
1917
2059
|
if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
|
|
1918
|
-
final_html = finalProtectContent(final_html);
|
|
2060
|
+
final_html = finalProtectContent(final_html, constants);
|
|
1919
2061
|
|
|
1920
2062
|
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
2063
|
+
if (tag_wrap > 0 || content_wrap > 0) {
|
|
2064
|
+
final_html = final_html.replace(
|
|
2065
|
+
/<(?<Element>[^>\s]+)[^>]*>[^<]*?[^><\/\s][^<]*?<\/\k<Element>>|<script[^>]*>[\s]*<\/script>|<([\w:\._-]+)([^>]*)><\/\2>|<([\w:\._-]+)([^>]*)>[\s]+<\/\4>/g,
|
|
2066
|
+
match => {
|
|
2067
|
+
// Check if this contains placeholder
|
|
2068
|
+
if (match.includes(constants.SELF_CLOSING_PLACEHOLDER) || match.includes(constants.CONTENT_IGNORE_PLACEHOLDER)) {
|
|
2069
|
+
return match // Don't modify if it contains the placeholder
|
|
2070
|
+
}
|
|
1928
2071
|
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
2072
|
+
return match.replace(/\n|\t|\s{2,}/g, '')
|
|
2073
|
+
}
|
|
2074
|
+
);
|
|
2075
|
+
}
|
|
1932
2076
|
|
|
1933
2077
|
/* Revert wrapped content. */
|
|
1934
|
-
if (content_wrap > 0) final_html = unprotectContent(final_html);
|
|
2078
|
+
if (content_wrap > 0) final_html = unprotectContent(final_html, constants);
|
|
1935
2079
|
|
|
1936
2080
|
/* Revert wrapped attributes. */
|
|
1937
|
-
if (tag_wrap > 0) final_html = unprotectAttributes(final_html);
|
|
2081
|
+
if (tag_wrap > 0) final_html = unprotectAttributes(final_html, constants);
|
|
1938
2082
|
|
|
1939
2083
|
/* Remove self-closing nature of void elements. */
|
|
1940
2084
|
if (strict) final_html = final_html.replace(/\s\/>|\/>/g, '>');
|
|
@@ -1954,43 +2098,44 @@ const process = () => {
|
|
|
1954
2098
|
* @returns {string} A well-formed HTML string.
|
|
1955
2099
|
*/
|
|
1956
2100
|
const prettify = (html, config) => {
|
|
1957
|
-
let reinsert_ignored = false;
|
|
1958
|
-
const { checked_html, ignored } = getState();
|
|
1959
|
-
|
|
1960
2101
|
/* Return content as-is if it does not contain any HTML elements. */
|
|
1961
|
-
if (!
|
|
2102
|
+
if (!isHtml(html)) return html
|
|
1962
2103
|
|
|
1963
|
-
/* Runs setState for config. */
|
|
1964
2104
|
const validated_config = validateConfig(config || {});
|
|
2105
|
+
const constants = createConstants(validated_config.ignore_with);
|
|
1965
2106
|
|
|
1966
2107
|
const ignore = validated_config.ignore.length > 0;
|
|
1967
2108
|
|
|
2109
|
+
/** @type {Map<any,any> | undefined} */
|
|
2110
|
+
let ignore_map;
|
|
2111
|
+
|
|
1968
2112
|
/* Allows you to trimify before ignoring. */
|
|
1969
2113
|
if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
|
|
1970
2114
|
|
|
1971
2115
|
/* Extract ignored elements. */
|
|
1972
|
-
if (
|
|
1973
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
2116
|
+
if (ignore) {
|
|
2117
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html, validated_config.ignore);
|
|
1974
2118
|
html = html_with_markers;
|
|
1975
2119
|
ignore_map = extracted_map;
|
|
1976
|
-
reinsert_ignored = true;
|
|
1977
2120
|
}
|
|
1978
2121
|
|
|
1979
2122
|
/* Preserve html text within attribute values. */
|
|
1980
|
-
html = setIgnoreAttribute(html);
|
|
2123
|
+
html = setIgnoreAttribute(html, constants);
|
|
1981
2124
|
|
|
1982
2125
|
/* Insert placeholder for void elements that aren't self-closing. */
|
|
1983
|
-
html = setSelfClosing(html);
|
|
2126
|
+
html = setSelfClosing(html, constants);
|
|
1984
2127
|
|
|
1985
|
-
html =
|
|
1986
|
-
enqueue(html);
|
|
1987
|
-
|
|
2128
|
+
html = minifyKnownHtml(html, validated_config);
|
|
2129
|
+
let lines = enqueue(html, constants);
|
|
2130
|
+
if (validated_config.tag_wrap === 0 && validated_config.content_wrap === 0)
|
|
2131
|
+
lines = collapseInlineTokens(lines);
|
|
2132
|
+
html = process(lines, validated_config, constants);
|
|
1988
2133
|
|
|
1989
2134
|
/* Revert html text within attribute values. */
|
|
1990
|
-
html = unsetIgnoreAttribute(html);
|
|
2135
|
+
html = unsetIgnoreAttribute(html, constants);
|
|
1991
2136
|
|
|
1992
2137
|
/* Re-insert ignored elements. */
|
|
1993
|
-
if (
|
|
2138
|
+
if (ignore_map) {
|
|
1994
2139
|
html = reinsertIgnoredBlocks(html, ignore_map);
|
|
1995
2140
|
}
|
|
1996
2141
|
|