@mgdis/core-ui-helpers 1.0.5 → 1.0.7
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 +53 -52
- package/CHANGELOG.md +14 -0
- package/dist/playwright/index.cjs +2 -2
- package/dist/playwright/index.cjs.map +1 -1
- package/dist/playwright/index.js +2 -2
- package/dist/playwright/index.js.map +1 -1
- package/dist/playwright/index.umd.js +2 -2
- package/dist/playwright/index.umd.js.map +1 -1
- package/dist/storybook/index.cjs +241 -153
- package/dist/storybook/index.cjs.map +1 -1
- package/dist/storybook/index.js +241 -153
- package/dist/storybook/index.js.map +1 -1
- package/dist/storybook/index.umd.js +241 -153
- package/dist/storybook/index.umd.js.map +1 -1
- package/package.json +17 -18
- package/rollup.config.mjs +1 -1
- package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +4 -4
- package/src/playwright/utils/utils.spec.ts +1 -1
- package/src/playwright/utils/utils.ts +2 -2
- package/src/stencil/ide/ide.spec.ts +5 -1
- package/src/stencil/index.spec.ts +5 -1
- package/src/storybook/index.spec.ts +5 -1
- package/src/storybook/stencil/stencil.spec.ts +4 -0
- package/src/tests/unit/__snapshots__/unit.spec.ts.snap +4 -4
- package/src/utils/components/__snapshots__/components.spec.ts.snap +1 -1
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
|
*/
|
|
@@ -1506,7 +1578,7 @@ let ignore_map$1;
|
|
|
1506
1578
|
*/
|
|
1507
1579
|
const minify = (html, config) => {
|
|
1508
1580
|
let reinsert_ignored = false;
|
|
1509
|
-
const { checked_html, ignored } = getState();
|
|
1581
|
+
const { checked_html, ignored, constants } = getState();
|
|
1510
1582
|
|
|
1511
1583
|
if (!checked_html && !isHtml(html)) return html
|
|
1512
1584
|
|
|
@@ -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,28 @@ const minify = (html, config) => {
|
|
|
1537
1609
|
// Collapse any remaining multiple spaces to single spaces.
|
|
1538
1610
|
html = html.replace(/ {2,}/g, ' ');
|
|
1539
1611
|
|
|
1540
|
-
//
|
|
1612
|
+
// Protect space between text content and an opening tag (e.g., "text <a>")
|
|
1613
|
+
html = html.replace(
|
|
1614
|
+
/(\S) (<[a-zA-Z][a-zA-Z0-9_:-]*)/g,
|
|
1615
|
+
`$1___MINIFY-PROTECTED-SPACE___$2`
|
|
1616
|
+
);
|
|
1617
|
+
|
|
1618
|
+
// Protect space between a closing tag and text content (e.g., "</a> text")
|
|
1619
|
+
html = html.replace(
|
|
1620
|
+
/(<\/[a-zA-Z][a-zA-Z0-9_:-]*>) (\S)/g,
|
|
1621
|
+
`$1___MINIFY-PROTECTED-SPACE___$2`
|
|
1622
|
+
);
|
|
1623
|
+
|
|
1624
|
+
// Remove specific single spaces between tags and whitespace within tags.
|
|
1541
1625
|
html = html.replace(/ >/g, ">"); // <tag > -> <tag>
|
|
1542
|
-
html = html.replace(/ </g, "<"); //
|
|
1543
|
-
html = html.replace(/> /g, ">"); //
|
|
1544
|
-
html = html.replace(
|
|
1626
|
+
html = html.replace(/ </g, "<"); // leading space before tag
|
|
1627
|
+
html = html.replace(/> /g, ">"); // trailing space after tag
|
|
1628
|
+
html = html.replace(/< /g, "<"); // < tag> -> <tag>
|
|
1629
|
+
html = html.replace(/<\s+\//g, '</'); // < /tag> -> </tag>
|
|
1630
|
+
html = html.replace(/<\/\s+/g, '</'); // </ tag> -> </tag>
|
|
1631
|
+
|
|
1632
|
+
// Unprotect space around inner tags
|
|
1633
|
+
html = html.replace(new RegExp('___MINIFY-PROTECTED-SPACE___', 'g'), ' ');
|
|
1545
1634
|
|
|
1546
1635
|
// Trim spaces around equals signs in attributes (run before value trim)
|
|
1547
1636
|
// This handles `attr = "value"` -> `attr="value"`
|
|
@@ -1562,6 +1651,9 @@ const minify = (html, config) => {
|
|
|
1562
1651
|
// Final trim for the whole string
|
|
1563
1652
|
html = html.trim();
|
|
1564
1653
|
|
|
1654
|
+
/* Remove protective entities. */
|
|
1655
|
+
html = dentify(html);
|
|
1656
|
+
|
|
1565
1657
|
/* Re-insert ignored elements. Skipped unless minify did the ignore. */
|
|
1566
1658
|
if (reinsert_ignored) {
|
|
1567
1659
|
html = reinsertIgnoredBlocks(html, ignore_map$1);
|
|
@@ -1586,7 +1678,6 @@ let ignore_map;
|
|
|
1586
1678
|
* Isolate tags, content, and comments.
|
|
1587
1679
|
*
|
|
1588
1680
|
* @param {string} html The HTML string to evaluate.
|
|
1589
|
-
* @returns {string}
|
|
1590
1681
|
* @example <div>Hello World!</div> =>
|
|
1591
1682
|
* [#-# : 0 : <div> : #-#]
|
|
1592
1683
|
* Hello World!
|
|
@@ -1598,7 +1689,7 @@ const enqueue = (html) => {
|
|
|
1598
1689
|
/* Regex to find tags OR text content between tags. */
|
|
1599
1690
|
const regex = /(<[^>]+>)|([^<]+)/g;
|
|
1600
1691
|
|
|
1601
|
-
html
|
|
1692
|
+
html.replace(regex, (match, c1, c2) => {
|
|
1602
1693
|
if (c1) {
|
|
1603
1694
|
convert.line.push({ type: "tag", value: match });
|
|
1604
1695
|
} else if (c2 && c2.trim().length > 0) {
|
|
@@ -1609,40 +1700,18 @@ const enqueue = (html) => {
|
|
|
1609
1700
|
i++;
|
|
1610
1701
|
return `\n[#-# : ${i} : ${match} : #-#]\n`
|
|
1611
1702
|
});
|
|
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
1703
|
};
|
|
1634
1704
|
|
|
1635
1705
|
/**
|
|
1636
|
-
*
|
|
1637
|
-
*
|
|
1706
|
+
* Process enqueued content.
|
|
1707
|
+
*
|
|
1638
1708
|
* @returns {string}
|
|
1639
1709
|
*/
|
|
1640
|
-
const process = (
|
|
1710
|
+
const process = () => {
|
|
1711
|
+
const { config, constants } = getState();
|
|
1641
1712
|
const step = " ".repeat(config.tab_size);
|
|
1642
1713
|
const tag_wrap = config.tag_wrap;
|
|
1643
1714
|
const content_wrap = config.content_wrap;
|
|
1644
|
-
const ignore_with = config.ignore_with;
|
|
1645
|
-
const placeholder_template = `-${ignore_with}`;
|
|
1646
1715
|
const strict = config.strict;
|
|
1647
1716
|
|
|
1648
1717
|
/* Track current number of indentations needed. */
|
|
@@ -1658,12 +1727,7 @@ const process = (config) => {
|
|
|
1658
1727
|
let current_line_value = source.value;
|
|
1659
1728
|
|
|
1660
1729
|
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--");
|
|
1730
|
+
current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
|
|
1667
1731
|
|
|
1668
1732
|
let subtrahend = 0;
|
|
1669
1733
|
const prev_line_data = convert.line[index - 1];
|
|
@@ -1681,8 +1745,12 @@ const process = (config) => {
|
|
|
1681
1745
|
if (prev_line_value.trim().startsWith("<!doctype")) subtrahend++;
|
|
1682
1746
|
/* prevLine is a comment. */
|
|
1683
1747
|
if (prev_line_value.trim().startsWith("<!--")) subtrahend++;
|
|
1684
|
-
/* prevLine is a
|
|
1685
|
-
if (
|
|
1748
|
+
/* prevLine is a void element. */
|
|
1749
|
+
if (
|
|
1750
|
+
prev_line_value.trim().endsWith("/>") // native self-closing
|
|
1751
|
+
||
|
|
1752
|
+
prev_line_value.trim().endsWith(constants.SELF_CLOSING_PLACEHOLDER) // synthetic self-closing
|
|
1753
|
+
) subtrahend++;
|
|
1686
1754
|
/* prevLine is a closing tag. */
|
|
1687
1755
|
if (prev_line_value.trim().startsWith("</")) subtrahend++;
|
|
1688
1756
|
/* prevLine is text. */
|
|
@@ -1698,8 +1766,6 @@ const process = (config) => {
|
|
|
1698
1766
|
/**
|
|
1699
1767
|
* Starts with a single punctuation character.
|
|
1700
1768
|
* Add punctuation to end of previous line.
|
|
1701
|
-
*
|
|
1702
|
-
* TODO - Implement inline groups instead?
|
|
1703
1769
|
*/
|
|
1704
1770
|
if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
|
|
1705
1771
|
if (current_line_value.length === 1) {
|
|
@@ -1710,6 +1776,9 @@ const process = (config) => {
|
|
|
1710
1776
|
output_lines[output_lines.length - 1] =
|
|
1711
1777
|
output_lines.at(-1) + current_line_value.charAt(0);
|
|
1712
1778
|
current_line_value = current_line_value.slice(1).trim();
|
|
1779
|
+
|
|
1780
|
+
/* If nothing left after extracting punctuation, skip this line. */
|
|
1781
|
+
if (current_line_value.length === 0) return
|
|
1713
1782
|
}
|
|
1714
1783
|
}
|
|
1715
1784
|
|
|
@@ -1725,6 +1794,9 @@ const process = (config) => {
|
|
|
1725
1794
|
|
|
1726
1795
|
let result = current_line_value;
|
|
1727
1796
|
|
|
1797
|
+
/* Remove self-closing placeholder, if needed. */
|
|
1798
|
+
result = unsetSelfClosing(result);
|
|
1799
|
+
|
|
1728
1800
|
if (
|
|
1729
1801
|
source.type === 'text' &&
|
|
1730
1802
|
content_wrap > 0 &&
|
|
@@ -1755,9 +1827,9 @@ const process = (config) => {
|
|
|
1755
1827
|
|
|
1756
1828
|
const tag_name_match = tag_parts[0].match(/<([A-Za-z_:-]+)/);
|
|
1757
1829
|
const tag_name = tag_name_match ? tag_name_match[1] : "";
|
|
1758
|
-
const
|
|
1830
|
+
const is_self_closing = tag_parts.at(-1)?.endsWith("/>") && VOID_ELEMENTS.includes(tag_name);
|
|
1759
1831
|
const closing_part = tag_parts[1].trim();
|
|
1760
|
-
const closing_padding = padding + (strict &&
|
|
1832
|
+
const closing_padding = padding + (strict && is_self_closing ? " " : "");
|
|
1761
1833
|
|
|
1762
1834
|
wrapped_tag += closing_padding + closing_part;
|
|
1763
1835
|
|
|
@@ -1782,13 +1854,20 @@ const process = (config) => {
|
|
|
1782
1854
|
if (tag_wrap > 0) final_html = protectAttributes(final_html);
|
|
1783
1855
|
|
|
1784
1856
|
/* Extra preserve wrapped content. */
|
|
1785
|
-
if (content_wrap > 0 &&
|
|
1857
|
+
if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
|
|
1786
1858
|
final_html = finalProtectContent(final_html);
|
|
1787
1859
|
|
|
1788
1860
|
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
|
|
1789
1861
|
final_html = final_html.replace(
|
|
1790
|
-
/<(?<Element
|
|
1791
|
-
match =>
|
|
1862
|
+
/<(?<Element>[^>\s]+)[^>]*>[^<]*?[^><\/\s][^<]*?<\/\k<Element>>|<script[^>]*>[\s]*<\/script>|<([\w:\._-]+)([^>]*)><\/\2>|<([\w:\._-]+)([^>]*)>[\s]+<\/\4>/g,
|
|
1863
|
+
match => {
|
|
1864
|
+
// Check if this contains placeholder
|
|
1865
|
+
if (match.includes(constants.SELF_CLOSING_PLACEHOLDER) || match.includes(constants.CONTENT_IGNORE_PLACEHOLDER)) {
|
|
1866
|
+
return match // Don't modify if it contains the placeholder
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
return match.replace(/\n|\t|\s{2,}/g, '')
|
|
1870
|
+
}
|
|
1792
1871
|
);
|
|
1793
1872
|
|
|
1794
1873
|
/* Revert wrapped content. */
|
|
@@ -1821,12 +1900,17 @@ const prettify = (html, config) => {
|
|
|
1821
1900
|
/* Return content as-is if it does not contain any HTML elements. */
|
|
1822
1901
|
if (!checked_html && !isHtml(html)) return html
|
|
1823
1902
|
|
|
1824
|
-
|
|
1903
|
+
/* Runs setState for config. */
|
|
1904
|
+
const validated_config = validateConfig(config || {});
|
|
1905
|
+
|
|
1825
1906
|
const ignore = validated_config.ignore.length > 0;
|
|
1826
1907
|
|
|
1908
|
+
/* Allows you to trimify before ignoring. */
|
|
1909
|
+
if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
|
|
1910
|
+
|
|
1827
1911
|
/* Extract ignored elements. */
|
|
1828
1912
|
if (!ignored && ignore) {
|
|
1829
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html
|
|
1913
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1830
1914
|
html = html_with_markers;
|
|
1831
1915
|
ignore_map = extracted_map;
|
|
1832
1916
|
reinsert_ignored = true;
|
|
@@ -1835,8 +1919,12 @@ const prettify = (html, config) => {
|
|
|
1835
1919
|
/* Preserve html text within attribute values. */
|
|
1836
1920
|
html = setIgnoreAttribute(html);
|
|
1837
1921
|
|
|
1838
|
-
|
|
1839
|
-
html =
|
|
1922
|
+
/* Insert placeholder for void elements that aren't self-closing. */
|
|
1923
|
+
html = setSelfClosing(html);
|
|
1924
|
+
|
|
1925
|
+
html = minify(html);
|
|
1926
|
+
enqueue(html);
|
|
1927
|
+
html = process();
|
|
1840
1928
|
|
|
1841
1929
|
/* Revert html text within attribute values. */
|
|
1842
1930
|
html = unsetIgnoreAttribute(html);
|