@mgdis/core-ui-helpers 1.0.0 → 1.0.2

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.
@@ -1074,73 +1074,6 @@
1074
1074
  return html
1075
1075
  };
1076
1076
 
1077
- /**
1078
- * @typedef {"<" | ">" | "\n" | "\r" | " " | "\t"} ReplacementKey
1079
- * Defines the possible keys for the replacements object.
1080
- */
1081
-
1082
- /**
1083
- * Replace entities with ignore string.
1084
- *
1085
- * @param {string} html
1086
- * @param {import('htmlfy').Config} config
1087
- * @returns {string}
1088
- */
1089
- const setIgnoreElement = (html, config) => {
1090
- const ignore = config.ignore;
1091
- const ignore_string = config.ignore_with;
1092
-
1093
- /* Pre-create replacement map for faster lookup. */
1094
- const replacements = {
1095
- "<": "-" + ignore_string + "lt-",
1096
- ">": "-" + ignore_string + "gt-",
1097
- "\n": "-" + ignore_string + "nl-",
1098
- "\r": "-" + ignore_string + "cr-",
1099
- " ": "-" + ignore_string + "ws-", // Use space ' ' as key
1100
- "\t": "-" + ignore_string + "tab-", // Use tab '\t' as key (add placeholder if needed)
1101
- // Add other specific whitespace chars like \v, \f if necessary
1102
- };
1103
- /* Regex to match any character we need to replace inside the ignored content. */
1104
- const charReplaceRegex = /[<>\n\r \t]/g;
1105
-
1106
- for (let e = 0; e < ignore.length; e++) {
1107
- const tagName = ignore[e].replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
1108
- const regex = new RegExp(
1109
- `<${tagName}[^>]*>(.*?)<\/${tagName}>`,
1110
- "gs" // Use 'g' (global) and 's' (dotAll) flags
1111
- );
1112
-
1113
- html = html.replace(
1114
- regex,
1115
- (/** @type {string} */ fullMatch, /** @type {string} */ capture) => {
1116
- const processedCapture = capture.replace(
1117
- charReplaceRegex,
1118
- (/** @type {string} */ char) => {
1119
- return replacements[/** @type {ReplacementKey} */ (char)] // Return the placeholder
1120
- // Or safer if map might be incomplete: return replacements[char] || char
1121
- }
1122
- );
1123
-
1124
- /* Reconstruct the string using original tags and processed content. */
1125
- const openingTagEnd = fullMatch.indexOf(">") + 1;
1126
- const closingTagStart = fullMatch.lastIndexOf(`</${tagName}>`);
1127
-
1128
- /* Check if tags were found (basic sanity check). */
1129
- if (openingTagEnd > 0 && closingTagStart > 0) {
1130
- const openingTag = fullMatch.substring(0, openingTagEnd);
1131
- const closingTag = fullMatch.substring(closingTagStart);
1132
- return openingTag + processedCapture + closingTag
1133
- } else {
1134
- // Should not happen with valid HTML and correct regex
1135
- // Return original match to avoid breaking things further
1136
- return fullMatch
1137
- }
1138
- }
1139
- );
1140
- }
1141
- return html
1142
- };
1143
-
1144
1077
  /**
1145
1078
  * Trim leading and trailing whitespace characters.
1146
1079
  *
@@ -1230,34 +1163,6 @@
1230
1163
  )
1231
1164
  };
1232
1165
 
1233
- /**
1234
- * Replace ignore string with entities.
1235
- *
1236
- * @param {string} html
1237
- * @param {import('htmlfy').Config} config
1238
- * @returns {string}
1239
- */
1240
- const unsetIgnoreElement = (html, config) => {
1241
- const ignore = config.ignore;
1242
- const ignore_string = config.ignore_with;
1243
-
1244
- for (let e = 0; e < ignore.length; e++) {
1245
- const regex = new RegExp(`<${ignore[e]}[^>]*>((.|\n)*?)<\/${ignore[e]}>`, "g");
1246
-
1247
- html = html.replace(regex, (/** @type {string} */match, /** @type {any} */capture) => {
1248
- return match.replace(capture, (match) => {
1249
- return match
1250
- .replace(new RegExp('-' + ignore_string + 'lt-', "g"), '<')
1251
- .replace(new RegExp('-' + ignore_string + 'gt-', "g"), '>')
1252
- .replace(new RegExp('-' + ignore_string + 'nl-', "g"), '\n')
1253
- .replace(new RegExp('-' + ignore_string + 'cr-', "g"), '\r')
1254
- .replace(new RegExp('-' + ignore_string + 'ws-', "g"), ' ')
1255
- })
1256
- });
1257
- }
1258
- return html
1259
- };
1260
-
1261
1166
  /**
1262
1167
  * Validate any passed-in config options and merge with CONFIG.
1263
1168
  *
@@ -1392,6 +1297,71 @@
1392
1297
  return protectContent(result)
1393
1298
  };
1394
1299
 
1300
+ /**
1301
+ * Extract any HTML blocks to be ignored,
1302
+ * and replace them with a placeholder
1303
+ * for re-insertion later.
1304
+ *
1305
+ * @param {string} html
1306
+ * @param {import('htmlfy').Config} config
1307
+ * @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
1308
+ */
1309
+ function extractIgnoredBlocks(html, config) {
1310
+ let current_html = html;
1311
+ const extracted_blocks = new Map();
1312
+ let marker_id = 0;
1313
+ const MARKER_PREFIX = "___HTMLFY_SPECIAL_IGNORE_MARKER_";
1314
+
1315
+ for (const tag of config.ignore) {
1316
+ /* Ensure tag is escaped if it can contain regex special chars. */
1317
+ const safe_tag_name = tag.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
1318
+
1319
+ const regex = new RegExp(
1320
+ `<${safe_tag_name}[^>]*>.*?<\/${safe_tag_name}>`,
1321
+ "gs" // global and dotAll
1322
+ );
1323
+
1324
+ let match;
1325
+ const replacements = []; // Store [startIndex, endIndex, marker]
1326
+
1327
+ while ((match = regex.exec(current_html)) !== null) {
1328
+ const marker = `${MARKER_PREFIX}${marker_id++}___`;
1329
+ extracted_blocks.set(marker, match[0]); // Store the full original match
1330
+ replacements.push({
1331
+ start: match.index,
1332
+ end: regex.lastIndex,
1333
+ marker: marker,
1334
+ });
1335
+ }
1336
+
1337
+ /* Apply replacements from the end to the beginning to keep indices valid. */
1338
+ for (let i = replacements.length - 1; i >= 0; i--) {
1339
+ const rep = replacements[i];
1340
+ current_html =
1341
+ current_html.substring(0, rep.start) +
1342
+ rep.marker +
1343
+ current_html.substring(rep.end);
1344
+ }
1345
+ }
1346
+ return { html_with_markers: current_html, extracted_map: extracted_blocks }
1347
+ }
1348
+
1349
+ /**
1350
+ * Re-insert ignored HTML blocks.
1351
+ *
1352
+ * @param {string} html_with_markers
1353
+ * @param {Map<any,any>} extracted_map
1354
+ * @returns
1355
+ */
1356
+ function reinsertIgnoredBlocks(html_with_markers, extracted_map) {
1357
+ let final_html = html_with_markers;
1358
+
1359
+ for (const [marker, original_block] of extracted_map) {
1360
+ final_html = final_html.split(marker).join(original_block);
1361
+ }
1362
+ return final_html
1363
+ }
1364
+
1395
1365
  /**
1396
1366
  * Ensure void elements are "self-closing".
1397
1367
  *
@@ -1538,6 +1508,11 @@
1538
1508
  line: []
1539
1509
  };
1540
1510
 
1511
+ /**
1512
+ * @type {Map<any,any>}
1513
+ */
1514
+ let ignore_map;
1515
+
1541
1516
  /**
1542
1517
  * Isolate tags, content, and comments.
1543
1518
  *
@@ -1648,6 +1623,25 @@
1648
1623
  const current_indent_level = offset; // Store the level for this line
1649
1624
 
1650
1625
  indents = indents.substring(0, current_indent_level); // Adjust for *next* round
1626
+
1627
+ /**
1628
+ * Starts with a single punctuation character.
1629
+ * Add punctuation to end of previous line.
1630
+ *
1631
+ * TODO - Implement inline groups instead?
1632
+ */
1633
+ if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
1634
+ if (current_line_value.length === 1) {
1635
+ output_lines[output_lines.length - 1] =
1636
+ output_lines.at(-1) + current_line_value;
1637
+ return
1638
+ } else {
1639
+ output_lines[output_lines.length - 1] =
1640
+ output_lines.at(-1) + current_line_value.charAt(0);
1641
+ current_line_value = current_line_value.slice(1).trim();
1642
+ }
1643
+ }
1644
+
1651
1645
  const padding = step.repeat(current_indent_level);
