@mgdis/core-ui-helpers 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +15 -15
- package/CHANGELOG.md +7 -0
- package/dist/storybook/index.cjs +222 -152
- package/dist/storybook/index.cjs.map +1 -1
- package/dist/storybook/index.js +222 -152
- package/dist/storybook/index.js.map +1 -1
- package/dist/storybook/index.umd.js +222 -152
- package/dist/storybook/index.umd.js.map +1 -1
- package/package.json +10 -10
package/dist/storybook/index.cjs
CHANGED
|
@@ -930,37 +930,55 @@ var renderVdom = (hostRef, renderFnResults, isInitialLoad = false) => {
|
|
|
930
930
|
const CONFIG = {
|
|
931
931
|
content_wrap: 0,
|
|
932
932
|
ignore: [],
|
|
933
|
-
ignore_with: '
|
|
933
|
+
ignore_with: '!i-£___£%_',
|
|
934
934
|
strict: false,
|
|
935
935
|
tab_size: 2,
|
|
936
936
|
tag_wrap: 0,
|
|
937
|
-
tag_wrap_width: 80,
|
|
938
937
|
trim: []
|
|
939
938
|
};
|
|
940
939
|
|
|
941
|
-
const CONTENT_IGNORE_STRING = '__!i-£___£%__';
|
|
942
|
-
const IGNORE_STRING = '!i-£___£%_';
|
|
943
|
-
|
|
944
940
|
const VOID_ELEMENTS = [
|
|
945
941
|
'area', 'base', 'br', 'col', 'embed', 'hr',
|
|
946
942
|
'img', 'input', 'link', 'meta',
|
|
947
943
|
'param', 'source', 'track', 'wbr'
|
|
948
944
|
];
|
|
949
945
|
|
|
946
|
+
/**
|
|
947
|
+
* Defined by state.js and configuration.
|
|
948
|
+
*
|
|
949
|
+
* CONTENT_IGNORE_PLACEHOLDER
|
|
950
|
+
* SELF_CLOSING_PLACEHOLDER
|
|
951
|
+
* ATTRIBUTE_IGNORE_PLACEHOLDER
|
|
952
|
+
*/
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* @typedef {object} Constants
|
|
956
|
+
* @property {string} CONTENT_IGNORE_PLACEHOLDER
|
|
957
|
+
* @property {string} SELF_CLOSING_PLACEHOLDER
|
|
958
|
+
* @property {string} ATTRIBUTE_IGNORE_PLACEHOLDER
|
|
959
|
+
*/
|
|
950
960
|
/**
|
|
951
961
|
* @typedef {object} State
|
|
952
962
|
* @property {boolean} checked_html - If passed in HTML has been checked for HTML within it.
|
|
953
963
|
* @property {import("htmlfy").Config} config - Validated configuration.
|
|
954
964
|
* @property {boolean} ignored
|
|
965
|
+
* @property {Constants} constants - Constant strings, influenced by ignore_with.
|
|
955
966
|
*/
|
|
956
967
|
|
|
957
968
|
/**
|
|
958
969
|
* @type State
|
|
970
|
+
*
|
|
971
|
+
* `constants` prefixes and suffixes must be in sync with those in utils.js
|
|
959
972
|
*/
|
|
960
973
|
const state = {
|
|
961
974
|
checked_html: false,
|
|
962
975
|
config: { ...CONFIG },
|
|
963
|
-
ignored: false
|
|
976
|
+
ignored: false,
|
|
977
|
+
constants: {
|
|
978
|
+
CONTENT_IGNORE_PLACEHOLDER: `${CONFIG.ignore_with}_`,
|
|
979
|
+
SELF_CLOSING_PLACEHOLDER: `${CONFIG.ignore_with}/_>`,
|
|
980
|
+
ATTRIBUTE_IGNORE_PLACEHOLDER: `${CONFIG.ignore_with}=_`
|
|
981
|
+
}
|
|
964
982
|
};
|
|
965
983
|
|
|
966
984
|
/**
|
|
@@ -1047,7 +1065,16 @@ const mergeObjects = (current, updates) => {
|
|
|
1047
1065
|
*/
|
|
1048
1066
|
const mergeConfig = (default_config, config) => {
|
|
1049
1067
|
const validated_config = mergeObjects(default_config, config);
|
|
1050
|
-
|
|
1068
|
+
|
|
1069
|
+
/* Below `constants` prefixes and suffixes must be in sync with those in state.js */
|
|
1070
|
+
setState({
|
|
1071
|
+
config: validated_config,
|
|
1072
|
+
constants: {
|
|
1073
|
+
CONTENT_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}_`,
|
|
1074
|
+
SELF_CLOSING_PLACEHOLDER: `${validated_config.ignore_with}/_>`,
|
|
1075
|
+
ATTRIBUTE_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}=_`
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1051
1078
|
return validated_config
|
|
1052
1079
|
};
|
|
1053
1080
|
|
|
@@ -1056,12 +1083,14 @@ const mergeConfig = (default_config, config) => {
|
|
|
1056
1083
|
* @param {string} html
|
|
1057
1084
|
*/
|
|
1058
1085
|
const protectAttributes = (html) => {
|
|
1086
|
+
const { constants } = getState();
|
|
1087
|
+
|
|
1059
1088
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1060
1089
|
return match.replace(capture, (match) => {
|
|
1061
1090
|
return match
|
|
1062
|
-
.replace(/\n/g,
|
|
1063
|
-
.replace(/\r/g,
|
|
1064
|
-
.replace(/\s/g,
|
|
1091
|
+
.replace(/\n/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'nl!')
|
|
1092
|
+
.replace(/\r/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'cr!')
|
|
1093
|
+
.replace(/\s/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'ws!')
|
|
1065
1094
|
})
|
|
1066
1095
|
});
|
|
1067
1096
|
|
|
@@ -1073,10 +1102,12 @@ const protectAttributes = (html) => {
|
|
|
1073
1102
|
* @param {string} html
|
|
1074
1103
|
*/
|
|
1075
1104
|
const protectContent = (html) => {
|
|
1105
|
+
const { constants } = getState();
|
|
1106
|
+
|
|
1076
1107
|
return html
|
|
1077
|
-
.replace(/\n/g,
|
|
1078
|
-
.replace(/\r/g,
|
|
1079
|
-
.replace(/\s/g,
|
|
1108
|
+
.replace(/\n/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!')
|
|
1109
|
+
.replace(/\r/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!')
|
|
1110
|
+
.replace(/\s/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'ws!')
|
|
1080
1111
|
};
|
|
1081
1112
|
|
|
1082
1113
|
/**
|
|
@@ -1084,7 +1115,9 @@ const protectContent = (html) => {
|
|
|
1084
1115
|
* @param {string} html
|
|
1085
1116
|
*/
|
|
1086
1117
|
const finalProtectContent = (html) => {
|
|
1087
|
-
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;
|
|
1118
|
+
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;
|
|
1119
|
+
const { constants } = getState();
|
|
1120
|
+
|
|
1088
1121
|
return html
|
|
1089
1122
|
.replace(regex, (/** @type {string} */match, p1, p2, p3, p4) => {
|
|
1090
1123
|
const text_to_protect = p2 || p4;
|
|
@@ -1093,9 +1126,9 @@ const finalProtectContent = (html) => {
|
|
|
1093
1126
|
return match
|
|
1094
1127
|
|
|
1095
1128
|
const protected_text = text_to_protect
|
|
1096
|
-
.replace(/\n/g,
|
|
1097
|
-
.replace(/\r/g,
|
|
1098
|
-
.replace(/\s/g,
|
|
1129
|
+
.replace(/\n/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!')
|
|
1130
|
+
.replace(/\r/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!')
|
|
1131
|
+
.replace(/\s/g, constants.CONTENT_IGNORE_PLACEHOLDER + "ws!");
|
|
1099
1132
|
|
|
1100
1133
|
return match.replace(text_to_protect, protected_text)
|
|
1101
1134
|
})
|
|
@@ -1108,13 +1141,14 @@ const finalProtectContent = (html) => {
|
|
|
1108
1141
|
* @returns {string}
|
|
1109
1142
|
*/
|
|
1110
1143
|
const setIgnoreAttribute = (html) => {
|
|
1111
|
-
const regex = /<([A-Za-z][A-Za-z0-9]*|[a-z][a-z0-9._]*-[a-z0-9._-]+)((?:\s+[A-Za-z0-9_-]+="[^"]*"|\s*[a-z]*)*)>/g;
|
|
1144
|
+
const regex = /<([A-Za-z][A-Za-z0-9]*|[a-z][a-z0-9._]*-[a-z0-9._-]+)((?:\s+[A-Za-z0-9_-]+="[^"]*"|\s*[a-z]*)*)>/g;
|
|
1145
|
+
const { constants } = getState();
|
|
1112
1146
|
|
|
1113
1147
|
html = html.replace(regex, (/** @type {string} */match, p1, p2) => {
|
|
1114
1148
|
return match.replace(p2, (match) => {
|
|
1115
1149
|
return match
|
|
1116
|
-
.replace(/</g,
|
|
1117
|
-
.replace(/>/g,
|
|
1150
|
+
.replace(/</g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'lt!')
|
|
1151
|
+
.replace(/>/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'gt!')
|
|
1118
1152
|
})
|
|
1119
1153
|
});
|
|
1120
1154
|
|
|
@@ -1147,12 +1181,14 @@ const trimify = (html, trim) => {
|
|
|
1147
1181
|
* @param {string} html
|
|
1148
1182
|
*/
|
|
1149
1183
|
const unprotectAttributes = (html) => {
|
|
1184
|
+
const { constants } = getState();
|
|
1185
|
+
|
|
1150
1186
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1151
1187
|
return match.replace(capture, (match) => {
|
|
1152
1188
|
return match
|
|
1153
|
-
.replace(new RegExp(
|
|
1154
|
-
.replace(new RegExp(
|
|
1155
|
-
.replace(new RegExp(
|
|
1189
|
+
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'nl!', "g"), '\n')
|
|
1190
|
+
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'cr!', "g"), '\r')
|
|
1191
|
+
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'ws!', "g"), ' ')
|
|
1156
1192
|
})
|
|
1157
1193
|
});
|
|
1158
1194
|
|
|
@@ -1164,25 +1200,20 @@ const unprotectAttributes = (html) => {
|
|
|
1164
1200
|
* @param {string} html
|
|
1165
1201
|
*/
|
|
1166
1202
|
const unprotectContent = (html) => {
|
|
1167
|
-
|
|
1168
|
-
|
|
1203
|
+
const { constants } = getState();
|
|
1204
|
+
|
|
1205
|
+
html = html.replace(new RegExp(`.*${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!.*`, "g"), (/** @type {string} */match) => {
|
|
1206
|
+
return match.replace(new RegExp(`${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!`, "g"), (match) => {
|
|
1169
1207
|
return match
|
|
1170
|
-
.replace(new RegExp(
|
|
1171
|
-
.replace(new RegExp(
|
|
1172
|
-
.replace(new RegExp(
|
|
1208
|
+
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!', "g"), '\n')
|
|
1209
|
+
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!', "g"), '\r')
|
|
1210
|
+
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'ws!', "g"), ' ')
|
|
1173
1211
|
})
|
|
1174
1212
|
});
|
|
1175
1213
|
|
|
1176
1214
|
return html
|
|
1177
1215
|
};
|
|
1178
1216
|
|
|
1179
|
-
const escapedIgnoreString = IGNORE_STRING.replace(
|
|
1180
|
-
/[-\/\\^$*+?.()|[\]{}]/g,
|
|
1181
|
-
"\\$&"
|
|
1182
|
-
);
|
|
1183
|
-
const ltPlaceholderRegex = new RegExp(escapedIgnoreString + "lt!", "g");
|
|
1184
|
-
const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
1185
|
-
|
|
1186
1217
|
/**
|
|
1187
1218
|
* Replace ignore string with html brackets.
|
|
1188
1219
|
*
|
|
@@ -1192,6 +1223,13 @@ const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
|
1192
1223
|
const unsetIgnoreAttribute = (html) => {
|
|
1193
1224
|
/* Regex to find opening tags and capture their attributes. */
|
|
1194
1225
|
const tagRegex = /<([\w:\-]+)([^>]*)>/g;
|
|
1226
|
+
const { constants } = getState();
|
|
1227
|
+
const escapedIgnoreString = constants.ATTRIBUTE_IGNORE_PLACEHOLDER.replace(
|
|
1228
|
+
/[-\/\\^$*+?.()|[\]{}]/g,
|
|
1229
|
+
"\\$&"
|
|
1230
|
+
);
|
|
1231
|
+
const ltPlaceholderRegex = new RegExp(escapedIgnoreString + "lt!", "g");
|
|
1232
|
+
const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
1195
1233
|
|
|
1196
1234
|
return html.replace(
|
|
1197
1235
|
tagRegex,
|
|
@@ -1228,7 +1266,6 @@ const validateConfig = (config) => {
|
|
|
1228
1266
|
Object.hasOwn(config, 'strict') ||
|
|
1229
1267
|
Object.hasOwn(config, 'tab_size') ||
|
|
1230
1268
|
Object.hasOwn(config, 'tag_wrap') ||
|
|
1231
|
-
Object.hasOwn(config, 'tag_wrap_width') ||
|
|
1232
1269
|
Object.hasOwn(config, 'trim')
|
|
1233
1270
|
);
|
|
1234
1271
|
|
|
@@ -1256,37 +1293,29 @@ const validateConfig = (config) => {
|
|
|
1256
1293
|
}
|
|
1257
1294
|
|
|
1258
1295
|
if (Object.hasOwn(config, 'content_wrap') && typeof config.content_wrap !== 'number')
|
|
1259
|
-
throw new Error(`content_wrap config must be a number, not ${typeof config.
|
|
1296
|
+
throw new Error(`content_wrap config must be a number, not ${typeof config.content_wrap}.`)
|
|
1260
1297
|
|
|
1261
1298
|
if (Object.hasOwn(config, 'ignore') && (!Array.isArray(config.ignore) || !config.ignore?.every((e) => typeof e === 'string')))
|
|
1262
1299
|
throw new Error('Ignore config must be an array of strings.')
|
|
1263
1300
|
|
|
1264
|
-
if (Object.hasOwn(config, 'ignore_with')
|
|
1265
|
-
|
|
1301
|
+
if (Object.hasOwn(config, 'ignore_with')) {
|
|
1302
|
+
if (typeof config.ignore_with !== 'string')
|
|
1303
|
+
throw new Error(`ignore_with must be a string, not ${typeof config.ignore_with}.`)
|
|
1304
|
+
else if (config.ignore_with.startsWith('_'))
|
|
1305
|
+
/**
|
|
1306
|
+
* This negatively affects processing of preserved tag attributes,
|
|
1307
|
+
* because tag names can end with an underscore, so the regex
|
|
1308
|
+
* does not capture them.
|
|
1309
|
+
*/
|
|
1310
|
+
throw new Error(`ignore_with cannot start with an underscore.`)
|
|
1311
|
+
}
|
|
1266
1312
|
|
|
1267
1313
|
if (Object.hasOwn(config, 'strict') && typeof config.strict !== 'boolean')
|
|
1268
1314
|
throw new Error(`Strict config must be a boolean, not ${typeof config.strict}.`)
|
|
1269
|
-
|
|
1270
|
-
/* TODO remove in v0.9.0 */
|
|
1271
|
-
if (Object.hasOwn(config, 'tag_wrap') && typeof config.tag_wrap === 'boolean') {
|
|
1272
|
-
console.warn('tag_wrap as a boolean is deprecated, and will not be supported in v0.9.0+. Use `tag_wrap: <number>` instead; where <number> is the max character width acceptable before wrapping attributes.');
|
|
1273
|
-
if (config.tag_wrap_width)
|
|
1274
|
-
config.tag_wrap = config.tag_wrap_width;
|
|
1275
|
-
else
|
|
1276
|
-
config.tag_wrap = default_config.tag_wrap_width;
|
|
1277
|
-
}
|
|
1278
1315
|
|
|
1279
1316
|
if (Object.hasOwn(config, 'tag_wrap') && typeof config.tag_wrap !== 'number')
|
|
1280
1317
|
throw new Error(`tag_wrap config must be a number, not ${typeof config.tag_wrap}.`)
|
|
1281
1318
|
|
|
1282
|
-
/* TODO remove in v0.9.0 */
|
|
1283
|
-
if (Object.hasOwn(config, 'tag_wrap_width'))
|
|
1284
|
-
console.warn('tag_wrap_width is deprecated, and will not be supported in v0.9.0+. Use `tag_wrap: <number>` instead; where <number> is the max character width acceptable before wrapping attributes.');
|
|
1285
|
-
|
|
1286
|
-
/* TODO remove in v0.9.0 */
|
|
1287
|
-
if (Object.hasOwn(config, 'tag_wrap_width') && typeof config.tag_wrap_width !== 'number')
|
|
1288
|
-
throw new Error(`tag_wrap_width config must be a number, not ${typeof config.tag_wrap_width}.`)
|
|
1289
|
-
|
|
1290
1319
|
if (Object.hasOwn(config, 'trim') && (!Array.isArray(config.trim) || !config.trim?.every((e) => typeof e === 'string')))
|
|
1291
1320
|
throw new Error('Trim config must be an array of strings.')
|
|
1292
1321
|
|
|
@@ -1355,11 +1384,11 @@ const wordWrap = (text, width, indent) => {
|
|
|
1355
1384
|
* for re-insertion later.
|
|
1356
1385
|
*
|
|
1357
1386
|
* @param {string} html
|
|
1358
|
-
* @
|
|
1359
|
-
* @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
|
|
1387
|
+
* @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
|
|
1360
1388
|
*/
|
|
1361
|
-
function extractIgnoredBlocks(html
|
|
1389
|
+
function extractIgnoredBlocks(html) {
|
|
1362
1390
|
setState({ ignored: true });
|
|
1391
|
+
const config = (getState()).config;
|
|
1363
1392
|
let current_html = html;
|
|
1364
1393
|
const extracted_blocks = new Map();
|
|
1365
1394
|
let marker_id = 0;
|
|
@@ -1370,19 +1399,27 @@ function extractIgnoredBlocks(html, config) {
|
|
|
1370
1399
|
const safe_tag_name = tag.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
1371
1400
|
|
|
1372
1401
|
const regex = new RegExp(
|
|
1373
|
-
|
|
1402
|
+
`(<\\s*${safe_tag_name}[^>]*>)(.*?)(<\\s*\/\\s*${safe_tag_name}\\s*>)`,
|
|
1374
1403
|
"gs" // global and dotAll
|
|
1375
1404
|
);
|
|
1376
1405
|
|
|
1406
|
+
/** @type RegExpExecArray | null */
|
|
1377
1407
|
let match;
|
|
1378
|
-
|
|
1408
|
+
|
|
1409
|
+
/**
|
|
1410
|
+
* @type {{ start: number; end: number; marker: string }[]}
|
|
1411
|
+
*/
|
|
1412
|
+
const replacements = [];
|
|
1379
1413
|
|
|
1380
1414
|
while ((match = regex.exec(current_html)) !== null) {
|
|
1381
1415
|
const marker = `${MARKER_PREFIX}${marker_id++}___`;
|
|
1382
|
-
|
|
1416
|
+
|
|
1417
|
+
/* Only store content, and minify tags later. */
|
|
1418
|
+
extracted_blocks.set(marker, match[2]);
|
|
1419
|
+
|
|
1383
1420
|
replacements.push({
|
|
1384
|
-
start: match.index,
|
|
1385
|
-
end:
|
|
1421
|
+
start: match.index + match[1].length, // start of content
|
|
1422
|
+
end: match.index + match[1].length + match[2].length, // end of content
|
|
1386
1423
|
marker: marker,
|
|
1387
1424
|
});
|
|
1388
1425
|
}
|
|
@@ -1396,6 +1433,7 @@ function extractIgnoredBlocks(html, config) {
|
|
|
1396
1433
|
current_html.substring(rep.end);
|
|
1397
1434
|
}
|
|
1398
1435
|
}
|
|
1436
|
+
|
|
1399
1437
|
return { html_with_markers: current_html, extracted_map: extracted_blocks }
|
|
1400
1438
|
}
|
|
1401
1439
|
|
|
@@ -1416,42 +1454,55 @@ function reinsertIgnoredBlocks(html_with_markers, extracted_map) {
|
|
|
1416
1454
|
return final_html
|
|
1417
1455
|
}
|
|
1418
1456
|
|
|
1457
|
+
const void_element_regex = new RegExp(`<(${VOID_ELEMENTS.join("|")})(?:\\s(?:[^/>]|/(?!>))*)*>`, 'g');
|
|
1458
|
+
|
|
1419
1459
|
/**
|
|
1420
|
-
*
|
|
1460
|
+
* Add a placeholder for void elements that are not self-closing.
|
|
1461
|
+
* This is for internal processing only.
|
|
1421
1462
|
*
|
|
1422
|
-
* @param {string} html
|
|
1423
|
-
* @returns
|
|
1424
|
-
* @example <br> => <br />
|
|
1463
|
+
* @param {string} html
|
|
1464
|
+
* @returns
|
|
1425
1465
|
*/
|
|
1426
|
-
|
|
1427
|
-
const {
|
|
1428
|
-
|
|
1429
|
-
if (!checked_html && !isHtml(html)) return html
|
|
1430
|
-
|
|
1431
|
-
return html.replace(/<([a-zA-Z\-0-9:]+)[^>]*>/g, (match, name) => {
|
|
1432
|
-
if (VOID_ELEMENTS.indexOf(name) > -1)
|
|
1433
|
-
return (`${match.substring(0, match.length - 1)} />`).replace(/\/\s\//g, '/')
|
|
1466
|
+
function setSelfClosing(html) {
|
|
1467
|
+
const { constants } = getState();
|
|
1434
1468
|
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1469
|
+
return html.replace(
|
|
1470
|
+
// match only void elements that are not self-closing
|
|
1471
|
+
void_element_regex,
|
|
1472
|
+
match => match.replace(/>$/, constants.SELF_CLOSING_PLACEHOLDER)
|
|
1473
|
+
)
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
/**
|
|
1477
|
+
* Remove internal placeholder for non-native self-closing void elements.
|
|
1478
|
+
*
|
|
1479
|
+
* @param {string} html
|
|
1480
|
+
* @returns
|
|
1481
|
+
*/
|
|
1482
|
+
function unsetSelfClosing(html) {
|
|
1483
|
+
const { constants } = getState();
|
|
1484
|
+
|
|
1485
|
+
return html.replace(constants.SELF_CLOSING_PLACEHOLDER, ">")
|
|
1486
|
+
}
|
|
1438
1487
|
|
|
1439
1488
|
/**
|
|
1440
1489
|
* Enforce entity characters for textarea content.
|
|
1441
|
-
* To also minifiy, pass `minify` as `true`.
|
|
1490
|
+
* To also minifiy tags, pass `minify` as `true`.
|
|
1442
1491
|
*
|
|
1443
1492
|
* @param {string} html The HTML string to evaluate.
|
|
1444
|
-
* @param {boolean} [minify]
|
|
1493
|
+
* @param {boolean} [minify] Minifies the textarea tags themselves.
|
|
1445
1494
|
* Defaults to `false`. We recommend a value of `true` if you're running `entify()`
|
|
1446
1495
|
* as a standalone function.
|
|
1447
1496
|
* @returns {string}
|
|
1448
|
-
* @example <textarea>3 > 2</textarea> => <textarea>3
|
|
1497
|
+
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1498
|
+
* @example With minify.
|
|
1499
|
+
* <textarea >3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1449
1500
|
*/
|
|
1450
1501
|
const entify = (html, minify = false) => {
|
|
1451
1502
|
/**
|
|
1452
1503
|
* Use entities inside textarea content.
|
|
1453
1504
|
*/
|
|
1454
|
-
html = html.replace(
|
|
1505
|
+
html = html.replace(/<\s*textarea[^>]*>((.|\n)*?)<s*\/\s*textarea\s*>/g, (match, capture) => {
|
|
1455
1506
|
return match.replace(capture, (match) => {
|
|
1456
1507
|
return match
|
|
1457
1508
|
.replace(/</g, '<')
|
|
@@ -1464,33 +1515,54 @@ const entify = (html, minify = false) => {
|
|
|
1464
1515
|
})
|
|
1465
1516
|
});
|
|
1466
1517
|
|
|
1467
|
-
/* Typical minification, but only for textareas. */
|
|
1468
1518
|
if (minify) {
|
|
1469
|
-
html = html.replace(
|
|
1470
|
-
/*
|
|
1471
|
-
|
|
1472
|
-
return match
|
|
1473
|
-
.replace(/\n|\t/g, '')
|
|
1474
|
-
.replace(/[a-z]+="\s*"/ig, '')
|
|
1475
|
-
.replace(/>\s+</g, '><')
|
|
1476
|
-
.replace(/\s+/g, ' ')
|
|
1477
|
-
});
|
|
1478
|
-
|
|
1479
|
-
/* Replace things in the entire element */
|
|
1480
|
-
match = match
|
|
1519
|
+
html = html.replace(/<\s*textarea[^>]*>(.|\n)*?<\s*\/\s*textarea\s*>/g, (match) => {
|
|
1520
|
+
/* This only affects the html tags, since everything else has been entified. */
|
|
1521
|
+
return match
|
|
1481
1522
|
.replace(/\s+/g, ' ')
|
|
1482
1523
|
.replace(/\s>/g, '>')
|
|
1483
1524
|
.replace(/>\s/g, '>')
|
|
1484
1525
|
.replace(/\s</g, '<')
|
|
1526
|
+
.replace(/<\s/g, '<')
|
|
1527
|
+
.replace(/<\/\s/g, '<\/')
|
|
1485
1528
|
.replace(/class=["']\s/g, (match) => match.replace(/\s/g, ''))
|
|
1486
|
-
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1487
|
-
return match
|
|
1529
|
+
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1488
1530
|
});
|
|
1489
1531
|
}
|
|
1490
1532
|
|
|
1491
1533
|
return html
|
|
1492
1534
|
};
|
|
1493
1535
|
|
|
1536
|
+
/**
|
|
1537
|
+
* Remove entity characters for textarea content.
|
|
1538
|
+
* Currently internal use only.
|
|
1539
|
+
*
|
|
1540
|
+
* @param {string} html The HTML string to evaluate.
|
|
1541
|
+
* @returns {string}
|
|
1542
|
+
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1543
|
+
*/
|
|
1544
|
+
const dentify = (html) => {
|
|
1545
|
+
/**
|
|
1546
|
+
* Remove entities inside textarea content.
|
|
1547
|
+
*/
|
|
1548
|
+
return html = html.replace(/<textarea[^>]*>((.|\n)*?)<\/textarea>/g, (match, capture) => {
|
|
1549
|
+
return match.replace(capture, (match) => {
|
|
1550
|
+
match = match
|
|
1551
|
+
.replace(/</g, '<')
|
|
1552
|
+
.replace(/>/g, '>')
|
|
1553
|
+
.replace(/"/g, '"')
|
|
1554
|
+
.replace(/'/g, "'")
|
|
1555
|
+
.replace(/ /g, '\n')
|
|
1556
|
+
.replace(/ /g, '\r')
|
|
1557
|
+
.replace(/ /g, ' ')
|
|
1558
|
+
// Ensure we collapse consecutive spaces, or they'll be completely removed later.
|
|
1559
|
+
.replace(/\s+/g, ' ');
|
|
1560
|
+
|
|
1561
|
+
return match
|
|
1562
|
+
})
|
|
1563
|
+
})
|
|
1564
|
+
};
|
|
1565
|
+
|
|
1494
1566
|
/**
|
|
1495
1567
|
* @type {Map<any,any>}
|
|
1496
1568
|
*/
|
|
@@ -1515,17 +1587,17 @@ const minify = (html, config) => {
|
|
|
1515
1587
|
|
|
1516
1588
|
/* Extract ignored elements. Skipped if prettify has already ignored blocks. */
|
|
1517
1589
|
if (!ignored && ignore) {
|
|
1518
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html
|
|
1590
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1519
1591
|
html = html_with_markers;
|
|
1520
1592
|
ignore_map$1 = extracted_map;
|
|
1521
1593
|
reinsert_ignored = true;
|
|
1522
1594
|
}
|
|
1523
1595
|
|
|
1524
1596
|
/**
|
|
1525
|
-
* Ensure textarea content is
|
|
1597
|
+
* Ensure textarea content is protected
|
|
1526
1598
|
* before general minification.
|
|
1527
1599
|
*/
|
|
1528
|
-
html = entify(html);
|
|
1600
|
+
html = entify(html, true);
|
|
1529
1601
|
|
|
1530
1602
|
/* All other minification. */
|
|
1531
1603
|
// Remove ALL newlines and tabs explicitly.
|
|
@@ -1537,11 +1609,13 @@ const minify = (html, config) => {
|
|
|
1537
1609
|
// Collapse any remaining multiple spaces to single spaces.
|
|
1538
1610
|
html = html.replace(/ {2,}/g, ' ');
|
|
1539
1611
|
|
|
1540
|
-
// Remove specific single spaces
|
|
1612
|
+
// Remove specific single spaces between tags and whitespace within tags.
|
|
1541
1613
|
html = html.replace(/ >/g, ">"); // <tag > -> <tag>
|
|
1542
|
-
html = html.replace(/ </g, "<"); //
|
|
1543
|
-
html = html.replace(/> /g, ">"); //
|
|
1544
|
-
html = html.replace(
|
|
1614
|
+
html = html.replace(/ </g, "<"); // leading space before tag
|
|
1615
|
+
html = html.replace(/> /g, ">"); // trailing space after tag
|
|
1616
|
+
html = html.replace(/< /g, "<"); // < tag> -> <tag>
|
|
1617
|
+
html = html.replace(/<\s+\//g, '</'); // < /tag> -> </tag>
|
|
1618
|
+
html = html.replace(/<\/\s+/g, '</'); // </ tag> -> </tag>
|
|
1545
1619
|
|
|
1546
1620
|
// Trim spaces around equals signs in attributes (run before value trim)
|
|
1547
1621
|
// This handles `attr = "value"` -> `attr="value"`
|
|
@@ -1562,6 +1636,9 @@ const minify = (html, config) => {
|
|
|
1562
1636
|
// Final trim for the whole string
|
|
1563
1637
|
html = html.trim();
|
|
1564
1638
|
|
|
1639
|
+
/* Remove protective entities. */
|
|
1640
|
+
html = dentify(html);
|
|
1641
|
+
|
|
1565
1642
|
/* Re-insert ignored elements. Skipped unless minify did the ignore. */
|
|
1566
1643
|
if (reinsert_ignored) {
|
|
1567
1644
|
html = reinsertIgnoredBlocks(html, ignore_map$1);
|
|
@@ -1586,7 +1663,6 @@ let ignore_map;
|
|
|
1586
1663
|
* Isolate tags, content, and comments.
|
|
1587
1664
|
*
|
|
1588
1665
|
* @param {string} html The HTML string to evaluate.
|
|
1589
|
-
* @returns {string}
|
|
1590
1666
|
* @example <div>Hello World!</div> =>
|
|
1591
1667
|
* [#-# : 0 : <div> : #-#]
|
|
1592
1668
|
* Hello World!
|
|
@@ -1598,7 +1674,7 @@ const enqueue = (html) => {
|
|
|
1598
1674
|
/* Regex to find tags OR text content between tags. */
|
|
1599
1675
|
const regex = /(<[^>]+>)|([^<]+)/g;
|
|
1600
1676
|
|
|
1601
|
-
html
|
|
1677
|
+
html.replace(regex, (match, c1, c2) => {
|
|
1602
1678
|
if (c1) {
|
|
1603
1679
|
convert.line.push({ type: "tag", value: match });
|
|
1604
1680
|
} else if (c2 && c2.trim().length > 0) {
|
|
@@ -1609,40 +1685,18 @@ const enqueue = (html) => {
|
|
|
1609
1685
|
i++;
|
|
1610
1686
|
return `\n[#-# : ${i} : ${match} : #-#]\n`
|
|
1611
1687
|
});
|
|
1612
|
-
|
|
1613
|
-
return html
|
|
1614
|
-
};
|
|
1615
|
-
|
|
1616
|
-
/**
|
|
1617
|
-
* Preprocess the HTML.
|
|
1618
|
-
*
|
|
1619
|
-
* @param {string} html The HTML string to preprocess.
|
|
1620
|
-
* @returns {string}
|
|
1621
|
-
*/
|
|
1622
|
-
const preprocess = (html) => {
|
|
1623
|
-
html = closify(html);
|
|
1624
|
-
|
|
1625
|
-
const { config } = getState();
|
|
1626
|
-
|
|
1627
|
-
if (config.trim.length > 0) html = trimify(html, config.trim);
|
|
1628
|
-
|
|
1629
|
-
html = minify(html);
|
|
1630
|
-
html = enqueue(html);
|
|
1631
|
-
|
|
1632
|
-
return html
|
|
1633
1688
|
};
|
|
1634
1689
|
|
|
1635
1690
|
/**
|
|
1636
|
-
*
|
|
1637
|
-
*
|
|
1691
|
+
* Process enqueued content.
|
|
1692
|
+
*
|
|
1638
1693
|
* @returns {string}
|
|
1639
1694
|
*/
|
|
1640
|
-
const process = (
|
|
1695
|
+
const process = () => {
|
|
1696
|
+
const { config, constants } = getState();
|
|
1641
1697
|
const step = " ".repeat(config.tab_size);
|
|
1642
1698
|
const tag_wrap = config.tag_wrap;
|
|
1643
1699
|
const content_wrap = config.content_wrap;
|
|
1644
|
-
const ignore_with = config.ignore_with;
|
|
1645
|
-
const placeholder_template = `-${ignore_with}`;
|
|
1646
1700
|
const strict = config.strict;
|
|
1647
1701
|
|
|
1648
1702
|
/* Track current number of indentations needed. */
|
|
@@ -1658,12 +1712,7 @@ const process = (config) => {
|
|
|
1658
1712
|
let current_line_value = source.value;
|
|
1659
1713
|
|
|
1660
1714
|
const is_ignored_content =
|
|
1661
|
-
current_line_value.startsWith(
|
|
1662
|
-
current_line_value.startsWith(placeholder_template + "gt--") ||
|
|
1663
|
-
current_line_value.startsWith(placeholder_template + "nl--") ||
|
|
1664
|
-
current_line_value.startsWith(placeholder_template + "cr--") ||
|
|
1665
|
-
current_line_value.startsWith(placeholder_template + "ws--") ||
|
|
1666
|
-
current_line_value.startsWith(placeholder_template + "tab--");
|
|
1715
|
+
current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
|
|
1667
1716
|
|
|
1668
1717
|
let subtrahend = 0;
|
|
1669
1718
|
const prev_line_data = convert.line[index - 1];
|
|
@@ -1681,8 +1730,12 @@ const process = (config) => {
|
|
|
1681
1730
|
if (prev_line_value.trim().startsWith("<!doctype")) subtrahend++;
|
|
1682
1731
|
/* prevLine is a comment. */
|
|
1683
1732
|
if (prev_line_value.trim().startsWith("<!--")) subtrahend++;
|
|
1684
|
-
/* prevLine is a
|
|
1685
|
-
if (
|
|
1733
|
+
/* prevLine is a void element. */
|
|
1734
|
+
if (
|
|
1735
|
+
prev_line_value.trim().endsWith("/>") // native self-closing
|
|
1736
|
+
||
|
|
1737
|
+
prev_line_value.trim().endsWith(constants.SELF_CLOSING_PLACEHOLDER) // synthetic self-closing
|
|
1738
|
+
) subtrahend++;
|
|
1686
1739
|
/* prevLine is a closing tag. */
|
|
1687
1740
|
if (prev_line_value.trim().startsWith("</")) subtrahend++;
|
|
1688
1741
|
/* prevLine is text. */
|
|
@@ -1698,8 +1751,6 @@ const process = (config) => {
|
|
|
1698
1751
|
/**
|
|
1699
1752
|
* Starts with a single punctuation character.
|
|
1700
1753
|
* Add punctuation to end of previous line.
|
|
1701
|
-
*
|
|
1702
|
-
* TODO - Implement inline groups instead?
|
|
1703
1754
|
*/
|
|
1704
1755
|
if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
|
|
1705
1756
|
if (current_line_value.length === 1) {
|
|
@@ -1725,6 +1776,9 @@ const process = (config) => {
|
|
|
1725
1776
|
|
|
1726
1777
|
let result = current_line_value;
|
|
1727
1778
|
|
|
1779
|
+
/* Remove self-closing placeholder, if needed. */
|
|
1780
|
+
result = unsetSelfClosing(result);
|
|
1781
|
+
|
|
1728
1782
|
if (
|
|
1729
1783
|
source.type === 'text' &&
|
|
1730
1784
|
content_wrap > 0 &&
|
|
@@ -1755,9 +1809,9 @@ const process = (config) => {
|
|
|
1755
1809
|
|
|
1756
1810
|
const tag_name_match = tag_parts[0].match(/<([A-Za-z_:-]+)/);
|
|
1757
1811
|
const tag_name = tag_name_match ? tag_name_match[1] : "";
|
|
1758
|
-
const
|
|
1812
|
+
const is_self_closing = tag_parts.at(-1)?.endsWith("/>") && VOID_ELEMENTS.includes(tag_name);
|
|
1759
1813
|
const closing_part = tag_parts[1].trim();
|
|
1760
|
-
const closing_padding = padding + (strict &&
|
|
1814
|
+
const closing_padding = padding + (strict && is_self_closing ? " " : "");
|
|
1761
1815
|
|
|
1762
1816
|
wrapped_tag += closing_padding + closing_part;
|
|
1763
1817
|
|
|
@@ -1782,13 +1836,20 @@ const process = (config) => {
|
|
|
1782
1836
|
if (tag_wrap > 0) final_html = protectAttributes(final_html);
|
|
1783
1837
|
|
|
1784
1838
|
/* Extra preserve wrapped content. */
|
|
1785
|
-
if (content_wrap > 0 &&
|
|
1839
|
+
if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
|
|
1786
1840
|
final_html = finalProtectContent(final_html);
|
|
1787
1841
|
|
|
1788
1842
|
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
|
|
1789
1843
|
final_html = final_html.replace(
|
|
1790
|
-
/<(?<Element
|
|
1791
|
-
match =>
|
|
1844
|
+
/<(?<Element>[^>\s]+)[^>]*>[^<]*?[^><\/\s][^<]*?<\/\k<Element>>|<script[^>]*>[\s]*<\/script>|<([\w:\._-]+)([^>]*)><\/\2>|<([\w:\._-]+)([^>]*)>[\s]+<\/\4>/g,
|
|
1845
|
+
match => {
|
|
1846
|
+
// Check if this contains placeholder
|
|
1847
|
+
if (match.includes(constants.SELF_CLOSING_PLACEHOLDER) || match.includes(constants.CONTENT_IGNORE_PLACEHOLDER)) {
|
|
1848
|
+
return match // Don't modify if it contains the placeholder
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
return match.replace(/\n|\t|\s{2,}/g, '')
|
|
1852
|
+
}
|
|
1792
1853
|
);
|
|
1793
1854
|
|
|
1794
1855
|
/* Revert wrapped content. */
|
|
@@ -1821,12 +1882,17 @@ const prettify = (html, config) => {
|
|
|
1821
1882
|
/* Return content as-is if it does not contain any HTML elements. */
|
|
1822
1883
|
if (!checked_html && !isHtml(html)) return html
|
|
1823
1884
|
|
|
1824
|
-
|
|
1885
|
+
/* Runs setState for config. */
|
|
1886
|
+
const validated_config = validateConfig(config || {});
|
|
1887
|
+
|
|
1825
1888
|
const ignore = validated_config.ignore.length > 0;
|
|
1826
1889
|
|
|
1890
|
+
/* Allows you to trimify before ignoring. */
|
|
1891
|
+
if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
|
|
1892
|
+
|
|
1827
1893
|
/* Extract ignored elements. */
|
|
1828
1894
|
if (!ignored && ignore) {
|
|
1829
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html
|
|
1895
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1830
1896
|
html = html_with_markers;
|
|
1831
1897
|
ignore_map = extracted_map;
|
|
1832
1898
|
reinsert_ignored = true;
|
|
@@ -1835,8 +1901,12 @@ const prettify = (html, config) => {
|
|
|
1835
1901
|
/* Preserve html text within attribute values. */
|
|
1836
1902
|
html = setIgnoreAttribute(html);
|
|
1837
1903
|
|
|
1838
|
-
|
|
1839
|
-
html =
|
|
1904
|
+
/* Insert placeholder for void elements that aren't self-closing. */
|
|
1905
|
+
html = setSelfClosing(html);
|
|
1906
|
+
|
|
1907
|
+
html = minify(html);
|
|
1908
|
+
enqueue(html);
|
|
1909
|
+
html = process();
|
|
1840
1910
|
|
|
1841
1911
|
/* Revert html text within attribute values. */
|
|
1842
1912
|
html = unsetIgnoreAttribute(html);
|