@mgdis/core-ui-helpers 1.0.5 → 1.0.6

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