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