langsys-js-typescript 0.6.2 → 0.6.3

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/dist/index.mjs CHANGED
@@ -1074,8 +1074,14 @@ var TRANSLATABLE_ATTRIBUTES = [
1074
1074
  "placeholder",
1075
1075
  "alt",
1076
1076
  "title",
1077
+ "label",
1078
+ // <option>, <optgroup>, <track> — the text a user reads in the picker
1077
1079
  "aria-label",
1078
1080
  "aria-placeholder",
1081
+ "aria-description",
1082
+ "aria-valuetext",
1083
+ // the spoken value of a slider/meter
1084
+ "aria-roledescription",
1079
1085
  "data-error",
1080
1086
  "data-error-message",
1081
1087
  "data-validation-message",
@@ -1192,9 +1198,15 @@ async function registerContentBlock(contentBlock) {
1192
1198
  function tokenizeElement(element) {
1193
1199
  const tokens = [];
1194
1200
  const clone = element.cloneNode(true);
1195
- _walkForTokens(element, Array.from(clone.childNodes), tokens, []);
1201
+ _walkForTokens(element, Array.from(clone.childNodes), tokens, [], false, true);
1196
1202
  return { tokens, content: normalizeMarkupPlaceholders(clone.outerHTML) };
1197
1203
  }
1204
+ function legacyTokenizeElement(element) {
1205
+ const tokens = [];
1206
+ const clone = element.cloneNode(true);
1207
+ _walkForTokens(element, Array.from(clone.childNodes), tokens, [], true, false);
1208
+ return tokens;
1209
+ }
1198
1210
  function _writeKnownContentBlockToCache(category, customId) {
1199
1211
  const lookupCat = category || "__uncategorized__";
1200
1212
  sTranslations.update((current) => {
@@ -1208,18 +1220,18 @@ function _writeKnownContentBlockToCache(category, customId) {
1208
1220
  return { ...current };
1209
1221
  });
1210
1222
  }
1211
- function _walkForTokens(liveRoot, cloneNodes, tokens, indices) {
1223
+ function _walkForTokens(liveRoot, cloneNodes, tokens, indices, duplicateSelectOptions, applyStyles) {
1212
1224
  cloneNodes.forEach((node, index) => {
1213
1225
  if (node.nodeType === Node.ELEMENT_NODE) {
1214
1226
  const el = node;
1215
1227
  if (isTranslationExcluded(el)) return;
1216
1228
  if (isPhraseMarked(el)) return;
1217
1229
  }
1218
- if (node.hasChildNodes()) {
1230
+ if (applyStyles && node.hasChildNodes()) {
1219
1231
  _applyStylesToClone(liveRoot, node, [...indices, index]);
1220
1232
  }
1221
1233
  if (node.nodeType === Node.ELEMENT_NODE) {
1222
- _tokenizeAttributes(node, tokens);
1234
+ _tokenizeAttributes(node, tokens, duplicateSelectOptions);
1223
1235
  }
1224
1236
  const contentToken = node.nodeValue?.replace(/\s+/g, " ").trim();
1225
1237
  if (node.nodeType === Node.TEXT_NODE && contentToken) {
@@ -1227,10 +1239,10 @@ function _walkForTokens(liveRoot, cloneNodes, tokens, indices) {
1227
1239
  return;
1228
1240
  }
1229
1241
  if (!node.hasChildNodes()) return;
1230
- _walkForTokens(liveRoot, Array.from(node.childNodes), tokens, [...indices, index]);
1242
+ _walkForTokens(liveRoot, Array.from(node.childNodes), tokens, [...indices, index], duplicateSelectOptions, applyStyles);
1231
1243
  });
1232
1244
  }
1233
- function _tokenizeAttributes(element, tokens) {
1245
+ function _tokenizeAttributes(element, tokens, duplicateSelectOptions) {
1234
1246
  const tagName = element.tagName.toLowerCase();
1235
1247
  if (tagName === "img") {
1236
1248
  const img = element;
@@ -1251,7 +1263,7 @@ function _tokenizeAttributes(element, tokens) {
1251
1263
  if (value) tokens.push(normalizeMarkupPlaceholders(value));
1252
1264
  }
1253
1265
  }
1254
- if (tagName === "select") {
1266
+ if (duplicateSelectOptions && tagName === "select") {
1255
1267
  const options = element.querySelectorAll("option");
1256
1268
  options.forEach((option) => {
1257
1269
  const optionText = option.textContent?.trim();
@@ -1377,9 +1389,15 @@ var Translate = class {
1377
1389
  return;
1378
1390
  }
1379
1391
  if (derivedId) {
1380
- const legacyId = generateLegacyCustomId(contentBlock.category, contentBlock.tokens);
1381
- if (legacyId !== this.custom_id && isContentBlockKnown(contentBlock.category, legacyId)) {
1382
- this.custom_id = legacyId;
1392
+ const legacyTokens = legacyTokenizeElement(this.element);
1393
+ const candidates = [
1394
+ generateCustomId(contentBlock.category, legacyTokens),
1395
+ generateLegacyCustomId(contentBlock.category, legacyTokens)
1396
+ ];
1397
+ for (const candidate of candidates) {
1398
+ if (candidate === this.custom_id) continue;
1399
+ if (!isContentBlockKnown(contentBlock.category, candidate)) continue;
1400
+ this.custom_id = candidate;
1383
1401
  if (this.tokens.length > 1 && this.element) {
1384
1402
  this.translate(Array.from(this.element.childNodes));
1385
1403
  this.lastTranslatedLocale = currentlyLoadedLocale.get();
@@ -1643,6 +1661,6 @@ var t = ((phrase, ...rest) => {
1643
1661
  });
1644
1662
  var tSignal = LangsysApp.Translations.tSignal;
1645
1663
 
1646
- export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, PHRASE_MARKER_ATTRS, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, findUnusedParamKeys, generateCustomId, generateLegacyCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, isPhraseMarked, isTranslationExcluded, logger, markupTokenValues, md5, md5Legacy, normalizeMarkupPlaceholders, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1664
+ export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, PHRASE_MARKER_ATTRS, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, findUnusedParamKeys, generateCustomId, generateLegacyCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, isPhraseMarked, isTranslationExcluded, legacyTokenizeElement, logger, markupTokenValues, md5, md5Legacy, normalizeMarkupPlaceholders, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1647
1665
  //# sourceMappingURL=index.mjs.map
1648
1666
  //# sourceMappingURL=index.mjs.map