@reviseio/engine 0.0.0-stage → 3.0.1

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 (78) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/FONT-LICENSE-HAN-EXT-B.txt +91 -0
  3. package/FONT-LICENSE-KATEX.txt +95 -0
  4. package/FONT-LICENSE-LIBERATION.txt +102 -0
  5. package/FONT-LICENSE-NOTO-MULTILINGUAL.txt +93 -0
  6. package/FONT-LICENSE-NOTO-SANS-SC.txt +93 -0
  7. package/FONT-LICENSE-NOTO.txt +94 -0
  8. package/FONT-LICENSE-SOURCE-CODE-PRO.txt +94 -0
  9. package/FONT-LICENSE-SOURCE-SERIF-4.txt +93 -0
  10. package/LICENSE +141 -0
  11. package/README.md +11 -2
  12. package/THIRD-PARTY-NOTICES.md +3376 -0
  13. package/assets/historyMaterializeWorker-Kl0syEbc.js +618 -0
  14. package/chunks/AgentHtmlConverter-CnnB8-WI.js +379 -0
  15. package/chunks/AgentPageRenderViews-BTLjPZJQ.js +2 -0
  16. package/chunks/AgentPageRenderer-DLgUGrGy.js +180 -0
  17. package/chunks/BidiText-Bkp4Y90s.js +160 -0
  18. package/chunks/BlockDiff-B6-aJxHo.js +42 -0
  19. package/chunks/CanvasRenderer-C1Vegx9Y.js +110 -0
  20. package/chunks/ColorParserFull-BKH72fK8.js +446 -0
  21. package/chunks/DocumentCompare-C8295s35.js +6 -0
  22. package/chunks/DocumentLayout-CsU5MR-3.js +12 -0
  23. package/chunks/DocumentLayoutEngine-C62pyGVf.js +218 -0
  24. package/chunks/DocumentMutator-Bat_oLBQ.js +13 -0
  25. package/chunks/DocumentScrubber-BZQn_z92.js +21 -0
  26. package/chunks/DocumentSession-BDcazSEv.js +1 -0
  27. package/chunks/DocumentStylesheet-CEEt18sr.js +7 -0
  28. package/chunks/DocxConverter-BC31uYIL.js +1236 -0
  29. package/chunks/DocxGraft-L10HXM3U.js +8 -0
  30. package/chunks/HorizontalRuleOrnaments-gmUZ3R7Q.js +3 -0
  31. package/chunks/HtmlConverter-C5ihhZ6G.js +267 -0
  32. package/chunks/ListNumbering-Qlu6AuRZ.js +23 -0
  33. package/chunks/MarkdownConverter-DofaSeC4.js +57 -0
  34. package/chunks/MathRenderer-Bz9WEVvF.js +68 -0
  35. package/chunks/OdtConverter-c1fTWoAt.js +125 -0
  36. package/chunks/OfficeImportShared-CrOpHpJ-.js +30 -0
  37. package/chunks/PageNumeralFonts-BWzI061X.js +5 -0
  38. package/chunks/ParagraphDiffAnalysis-DrmStvmz.js +36 -0
  39. package/chunks/PdfConverter-DX7fsRH5.js +457 -0
  40. package/chunks/RtfConverter-hbJ2529G.js +186 -0
  41. package/chunks/TableBorders-DVhdvo6f.js +32 -0
  42. package/chunks/TableGrid-DbroRucl.js +12 -0
  43. package/chunks/TextDecoding-IPbuUqcC.js +2 -0
  44. package/chunks/TextFileDecoder-BZBBejlR.js +10 -0
  45. package/chunks/TxtConverter-YHv5FXWb.js +14 -0
  46. package/chunks/Typography-B4gndEdB.js +28 -0
  47. package/chunks/UnicodeUtils-CXtusU4g.js +10 -0
  48. package/chunks/XmlConverter-BRRluy9k.js +55 -0
  49. package/chunks/_commonjsHelpers-DaMA6jEr.js +2 -0
  50. package/chunks/_revise-sdk-lazy-harfbuzz-CDWdjF9f.js +1 -0
  51. package/chunks/base64-Di1bxoXD.js +1 -0
  52. package/chunks/browser-module-CFmqfGkE.js +1791 -0
  53. package/chunks/bundle.min-DRpfgV69.js +782 -0
  54. package/chunks/capture-i_dPkQIS.js +11 -0
  55. package/chunks/carrier-pYM9xf6x.js +6 -0
  56. package/chunks/chromeImageLayout-BiQ3Iz1-.js +18 -0
  57. package/chunks/engine-LMUnxdm2.js +520 -0
  58. package/chunks/footnoteFlatten-EE85Hjre.js +6 -0
  59. package/chunks/graftExport-G-dG7Vcc.js +104 -0
  60. package/chunks/html2canvas.esm-CntiHiYW.js +997 -0
  61. package/chunks/index-BLb10wQt.js +1002 -0
  62. package/chunks/index-BWdRWxoH.js +1807 -0
  63. package/chunks/index.es-CI0QPo8w.js +780 -0
  64. package/chunks/jspdf.es.min-DU74zN8b.js +1792 -0
  65. package/chunks/jszip.min-CPW0h1Kx.js +476 -0
  66. package/chunks/katex-DWj8TvwO.js +1462 -0
  67. package/chunks/pdf-BLWDCiFF.js +1828 -0
  68. package/chunks/purify.es-BwLardii.js +107 -0
  69. package/chunks/serverDocumentSession-DGDU5Lh3.js +1011 -0
  70. package/chunks/signatures-DTNDS2PH.js +21 -0
  71. package/chunks/suggestionAuthorship-B-IuLQlE.js +1 -0
  72. package/chunks/web-tree-sitter-BXGZGnNj.js +2 -0
  73. package/chunks/xmlScan-DXjt2cTS.js +8 -0
  74. package/engine.d.ts +179 -0
  75. package/engine.js +3 -0
  76. package/package.json +64 -4
  77. package/serverToolContract.d.ts +420 -0
  78. package/serverToolInputs.generated.d.ts +264 -0
