onionskin-cli 0.0.5 → 0.0.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/README.md +4 -4
  3. package/package.json +1 -1
  4. package/scripts/checker/check.py +24 -5
  5. package/scripts/checker/requirements.txt +482 -5
  6. package/scripts/requirements.txt +482 -5
  7. package/scripts/rewriter/apple/main.swift +1 -1
  8. package/scripts/rewriter/apple/onionskin-fm +0 -0
  9. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  10. package/skills/onionskin/SKILL.md +4 -4
  11. package/skills/onionskin/reference/ops.md +1 -1
  12. package/src/agent-runner.js +94 -15
  13. package/src/checker.js +238 -33
  14. package/src/cli.js +173 -33
  15. package/src/doctor.js +12 -3
  16. package/src/docx.js +130 -63
  17. package/src/fsutil.js +13 -5
  18. package/src/history.js +11 -2
  19. package/src/html-render.js +74 -15
  20. package/src/html-utf8.js +1 -1
  21. package/src/html.js +3 -1
  22. package/src/merge-package.js +82 -26
  23. package/src/offload.js +25 -6
  24. package/src/ops/docx-table.js +5 -3
  25. package/src/ops/pptx.js +8 -2
  26. package/src/ops/xlsx.js +114 -8
  27. package/src/package.js +17 -2
  28. package/src/pptx.js +244 -8
  29. package/src/question.js +10 -3
  30. package/src/quick.js +1 -1
  31. package/src/redline-check.js +12 -2
  32. package/src/rewriter.js +38 -8
  33. package/src/server-entry.js +4 -3
  34. package/src/server.js +97 -28
  35. package/src/session.js +13 -2
  36. package/src/setup.js +20 -2
  37. package/src/update.js +248 -19
  38. package/src/voice-guide.js +12 -7
  39. package/src/voice-learn.js +11 -1
  40. package/src/voice-store.js +11 -4
  41. package/src/voice.js +2 -1
  42. package/src/xlsx.js +25 -5
  43. package/src/zip-write.js +8 -4
  44. package/src/zip.js +64 -13
  45. package/web/dist/assets/{index-DytKkMdi.js → index-CjsTf5gj.js} +108 -106
  46. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  47. package/web/dist/index.html +2 -2
  48. package/web/dist/assets/index-DkNx0LSH.css +0 -1
package/src/fsutil.js CHANGED
@@ -92,16 +92,13 @@ export function acquireLock(file, { waitMs = 5000 } = {}) {
92
92
  stale = e.code === "ESRCH";
93
93
  }
94
94
  } else stale = Date.now() - fs.statSync(lock).mtimeMs > 2000; // being created, or garbage
95
- } catch {
96
- continue; // released between our attempts
97
- }
95
+ } catch {} // it may have been released, or its pid may be unreadable
98
96
  if (stale) {
99
97
  try {
100
98
  fs.unlinkSync(lock);
101
99
  } catch {}
102
- continue;
103
100
  }
104
- if (Date.now() > deadline) throw new LockBusy(file);
101
+ if (Date.now() >= deadline) throw new LockBusy(file);
105
102
  sleepSync(20);
106
103
  }
107
104
  }
