@mgdis/core-ui-helpers 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
  */
@@ -1504,7 +1576,7 @@ let ignore_map$1;
1504
1576
  */
1505
1577
  const minify = (html, config) => {
1506
1578
  let reinsert_ignored = false;
1507
- const { checked_html, ignored } = getState();
1579
+ const { checked_html, ignored, constants } = getState();
1508
1580
 
1509
1581
  if (!checked_html && !isHtml(html)) return html
1510
1582
 
@@ -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,28 @@ 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
+ // Protect space between text content and an opening tag (e.g., "text <a>")
1611
+ html = html.replace(
1612
+ /(\S) (<[a-zA-Z][a-zA-Z0-9_:-]*)/g,
1613
+ `$1___MINIFY-PROTECTED-SPACE___$2`
1614
+ );
1615
+
1616
+ // Protect space between a closing tag and text content (e.g., "</a> text")
1617
+ html = html.replace(
1618
+ /(<\/[a-zA-Z][a-zA-Z0-9_:-]*>) (\S)/g,
1619
+ `$1___MINIFY-PROTECTED-SPACE___$2`
1620
+ );
1621
+
1622
+ // Remove specific single spaces between tags and whitespace within tags.
1539
1623
  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>
1624
+ html = html.replace(/ </g, "<"); // leading space before tag
1625
+ html = html.replace(/> /g, ">"); // trailing space after tag
1626
+ html = html.replace(/< /g, "<"); // < tag> -> <tag>
1627
+ html = html.replace(/<\s+\//g, '</'); // < /tag> -> </tag>
1628
+ html = html.replace(/<\/\s+/g, '</'); // </ tag> -> </tag>
1629
+
1630
+ // Unprotect space around inner tags
1631
+ html = html.replace(new RegExp('___MINIFY-PROTECTED-SPACE___', 'g'), ' ');
1543
1632
 
1544
1633
  // Trim spaces around equals signs in attributes (run before value trim)
1545
1634
  // This handles `attr = "value"` -> `attr="value"`
@@ -1560,6 +1649,9 @@ const minify = (html, config) => {
1560
1649
  // Final trim for the whole string
1561
1650
  html = html.trim();
1562
1651
 
1652
+ /* Remove protective entities. */
1653
+ html = dentify(html);
1654
+
1563
1655
  /* Re-insert ignored elements. Skipped unless minify did the ignore. */
1564
1656
  if (reinsert_ignored) {
1565
1657
  html = reinsertIgnoredBlocks(html, ignore_map$1);
@@ -1584,7 +1676,6 @@ let ignore_map;
1584
1676
  * Isolate tags, content, and comments.
1585
1677
  *
1586
1678
  * @param {string} html The HTML string to evaluate.
1587
- * @returns {string}
1588
1679
  * @example <div>Hello World!</div> =>
1589
1680
  * [#-# : 0 : <div> : #-#]
1590
1681
  * Hello World!
@@ -1596,7 +1687,7 @@ const enqueue = (html) => {
1596
1687
  /* Regex to find tags OR text content between tags. */
1597
1688
  const regex = /(<[^>]+>)|([^<]+)/g;
1598
1689
 
1599
- html = html.replace(regex, (match, c1, c2) => {
1690
+ html.replace(regex, (match, c1, c2) => {
1600
1691
  if (c1) {
1601
1692
  convert.line.push({ type: "tag", value: match });
1602
1693
  } else if (c2 && c2.trim().length > 0) {
@@ -1607,40 +1698,18 @@ const enqueue = (html) => {
1607
1698
  i++;
1608
1699
  return `\n[#-# : ${i} : ${match} : #-#]\n`
1609
1700
  });
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
1701
  };
1632
1702
 
1633
1703
  /**
1634
- *
1635
- * @param {import('htmlfy').Config} config
1704
+ * Process enqueued content.
1705
+ *
1636
1706
  * @returns {string}
1637
1707
  */
1638
- const process = (config) => {
1708
+ const process = () => {
1709
+ const { config, constants } = getState();
1639
1710
  const step = " ".repeat(config.tab_size);
1640
1711
  const tag_wrap = config.tag_wrap;
1641
1712
  const content_wrap = config.content_wrap;
1642
- const ignore_with = config.ignore_with;
1643
- const placeholder_template = `-${ignore_with}`;
1644
1713
  const strict = config.strict;
1645
1714
 
1646
1715
  /* Track current number of indentations needed. */
@@ -1656,12 +1725,7 @@ const process = (config) => {
1656
1725
  let current_line_value = source.value;
1657
1726
 
1658
1727
  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--");
1728
+ current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
1665
1729
 
1666
1730
  let subtrahend = 0;
1667
1731
  const prev_line_data = convert.line[index - 1];
@@ -1679,8 +1743,12 @@ const process = (config) => {
1679
1743
  if (prev_line_value.trim().startsWith("<!doctype")) subtrahend++;
1680
1744
  /* prevLine is a comment. */
1681
1745
  if (prev_line_value.trim().startsWith("<!--")) subtrahend++;
1682
- /* prevLine is a self-closing tag. */
1683
- if (prev_line_value.trim().endsWith("/>")) subtrahend++;
1746
+ /* prevLine is a void element. */
1747
+ if (
1748
+ prev_line_value.trim().endsWith("/>") // native self-closing
1749
+ ||
1750
+ prev_line_value.trim().endsWith(constants.SELF_CLOSING_PLACEHOLDER) // synthetic self-closing
1751
+ ) subtrahend++;
1684
1752
  /* prevLine is a closing tag. */
1685
1753
  if (prev_line_value.trim().startsWith("</")) subtrahend++;
1686
1754
  /* prevLine is text. */
@@ -1696,8 +1764,6 @@ const process = (config) => {
1696
1764
  /**
1697
1765
  * Starts with a single punctuation character.
1698
1766
  * Add punctuation to end of previous line.
1699
- *
1700
- * TODO - Implement inline groups instead?
1701
1767
  */
1702
1768
  if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
1703
1769
  if (current_line_value.length === 1) {
@@ -1708,6 +1774,9 @@ const process = (config) => {
1708
1774
  output_lines[output_lines.length - 1] =
1709
1775
  output_lines.at(-1) + current_line_value.charAt(0);
1710
1776
  current_line_value = current_line_value.slice(1).trim();
1777
+
1778
+ /* If nothing left after extracting punctuation, skip this line. */
1779
+ if (current_line_value.length === 0) return
1711
1780
  }
1712
1781
  }
1713
1782
 
@@ -1723,6 +1792,9 @@ const process = (config) => {
1723
1792
 
1724
1793
  let result = current_line_value;
1725
1794
 
1795
+ /* Remove self-closing placeholder, if needed. */
1796
+ result = unsetSelfClosing(result);
1797
+
1726
1798
  if (
1727
1799
  source.type === 'text' &&
1728
1800
  content_wrap > 0 &&
@@ -1753,9 +1825,9 @@ const process = (config) => {
1753
1825
 
1754
1826
  const tag_name_match = tag_parts[0].match(/<([A-Za-z_:-]+)/);
1755
1827
  const tag_name = tag_name_match ? tag_name_match[1] : "";
1756
- const is_void = VOID_ELEMENTS.includes(tag_name);
1828
+ const is_self_closing = tag_parts.at(-1)?.endsWith("/>") && VOID_ELEMENTS.includes(tag_name);
1757
1829
  const closing_part = tag_parts[1].trim();
1758
- const closing_padding = padding + (strict && is_void ? " " : ""); // Add space if void/strict
1830
+ const closing_padding = padding + (strict && is_self_closing ? " " : "");
1759
1831
 
1760
1832
  wrapped_tag += closing_padding + closing_part;
1761
1833
 
@@ -1780,13 +1852,20 @@ const process = (config) => {
1780
1852
  if (tag_wrap > 0) final_html = protectAttributes(final_html);
1781
1853
 
1782
1854
  /* Extra preserve wrapped content. */
1783
- if (content_wrap > 0 && /\n[ ]*[^\n]*__!i-£___£%__[^\n]*\n/.test(final_html))
1855
+ if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
1784
1856
  final_html = finalProtectContent(final_html);
1785
1857
 
1786
1858
  /* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
1787
1859
  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, '')
1860
+ /<(?<Element>[^>\s]+)[^>]*>[^<]*?[^><\/\s][^<]*?<\/\k<Element>>|<script[^>]*>[\s]*<\/script>|<([\w:\._-]+)([^>]*)><\/\2>|<([\w:\._-]+)([^>]*)>[\s]+<\/\4>/g,
1861
+ match => {
1862
+ // Check if this contains placeholder
1863
+ if (match.includes(constants.SELF_CLOSING_PLACEHOLDER) || match.includes(constants.CONTENT_IGNORE_PLACEHOLDER)) {
1864
+ return match // Don't modify if it contains the placeholder
1865
+ }
1866
+
1867
+ return match.replace(/\n|\t|\s{2,}/g, '')
1868
+ }
1790
1869
  );
1791
1870
 
1792
1871
  /* Revert wrapped content. */
@@ -1819,12 +1898,17 @@ const prettify = (html, config) => {
1819
1898
  /* Return content as-is if it does not contain any HTML elements. */
1820
1899
  if (!checked_html && !isHtml(html)) return html
1821
1900
 
1822
- const validated_config = config ? validateConfig(config) : { ...CONFIG };
1901
+ /* Runs setState for config. */
1902
+ const validated_config = validateConfig(config || {});
1903
+
1823
1904
  const ignore = validated_config.ignore.length > 0;
1824
1905
 
1906
+ /* Allows you to trimify before ignoring. */
1907
+ if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
1908
+
1825
1909
  /* Extract ignored elements. */
1826
1910
  if (!ignored && ignore) {
1827
- const { html_with_markers, extracted_map } = extractIgnoredBlocks(html, validated_config);
1911
+ const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
1828
1912
  html = html_with_markers;
1829
1913
  ignore_map = extracted_map;
1830
1914
  reinsert_ignored = true;
@@ -1833,8 +1917,12 @@ const prettify = (html, config) => {
1833
1917
  /* Preserve html text within attribute values. */
1834
1918
  html = setIgnoreAttribute(html);
1835
1919
 
1836
- html = preprocess(html);
1837
- html = process(validated_config);
1920
+ /* Insert placeholder for void elements that aren't self-closing. */
1921
+ html = setSelfClosing(html);
1922
+
1923
+ html = minify(html);
1924
+ enqueue(html);
1925
+ html = process();
1838
1926
 
1839
1927
  /* Revert html text within attribute values. */
1840
1928
  html = unsetIgnoreAttribute(html);