@@ -0,0 +1,6 @@
1
+ import{a as h}from"./engine-LMUnxdm2.js";function y(e){e=e.children.some(t=>t.type==="header-footer")?{...e,children:e.children.filter(t=>t.type!=="header-footer")}:e;const f=[...e.notes??[],...e.children.
2
+ filter(t=>t.type==="footnote")];if(f.length===0)return e;const r=new Map,c=t=>{if(!t||typeof t!="object")return;if(Array.isArray(t)){t.forEach(c);return}const o=t;o.type!=="footnote"&&(o.marks?.footnoteRef&&
3
+ !r.has(o.marks.footnoteRef)&&r.set(o.marks.footnoteRef,o.text??""),c(o.children))};c(e.children.filter(t=>t.type!=="footnote"));const i=[];let s=r.size;const d=[...f.filter(t=>t.noteKind!=="endnote"),
4
+ ...f.filter(t=>t.noteKind==="endnote")];for(const t of d){const o=r.get(t.id)||String(s+=1);t.children.forEach((n,p)=>{if(n.type!=="paragraph"){i.push(n);return}if(p===0){const a={...n,id:h(),children:[
5
+ {type:"text",text:`${o}. `},...n.children]};i.push(a)}else i.push({...n,id:h()})})}const l={type:"paragraph",id:h(),children:[{type:"text",text:"―――――"}]};return{...e,children:[...e.children.
6
+ filter(t=>t.type!=="footnote"),l,...i]}}export{y as f};
@@ -0,0 +1,104 @@
1
+ import{J as He}from"./jszip.min-CPW0h1Kx.js";import{findOpenTag as Ue,endOfTag as Je,scanElementChildren as _}from"./xmlScan-DXjt2cTS.js";import{C as Y,d as ht,e as wt,c as pt,p as xt,m as yt,h as bt,
2
+ g as St,f as vt,n as $t,b as It,l as ve,k as qe}from"./signatures-DTNDS2PH.js";import{h as At}from"./capture-i_dPkQIS.js";function Xe(t){return!!(t==="word/document.xml"||t==="word/styles.xml"||t==="w\
3
+ ord/settings.xml"||t==="word/footnotes.xml"||t==="word/endnotes.xml"||t==="word/comments.xml"||t==="word/commentsExtended.xml"||t==="docProps/core.xml"||/^word\/(header|footer)\d*\.xml$/.test(t)||/^word\/_rels\/(header|footer)\d*\.xml\.rels$/.
4
+ test(t)||t==="word/_rels/footnotes.xml.rels"||t==="word/_rels/endnotes.xml.rels")}function oe(t){return t==="word/footnotes.xml"||t==="word/endnotes.xml"||t==="word/_rels/footnotes.xml.rels"||t==="wor\
5
+ d/_rels/endnotes.xml.rels"?"notes":/^word\/comments[A-Za-z]*\.xml$/.test(t)||/^word\/_rels\/comments[A-Za-z]*\.xml\.rels$/.test(t)?"comments":/^word\/(header|footer)\d*\.xml$/.test(t)||/^word\/_rels\/(header|footer)\d*\.xml\.rels$/.
6
+ test(t)?"chrome":t==="docProps/core.xml"?"coreProps":t==="word/styles.xml"?"styles":t==="word/settings.xml"?"settings":t==="word/numbering.xml"?"numbering":null}const Ye=/\b(?:r:id|r:embed|r:link|r:pict|r:dm|r:lo|r:qs|r:cs)\s*=\s*(?:"([^"]+)"|'([^']+)')/g;
7
+ function N(t,r){const o=new RegExp("\\b"+r+`\\s*=\\s*(?:"([^"]*)"|'([^']*)')`).exec(t);return o?.[1]??o?.[2]}function ie(t,r,o){return t.replace(new RegExp("(\\b"+r+`\\s*=\\s*)(["'])([\\s\\S]*?)\\2`),
8
+ (c,d,l)=>d+l+o+l)}function Ke(t){const r=new Set;for(const o of t)for(const c of o.matchAll(Ye)){const d=c[1]??c[2];d&&r.add(d)}return r}function kt(t,r){return t.replace(Ye,(o,c,d)=>{const l=c??d,m=l?
9
+ r.get(l):void 0;if(!m||!l)return o;const g=o.indexOf("="),v=o.slice(g+1).search(/["']/)+g+1;return o.slice(0,v+1)+m+o.slice(v+1+l.length)})}function $e(t,r){let o=0;for(const c of t.matchAll(r)){const d=Number(
10
+ c[1]);Number.isFinite(d)&&d>o&&(o=d)}return o}function K(t){const r=[];for(const o of t.matchAll(/<(?:[A-Za-z_][\w.-]*:)?Relationship\b[^>]*\/?>/g)){const c=o[0],d=N(c,"Id"),l=N(c,"Target");!d||l===void 0||
11
+ r.push({id:d,raw:c,target:l,external:N(c,"TargetMode")==="External"})}return r}function Et(t,r,o){const c=Ue(t,"w:document",0),d=Ue(r,"w:document",0);if(c<0||d<0)throw new Error("graft: missing w:docu\
12
+ ment root");const l=Je(t,c),m=Je(r,d),g=t.slice(c,l),v=r.slice(d,m),k=C=>{const E=new Map;for(const P of C.matchAll(/([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g))E.set(P[1],P[2]??P[3]);return E},A=k(g),
13
+ R=k(v),T=[],f=[];for(const[C,E]of R){if(!C.startsWith("xmlns:")||A.has(C))continue;const P=C.slice(6);o.has(P)&&(T.push(`${C}="${E}"`),f.push(P))}let b=g;if(T.length>0){const C=b.endsWith("/>")?b.length-
14
+ 2:b.length-1;b=`${b.slice(0,C)} ${T.join(" ")}${b.slice(C)}`}const Z=A.get("mc:Ignorable"),V=R.get("mc:Ignorable");if(Z!==void 0&&V&&f.length>0){const C=new Set(Z.split(/\s+/).filter(Boolean)),E=new Set(
15
+ V.split(/\s+/).filter(Boolean));let P=!1;for(const U of f)E.has(U)&&!C.has(U)&&(C.add(U),P=!0);P&&(b=ie(b,"mc:Ignorable",Array.from(C).join(" ")))}return t.slice(0,c)+b+t.slice(l)}function Pt(t,r,o){const c=(m,g,v)=>Array.
16
+ from(m.matchAll(/<(?:Default|Override)\b[^>]*\/?>/g)).some(k=>N(k[0],g)===v),d=new Set;for(const m of o){const g=m.lastIndexOf(".");g>=0&&d.add(m.slice(g+1).toLowerCase())}const l=[];for(const m of r.
17
+ matchAll(/<(Default|Override)\b[^>]*\/?>/g)){const g=m[0],v=N(g,"Extension"),k=N(g,"PartName");(v&&d.has(v.toLowerCase())&&!c(t,"Extension",v)||k&&o.has(k.replace(/^\//,""))&&!c(t,"PartName",k))&&l.push(
18
+ g)}return l.length===0?t:t.replace("</Types>",`${l.join("")}</Types>`)}function Ct(t,r){if(!r)return t;if(!t)return r;let o=t;for(const c of["autoHyphenation","evenAndOddHeaders","updateFields"]){const d=new RegExp(
19
+ "<w:"+c+"\\b[^>]*(?:\\/\\s*>|>[\\s\\S]*?<\\/w:"+c+"\\s*>)","g"),l=r.match(d)?.[0];o=o.replace(d,""),l&&(o=o.replace(/<\/w:settings\s*>/,l+"</w:settings>"))}return o}const Bt=/<w:style\b[^>]*?(?:\/>|>[\s\S]*?<\/w:style\s*>)/g,
20
+ Mt=/<w:(?:pStyle|rStyle|tblStyle|numStyleLink|styleLink)\b[^>]*\bw:val\s*=\s*(?:"([^"]+)"|'([^']+)')/g,jt=/<w:(?:basedOn|next|link)\b[^>]*\bw:val\s*=\s*(?:"([^"]+)"|'([^']+)')/g;function Rt(t){return t.
21
+ replace(/>\s+</g,"><").replace(/\s+/g," ").trim()}function Qe(t){const r=new Map;for(const o of t.matchAll(Bt)){const c=N(o[0].slice(0,o[0].indexOf(">")+1),"w:styleId");c&&!r.has(c)&&r.set(c,o[0])}return r}
22
+ function Ot(t){const r=new Set;for(const o of t)for(const c of o.matchAll(Mt))r.add(c[1]??c[2]);return r}function Tt(t,r,o){if(!t||!r)return;const c=A=>Rt(/<w:docDefaults\b[\s\S]*?<\/w:docDefaults\s*>/.
23
+ exec(A)?.[0]??"");if(c(t)!==c(r))return;const d=Qe(t),l=Qe(r),m=[],g=new Set,v=[...o];for(;v.length>0;){const A=v.shift();if(g.has(A))continue;g.add(A);const R=d.get(A),T=l.get(A);if(!R&&T){m.push(T);
24
+ for(const f of T.matchAll(jt))v.push(f[1]??f[2])}}if(m.length===0)return t;const k=t.lastIndexOf("</w:styles");if(!(k<0))return t.slice(0,k)+m.join("")+t.slice(k)}class B extends Error{constructor(r,o){
25
+ super(o?`graft[${r}]: ${o}`:`graft[${r}]`),this.code=r,this.name="GraftProofError"}}const D={attempts:0,grafted:0,rejections:{}};function _t(){return{attempts:D.attempts,grafted:D.grafted,rejections:{
26
+ ...D.rejections}}}function Nt(){D.attempts=0,D.grafted=0,D.rejections={}}function Wt(t,r){const o=[];let c;const d=(l,m)=>{if(l.tag==="w:sdt"){const g=m??{start:l.start,xml:t.slice(l.start,l.end)},v=_(
27
+ t,"w:sdtContent",l.start);if(v.open.start>l.end)throw new B("sdtWithoutContent");for(const k of v.children)d(k,g);return}if(l.tag==="w:sectPr"){c=t.slice(l.start,l.end);return}l.tag==="w:bookmarkStart"||
28
+ l.tag==="w:bookmarkEnd"||o.push({xml:t.slice(l.start,l.end),outerSdt:m})};for(const l of r)d(l);return{slots:o,finalSectPr:c}}async function et(t){const r=t instanceof ArrayBuffer||ArrayBuffer.isView(
29
+ t)?t:typeof t.arrayBuffer=="function"?await t.arrayBuffer():t;return He.loadAsync(r)}async function Dt(t){if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(
30
+ t.buffer,t.byteOffset,t.byteLength);if(typeof t.arrayBuffer=="function")return new Uint8Array(await t.arrayBuffer());throw new B("carrierUnreadable")}function Ie(t){const r=[];for(const o of t.split("\
31
+ /"))!o||o==="."||(o===".."?r.pop():r.push(o));return r.join("/")}function Lt(t){if(t==="_rels/.rels")return"";const r=/^(.*)\/_rels\/([^/]+)\.rels$/.exec(t);return r?`${r[1]}/${r[2]}`:void 0}function tt(t,r){
32
+ if(r.startsWith("/"))return Ie(r);const o=Lt(t);if(o===void 0)return Ie(r);const c=o.lastIndexOf("/"),d=c>=0?o.slice(0,c+1):"";return Ie(d+r)}async function zt(t){D.attempts++;try{const r=await Vt(t);
33
+ return D.grafted++,r}catch(r){const o=r instanceof B?r.code:"internalError";throw D.rejections[o]=(D.rejections[o]??0)+1,r}}function nt(t){const r=t.docxSource,o=Ft(t);if(!r||r.signatureVersion!==void 0&&
34
+ r.signatureVersion!==1&&r.signatureVersion!==Y)return{hasCapture:!!r,domains:{notes:!1,chrome:!1,comments:!1,coreProps:!1,layout:!1,bodySidecars:!1,styles:!1,settings:!1},graftableGroups:new Set,groupByBlockId:new Map,
35
+ bodyBlocks:o,regeneratedBlockIds:o.map(f=>f.id).filter(f=>!!f),fullyExact:!1};const c=r.signatureVersion===Y?Y:1,d={notes:r.notesSignature===$t(t,c),chrome:r.chromeSignature===vt(t,c),comments:r.commentsSignature===
36
+ St(t,c),coreProps:r.corePropertiesSignature===bt(t),layout:r.pageLayoutSignature===xt(t)||yt(t).includes(r.pageLayoutSignature),bodySidecars:r.bodySidecarsSignature===pt(t,c),styles:r.stylesSignature===
37
+ wt(t,c),settings:r.settingsSignature===ht(t)},l=new Map;for(const f of r.spans)if(!(!d.bodySidecars||!d.styles))for(const b of f.blockIds)l.set(b,f);const m=new Map;o.forEach((f,b)=>{f.id&&m.set(f.id,
38
+ b)});const g=new Set;for(const f of r.spans){if(f.notesCoupled&&!d.notes)continue;const b=f.blockIds.map(E=>m.get(E));if(b.some(E=>E===void 0))continue;const Z=[...b].sort((E,P)=>E-P),V=Z.every((E,P)=>P===
39
+ 0||E===Z[P-1]+1),C=b.every((E,P)=>P===0||E>b[P-1]);!V||!C||f.blockIds.every(E=>{const P=o[m.get(E)];return r.signatures[E]===It(P,c)})&&g.add(f)}const v=o.filter(f=>{const b=f.id?l.get(f.id):void 0;return!(b&&
40
+ g.has(b))}).map(f=>f.id).filter(f=>!!f),k=r.spans.flatMap(f=>f.blockIds),A=o.map(f=>f.id),R=k.length===A.length&&k.every((f,b)=>f===A[b]),T=v.length===0&&R&&d.notes&&d.chrome&&d.comments&&d.coreProps&&
41
+ d.layout&&d.bodySidecars&&d.styles&&d.settings;return{hasCapture:!0,domains:d,graftableGroups:g,groupByBlockId:l,bodyBlocks:o,regeneratedBlockIds:v,fullyExact:T}}async function Vt(t){const{doc:r,blockRanges:o}=t,
42
+ c=r.docxSource;if(!c)throw new B("noCapture");let d;if(c.sourceHash){const e=await Dt(t.originalDocx),n=await At(e);if(n!==c.sourceHash)throw new B("carrierHashMismatch",`capture ${c.sourceHash.slice(
43
+ 0,12)}… vs carrier ${n.slice(0,12)}…`);d=e}const l=nt(r);if(l.fullyExact&&d)return new Blob([d],{type:"application/vnd.openxmlformats-officedocument.wordprocessingml.document"});const[m,g]=await Promise.
44
+ all([et(t.generatedBlob),et(t.originalDocx)]),v=new Set;g.forEach((e,n)=>{n.dir||v.add(e)});const k=await m.file("word/document.xml")?.async("string"),A=await g.file("word/document.xml")?.async("strin\
45
+ g");if(!k||!A)throw new B("missingDocumentXml");const R=_(A,"w:body"),T=_(k,"w:body"),{slots:f,finalSectPr:b}=Wt(k,T.children),Z=r.children.filter(e=>e.type!=="footnote"&&e.type!=="header-footer"),V=new Map;
46
+ for(const e of o)e.blockId&&V.set(e.blockId,e);const C=o.reduce((e,n)=>e+n.count,0);if(f.length!==C)throw new B("slotCountMismatch",`generated body has ${f.length} slots, expected ${C}`);const{graftableGroups:E,
47
+ groupByBlockId:P}=l,U=l.domains.notes,ae=l.domains.chrome,rt=l.domains.comments,st=l.domains.coreProps,Ae=l.domains.layout,ke=l.domains.styles,Ee=l.domains.settings,$=[],O=[],Pe=new Set;let W=0,Ce=-1;
48
+ const ce=new Set,q=c.signatureVersion===Y?Y:1,Be=(e,n,i)=>{const a=e.children[n],s=i.parts.find(u=>u.id===a?.id);return s!==void 0&&s.signature===qe(a,q)},Me=(e,n,i)=>{const a=e;if(a.diffState||a.suggestionId||
49
+ a.structuralSuggestionId)return null;const s=_(n,"w:p"),u=_(i,"w:p"),h=p=>p.children[0]?.tag==="w:pPr"?p.children[0]:void 0,y=h(s),S=h(u),w=/<w:(?:ins|del|pPrChange|rPrChange|moveFrom|moveTo|moveFromRangeStart|moveToRangeStart)\b/;
50
+ return y&&w.test(n.slice(y.start,y.end))||S&&w.test(i.slice(S.start,S.end))?null:[{xml:n.slice(0,y?y.end:s.open.end),generated:!1},{xml:i.slice(S?S.end:u.open.end),generated:!0}]},ot=(e,n,i,a)=>{const s=e.
51
+ children,u=e;if(!n.cells||!s||u.diffState||u.suggestionId||s.some(I=>I.diffState||I.suggestionId)||n.head!==ve(e,q)||/<w:(?:ins|del|trPrChange|tcPrChange|cellIns|cellDel)\b/.test(a.replace(/<w:tc\b[\s\S]*$/,
52
+ "")))return null;const h=_(i,"w:tr"),y=_(a,"w:tr"),S=h.children.filter(I=>I.tag==="w:tc"),w=/<w:(?:ins|del|trPrChange|tcPrChange|cellIns|cellDel|cellMerge)\b/;if(w.test(i.slice(0,S[0]?.start??0))||S.some(
53
+ I=>{const x=/^<w:tc\b[^>]*>\s*(<w:tcPr\b[\s\S]*?<\/w:tcPr>)?/.exec(i.slice(I.start,I.end))?.[1];return x!==void 0&&w.test(x)}))return null;const p=y.children.filter(I=>I.tag==="w:tc"),j=h.children.findIndex(
54
+ I=>I.tag==="w:tc");if(j<0||h.children.slice(j).some(I=>I.tag!=="w:tc")||S.length!==n.cells.length||p.length!==s.length||S.length!==s.length)return null;const L=[{xml:i.slice(0,S[0].start),generated:!1}];
55
+ return s.forEach((I,x)=>{const z=n.cells[x];L.push(z.id===I.id&&z.signature===qe(I,q)?{xml:i.slice(S[x].start,S[x].end),generated:!1}:{xml:a.slice(p[x].start,p[x].end),generated:!0})}),L.push({xml:i.slice(
56
+ S[S.length-1].end),generated:!1}),L},it=(e,n)=>{const i=e.id?c.parts?.[e.id]:void 0,a=e.id?V.get(e.id):void 0;if(!i||!n||!a||n.blockIds.length!==1||i.head!==ve(e,q))return!1;if(e.type==="table"){if(n.
57
+ start!==n.end||a.count!==1)return!1;const s=f[a.start],u=R.children[n.start];if(!s||s.outerSdt||u?.tag!=="w:tbl")return!1;const h=(()=>{try{return JSON.parse(e.structuralSuggestionRecords??"[]")}catch{
58
+ return null}})();if(!h||h.some(x=>(x.tableRevision?.markers??[]).some(z=>z.kind!=="row-insert"&&z.kind!=="row-delete")))return!1;const y=A.slice(u.start,u.end);if(y.includes("<w:vMerge"))return!1;const S=_(
59
+ y,"w:tbl"),w=_(s.xml,"w:tbl"),p=S.children.filter(x=>x.tag==="w:tr"),j=w.children.filter(x=>x.tag==="w:tr"),L=S.children.findIndex(x=>x.tag==="w:tr");if(L<0||S.children.slice(L).some(x=>x.tag!=="w:tr")||
60
+ p.length!==i.parts.length||j.length!==e.children.length||w.children.some(x=>x.tag!=="w:tr"&&x.tag!=="w:tblPr"&&x.tag!=="w:tblGrid"))return!1;const I=[{xml:y.slice(0,p[0].start),generated:!1}];e.children.
61
+ forEach((x,z)=>{const H=i.parts.findIndex(gt=>gt.id===x.id),Ge=s.xml.slice(j[z].start,j[z].end);if(H>=0&&Be(e,z,i)){I.push({xml:y.slice(p[H].start,p[H].end),generated:!1});return}const Ze=H>=0?ot(x,i.
62
+ parts[H],y.slice(p[H].start,p[H].end),Ge):null;Ze?I.push(...Ze):I.push({xml:Ge,generated:!0})}),I.push({xml:y.slice(p[p.length-1].end),generated:!1});for(const x of I)x.generated&&O.push($.length),$.push(
63
+ x.xml);return W+=1,!0}if(e.type==="list"){const s=R.children.slice(n.start,n.end+1);if(s.length!==i.parts.length||a.count!==e.children.length||s.some(w=>w.tag!=="w:p"))return!1;const u=Array.from({length:a.
64
+ count},(w,p)=>f[a.start+p]);if(u.some(w=>!w||w.outerSdt))return!1;const h=new Set(s.map(w=>/<w:numId\s+w:val="(\d+)"/.exec(A.slice(w.start,w.end))?.[1])),y=h.size===1?[...h][0]:void 0,S=[];for(let w=0;w<
65
+ e.children.length;w++){const p=e.children[w],j=i.parts.findIndex(I=>I.id===p.id);if(j>=0&&Be(e,w,i)){S.push([{xml:A.slice(s[j].start,s[j].end),generated:!1}]);continue}const L=j>=0&&i.parts[j].head===
66
+ ve(p,q)?Me(p,A.slice(s[j].start,s[j].end),u[w].xml):null;if(L){S.push(L);continue}if(y===void 0)return!1;S.push([{xml:u[w].xml.replace(/(<w:numId\s+w:val=")\d+(")/,`$1${y}$2`),generated:!0,pin:!0}])}for(const w of S){
67
+ for(const p of w)p.generated&&(p.pin&&p.xml.includes("<w:numId")&&ce.add($.length),O.push($.length)),$.push(p.xml);W+=1}return!0}if(e.type==="paragraph"){const s=f[a.start],u=R.children[n.start];if(n.
68
+ start!==n.end||a.count!==1||!s||s.outerSdt||u?.tag!=="w:p"||!s.xml.startsWith("<w:p"))return!1;const h=Me(e,A.slice(u.start,u.end),s.xml);if(!h)return!1;for(const y of h)y.generated&&O.push($.length),
69
+ $.push(y.xml);return W+=1,!0}return!1};for(const e of Z){const n=e.id?P.get(e.id):void 0;if(!(n&&E.has(n))&&it(e,n))continue;if(n&&E.has(n)){if(Pe.has(n))continue;Pe.add(n);const a=R.children[n.start],
70
+ s=R.children[n.end];if(!a||!s)throw new B("missingOriginalChild",`original body children ${n.start}-${n.end}`);$.push(A.slice(a.start,s.end)),W+=n.end-n.start+1;continue}const i=e.id?V.get(e.id):void 0;
71
+ if(!i)throw new B("missingGeneratedRange",`block ${e.id}`);for(let a=i.start;a<i.start+i.count;a++){const s=f[a];if(!s)throw new B("missingGeneratedSlot",`slot ${a}`);if(s.outerSdt){if(s.outerSdt.start===
72
+ Ce)continue;Ce=s.outerSdt.start,O.push($.length),$.push(s.outerSdt.xml),W+=1;continue}O.push($.length),$.push(s.xml),W+=1}}const Q=R.children.find(e=>e.tag==="w:sectPr");Ae&&ae&&Q&&Q?($.push(A.slice(Q.
73
+ start,Q.end)),W+=1):b&&(O.push($.length),$.push(b),W+=1);const je=O.some(e=>!ce.has(e)&&$[e].includes("w:numId")),at=O.length>0,le={notes:U,comments:rt,chrome:ae&&Ae,coreProps:st,styles:ke&&!at&&U&&ae,
74
+ settings:Ee,numbering:!je},J=e=>{const n=oe(e);return n!==null&&le[n]===!0},X="word/_rels/document.xml.rels",de=await g.file(X)?.async("string")??'<?xml version="1.0" encoding="UTF-8" standalone="yes"\
75
+ ?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>',Re=await m.file(X)?.async("string"),ct=Re?K(Re):[],fe=K(de),ue=new Set(fe.map(e=>e.id)),lt=O.map(
76
+ e=>$[e]),dt=Ke(lt);let me=Math.max(0,...fe.map(e=>{const n=/^rId(\d+)$/.exec(e.id);return n?Number(n[1]):0}))+1;const Oe=new Map,ee=[],F=new Map,ge=new Set(v),Te=e=>{const n=F.get(e);if(n)return n;let i=e;
77
+ if(ge.has(i)){const a=e.lastIndexOf("/"),s=e.slice(0,a+1),u=e.slice(a+1);let h=1;do{const y=h===1?"rvseg_":`rvseg_${h}_`;i=`${s}${y}${u}`,h+=1}while(ge.has(i))}return ge.add(i),F.set(e,i),i};for(const e of ct){
78
+ const n=dt.has(e.id),i=fe.some(u=>u.target===e.target&&!u.external);if(!n&&(i||e.external)||!n&&e.target.startsWith("media/"))continue;if(!n){const u=`word/${e.target}`.replace("word/../",""),h=oe(u);
79
+ if(!h||le[h])continue}if(!e.external&&J(`word/${e.target}`.replace("word/../","")))continue;let a=e.target;if(!e.external&&a.startsWith("media/")){const u=`word/${a}`;a=Te(u).replace(/^word\//,"")}let s=e.
80
+ id;if(ue.has(e.id)){for(;ue.has(`rId${me}`);)me+=1;s=`rId${me++}`}ue.add(s),s!==e.id&&Oe.set(e.id,s),ee.push(ie(ie(e.raw,"Id",s),"Target",a))}for(const e of O)$[e]=kt($[e],Oe);const _e=ee.length?de.replace(
81
+ "</Relationships>",`${ee.join("")}</Relationships>`):de;let te;const Ne=await m.file("word/numbering.xml")?.async("string"),G=await g.file("word/numbering.xml")?.async("string");if(je&&Ne&&G){const e=Math.
82
+ max($e(G,/w:numId w:val="(\d+)"/g),$e(G,/<w:num\b[^>]*w:numId="(\d+)"/g),$e(G,/w:abstractNumId(?: w:val)?="(\d+)"/g))+100,n=Ne.replace(/(<w:num\b[^>]*w:numId=")(\d+)(")/g,(s,u,h,y)=>`${u}${Number(h)+e}${y}`).
83
+ replace(/(w:abstractNumId(?: w:val)?=")(\d+)(")/g,(s,u,h,y)=>`${u}${Number(h)+e}${y}`),i=Array.from(n.matchAll(/<w:abstractNum\b[\s\S]*?<\/w:abstractNum>/g),s=>s[0]),a=Array.from(n.matchAll(/<w:num\b[\s\S]*?<\/w:num>/g),
84
+ s=>s[0]);if(i.length>0||a.length>0){const s=G.search(/<w:num\b/);let u=s>=0?G.slice(0,s)+i.join("")+G.slice(s):G.replace("</w:numbering>",`${i.join("")}</w:numbering>`);u=u.replace("</w:numbering>",`${a.
85
+ join("")}</w:numbering>`),te=u;for(const h of O)ce.has(h)||($[h]=$[h].replace(/(<w:numId w:val=")(\d+)(")/g,(y,S,w,p)=>`${S}${Number(w)+e}${p}`))}}let ne;if(ke&&!le.styles){const e=O.map(n=>$[n]);for(const n of Object.
86
+ keys(m.files)){const i=oe(n),a=m.file(n);a&&n.endsWith(".xml")&&(i==="chrome"||i==="notes"||i==="comments")&&!J(n)&&e.push(await a.async("string"))}ne=Tt(await g.file("word/styles.xml")?.async("string"),
87
+ await m.file("word/styles.xml")?.async("string"),Ot(e))}const re=Ee?void 0:Ct(await g.file("word/settings.xml")?.async("string"),await m.file("word/settings.xml")?.async("string")),he=new Set;for(const e of O){
88
+ const n=$[e];for(const i of n.matchAll(/<([A-Za-z][A-Za-z0-9]*):/g))he.add(i[1]);for(const i of n.matchAll(/[\s"']([A-Za-z][A-Za-z0-9]*):[A-Za-z][A-Za-z0-9]*="/g))he.add(i[1])}const we=Et(A,k,he),We=_(
89
+ we,"w:body"),pe=we.slice(0,We.contentStart)+$.join("")+we.slice(We.contentEnd);let xe;try{xe=_(pe,"w:body")}catch(e){throw new B("verifyReparseFailed",e instanceof Error?e.message:String(e))}if(xe.children.
90
+ length!==W)throw new B("verifyChildCountMismatch",`composed body has ${xe.children.length} children, accounted ${W}`);const ft=new Set(K(_e).map(e=>e.id));for(const e of Ke([pe]))if(!ft.has(e))throw new B(
91
+ "verifyRelsUnresolved",e);const M=new He,se=[],ye=new Map;for(const e of Object.keys(m.files)){if(e===X||!e.endsWith(".rels")||!Xe(e)||J(e))continue;const n=m.file(e);if(!n)continue;let i=await n.async(
92
+ "string");for(const a of K(i)){if(a.external)continue;const s=tt(e,a.target);if(!s.startsWith("word/media/")||!m.file(s))continue;const u=Te(s).replace(/^word\//,"");i=i.replace(a.raw,ie(a.raw,"Target",
93
+ u))}ye.set(e,i)}const De=new Set;for(const e of ee){const n=N(e,"Target");n&&N(e,"TargetMode")!=="External"&&De.add(`word/${n}`.replace("word/../",""))}const Le=e=>oe(e)!==null&&!J(e)||De.has(e)||F.has(
94
+ e);m.forEach((e,n)=>{if(n.dir||v.has(e)||J(e))return;const i=/^(.*)\/_rels\/([^/]+)\.rels$/.exec(e),a=i?`${i[1]}/${i[2]}`:null;(Le(e)||a!==null&&Le(a))&&(F.has(e)||F.set(e,e))});const be=new Set(F.values());
95
+ re&&be.add("word/settings.xml");for(const e of v)if(e!=="word/document.xml"&&e!==X&&e!=="[Content_Types].xml"&&!(e==="word/numbering.xml"&&te)&&!(e==="word/settings.xml"&&re)&&!(e==="word/styles.xml"&&
96
+ ne)){if(!J(e)&&Xe(e)&&m.file(e)){be.add(e);const n=ye.get(e);n!==void 0?M.file(e,n):se.push(m.file(e).async("uint8array").then(i=>{M.file(e,i)}));continue}se.push(g.file(e).async("uint8array").then(n=>{
97
+ M.file(e,n)}))}for(const[e,n]of F){const i=m.file(e);if(!i)continue;const a=ye.get(e);if(a!==void 0){M.file(n,a);continue}se.push(i.async("uint8array").then(s=>{M.file(n,s)}))}await Promise.all(se),M.
98
+ file("word/document.xml",pe),M.file(X,_e),te&&M.file("word/numbering.xml",te),re&&M.file("word/settings.xml",re),ne&&M.file("word/styles.xml",ne);const Se=await g.file("[Content_Types].xml")?.async("s\
99
+ tring"),ze=await m.file("[Content_Types].xml")?.async("string");if(!Se)throw new B("missingContentTypes");const Ve=ze?Pt(Se,ze,be):Se,Fe=Array.from(Ve.matchAll(/<(?:Default|Override)\b[^>]*\/?>/g),e=>e[0]),
100
+ ut=new Set(Fe.map(e=>N(e,"Extension")?.toLowerCase()).filter(e=>!!e)),mt=new Set(Fe.map(e=>N(e,"PartName")?.replace(/^\//,"")).filter(e=>!!e));for(const e of new Set(F.values())){const n=e.slice(e.lastIndexOf(
101
+ ".")+1).toLowerCase();if(!mt.has(e)&&!ut.has(n))throw new B("verifyContentTypeMissing",e)}M.file("[Content_Types].xml",Ve);for(const e of Object.keys(M.files).filter(n=>n.endsWith(".rels"))){const n=M.
102
+ file(e);if(!n)continue;const i=K(await n.async("string")),a=new Set;for(const s of i){if(a.has(s.id))throw new B("verifyDuplicateRelationshipId",`${e}: ${s.id}`);if(a.add(s.id),s.external||s.target.startsWith(
103
+ "#"))continue;const u=tt(e,s.target);if(!M.file(u))throw new B("verifyRelationshipTargetMissing",`${e}: ${s.target}`)}}return M.generateAsync({type:"blob",compression:"DEFLATE",compressionOptions:{level:6}})}
104
+ function Ft(t){return t.children.filter(r=>r.type!=="footnote"&&r.type!=="header-footer")}export{B as G,nt as a,_t as b,zt as g,Nt as r};