@@ -209,6 +206,8 @@ export function replaceGuarded(file, bytes, { expectSha = null, snapshotBefore =
209
206
  const dfd = fs.openSync(path.dirname(file), "r");
210
207
  try {
211
208
  fs.fsyncSync(dfd);
209
+ } catch (err) {
210
+ if (err?.code !== "EINVAL" && err?.code !== "ENOTSUP" && err?.code !== "EPERM" && err?.code !== "EROFS") throw err;
212
211
  } finally {
213
212
  fs.closeSync(dfd);
214
213
  }
@@ -258,6 +257,15 @@ export function writeSiblingCopy(file, bytes, { now = new Date(), label = "Onion
258
257
  fs.linkSync(tmp, dest);
259
258
  return { name, path: dest };
260
259
  } catch (err) {
260
+ if (err.code === "ENOTSUP" || err.code === "EXDEV" || err.code === "EPERM") {
261
+ try {
262
+ fs.copyFileSync(tmp, dest, fs.constants.COPYFILE_EXCL);
263
+ return { name, path: dest };
264
+ } catch (copyErr) {
265
+ if (copyErr.code !== "EEXIST") throw copyErr;
266
+ continue;
267
+ }
268
+ }
261
269
  if (err.code !== "EEXIST") throw err;
262
270
  }
263
271
  }
package/src/history.js CHANGED
@@ -478,12 +478,21 @@ function compareHtml(RA, RB, out) {
478
478
  let j = 0;
479
479
  const flushGap = (iEnd, jEnd) => {
480
480
  const gapA = [];
481
- for (let x = i; x < iEnd; x++) if (mapA[x] < 0) gapA.push(A[x]);
481
+ const byTag = new Map();
482
+ for (let x = i; x < iEnd; x++) {
483
+ if (mapA[x] >= 0) continue;
484
+ const ua = A[x];
485
+ gapA.push(ua);
486
+ let entry = byTag.get(ua.tag);
487
+ if (!entry) byTag.set(ua.tag, (entry = { list: [], idx: 0 }));
488
+ entry.list.push(ua);
489
+ }
482
490
  const used = new Set();
483
491
  for (let y = j; y < jEnd; y++) {
484
492
  if (mapB[y] >= 0) continue;
485
493
  const ub = B[y];
486
- const ua = gapA.find((u) => !used.has(u) && u.tag === ub.tag);
494
+ const entry = byTag.get(ub.tag);
495
+ const ua = entry && entry.idx < entry.list.length ? entry.list[entry.idx++] : null;
487
496
  if (ua) {
488
497
  used.add(ua);
489
498
  out.add("changed", () => place({ format: "html", label: htmlLabel(ub), locator: htmlLocator(RB, ub), ...textChange(htmlUnitText(ua), htmlUnitText(ub)) }));
@@ -378,8 +378,7 @@ function dataUrl(v) {
378
378
  const UTF8 = new TextDecoder("utf-8", { fatal: true });
379
379
 
380
380
  // ---- CSS (R1's rule, R16, R21) -------------------------------------------------------------------------------------------------------
381
- const IMPORT = /@import\s+(?:url\(\s*(?:"([^"]*)"|'([^']*)'|([^)"'\s]*))\s*\)|"([^"]*)"|'([^']*)')\s*([^;{}]*)(?:;|$)/gi;
382
- const URL_RE = /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)"'\s]*))\s*\)/gi; // the url() src/html-active.js cssPlain reads
381
+ const IMPORT = /@import[ \t\n\f\r]+(?:url\([ \t\n\f\r]*(?:"([^"]*)"|'([^']*)'|([^)"' \t\n\f\r]+)?)[ \t\n\f\r]*\)|"([^"]*)"|'([^']*)')[ \t\n\f\r]*([^;{}]*)(?:;|$)/gi;
383
382
  const MEDIA_LIST = /^[a-z0-9 ,()\-:._]*$/i;
384
383
 
385
384
  // ---- the render ---------------------------------------------------------------------------------------------------------------------
@@ -506,6 +505,20 @@ export function renderCopy(read, { fileDir, readAsset = makeAssetReader(), limit
506
505
  if (got.ok) sheetsSeen.add(got.key);
507
506
  return got;
508
507
  };
508
+ const readDataSheet = (ref) => {
509
+ const d = dataUrl(ref);
510
+ if (!d || d.mime !== "text/css") return refuse("not_text");
511
+ const room = Math.min(limits.sheet, limits.cssBytes - cssRead);
512
+ if (room <= 0) return (stats.styles.heavy ??= "css_bytes"), refuse("css_cap");
513
+ if (d.bytes.length > room) {
514
+ if (room < limits.sheet) return (stats.styles.heavy ??= "css_bytes"), refuse("css_cap");
515
+ return refuse("too_large");
516
+ }
517
+ cssRead += d.bytes.length;
518
+ const text = sheetText(d.bytes);
519
+ if (text === null) return refuse("not_text");
520
+ return text;
521
+ };
509
522
  const sheetNodes = [];
510
523
  {
511
524
  const stack = [read.doc];
@@ -536,9 +549,7 @@ export function renderCopy(read, { fileDir, readAsset = makeAssetReader(), limit
536
549
  else {
537
550
  const href = attr(n, "href") ?? "";
538
551
  if (/^[ \t\n\f\r]*data:/i.test(href)) {
539
- const d = dataUrl(href);
540
- if (d && d.mime === "text/css") css = sheetText(d.bytes);
541
- else refuse("not_text");
552
+ css = readDataSheet(href);
542
553
  base = null;
543
554
  } else if (kindOf(href) === "relative") {
544
555
  const got = readSheet(href, []);
@@ -587,17 +598,16 @@ export function renderCopy(read, { fileDir, readAsset = makeAssetReader(), limit
587
598
  return out;
588
599
  }
589
600
  function importTarget(m, b, depth) {
601
+ if (depth >= limits.importDepth) return (bump(stats.styles.left, "import_depth"), null);
590
602
  const target = m[1] ?? m[2] ?? m[3] ?? m[4] ?? m[5] ?? "";
591
603
  const media = String(m[6] || "").trim();
592
604
  if (media && (!MEDIA_LIST.test(media) || /layer|supports|url/i.test(media))) return (bump(stats.styles.left, "import_media"), null);
593
605
  if (/^[ \t\n\f\r]*data:/i.test(target)) {
594
- const d = dataUrl(target);
595
- const text = d && d.mime === "text/css" ? sheetText(d.bytes) : null;
596
- if (text === null) return refuse("not_text");
606
+ const text = readDataSheet(target);
607
+ if (text === null) return null;
597
608
  return { text, base: null, media };
598
609
  }
599
610
  if (kindOf(target) !== "relative" || b === null) return refuse("remote");
600
- if (depth >= limits.importDepth) return (bump(stats.styles.left, "import_depth"), null);
601
611
  const got = readSheet(target, b);
602
612
  if (!got.ok) return refuse(got.code);
603
613
  const text = sheetText(got.bytes);
@@ -612,12 +622,61 @@ export function renderCopy(read, { fileDir, readAsset = makeAssetReader(), limit
612
622
  const stylesOn = !stats.styles.heavy;
613
623
 
614
624
  /** CSS text with every url() made a checked data: image or `none`; null when the result is not plain (R1's rule) or not writable. */
615
- const resolveUrls = (css, base) =>
616
- css.replace(URL_RE, (m, a, b, c) => {
617
- const ref = a ?? b ?? c ?? "";
618
- const uri = image(ref, base);
619
- return uri ? `url("${uri}")` : "none";
620
- });
625
+ const resolveUrls = (css, base) => {
626
+ const s = css;
627
+ if (s.length < 4 || !/url\(/i.test(s)) return s;
628
+ const space = (c) => {
629
+ const k = c.charCodeAt(0);
630
+ return k < 128 ? k === 32 || (k >= 9 && k <= 13) : /\s/.test(c);
631
+ };
632
+ const isUrl = (i) => (s.charCodeAt(i) | 32) === 117 && (s.charCodeAt(i + 1) | 32) === 114 && (s.charCodeAt(i + 2) | 32) === 108;
633
+ let stop = null;
634
+ const stops = () => {
635
+ if (stop) return stop;
636
+ const n = s.length;
637
+ stop = { any: new Int32Array(n + 1), dq: new Int32Array(n + 1), sq: new Int32Array(n + 1), ns: new Int32Array(n + 1) };
638
+ stop.any[n] = stop.dq[n] = stop.sq[n] = stop.ns[n] = n;
639
+ for (let i = n - 1; i >= 0; i--) {
640
+ const c = s[i];
641
+ const ws = space(c);
642
+ stop.any[i] = c === ")" || c === '"' || c === "'" || ws ? i : stop.any[i + 1];
643
+ stop.dq[i] = c === '"' ? i : stop.dq[i + 1];
644
+ stop.sq[i] = c === "'" ? i : stop.sq[i + 1];
645
+ stop.ns[i] = ws ? stop.ns[i + 1] : i;
646
+ }
647
+ return stop;
648
+ };
649
+ const out = [];
650
+ let from = 0;
651
+ for (let i = 0; i + 3 < s.length; ) {
652
+ if (!(isUrl(i) && s[i + 3] === "(")) {
653
+ i++;
654
+ continue;
655
+ }
656
+ const st = stops();
657
+ const j = st.ns[i + 4];
658
+ let val = null;
659
+ let k = j;
660
+ if (j < s.length && (s[j] === '"' || s[j] === "'")) {
661
+ const close = j + 1 <= s.length ? (s[j] === '"' ? st.dq : st.sq)[j + 1] : s.length;
662
+ if (close < s.length) [val, k] = [s.slice(j + 1, close), close + 1];
663
+ } else {
664
+ k = st.any[j];
665
+ val = s.slice(j, k);
666
+ }
667
+ const m = val !== null ? st.ns[k] : s.length;
668
+ if (val === null || m >= s.length || s[m] !== ")") {
669
+ i++;
670
+ continue;
671
+ }
672
+ const uri = image(val, base);
673
+ out.push(s.slice(from, i), uri ? `url("${uri}")` : "none");
674
+ from = i = m + 1;
675
+ }
676
+ if (!from) return s;
677
+ out.push(s.slice(from));
678
+ return out.join("");
679
+ };
621
680
  const finalCss = (parts, inElement) => {
622
681
  const text = parts.map((p) => resolveUrls(p.text, p.base)).join("");
623
682
  const c = cssPlain(text);
package/src/html-utf8.js CHANGED
@@ -28,7 +28,7 @@ export function utf8Bytes(bytes) {
28
28
  if (!d.copyable) return { ok: false, reason: NOT_READABLE_ENCODING };
29
29
  let text = new TextDecoder("windows-1252").decode(bytes);
30
30
  // the declaration sits in the first 1,024 bytes (src/html.js declaredCharset); those are one byte a character in windows-1252
31
- const head = text.slice(0, 1024).replace(/(<meta\b[^>]*?\bcharset\s*=\s*["']?\s*)([A-Za-z0-9_.:-]+)/gi, "$1utf-8");
31
+ const head = text.slice(0, 1024).replace(/<!--[\s\S]*?(?:-->|$)|(<meta\b[^>]*?\bcharset\s*=\s*["']?\s*)([A-Za-z0-9_.:-]+)/gi, (m, pre) => (pre ? `${pre}utf-8` : m));
32
32
  text = head + text.slice(1024);
33
33
  const out = Buffer.from(text, "utf8");
34
34
  const check = decodeHtml(out);
package/src/html.js CHANGED
@@ -55,7 +55,9 @@ const WINDOWS_1252 = new Set(["ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csi
55
55
 
56
56
  /** The charset a page declares in its first 1,024 bytes (a meta charset, or a meta http-equiv content-type), lowercased; null when none. */
57
57
  function declaredCharset(bytes) {
58
- const head = Buffer.from(bytes.buffer, bytes.byteOffset, Math.min(bytes.length, 1024)).toString("latin1");
58
+ const head = Buffer.from(bytes.buffer, bytes.byteOffset, Math.min(bytes.length, 1024))
59
+ .toString("latin1")
60
+ .replace(/<!--[\s\S]*?(?:-->|$)/g, "");
59
61
  for (const m of head.matchAll(/<meta\b[^>]*>/gi)) {
60
62
  const cs = /\bcharset\s*=\s*["']?\s*([A-Za-z0-9_.:-]+)/i.exec(m[0]);
61
63
  if (cs) return cs[1].toLowerCase();
@@ -468,7 +468,9 @@ export function docxUnits(xml, intern) {
468
468
  let inBody = false;
469
469
  let cur = null; // the listed paragraph open: {text, look, fmt, obj, box}
470
470
  let pDepth = 0;
471
- let delDepth = 0;
471
+ const revStack = [];
472
+ let mcFallbackDepth = 0;
473
+ const visible = () => !revStack.includes("del") && (revStack.length === 0 || revStack[revStack.length - 1] !== "moveFrom");
472
474
  let inT = false;
473
475
  const runs = []; // open w:r: {rpr: [] | null, id}
474
476
  let rprDepth = 0; // > 0 while inside a run's own w:rPr (collected into the run's look)
@@ -486,13 +488,41 @@ export function docxUnits(xml, intern) {
486
488
  for (let k = 0; k < s.length; k++) chars.push(lookNow());
487
489
  };
488
490
  const token = (into, s) => into.push(`${cur.text.length}|${s}`);
491
+ const closeRpr = () => {
492
+ runs[runs.length - 1].rpr.push("</>");
493
+ if (--rprDepth === 0) {
494
+ const r = runs[runs.length - 1];
495
+ r.id = intern.id(r.rpr.join(""));
496
+ }
497
+ };
498
+ const closePpr = () => {
499
+ pprDepth--;
500
+ if (skipDepth) {
501
+ mod[1].push("</>");
502
+ if (--skipDepth === 0) cur.mod[mod[0]] = mod[1].join("");
503
+ } else token(cur.fmt, "</>");
504
+ };
489
505
  walkCanonical(xml, {
490
506
  open(name, a, tag) {
491
507
  stack.push(name);
508
+ if (name === "mc:Fallback" || mcFallbackDepth > 0) {
509
+ if (name === "mc:Fallback") mcFallbackDepth++;
510
+ if (!cur) skel.push(tag);
511
+ else if (rprDepth) (rprDepth++, runs[runs.length - 1].rpr.push(tag));
512
+ else if (pprDepth) {
513
+ pprDepth++;
514
+ if (skipDepth) skipDepth++, mod[1].push(tag);
515
+ else token(cur.fmt, tag);
516
+ quiet.push(false);
517
+ } else (quiet.push(false), token(cur.obj, `${runs.length ? `r${lookNow()}` : ""}${tag}`));
518
+ return;
519
+ }
492
520
  if (name === "w:body") inBody = true;
493
- if (name === "w:tbl") cells.push({ t: tables++, row: -1, col: -1 });
494
- else if (name === "w:tr" && cells.length) (cells[cells.length - 1].row++, (cells[cells.length - 1].col = -1));
495
- else if (name === "w:tc" && cells.length) cells[cells.length - 1].col++;
521
+ if (!cur) {
522
+ if (name === "w:tbl") cells.push({ t: tables++, row: -1, col: -1 });
523
+ else if (name === "w:tr" && cells.length) (cells[cells.length - 1].row++, (cells[cells.length - 1].col = -1));
524
+ else if (name === "w:tc" && cells.length) cells[cells.length - 1].col++;
525
+ }
496
526
  if (name === "w:p") pDepth++;
497
527
  if (name === "w:p" && inBody && pDepth === 1) {
498
528
  // `own`: the paragraph's own attributes (revision stamps, its text id) but its id, which is how the proof pairs paragraphs
@@ -502,6 +532,7 @@ export function docxUnits(xml, intern) {
502
532
  return;
503
533
  }
504
534
  if (!cur) return void skel.push(tag);
535
+ if (pDepth > 1) return void (quiet.push(false), token(cur.obj, `${runs.length ? `r${lookNow()}` : ""}${tag}`));
505
536
  // inside a listed paragraph
506
537
  if (rprDepth) {
507
538
  rprDepth++;
@@ -513,19 +544,21 @@ export function docxUnits(xml, intern) {
513
544
  runs[runs.length - 1].rpr = [tag];
514
545
  return;
515
546
  }
516
- if (name === "w:del") delDepth++;
547
+ if (name === "w:del") revStack.push("del");
548
+ else if (name === "w:moveFrom") revStack.push("moveFrom");
549
+ else if (name === "w:ins") revStack.push("ins");
517
550
  if (name === "w:r") {
518
551
  runs.push({ rpr: null, id: 0 });
519
552
  return void quiet.push(true);
520
553
  }
521
554
  // text, by the reader's rules (src/docx.js parseDocxParagraphs), wherever it stands in the paragraph
522
555
  let counted = false;
523
- if (name === "w:t" && delDepth === 0) (inT = true), (counted = true), (tTrim = !/ \u0001\{[^}]*\}space="preserve"/.test(tag));
524
- else if (name === "w:tab" && delDepth === 0) add("\t"), (counted = true);
525
- else if ((name === "w:br" || name === "w:cr") && delDepth === 0 && a["w:type"] !== "page" && a["w:type"] !== "column") add("\n"), (counted = true);
556
+ if (name === "w:t" && visible()) (inT = true), (counted = true), (tTrim = !/ \u0001\{[^}]*\}space="preserve"/.test(tag));
557
+ else if (name === "w:tab" && visible()) add("\t"), (counted = true);
558
+ else if ((name === "w:br" || name === "w:cr") && visible() && a["w:type"] !== "page" && a["w:type"] !== "column") add("\n"), (counted = true);
526
559
  // a counted tab or break with attributes of its own (a w:br w:clear="all"): its tag, where it stands
527
560
  if (counted && name !== "w:t" && tag.includes(" ") && !pprDepth) token(cur.obj, `${runs.length ? `r${lookNow()}` : ""}${tag}</>`);
528
- else if ((name === "w:drawing" || name === "w:pict") && delDepth === 0) add(OBJ); // one character, and an object too
561
+ else if ((name === "w:drawing" || name === "w:pict") && visible()) add(OBJ); // one character, and an object too
529
562
  if (pprDepth) {
530
563
  // the paragraph's own properties (a tab stop inside them is counted as text by the reader as well, and kept here as written)
531
564
  pprDepth++;
@@ -546,45 +579,46 @@ export function docxUnits(xml, intern) {
546
579
  text(t) {
547
580
  if (!cur) return void skel.push(esc(t));
548
581
  if (rprDepth) return void runs[runs.length - 1].rpr.push(esc(t));
582
+ if (pprDepth) return void (skipDepth ? mod[1].push(esc(t)) : token(cur.fmt, esc(t)));
583
+ if (mcFallbackDepth > 0 || pDepth > 1) return void token(cur.obj, esc(t));
549
584
  if (inT) {
550
585
  if (tTrim && /^[ \t\r\n]|[ \t\r\n]$/.test(t)) token(cur.obj, `${VOL}unkept spaces`);
551
586
  return void add(t);
552
587
  }
553
- if (pprDepth) return void (skipDepth ? mod[1].push(esc(t)) : token(cur.fmt, esc(t)));
554
588
  token(cur.obj, esc(t));
555
589
  },
556
590
  close(name) {
557
591
  stack.pop();
558
- if (name === "w:tbl") cells.pop();
592
+ if (mcFallbackDepth > 0) {
593
+ if (name === "mc:Fallback") mcFallbackDepth--;
594
+ if (!cur) return void skel.push("</>");
595
+ if (rprDepth) return void closeRpr();
596
+ const silent = quiet.pop();
597
+ if (pprDepth) return void closePpr();
598
+ if (!silent) token(cur.obj, "</>");
599
+ return;
600
+ }
601
+ if (!cur && name === "w:tbl") cells.pop();
559
602
  if (name === "w:body") inBody = false;
560
603
  if (name === "w:p") pDepth--;
561
604
  if (cur && name === "w:p" && pDepth === 0) {
562
605
  cur.look = Int32Array.from(chars);
563
606
  paras.push(cur);
564
607
  cur = null;
608
+ revStack.length = 0;
565
609
  return;
566
610
  }
567
611
  if (!cur) return void skel.push("</>");
568
- if (rprDepth) {
569
- runs[runs.length - 1].rpr.push("</>");
570
- if (--rprDepth === 0) {
571
- const r = runs[runs.length - 1];
572
- r.id = intern.id(r.rpr.join(""));
573
- }
612
+ if ((pDepth >= 1 && name === "w:p") || pDepth > 1) {
613
+ if (!quiet.pop()) token(cur.obj, "</>");
574
614
  return;
575
615
  }
576
- if (name === "w:del") delDepth = Math.max(0, delDepth - 1);
616
+ if (rprDepth) return void closeRpr();
617
+ if (name === "w:del" || name === "w:moveFrom" || name === "w:ins") revStack.pop();
577
618
  if (name === "w:r" && runs.length) runs.pop();
578
619
  if (name === "w:t") inT = false;
579
620
  const silent = quiet.pop();
580
- if (pprDepth) {
581
- pprDepth--;
582
- if (skipDepth) {
583
- mod[1].push("</>");
584
- if (--skipDepth === 0) cur.mod[mod[0]] = mod[1].join("");
585
- } else token(cur.fmt, "</>");
586
- return;
587
- }
621
+ if (pprDepth) return void closePpr();
588
622
  if (!silent) token(cur.obj, "</>");
589
623
  },
590
624
  });
@@ -614,9 +648,18 @@ export function slideUnits(xml, intern) {
614
648
  let run = null; // the a:r open: {rpr: [] | null, id, depth}
615
649
  let rprDepth = 0;
616
650
  let body = 0; // depth of the open p:txBody (0: none)
651
+ let mcFallbackDepth = 0;
617
652
  walkCanonical(xml, {
618
653
  open(name, a, tag) {
619
654
  depth++;
655
+ if (name === "mc:Fallback" || mcFallbackDepth > 0) {
656
+ if (name === "mc:Fallback") mcFallbackDepth++;
657
+ const sh = open[open.length - 1];
658
+ if (!sh) skel.push(tag);
659
+ else if (rprDepth) (rprDepth++, run.rpr.push(tag));
660
+ else (body ? sh.fmt : sh.obj).push(`${sh.text.length}|${run ? `r${run.id}` : ""}${tag}`);
661
+ return;
662
+ }
620
663
  const l = localOf(name);
621
664
  if (SHAPE_TAGS.has(l)) {
622
665
  const parent = open[open.length - 1];
@@ -649,6 +692,7 @@ export function slideUnits(xml, intern) {
649
692
  const sh = open[open.length - 1];
650
693
  if (!sh) return void skel.push(esc(t));
651
694
  if (rprDepth) return void run.rpr.push(esc(t));
695
+ if (mcFallbackDepth > 0) return void (body ? sh.fmt : sh.obj).push(`${sh.text.length}|${esc(t)}`);
652
696
  if (inT && para) {
653
697
  sh.text += t;
654
698
  for (let k = 0; k < t.length; k++) sh.chars.push(run ? run.id : 0);
@@ -659,6 +703,18 @@ export function slideUnits(xml, intern) {
659
703
  close(name) {
660
704
  const l = localOf(name);
661
705
  depth--;
706
+ if (mcFallbackDepth > 0) {
707
+ if (name === "mc:Fallback") mcFallbackDepth--;
708
+ const sh = open[open.length - 1];
709
+ if (!sh) return void skel.push("</>");
710
+ if (rprDepth) {
711
+ run.rpr.push("</>");
712
+ if (--rprDepth === 0) run.id = intern.id(run.rpr.join(""));
713
+ return;
714
+ }
715
+ (body ? sh.fmt : sh.obj).push(`${sh.text.length}|</>`);
716
+ return;
717
+ }
662
718
  const sh = open[open.length - 1];
663
719
  if (!sh) return void skel.push("</>");
664
720
  if (SHAPE_TAGS.has(l) && depth + 1 === sh.depth) {
package/src/offload.js CHANGED
@@ -13,21 +13,36 @@ export function startWorker(name, { workerData, ...opts } = {}) {
13
13
  const flag = new Int32Array(loaded);
14
14
  const worker = new Worker(new URL("./worker-host.js", import.meta.url), { ...opts, workerData: { data: workerData, loaded }, argv: [name] });
15
15
  let exited = false;
16
- worker.once("exit", () => (exited = true));
16
+ let onExit = null;
17
+ worker.once("exit", () => {
18
+ exited = true;
19
+ onExit?.();
20
+ });
17
21
  const stop = worker.terminate.bind(worker);
18
22
  let stopping = null;
19
23
  worker.terminate = () =>
20
24
  (stopping ??= new Promise((resolve) => {
21
25
  let marked = false;
26
+ let timer = null;
27
+ const done = () => {
28
+ clearTimeout(timer);
29
+ onExit = null;
30
+ resolve();
31
+ };
32
+ onExit = done;
33
+ const deadline = Date.now() + 10_000;
22
34
  const tryStop = () => {
23
- if (exited) return resolve();
35
+ if (exited) return done();
24
36
  if (Atomics.load(flag, 0) === 1) {
25
37
  marked = true;
26
38
  // from here on the worker enters no zlib call; it may still be inside one
27
- if ((Atomics.or(flag, 1, STOPPING) & ~STOPPING) === 0) return stop().then(resolve, resolve);
39
+ if ((Atomics.or(flag, 1, STOPPING) & ~STOPPING) === 0) return stop().then(done, done);
28
40
  }
29
- setTimeout(tryStop, marked ? 2 : 10);
41
+ if (Date.now() >= deadline) return stop().then(done, done);
42
+ timer = setTimeout(tryStop, marked ? 2 : 10);
43
+ timer.unref?.();
30
44
  };
45
+ worker.ref?.();
31
46
  tryStop();
32
47
  }));
33
48
  return worker;
@@ -331,8 +346,6 @@ function anchorWorker() {
331
346
  function runAnchor(w, job) {
332
347
  clearTimeout(w.idle);
333
348
  w.job = job;
334
- const msg = { id: job.id, job: job.data };
335
- w.worker.postMessage(msg);
336
349
  job.onDone = () => {
337
350
  w.job = null;
338
351
  if (w.dead) return;
@@ -342,6 +355,12 @@ function runAnchor(w, job) {
342
355
  }
343
356
  nextAnchor();
344
357
  };
358
+ const msg = { id: job.id, job: job.data };
359
+ try {
360
+ w.worker.postMessage(msg);
361
+ } catch (err) {
362
+ w.fail("crashed", String(err?.message || err).slice(0, 200));
363
+ }
345
364
  }
346
365
 
347
366
  function nextAnchor() {
@@ -74,7 +74,7 @@ export function rowGrid(tr) {
74
74
  return { cells, before, after };
75
75
  }
76
76
 
77
- const XML_BAD = /[\u0000-\u0008\u000B\u000C\u000E-\u001F￾￿]/; // not allowed in XML 1.0
77
+ const XML_BAD = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/; // not allowed in XML 1.0
78
78
  /** One run's content: TAB and LF become w:tab and w:br, like the editor writes them. */
79
79
  function runContent(text) {
80
80
  return text
@@ -245,8 +245,10 @@ export class TableEdit {
245
245
  const total = widths.reduce((a, b) => a + b, 0);
246
246
  const all = [...widths.slice(0, pos), widths[cur], ...widths.slice(pos)];
247
247
  const sum = total + widths[cur];
248
- next = all.map((w) => Math.max(1, Math.round((w * total) / sum)));
249
- kidsNamed(grid, "w:gridCol").forEach((g, i) => setAttr(g, "w:w", String(next[i])));
248
+ if (total > 0 && sum > 0) {
249
+ next = all.map((w) => Math.max(1, Math.round((w * total) / sum)));
250
+ kidsNamed(grid, "w:gridCol").forEach((g, i) => setAttr(g, "w:w", String(next[i])));
251
+ }
250
252
  }
251
253
  for (const [i, tr] of rows.entries()) {
252
254
  const tcs = kidsNamed(tr, "w:tc");
package/src/ops/pptx.js CHANGED
@@ -297,6 +297,7 @@ export function replaceInRuns(runs, s, e, replacement) {
297
297
 
298
298
  export async function exec(st, b) {
299
299
  const { op } = b;
300
+ let mutated = 0;
300
301
  try {
301
302
  if (op.op === "set_shape_text") {
302
303
  const f = await findShape(st, b.target);
@@ -347,6 +348,7 @@ export async function exec(st, b) {
347
348
  }
348
349
  return models.get(slide).get(sid);
349
350
  };
351
+ const writes = [];
350
352
  for (const [key, hs] of bySlide) {
351
353
  const [slideId, sid] = key.split(":");
352
354
  const slide = indexOf(st, slideId); // by id: an earlier slide op of this file may have moved or removed it
@@ -368,14 +370,18 @@ export async function exec(st, b) {
368
370
  n += found.length;
369
371
  return replaceAllInRuns(runs, found.map((r) => r.start), found.map((r) => r.end - r.start), op.replace);
370
372
  });
371
- await st.h.engine.call("pptx.setText", [slide, Number(sid), specs]);
373
+ writes.push({ slide, sid: Number(sid), specs });
372
374
  }
373
375
  if (n + skippedMatches !== b.total) return { ...refused("incomplete_replace", `replaced ${n} and listed ${skippedMatches} of ${b.total} match(es)`), dirty: true };
376
+ for (const w of writes) {
377
+ await st.h.engine.call("pptx.setText", [w.slide, w.sid, w.specs]);
378
+ mutated++;
379
+ }
374
380
  if (!n && skipped.length) return { status: "skipped_protected", reason: "every shape with a match is protected", detail: { skipped_protected: skipped } };
375
381
  return { status: "applied", detail: { replaced: n, ...(skipped.length ? { skipped_protected: skipped } : {}) } };
376
382
  }
377
383
  } catch (err) {
378
- if (err instanceof RangeError) return refused("match_in_field", err.message);
384
+ if (err instanceof RangeError) return { ...refused("match_in_field", err.message), ...(mutated > 0 ? { dirty: true } : {}) };
379
385
  return engineFailure(err);
380
386
  }
381
387
  return refused("unsupported_op", op.op);