eat-js-sdk 2.2.5 → 2.2.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.
@@ -19100,27 +19100,38 @@ registerInteraction("dropdown", createDropdownInteraction, DropdownComponent);
19100
19100
  const decodeNbsp = (text) => text.replace(/ /g, " ").replace(/ /g, " ");
19101
19101
  const stripTags = (s) => s.replace(/<[^>]+>/g, "");
19102
19102
  const hasHtmlTags = (s) => /<[^>]/.test(s);
19103
+ const getTagName = (tag) => tag.replace(/^<\/?([^\s>]+).*$/, "$1").toLowerCase();
19103
19104
  const extractHtmlWords = (htmlText) => {
19104
- const re = /(<\/[^>]+>)|(<[^>]+>)|(\S+)/g;
19105
- const result = [];
19106
- let openPending = "";
19105
+ const re = /(<\/[^>]+>)|(<[^>]+>)|([^\s<>]+)/g;
19106
+ const tokens = [];
19107
19107
  let m;
19108
19108
  while ((m = re.exec(htmlText)) !== null) {
19109
- if (m[3]) {
19110
- result.push(openPending + m[3]);
19111
- openPending = "";
19112
- } else if (m[2]) {
19113
- openPending += m[2];
19114
- } else if (m[1]) {
19115
- if (result.length > 0) {
19116
- result[result.length - 1] += m[1];
19109
+ if (m[1]) tokens.push({ type: "close", value: m[1] });
19110
+ else if (m[2]) tokens.push({ type: "open", value: m[2] });
19111
+ else tokens.push({ type: "word", value: m[3] });
19112
+ }
19113
+ const words = [];
19114
+ const openStack = [];
19115
+ for (const tok of tokens) {
19116
+ if (tok.type === "open") {
19117
+ openStack.push(tok.value);
19118
+ } else if (tok.type === "close") {
19119
+ const closingName = getTagName(tok.value);
19120
+ for (let i = openStack.length - 1; i >= 0; i--) {
19121
+ if (getTagName(openStack[i]) === closingName) {
19122
+ openStack.splice(i, 1);
19123
+ break;
19124
+ }
19117
19125
  }
19126
+ } else {
19127
+ words.push({ value: tok.value, openStack: [...openStack] });
19118
19128
  }
19119
19129
  }
19120
- if (openPending && result.length > 0) {
19121
- result[result.length - 1] += openPending;
19122
- }
19123
- return result;
19130
+ return words.map(({ value, openStack: openStack2 }) => {
19131
+ const open = openStack2.join("");
19132
+ const close = [...openStack2].reverse().map((t2) => `</${getTagName(t2)}>`).join("");
19133
+ return open + value + close;
19134
+ });
19124
19135
  };
19125
19136
  const splitParagraphs = (rawText) => rawText.split(/<\/p>/i).map((p) => p.replace(/^<p>/i, "").trim()).filter(Boolean);
19126
19137
  const parseSentences = (rawText, correctAnswers = []) => {
@@ -19169,13 +19180,10 @@ const parseWords = (rawText, correctAnswers = []) => {
19169
19180
  const decoded = para.replace(/&lt;eat-contentful&gt;/g, "<eat-contentful>").replace(/&lt;\/eat-contentful&gt;/g, "</eat-contentful>");
19170
19181
  tokenRegex.lastIndex = 0;
19171
19182
  let match;
19172
- let pendingOpenTags = "";
19173
- const trailingOpenTagsRe = /((?:<(?!\/?eat-)[^>]+>)+\s*)$/;
19174
19183
  while ((match = tokenRegex.exec(decoded)) !== null) {
19175
19184
  if (match[2] !== void 0) {
19176
- const outerOpen = pendingOpenTags + (match[1] ?? "");
19185
+ const outerOpen = match[1] ?? "";
19177
19186
  const outerClose = match[3] ?? "";
19178
- pendingOpenTags = "";
19179
19187
  const rawContent = match[2].trim();
19180
19188
  const displayText = decodeNbsp(rawContent);
19181
19189
  if (displayText) {
@@ -19191,10 +19199,7 @@ const parseWords = (rawText, correctAnswers = []) => {
19191
19199
  }
19192
19200
  } else if (match[4] !== void 0) {
19193
19201
  const rawChunk = match[4];
19194
- const trailingTagMatch = rawChunk.match(trailingOpenTagsRe);
19195
- pendingOpenTags = trailingTagMatch ? trailingTagMatch[1].trimEnd() : "";
19196
- const chunk = trailingTagMatch ? rawChunk.slice(0, -trailingTagMatch[1].length) : rawChunk;
19197
- const words = hasHtmlTags(chunk) ? extractHtmlWords(chunk) : chunk.split(/\s+/).filter(Boolean);
19202
+ const words = hasHtmlTags(rawChunk) ? extractHtmlWords(rawChunk) : rawChunk.split(/\s+/).filter(Boolean);
19198
19203
  for (const word of words) {
19199
19204
  const visibleWord = hasHtmlTags(word) ? stripTags(word) : word;
19200
19205
  if (purePunctRe.test(visibleWord)) {
@@ -19209,7 +19214,15 @@ const parseWords = (rawText, correctAnswers = []) => {
19209
19214
  } else {
19210
19215
  while (correctAnswerIdSet.has(`${globalIndex}`)) globalIndex++;
19211
19216
  if (hasHtmlTags(word)) {
19212
- segments.push({ id: `${globalIndex}`, text: decodeNbsp(word) });
19217
+ const innerVisible = stripTags(word);
19218
+ const punctMatch = innerVisible.match(trailingPunctRe);
19219
+ if (punctMatch) {
19220
+ const punc = punctMatch[1];
19221
+ const rebuilt = word.replace(new RegExp(punc.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "(</)"), "$1");
19222
+ segments.push({ id: `${globalIndex}`, text: decodeNbsp(rebuilt), trailingPunctuation: punc });
19223
+ } else {
19224
+ segments.push({ id: `${globalIndex}`, text: decodeNbsp(word) });
19225
+ }
19213
19226
  } else {
19214
19227
  const punctMatch = word.match(trailingPunctRe);
19215
19228
  const text = decodeNbsp(punctMatch ? word.slice(0, -punctMatch[1].length) : word);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eat-js-sdk",
3
- "version": "2.2.5",
3
+ "version": "2.2.6",
4
4
  "change version": "2.2.0",
5
5
  "description": "Authoring tool frontend SDK",
6
6
  "contributors": [