eat-js-sdk 2.2.7 → 2.2.9

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.
@@ -19098,31 +19098,69 @@ const decodeNbsp = (text) => text.replace(/ /g, " ").replace(/ /g,
19098
19098
  const stripTags = (s) => s.replace(/<[^>]+>/g, "");
19099
19099
  const hasHtmlTags = (s) => /<[^>]/.test(s);
19100
19100
  const getTagName = (tag) => tag.replace(/^<\/?([^\s>]+).*$/, "$1").toLowerCase();
19101
+ const isSuperSubTag = (tag) => {
19102
+ const name = getTagName(tag);
19103
+ return name === "sup" || name === "sub";
19104
+ };
19101
19105
  const extractHtmlWords = (htmlText) => {
19102
- const re = /(<\/[^>]+>)|(<[^>]+>)|([^\s<>]+)/g;
19106
+ const re = /(\s+)|(<\/[^>]+>)|(<[^>]+>)|([^\s<>]+)/g;
19103
19107
  const tokens = [];
19104
19108
  let m;
19105
19109
  while ((m = re.exec(htmlText)) !== null) {
19106
- if (m[1]) tokens.push({ type: "close", value: m[1] });
19107
- else if (m[2]) tokens.push({ type: "open", value: m[2] });
19108
- else tokens.push({ type: "word", value: m[3] });
19110
+ if (m[1]) tokens.push({ type: "space", value: m[1] });
19111
+ else if (m[2]) tokens.push({ type: "close", value: m[2] });
19112
+ else if (m[3]) tokens.push({ type: "open", value: m[3] });
19113
+ else tokens.push({ type: "word", value: m[4] });
19109
19114
  }
19110
19115
  const words = [];
19111
19116
  const openStack = [];
19112
- for (const tok of tokens) {
19117
+ let lastWasSpace = true;
19118
+ let mergingIntoLast = false;
19119
+ let mergeDepth = 0;
19120
+ for (let i = 0; i < tokens.length; i++) {
19121
+ const tok = tokens[i];
19122
+ if (tok.type === "space") {
19123
+ lastWasSpace = true;
19124
+ continue;
19125
+ }
19113
19126
  if (tok.type === "open") {
19114
- openStack.push(tok.value);
19115
- } else if (tok.type === "close") {
19116
- const closingName = getTagName(tok.value);
19117
- for (let i = openStack.length - 1; i >= 0; i--) {
19118
- if (getTagName(openStack[i]) === closingName) {
19119
- openStack.splice(i, 1);
19120
- break;
19127
+ if (!lastWasSpace && isSuperSubTag(tok.value) && words.length > 0) {
19128
+ mergingIntoLast = true;
19129
+ mergeDepth = 1;
19130
+ words[words.length - 1].value += tok.value;
19131
+ } else if (mergingIntoLast) {
19132
+ mergeDepth++;
19133
+ words[words.length - 1].value += tok.value;
19134
+ } else {
19135
+ openStack.push(tok.value);
19136
+ }
19137
+ continue;
19138
+ }
19139
+ if (tok.type === "close") {
19140
+ if (mergingIntoLast) {
19141
+ words[words.length - 1].value += tok.value;
19142
+ mergeDepth--;
19143
+ if (mergeDepth <= 0) {
19144
+ mergingIntoLast = false;
19145
+ mergeDepth = 0;
19146
+ }
19147
+ } else {
19148
+ const closingName = getTagName(tok.value);
19149
+ for (let j = openStack.length - 1; j >= 0; j--) {
19150
+ if (getTagName(openStack[j]) === closingName) {
19151
+ openStack.splice(j, 1);
19152
+ break;
19153
+ }
19121
19154
  }
19122
19155
  }
19156
+ continue;
19157
+ }
19158
+ if (mergingIntoLast) {
19159
+ words[words.length - 1].value += tok.value;
19123
19160
  } else {
19124
19161
  words.push({ value: tok.value, openStack: [...openStack] });
19125
19162
  }
19163
+ lastWasSpace = false;
19126
19164
  }
19127
19165
  return words.map(({ value, openStack: openStack2 }) => {
19128
19166
  const open = openStack2.join("");
@@ -19142,7 +19180,7 @@ const parseSentences = (rawText, correctAnswers = []) => {
19142
19180
  const chunks = decoded.split("<eat-sentence>");
19143
19181
  for (const chunk of chunks) {
19144
19182
  const contentfulMatch = chunk.match(
19145
- /((?:<(?!\/?eat-)[^>]+>)*)<eat-contentful>(.*?)<\/eat-contentful>((?:<\/[^>]+>)*)/
19183
+ /((?:<(?!\/?eat-)[^>]+>)*)<eat-contentful>(.*?)<\/eat-contentful>([\s\S]*)/
19146
19184
  );
19147
19185
  const outerOpen = contentfulMatch ? contentfulMatch[1] : "";
19148
19186
  const outerClose = contentfulMatch ? contentfulMatch[3] : "";
@@ -19418,9 +19456,48 @@ function SelectableTextInteractionContent($$anchor, $$props) {
19418
19456
  }
19419
19457
  });
19420
19458
  const splitLastWord = (text) => {
19421
- const lastSpace = text.lastIndexOf(" ");
19422
- if (lastSpace === -1) return ["", text];
19423
- return [text.slice(0, lastSpace + 1), text.slice(lastSpace + 1)];
19459
+ const tagRe = /<[^>]+>|[^<]+/g;
19460
+ const tokens = [];
19461
+ let m;
19462
+ while ((m = tagRe.exec(text)) !== null) {
19463
+ tokens.push({ isTag: m[0].startsWith("<"), value: m[0] });
19464
+ }
19465
+ let splitTi = -1;
19466
+ let splitPos = -1;
19467
+ for (let i = tokens.length - 1; i >= 0; i--) {
19468
+ if (!tokens[i].isTag) {
19469
+ const sp = tokens[i].value.lastIndexOf(" ");
19470
+ if (sp !== -1) {
19471
+ splitTi = i;
19472
+ splitPos = sp;
19473
+ break;
19474
+ }
19475
+ }
19476
+ }
19477
+ if (splitTi === -1) return ["", text];
19478
+ const getTagName2 = (tag) => tag.replace(/^<\/?([^\s>/]+).*$/, "$1").toLowerCase();
19479
+ const openStack = [];
19480
+ for (let i = 0; i < splitTi; i++) {
19481
+ const tok = tokens[i];
19482
+ if (!tok.isTag) continue;
19483
+ if (tok.value.startsWith("</")) {
19484
+ const name = getTagName2(tok.value);
19485
+ for (let j = openStack.length - 1; j >= 0; j--) {
19486
+ if (getTagName2(openStack[j]) === name) {
19487
+ openStack.splice(j, 1);
19488
+ break;
19489
+ }
19490
+ }
19491
+ } else if (!tok.value.endsWith("/>")) {
19492
+ openStack.push(tok.value);
19493
+ }
19494
+ }
19495
+ const closeTags = [...openStack].reverse().map((t2) => `</${getTagName2(t2)}>`).join("");
19496
+ const reopenTags = openStack.join("");
19497
+ const textVal = tokens[splitTi].value;
19498
+ const bodyText = tokens.slice(0, splitTi).map((t2) => t2.value).join("") + textVal.slice(0, splitPos + 1) + closeTags;
19499
+ const lastWord = reopenTags + textVal.slice(splitPos + 1) + tokens.slice(splitTi + 1).map((t2) => t2.value).join("");
19500
+ return [bodyText, lastWord];
19424
19501
  };
19425
19502
  const focusFirstSegment = () => {
19426
19503
  const first = get$1(passageEl)?.querySelector('[role="button"][tabindex="0"]');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eat-js-sdk",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "change version": "2.2.0",
5
5
  "description": "Authoring tool frontend SDK",
6
6
  "contributors": [