@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
package/dist/storybook/index.cjs
CHANGED
|
@@ -9,7 +9,7 @@ var BUILD = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
/*
|
|
12
|
-
Stencil Client Platform v4.
|
|
12
|
+
Stencil Client Platform v4.38.0 | MIT Licensed | https://stenciljs.com
|
|
13
13
|
*/
|
|
14
14
|
var __defProp = Object.defineProperty;
|
|
15
15
|
var __export = (target, all) => {
|
|
@@ -817,9 +817,13 @@ var renderVdom = (hostRef, renderFnResults, isInitialLoad = false) => {
|
|
|
817
817
|
hostTagName = hostElm.tagName;
|
|
818
818
|
if (cmpMeta.$attrsToReflect$) {
|
|
819
819
|
rootVnode.$attrs$ = rootVnode.$attrs$ || {};
|
|
820
|
-
cmpMeta.$attrsToReflect$.
|
|
821
|
-
(
|
|
822
|
-
|
|
820
|
+
cmpMeta.$attrsToReflect$.forEach(([propName, attribute]) => {
|
|
821
|
+
if (BUILD.serializer && hostRef.$serializerValues$.has(propName)) {
|
|
822
|
+
rootVnode.$attrs$[attribute] = hostRef.$serializerValues$.get(propName);
|
|
823
|
+
} else {
|
|
824
|
+
rootVnode.$attrs$[attribute] = hostElm[propName];
|
|
825
|
+
}
|
|
826
|
+
});
|
|
823
827
|
}
|
|
824
828
|
if (isInitialLoad && rootVnode.$attrs$) {
|
|
825
829
|
for (const key of Object.keys(rootVnode.$attrs$)) {
|
|
@@ -926,37 +930,55 @@ var renderVdom = (hostRef, renderFnResults, isInitialLoad = false) => {
|
|
|
926
930
|
const CONFIG = {
|
|
927
931
|
content_wrap: 0,
|
|
928
932
|
ignore: [],
|
|
929
|
-
ignore_with: '
|
|
933
|
+
ignore_with: '!i-£___£%_',
|
|
930
934
|
strict: false,
|
|
931
935
|
tab_size: 2,
|
|
932
936
|
tag_wrap: 0,
|
|
933
|
-
tag_wrap_width: 80,
|
|
934
937
|
trim: []
|
|
935
938
|
};
|
|
936
939
|
|
|
937
|
-
const CONTENT_IGNORE_STRING = '__!i-£___£%__';
|
|
938
|
-
const IGNORE_STRING = '!i-£___£%_';
|
|
939
|
-
|
|
940
940
|
const VOID_ELEMENTS = [
|
|
941
941
|
'area', 'base', 'br', 'col', 'embed', 'hr',
|
|
942
942
|
'img', 'input', 'link', 'meta',
|
|
943
943
|
'param', 'source', 'track', 'wbr'
|
|
944
944
|
];
|
|
945
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
|
+
*/
|
|
946
960
|
/**
|
|
947
961
|
* @typedef {object} State
|
|
948
962
|
* @property {boolean} checked_html - If passed in HTML has been checked for HTML within it.
|
|
949
963
|
* @property {import("htmlfy").Config} config - Validated configuration.
|
|
950
964
|
* @property {boolean} ignored
|
|
965
|
+
* @property {Constants} constants - Constant strings, influenced by ignore_with.
|
|
951
966
|
*/
|
|
952
967
|
|
|
953
968
|
/**
|
|
954
969
|
* @type State
|
|
970
|
+
*
|
|
971
|
+
* `constants` prefixes and suffixes must be in sync with those in utils.js
|
|
955
972
|
*/
|
|
956
973
|
const state = {
|
|
957
974
|
checked_html: false,
|
|
958
975
|
config: { ...CONFIG },
|
|
959
|
-
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
|
+
}
|
|
960
982
|
};
|
|
961
983
|
|
|
962
984
|
/**
|
|
@@ -1043,7 +1065,16 @@ const mergeObjects = (current, updates) => {
|
|
|
1043
1065
|
*/
|
|
1044
1066
|
const mergeConfig = (default_config, config) => {
|
|
1045
1067
|
const validated_config = mergeObjects(default_config, config);
|
|
1046
|
-
|
|
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
|
+
});
|
|
1047
1078
|
return validated_config
|
|
1048
1079
|
};
|
|
1049
1080
|
|
|
@@ -1052,12 +1083,14 @@ const mergeConfig = (default_config, config) => {
|
|
|
1052
1083
|
* @param {string} html
|
|
1053
1084
|
*/
|
|
1054
1085
|
const protectAttributes = (html) => {
|
|
1086
|
+
const { constants } = getState();
|
|
1087
|
+
|
|
1055
1088
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1056
1089
|
return match.replace(capture, (match) => {
|
|
1057
1090
|
return match
|
|
1058
|
-
.replace(/\n/g,
|
|
1059
|
-
.replace(/\r/g,
|
|
1060
|
-
.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!')
|
|
1061
1094
|
})
|
|
1062
1095
|
});
|
|
1063
1096
|
|
|
@@ -1069,10 +1102,12 @@ const protectAttributes = (html) => {
|
|
|
1069
1102
|
* @param {string} html
|
|
1070
1103
|
*/
|
|
1071
1104
|
const protectContent = (html) => {
|
|
1105
|
+
const { constants } = getState();
|
|
1106
|
+
|
|
1072
1107
|
return html
|
|
1073
|
-
.replace(/\n/g,
|
|
1074
|
-
.replace(/\r/g,
|
|
1075
|
-
.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!')
|
|
1076
1111
|
};
|
|
1077
1112
|
|
|
1078
1113
|
/**
|
|
@@ -1080,7 +1115,9 @@ const protectContent = (html) => {
|
|
|
1080
1115
|
* @param {string} html
|
|
1081
1116
|
*/
|
|
1082
1117
|
const finalProtectContent = (html) => {
|
|
1083
|
-
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
|
+
|
|
1084
1121
|
return html
|
|
1085
1122
|
.replace(regex, (/** @type {string} */match, p1, p2, p3, p4) => {
|
|
1086
1123
|
const text_to_protect = p2 || p4;
|
|
@@ -1089,9 +1126,9 @@ const finalProtectContent = (html) => {
|
|
|
1089
1126
|
return match
|
|
1090
1127
|
|
|
1091
1128
|
const protected_text = text_to_protect
|
|
1092
|
-
.replace(/\n/g,
|
|
1093
|
-
.replace(/\r/g,
|
|
1094
|
-
.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!");
|
|
1095
1132
|
|
|
1096
1133
|
return match.replace(text_to_protect, protected_text)
|
|
1097
1134
|
})
|
|
@@ -1104,13 +1141,14 @@ const finalProtectContent = (html) => {
|
|
|
1104
1141
|
* @returns {string}
|
|
1105
1142
|
*/
|
|
1106
1143
|
const setIgnoreAttribute = (html) => {
|
|
1107
|
-
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();
|
|
1108
1146
|
|
|
1109
1147
|
html = html.replace(regex, (/** @type {string} */match, p1, p2) => {
|
|
1110
1148
|
return match.replace(p2, (match) => {
|
|
1111
1149
|
return match
|
|
1112
|
-
.replace(/</g,
|
|
1113
|
-
.replace(/>/g,
|
|
1150
|
+
.replace(/</g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'lt!')
|
|
1151
|
+
.replace(/>/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'gt!')
|
|
1114
1152
|
})
|
|
1115
1153
|
});
|
|
1116
1154
|
|
|
@@ -1143,12 +1181,14 @@ const trimify = (html, trim) => {
|
|
|
1143
1181
|
* @param {string} html
|
|
1144
1182
|
*/
|
|
1145
1183
|
const unprotectAttributes = (html) => {
|
|
1184
|
+
const { constants } = getState();
|
|
1185
|
+
|
|
1146
1186
|
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1147
1187
|
return match.replace(capture, (match) => {
|
|
1148
1188
|
return match
|
|
1149
|
-
.replace(new RegExp(
|
|
1150
|
-
.replace(new RegExp(
|
|
1151
|
-
.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"), ' ')
|
|
1152
1192
|
})
|
|
1153
1193
|
});
|
|
1154
1194
|
|
|
@@ -1160,25 +1200,20 @@ const unprotectAttributes = (html) => {
|
|
|
1160
1200
|
* @param {string} html
|
|
1161
1201
|
*/
|
|
1162
1202
|
const unprotectContent = (html) => {
|
|
1163
|
-
|
|
1164
|
-
|
|
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) => {
|
|
1165
1207
|
return match
|
|
1166
|
-
.replace(new RegExp(
|
|
1167
|
-
.replace(new RegExp(
|
|
1168
|
-
.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"), ' ')
|
|
1169
1211
|
})
|
|
1170
1212
|
});
|
|
1171
1213
|
|
|
1172
1214
|
return html
|
|
1173
1215
|
};
|
|
1174
1216
|
|
|
1175
|
-
const escapedIgnoreString = IGNORE_STRING.replace(
|
|
1176
|
-
/[-\/\\^$*+?.()|[\]{}]/g,
|
|
1177
|
-
"\\$&"
|
|
1178
|
-
);
|
|
1179
|
-
const ltPlaceholderRegex = new RegExp(escapedIgnoreString + "lt!", "g");
|
|
1180
|
-
const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
1181
|
-
|
|
1182
1217
|
/**
|
|
1183
1218
|
* Replace ignore string with html brackets.
|
|
1184
1219
|
*
|
|
@@ -1188,6 +1223,13 @@ const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
|
1188
1223
|
const unsetIgnoreAttribute = (html) => {
|
|
1189
1224
|
/* Regex to find opening tags and capture their attributes. */
|
|
1190
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");
|
|
1191
1233
|
|
|
1192
1234
|
return html.replace(
|
|
1193
1235
|
tagRegex,
|
|
@@ -1224,7 +1266,6 @@ const validateConfig = (config) => {
|
|
|
1224
1266
|
Object.hasOwn(config, 'strict') ||
|
|
1225
1267
|
Object.hasOwn(config, 'tab_size') ||
|
|
1226
1268
|
Object.hasOwn(config, 'tag_wrap') ||
|
|
1227
|
-
Object.hasOwn(config, 'tag_wrap_width') ||
|
|
1228
1269
|
Object.hasOwn(config, 'trim')
|
|
1229
1270
|
);
|
|
1230
1271
|
|
|
@@ -1252,37 +1293,29 @@ const validateConfig = (config) => {
|
|
|
1252
1293
|
}
|
|
1253
1294
|
|
|
1254
1295
|
if (Object.hasOwn(config, 'content_wrap') && typeof config.content_wrap !== 'number')
|
|
1255
|
-
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}.`)
|
|
1256
1297
|
|
|
1257
1298
|
if (Object.hasOwn(config, 'ignore') && (!Array.isArray(config.ignore) || !config.ignore?.every((e) => typeof e === 'string')))
|
|
1258
1299
|
throw new Error('Ignore config must be an array of strings.')
|
|
1259
1300
|
|
|
1260
|
-
if (Object.hasOwn(config, 'ignore_with')
|
|
1261
|
-
|
|
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
|
+
}
|
|
1262
1312
|
|
|
1263
1313
|
if (Object.hasOwn(config, 'strict') && typeof config.strict !== 'boolean')
|
|
1264
1314
|
throw new Error(`Strict config must be a boolean, not ${typeof config.strict}.`)
|
|
1265
|
-
|
|
1266
|
-
/* TODO remove in v0.9.0 */
|
|
1267
|
-
if (Object.hasOwn(config, 'tag_wrap') && typeof config.tag_wrap === 'boolean') {
|
|
1268
|
-
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.');
|
|
1269
|
-
if (config.tag_wrap_width)
|
|
1270
|
-
config.tag_wrap = config.tag_wrap_width;
|
|
1271
|
-
else
|
|
1272
|
-
config.tag_wrap = default_config.tag_wrap_width;
|
|
1273
|
-
}
|
|
1274
1315
|
|
|
1275
1316
|
if (Object.hasOwn(config, 'tag_wrap') && typeof config.tag_wrap !== 'number')
|
|
1276
1317
|
throw new Error(`tag_wrap config must be a number, not ${typeof config.tag_wrap}.`)
|
|
1277
1318
|
|
|
1278
|
-
/* TODO remove in v0.9.0 */
|
|
1279
|
-
if (Object.hasOwn(config, 'tag_wrap_width'))
|
|
1280
|
-
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.');
|
|
1281
|
-
|
|
1282
|
-
/* TODO remove in v0.9.0 */
|
|
1283
|
-
if (Object.hasOwn(config, 'tag_wrap_width') && typeof config.tag_wrap_width !== 'number')
|
|
1284
|
-
throw new Error(`tag_wrap_width config must be a number, not ${typeof config.tag_wrap_width}.`)
|
|
1285
|
-
|
|
1286
1319
|
if (Object.hasOwn(config, 'trim') && (!Array.isArray(config.trim) || !config.trim?.every((e) => typeof e === 'string')))
|
|
1287
1320
|
throw new Error('Trim config must be an array of strings.')
|
|
1288
1321
|
|
|
@@ -1351,11 +1384,11 @@ const wordWrap = (text, width, indent) => {
|
|
|
1351
1384
|
* for re-insertion later.
|
|
1352
1385
|
*
|
|
1353
1386
|
* @param {string} html
|
|
1354
|
-
* @
|
|
1355
|
-
* @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
|
|
1387
|
+
* @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
|
|
1356
1388
|
*/
|
|
1357
|
-
function extractIgnoredBlocks(html
|
|
1389
|
+
function extractIgnoredBlocks(html) {
|
|
1358
1390
|
setState({ ignored: true });
|
|
1391
|
+
const config = (getState()).config;
|
|
1359
1392
|
let current_html = html;
|
|
1360
1393
|
const extracted_blocks = new Map();
|
|
1361
1394
|
let marker_id = 0;
|
|
@@ -1366,19 +1399,27 @@ function extractIgnoredBlocks(html, config) {
|
|
|
1366
1399
|
const safe_tag_name = tag.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
1367
1400
|
|
|
1368
1401
|
const regex = new RegExp(
|
|
1369
|
-
|
|
1402
|
+
`(<\\s*${safe_tag_name}[^>]*>)(.*?)(<\\s*\/\\s*${safe_tag_name}\\s*>)`,
|
|
1370
1403
|
"gs" // global and dotAll
|
|
1371
1404
|
);
|
|
1372
1405
|
|
|
1406
|
+
/** @type RegExpExecArray | null */
|
|
1373
1407
|
let match;
|
|
1374
|
-
|
|
1408
|
+
|
|
1409
|
+
/**
|
|
1410
|
+
* @type {{ start: number; end: number; marker: string }[]}
|
|
1411
|
+
*/
|
|
1412
|
+
const replacements = [];
|
|
1375
1413
|
|
|
1376
1414
|
while ((match = regex.exec(current_html)) !== null) {
|
|
1377
1415
|
const marker = `${MARKER_PREFIX}${marker_id++}___`;
|
|
1378
|
-
|
|
1416
|
+
|
|
1417
|
+
/* Only store content, and minify tags later. */
|
|
1418
|
+
extracted_blocks.set(marker, match[2]);
|
|
1419
|
+
|
|
1379
1420
|
replacements.push({
|
|
1380
|
-
start: match.index,
|
|
1381
|
-
end:
|
|
1421
|
+
start: match.index + match[1].length, // start of content
|
|
1422
|
+
end: match.index + match[1].length + match[2].length, // end of content
|
|
1382
1423
|
marker: marker,
|
|
1383
1424
|
});
|
|
1384
1425
|
}
|
|
@@ -1392,6 +1433,7 @@ function extractIgnoredBlocks(html, config) {
|
|
|
1392
1433
|
current_html.substring(rep.end);
|
|
1393
1434
|
}
|
|
1394
1435
|
}
|
|
1436
|
+
|
|
1395
1437
|
return { html_with_markers: current_html, extracted_map: extracted_blocks }
|
|
1396
1438
|
}
|
|
1397
1439
|
|
|
@@ -1412,42 +1454,55 @@ function reinsertIgnoredBlocks(html_with_markers, extracted_map) {
|
|
|
1412
1454
|
return final_html
|
|
1413
1455
|
}
|
|
1414
1456
|
|
|
1457
|
+
const void_element_regex = new RegExp(`<(${VOID_ELEMENTS.join("|")})(?:\\s(?:[^/>]|/(?!>))*)*>`, 'g');
|
|
1458
|
+
|
|
1415
1459
|
/**
|
|
1416
|
-
*
|
|
1460
|
+
* Add a placeholder for void elements that are not self-closing.
|
|
1461
|
+
* This is for internal processing only.
|
|
1417
1462
|
*
|
|
1418
|
-
* @param {string} html
|
|
1419
|
-
* @returns
|
|
1420
|
-
* @example <br> => <br />
|
|
1463
|
+
* @param {string} html
|
|
1464
|
+
* @returns
|
|
1421
1465
|
*/
|
|
1422
|
-
|
|
1423
|
-
const {
|
|
1424
|
-
|
|
1425
|
-
if (!checked_html && !isHtml(html)) return html
|
|
1426
|
-
|
|
1427
|
-
return html.replace(/<([a-zA-Z\-0-9:]+)[^>]*>/g, (match, name) => {
|
|
1428
|
-
if (VOID_ELEMENTS.indexOf(name) > -1)
|
|
1429
|
-
return (`${match.substring(0, match.length - 1)} />`).replace(/\/\s\//g, '/')
|
|
1466
|
+
function setSelfClosing(html) {
|
|
1467
|
+
const { constants } = getState();
|
|
1430
1468
|
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
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
|
+
}
|
|
1434
1487
|
|
|
1435
1488
|
/**
|
|
1436
1489
|
* Enforce entity characters for textarea content.
|
|
1437
|
-
* To also minifiy, pass `minify` as `true`.
|
|
1490
|
+
* To also minifiy tags, pass `minify` as `true`.
|
|
1438
1491
|
*
|
|
1439
1492
|
* @param {string} html The HTML string to evaluate.
|
|
1440
|
-
* @param {boolean} [minify]
|
|
1493
|
+
* @param {boolean} [minify] Minifies the textarea tags themselves.
|
|
1441
1494
|
* Defaults to `false`. We recommend a value of `true` if you're running `entify()`
|
|
1442
1495
|
* as a standalone function.
|
|
1443
1496
|
* @returns {string}
|
|
1444
|
-
* @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>
|
|
1445
1500
|
*/
|
|
1446
1501
|
const entify = (html, minify = false) => {
|
|
1447
1502
|
/**
|
|
1448
1503
|
* Use entities inside textarea content.
|
|
1449
1504
|
*/
|
|
1450
|
-
html = html.replace(
|
|
1505
|
+
html = html.replace(/<\s*textarea[^>]*>((.|\n)*?)<s*\/\s*textarea\s*>/g, (match, capture) => {
|
|
1451
1506
|
return match.replace(capture, (match) => {
|
|
1452
1507
|
return match
|
|
1453
1508
|
.replace(/</g, '<')
|
|
@@ -1460,33 +1515,54 @@ const entify = (html, minify = false) => {
|
|
|
1460
1515
|
})
|
|
1461
1516
|
});
|
|
1462
1517
|
|
|
1463
|
-
/* Typical minification, but only for textareas. */
|
|
1464
1518
|
if (minify) {
|
|
1465
|
-
html = html.replace(
|
|
1466
|
-
/*
|
|
1467
|
-
|
|
1468
|
-
return match
|
|
1469
|
-
.replace(/\n|\t/g, '')
|
|
1470
|
-
.replace(/[a-z]+="\s*"/ig, '')
|
|
1471
|
-
.replace(/>\s+</g, '><')
|
|
1472
|
-
.replace(/\s+/g, ' ')
|
|
1473
|
-
});
|
|
1474
|
-
|
|
1475
|
-
/* Replace things in the entire element */
|
|
1476
|
-
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
|
|
1477
1522
|
.replace(/\s+/g, ' ')
|
|
1478
1523
|
.replace(/\s>/g, '>')
|
|
1479
1524
|
.replace(/>\s/g, '>')
|
|
1480
1525
|
.replace(/\s</g, '<')
|
|
1526
|
+
.replace(/<\s/g, '<')
|
|
1527
|
+
.replace(/<\/\s/g, '<\/')
|
|
1481
1528
|
.replace(/class=["']\s/g, (match) => match.replace(/\s/g, ''))
|
|
1482
|
-
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1483
|
-
return match
|
|
1529
|
+
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1484
1530
|
});
|
|
1485
1531
|
}
|
|
1486
1532
|
|
|
1487
1533
|
return html
|
|
1488
1534
|
};
|
|
1489
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
|
+
|
|
1490
1566
|
/**
|
|
1491
1567
|
* @type {Map<any,any>}
|
|
1492
1568
|
*/
|
|
@@ -1511,17 +1587,17 @@ const minify = (html, config) => {
|
|
|
1511
1587
|
|
|
1512
1588
|
/* Extract ignored elements. Skipped if prettify has already ignored blocks. */
|
|
1513
1589
|
if (!ignored && ignore) {
|
|
1514
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html
|
|
1590
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1515
1591
|
html = html_with_markers;
|
|
1516
1592
|
ignore_map$1 = extracted_map;
|
|
1517
1593
|
reinsert_ignored = true;
|
|
1518
1594
|
}
|
|
1519
1595
|
|
|
1520
1596
|
/**
|
|
1521
|
-
* Ensure textarea content is
|
|
1597
|
+
* Ensure textarea content is protected
|
|
1522
1598
|
* before general minification.
|
|
1523
1599
|
*/
|
|
1524
|
-
html = entify(html);
|
|
1600
|
+
html = entify(html, true);
|
|
1525
1601
|
|
|
1526
1602
|
/* All other minification. */
|
|
1527
1603
|
// Remove ALL newlines and tabs explicitly.
|
|
@@ -1533,11 +1609,13 @@ const minify = (html, config) => {
|
|
|
1533
1609
|
// Collapse any remaining multiple spaces to single spaces.
|
|
1534
1610
|
html = html.replace(/ {2,}/g, ' ');
|
|
1535
1611
|
|
|
1536
|
-
// Remove specific single spaces
|
|
1612
|
+
// Remove specific single spaces between tags and whitespace within tags.
|
|
1537
1613
|
html = html.replace(/ >/g, ">"); // <tag > -> <tag>
|
|
1538
|
-
html = html.replace(/ </g, "<"); //
|
|
1539
|
-
html = html.replace(/> /g, ">"); //
|
|
1540
|
-
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>
|
|
1541
1619
|
|
|
1542
1620
|
// Trim spaces around equals signs in attributes (run before value trim)
|
|
1543
1621
|
// This handles `attr = "value"` -> `attr="value"`
|
|
@@ -1558,6 +1636,9 @@ const minify = (html, config) => {
|
|
|
1558
1636
|
// Final trim for the whole string
|
|
1559
1637
|
html = html.trim();
|
|
1560
1638
|
|
|
1639
|
+
/* Remove protective entities. */
|
|
1640
|
+
html = dentify(html);
|
|
1641
|
+
|
|
1561
1642
|
/* Re-insert ignored elements. Skipped unless minify did the ignore. */
|
|
1562
1643
|
if (reinsert_ignored) {
|
|
1563
1644
|
html = reinsertIgnoredBlocks(html, ignore_map$1);
|
|
@@ -1582,7 +1663,6 @@ let ignore_map;
|
|
|
1582
1663
|
* Isolate tags, content, and comments.
|
|
1583
1664
|
*
|
|
1584
1665
|
* @param {string} html The HTML string to evaluate.
|
|
1585
|
-
* @returns {string}
|
|
1586
1666
|
* @example <div>Hello World!</div> =>
|
|
1587
1667
|
* [#-# : 0 : <div> : #-#]
|
|
1588
1668
|
* Hello World!
|
|
@@ -1594,7 +1674,7 @@ const enqueue = (html) => {
|
|
|
1594
1674
|
/* Regex to find tags OR text content between tags. */
|
|
1595
1675
|
const regex = /(<[^>]+>)|([^<]+)/g;
|
|
1596
1676
|
|
|
1597
|
-
html
|
|
1677
|
+
html.replace(regex, (match, c1, c2) => {
|
|
1598
1678
|
if (c1) {
|
|
1599
1679
|
convert.line.push({ type: "tag", value: match });
|
|
1600
1680
|
} else if (c2 && c2.trim().length > 0) {
|
|
@@ -1605,40 +1685,18 @@ const enqueue = (html) => {
|
|
|
1605
1685
|
i++;
|
|
1606
1686
|
return `\n[#-# : ${i} : ${match} : #-#]\n`
|
|
1607
1687
|
});
|
|
1608
|
-
|
|
1609
|
-
return html
|
|
1610
1688
|
};
|
|
1611
1689
|
|
|
1612
1690
|
/**
|
|
1613
|
-
*
|
|
1614
|
-
*
|
|
1615
|
-
* @param {string} html The HTML string to preprocess.
|
|
1691
|
+
* Process enqueued content.
|
|
1692
|
+
*
|
|
1616
1693
|
* @returns {string}
|
|
1617
1694
|
*/
|
|
1618
|
-
const
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
const { config } = getState();
|
|
1622
|
-
|
|
1623
|
-
if (config.trim.length > 0) html = trimify(html, config.trim);
|
|
1624
|
-
|
|
1625
|
-
html = minify(html);
|
|
1626
|
-
html = enqueue(html);
|
|
1627
|
-
|
|
1628
|
-
return html
|
|
1629
|
-
};
|
|
1630
|
-
|
|
1631
|
-
/**
|
|
1632
|
-
*
|
|
1633
|
-
* @param {import('htmlfy').Config} config
|
|
1634
|
-
* @returns {string}
|
|
1635
|
-
*/
|
|
1636
|
-
const process = (config) => {
|
|
1695
|
+
const process = () => {
|
|
1696
|
+
const { config, constants } = getState();
|
|
1637
1697
|
const step = " ".repeat(config.tab_size);
|
|
1638
1698
|
const tag_wrap = config.tag_wrap;
|
|
1639
1699
|
const content_wrap = config.content_wrap;
|
|
1640
|
-
const ignore_with = config.ignore_with;
|
|
1641
|
-
const placeholder_template = `-${ignore_with}`;
|
|
1642
1700
|
const strict = config.strict;
|
|
1643
1701
|
|
|
1644
1702
|
/* Track current number of indentations needed. */
|
|
@@ -1654,12 +1712,7 @@ const process = (config) => {
|
|
|
1654
1712
|
let current_line_value = source.value;
|
|
1655
1713
|
|
|
1656
1714
|
const is_ignored_content =
|
|
1657
|
-
current_line_value.startsWith(
|
|
1658
|
-
current_line_value.startsWith(placeholder_template + "gt--") ||
|
|
1659
|
-
current_line_value.startsWith(placeholder_template + "nl--") ||
|
|
1660
|
-
current_line_value.startsWith(placeholder_template + "cr--") ||
|
|
1661
|
-
current_line_value.startsWith(placeholder_template + "ws--") ||
|
|
1662
|
-
current_line_value.startsWith(placeholder_template + "tab--");
|
|
1715
|
+
current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
|
|
1663
1716
|
|
|
1664
1717
|
let subtrahend = 0;
|
|
1665
1718
|
const prev_line_data = convert.line[index - 1];
|
|
@@ -1677,8 +1730,12 @@ const process = (config) => {
|
|
|
1677
1730
|
if (prev_line_value.trim().startsWith("<!doctype")) subtrahend++;
|
|
1678
1731
|
/* prevLine is a comment. */
|
|
1679
1732
|
if (prev_line_value.trim().startsWith("<!--")) subtrahend++;
|
|
1680
|
-
/* prevLine is a
|
|
1681
|
-
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++;
|
|
1682
1739
|
/* prevLine is a closing tag. */
|
|
1683
1740
|
if (prev_line_value.trim().startsWith("</")) subtrahend++;
|
|
1684
1741
|
/* prevLine is text. */
|
|
@@ -1694,8 +1751,6 @@ const process = (config) => {
|
|
|
1694
1751
|
/**
|
|
1695
1752
|
* Starts with a single punctuation character.
|
|
1696
1753
|
* Add punctuation to end of previous line.
|
|
1697
|
-
*
|
|
1698
|
-
* TODO - Implement inline groups instead?
|
|
1699
1754
|
*/
|
|
1700
1755
|
if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
|
|
1701
1756
|
if (current_line_value.length === 1) {
|
|
@@ -1721,6 +1776,9 @@ const process = (config) => {
|
|
|
1721
1776
|
|
|
1722
1777
|
let result = current_line_value;
|
|
1723
1778
|
|
|
1779
|
+
/* Remove self-closing placeholder, if needed. */
|
|
1780
|
+
result = unsetSelfClosing(result);
|
|
1781
|
+
|
|
1724
1782
|
if (
|
|
1725
1783
|
source.type === 'text' &&
|
|
1726
1784
|
content_wrap > 0 &&
|
|
@@ -1751,9 +1809,9 @@ const process = (config) => {
|
|
|
1751
1809
|
|
|
1752
1810
|
const tag_name_match = tag_parts[0].match(/<([A-Za-z_:-]+)/);
|
|
1753
1811
|
const tag_name = tag_name_match ? tag_name_match[1] : "";
|
|
1754
|
-
const
|
|
1812
|
+
const is_self_closing = tag_parts.at(-1)?.endsWith("/>") && VOID_ELEMENTS.includes(tag_name);
|
|
1755
1813
|
const closing_part = tag_parts[1].trim();
|
|
1756
|
-
const closing_padding = padding + (strict &&
|
|
1814
|
+
const closing_padding = padding + (strict && is_self_closing ? " " : "");
|
|
1757
1815
|
|
|
1758
1816
|
wrapped_tag += closing_padding + closing_part;
|
|
1759
1817
|
|
|
@@ -1778,13 +1836,20 @@ const process = (config) => {
|
|
|
1778
1836
|
if (tag_wrap > 0) final_html = protectAttributes(final_html);
|
|
1779
1837
|
|
|
1780
1838
|
/* Extra preserve wrapped content. */
|
|
1781
|
-
if (content_wrap > 0 &&
|
|
1839
|
+
if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
|
|
1782
1840
|
final_html = finalProtectContent(final_html);
|
|
1783
1841
|
|
|
1784
1842
|
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
|
|
1785
1843
|
final_html = final_html.replace(
|
|
1786
|
-
/<(?<Element
|
|
1787
|
-
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
|
+
}
|
|
1788
1853
|
);
|
|
1789
1854
|
|
|
1790
1855
|
/* Revert wrapped content. */
|
|
@@ -1817,12 +1882,17 @@ const prettify = (html, config) => {
|
|
|
1817
1882
|
/* Return content as-is if it does not contain any HTML elements. */
|
|
1818
1883
|
if (!checked_html && !isHtml(html)) return html
|
|
1819
1884
|
|
|
1820
|
-
|
|
1885
|
+
/* Runs setState for config. */
|
|
1886
|
+
const validated_config = validateConfig(config || {});
|
|
1887
|
+
|
|
1821
1888
|
const ignore = validated_config.ignore.length > 0;
|
|
1822
1889
|
|
|
1890
|
+
/* Allows you to trimify before ignoring. */
|
|
1891
|
+
if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
|
|
1892
|
+
|
|
1823
1893
|
/* Extract ignored elements. */
|
|
1824
1894
|
if (!ignored && ignore) {
|
|
1825
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html
|
|
1895
|
+
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1826
1896
|
html = html_with_markers;
|
|
1827
1897
|
ignore_map = extracted_map;
|
|
1828
1898
|
reinsert_ignored = true;
|
|
@@ -1831,8 +1901,12 @@ const prettify = (html, config) => {
|
|
|
1831
1901
|
/* Preserve html text within attribute values. */
|
|
1832
1902
|
html = setIgnoreAttribute(html);
|
|
1833
1903
|
|
|
1834
|
-
|
|
1835
|
-
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();
|
|
1836
1910
|
|
|
1837
1911
|
/* Revert html text within attribute values. */
|
|
1838
1912
|
html = unsetIgnoreAttribute(html);
|