1652
1646
 
1653
1647
  if (is_ignored_content) {
@@ -1759,8 +1753,12 @@
1759
1753
  const ignore = validated_config.ignore.length > 0;
1760
1754
  trim = validated_config.trim;
1761
1755
 
1762
- /* Preserve ignored elements. */
1763
- if (ignore) html = setIgnoreElement(html, validated_config);
1756
+ /* Extract ignored elements. */
1757
+ if (ignore) {
1758
+ const { html_with_markers, extracted_map } = extractIgnoredBlocks(html, validated_config);
1759
+ html = html_with_markers;
1760
+ ignore_map = extracted_map;
1761
+ }
1764
1762
 
1765
1763
  /* Preserve html text within attribute values. */
1766
1764
  html = setIgnoreAttribute(html);
@@ -1771,8 +1769,10 @@
1771
1769
  /* Revert html text within attribute values. */
1772
1770
  html = unsetIgnoreAttribute(html);
1773
1771
 
1774
- /* Revert ignored elements. */
1775
- if (ignore) html = unsetIgnoreElement(html, validated_config);
1772
+ /* Re-insert ignored elements. */
1773
+ if (ignore) {
1774
+ html = reinsertIgnoredBlocks(html, ignore_map);
1775
+ }
1776
1776
 
1777
1777
  return html
1778
1778
  };