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