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