@a3s-lab/office 0.12.0 → 0.13.0

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 (111) hide show
  1. package/README.md +99 -6
  2. package/dist/0~1403.js +20 -12
  3. package/dist/0~2805.js +241 -241
  4. package/dist/0~4980.js +1 -1
  5. package/dist/0~5050.js +1 -20
  6. package/dist/0~5093.js +547 -547
  7. package/dist/0~7048.js +31 -29
  8. package/dist/0~document-editor.js +694 -90
  9. package/dist/0~pdf-viewer.js +41 -10
  10. package/dist/0~presentation-editor.js +8 -6
  11. package/dist/0~spreadsheet-editor.js +11528 -10094
  12. package/dist/0~work-document-import-worker-client.js +237 -0
  13. package/dist/0~work-docx-export.js +1 -1
  14. package/dist/0~work-docx-import.js +1 -1
  15. package/dist/0~work-docx-large-document-import.js +559 -0
  16. package/dist/0~work-office-diagnostics.js +46 -32
  17. package/dist/4104.js +4092 -3210
  18. package/dist/432.js +51 -7
  19. package/dist/4476.js +201 -7
  20. package/dist/5184.js +24 -2
  21. package/dist/{8928.js → 6282.js} +2338 -93
  22. package/dist/8715.js +895 -888
  23. package/dist/9356.js +3 -1
  24. package/dist/core.js +1 -1
  25. package/dist/index.js +1 -1
  26. package/dist/internal/features/work/editors/document-editor-support.d.ts +1 -9
  27. package/dist/internal/features/work/editors/document-external-content.d.ts +1 -1
  28. package/dist/internal/features/work/editors/presentation-slide-canvas.d.ts +3 -1
  29. package/dist/internal/features/work/editors/presentation-slide-thumbnail.d.ts +3 -2
  30. package/dist/internal/features/work/editors/presentation-thumbnail-rail.d.ts +2 -1
  31. package/dist/internal/features/work/editors/presentation-workspace.d.ts +2 -1
  32. package/dist/internal/features/work/editors/spreadsheet-border-ribbon.d.ts +5 -0
  33. package/dist/internal/features/work/editors/spreadsheet-calculation-projection.d.ts +10 -0
  34. package/dist/internal/features/work/editors/spreadsheet-cell-border-command.d.ts +3 -0
  35. package/dist/internal/features/work/editors/spreadsheet-cell-border.d.ts +14 -0
  36. package/dist/internal/features/work/editors/spreadsheet-cell-fill-command.d.ts +3 -0
  37. package/dist/internal/features/work/editors/spreadsheet-cell-fill.d.ts +12 -0
  38. package/dist/internal/features/work/editors/spreadsheet-cell-range.d.ts +14 -0
  39. package/dist/internal/features/work/editors/spreadsheet-command-catalog.d.ts +159 -0
  40. package/dist/internal/features/work/editors/spreadsheet-command-controller.d.ts +8 -4
  41. package/dist/internal/features/work/editors/spreadsheet-editor-support.d.ts +8 -2
  42. package/dist/internal/features/work/editors/spreadsheet-home-format-ribbon.d.ts +10 -0
  43. package/dist/internal/features/work/editors/spreadsheet-in-place-workbook-sync.d.ts +10 -0
  44. package/dist/internal/features/work/editors/spreadsheet-operation-projection.d.ts +14 -0
  45. package/dist/internal/features/work/editors/spreadsheet-structure-selection.d.ts +7 -0
  46. package/dist/internal/features/work/editors/use-document-pagination.d.ts +5 -0
  47. package/dist/internal/features/work/editors/use-presentation-design-commands.d.ts +2 -1
  48. package/dist/internal/features/work/editors/use-spreadsheet-workbook-sync.d.ts +2 -1
  49. package/dist/internal/features/work/spreadsheet-history-value.d.ts +1 -0
  50. package/dist/internal/features/work/spreadsheet-sparse.d.ts +6 -1
  51. package/dist/internal/features/work/work-document-chunk-geometry.d.ts +28 -0
  52. package/dist/internal/features/work/work-document-chunk-node.d.ts +23 -0
  53. package/dist/internal/features/work/work-document-extensions.d.ts +1 -0
  54. package/dist/internal/features/work/work-document-html-fingerprint.d.ts +15 -0
  55. package/dist/internal/features/work/work-document-import-worker-client.d.ts +13 -0
  56. package/dist/internal/features/work/work-document-import-worker-protocol.d.ts +36 -0
  57. package/dist/internal/features/work/work-document-import.worker.d.ts +1 -0
  58. package/dist/internal/features/work/work-document-incremental-layout.d.ts +1 -1
  59. package/dist/internal/features/work/work-document-integrity-index.d.ts +14 -0
  60. package/dist/internal/features/work/work-document-lazy-block.d.ts +3 -0
  61. package/dist/internal/features/work/work-document-lazy-html.d.ts +21 -0
  62. package/dist/internal/features/work/work-document-lazy-model.d.ts +29 -0
  63. package/dist/internal/features/work/work-document-lazy-pagination-measurement.d.ts +3 -0
  64. package/dist/internal/features/work/work-document-lazy-preview.d.ts +21 -0
  65. package/dist/internal/features/work/work-document-lazy-update.d.ts +14 -0
  66. package/dist/internal/features/work/work-document-model.d.ts +18 -2
  67. package/dist/internal/features/work/work-document-page-surface-registry.d.ts +16 -0
  68. package/dist/internal/features/work/work-document-pagination-dom.d.ts +1 -0
  69. package/dist/internal/features/work/work-document-pagination-types.d.ts +1 -0
  70. package/dist/internal/features/work/work-document-pagination.d.ts +1 -1
  71. package/dist/internal/features/work/work-document-text-statistics.d.ts +25 -0
  72. package/dist/internal/features/work/work-document-windowing.d.ts +23 -0
  73. package/dist/internal/features/work/work-docx-large-document-import.d.ts +35 -0
  74. package/dist/internal/features/work/work-docx-large-document-parser.d.ts +29 -0
  75. package/dist/internal/features/work/work-file-import.d.ts +5 -0
  76. package/dist/internal/features/work/work-file-io.d.ts +3 -3
  77. package/dist/internal/features/work/work-file-kind.d.ts +5 -0
  78. package/dist/internal/features/work/work-office-diagnostics.d.ts +10 -1
  79. package/dist/internal/features/work/work-ooxml-package.d.ts +8 -0
  80. package/dist/internal/features/work/work-presentation-layouts.d.ts +7 -1
  81. package/dist/internal/features/work/work-repository.d.ts +1 -0
  82. package/dist/internal/features/work/work-spreadsheet-compatibility-summary.d.ts +10 -0
  83. package/dist/internal/features/work/work-spreadsheet-file-export.d.ts +2 -0
  84. package/dist/internal/features/work/work-spreadsheet-file-import.d.ts +3 -0
  85. package/dist/internal/features/work/work-spreadsheet-import-diagnostics.d.ts +2 -0
  86. package/dist/internal/features/work/work-spreadsheet-import-worker-client.d.ts +6 -0
  87. package/dist/internal/features/work/work-spreadsheet-import-worker-protocol.d.ts +32 -0
  88. package/dist/internal/features/work/work-spreadsheet-import.worker.d.ts +1 -0
  89. package/dist/internal/features/work/work-spreadsheet-matrix-profile.d.ts +64 -0
  90. package/dist/internal/features/work/work-spreadsheet-package-scan-worker-client.d.ts +6 -0
  91. package/dist/internal/features/work/work-spreadsheet-package-scan-worker-protocol.d.ts +49 -0
  92. package/dist/internal/features/work/work-spreadsheet-package-scan.worker.d.ts +1 -0
  93. package/dist/internal/features/work/work-xlsx-formula-diagnostics.d.ts +3 -1
  94. package/dist/internal/features/work/work-xlsx-formulas.d.ts +2 -1
  95. package/dist/internal/features/work/work-xlsx-interop.d.ts +6 -5
  96. package/dist/internal/features/work/work-xlsx-pivot-diagnostics.d.ts +2 -1
  97. package/dist/internal/features/work/work-xlsx-plain-fast-path.d.ts +54 -0
  98. package/dist/internal/features/work/work-xlsx-style-gate.d.ts +1 -0
  99. package/dist/internal/features/work/work-xlsx-worksheet-scan.d.ts +12 -0
  100. package/dist/internal/features/work/work-xlsx-worksheet.d.ts +12 -0
  101. package/dist/office-kernel.wasm +0 -0
  102. package/dist/office-kernel.worker.js +31 -29
  103. package/dist/react.d.ts +7 -1
  104. package/dist/styles.css +440 -3
  105. package/dist/work-document-import.worker.js +558 -0
  106. package/dist/work-spreadsheet-import.worker.js +24745 -0
  107. package/dist/work-spreadsheet-import.worker.js.LICENSE.txt +3 -0
  108. package/dist/work-spreadsheet-package-scan.worker.js +4157 -0
  109. package/dist/work-spreadsheet-package-scan.worker.js.LICENSE.txt +11 -0
  110. package/docs/latest/en/browser-editor-architecture.md +351 -15
  111. package/package.json +19 -1
package/dist/0~5093.js CHANGED
@@ -1,6 +1,31 @@
1
1
  import { descendants, directChildren as work_ooxml_package_directChildren, xmlNamespacePrefix, directChild, attribute as work_ooxml_package_attribute, childPath } from "./5184.js";
2
2
  import { xmlAttributeLocalName, docxThemeFont, XML_NAMESPACE as work_docx_settings_xml_XML_NAMESPACE, docxThemeColor, parseDocxParagraphDefaultCollapsed, resolveDocxThemeResolver as work_docx_theme_resolveDocxThemeResolver, DOCX_WORDPROCESSING_NAMESPACES, XMLNS_NAMESPACE as work_docx_settings_xml_XMLNS_NAMESPACE, xmlAttributeNamespace } from "./0~2805.js";
3
- import { DOCUMENT_PARAGRAPH_BORDER_STYLES, normalizeDocumentParagraphBorders as work_document_paragraph_borders_normalizeDocumentParagraphBorders, DOCUMENT_PARAGRAPH_BORDER_EDGES, normalizeDocumentPageBorders, normalizeDocumentParagraphBorder, normalizeDocumentParagraphIndent, serializeDocumentParagraphFormatting, normalizeDocumentEquation, DOCUMENT_PAGE_BORDER_EDGES, normalizeDocumentTabStops, DOCUMENT_PARAGRAPH_SHADING_PATTERNS, documentWordLineHeightFactor, documentAutoLineHeight, renderDocumentAutoLineHeight, serializeDocxThemeReference, documentParagraphShadingDomAttributes, importedDocumentCharacterFormatting, documentParagraphBordersDomAttributes, createDocumentEquationElement, normalizeDocumentPageGeometry, normalizeDocumentPageMargins, documentEquationText, normalizeDocumentPaperSource, serializeDocumentTabStops } from "./8928.js";
3
+ import { DOCUMENT_PARAGRAPH_BORDER_STYLES, normalizeDocumentParagraphBorders as work_document_paragraph_borders_normalizeDocumentParagraphBorders, DOCUMENT_PARAGRAPH_BORDER_EDGES, normalizeDocumentPageBorders, normalizeDocumentParagraphBorder, normalizeDocumentParagraphIndent, serializeDocumentParagraphFormatting, normalizeDocumentEquation, DOCUMENT_PAGE_BORDER_EDGES, normalizeDocumentTabStops, DOCUMENT_PARAGRAPH_SHADING_PATTERNS, documentWordLineHeightFactor, documentAutoLineHeight, renderDocumentAutoLineHeight, serializeDocxThemeReference, documentParagraphShadingDomAttributes, importedDocumentCharacterFormatting, documentParagraphBordersDomAttributes, createDocumentEquationElement, normalizeDocumentPageGeometry, normalizeDocumentPageMargins, documentEquationText, normalizeDocumentPaperSource, serializeDocumentTabStops } from "./6282.js";
4
+ function docxCaptionSequenceKind(instruction) {
5
+ const identifier = /^\s*SEQ\s+([^\s\\]+)/i.exec(instruction)?.[1]?.toLowerCase();
6
+ if (!identifier) return null;
7
+ if ('table' === identifier || '表' === identifier) return 'table';
8
+ if ('figure' === identifier || 'fig' === identifier || '图' === identifier) return 'figure';
9
+ return null;
10
+ }
11
+ function docxCaptionReferenceTarget(instruction) {
12
+ return /^\s*REF\s+([^\s\\]+)/i.exec(instruction)?.[1] ?? null;
13
+ }
14
+ function docxCaptionBookmark(paragraph, field) {
15
+ const bookmarks = descendants(paragraph, 'bookmarkStart').filter((bookmark)=>!/^_(?:GoBack|Toc)/i.test(work_ooxml_package_attribute(bookmark, 'name') ?? ''));
16
+ const enclosing = bookmarks.filter((bookmark)=>bookmarkContains(paragraph, bookmark, field));
17
+ if (enclosing.length) return enclosing.at(-1);
18
+ return bookmarks.filter((bookmark)=>precedes(bookmark, field.start)).at(-1) ?? bookmarks[0];
19
+ }
20
+ function bookmarkContains(paragraph, start, field) {
21
+ const id = work_ooxml_package_attribute(start, 'id');
22
+ if (!id) return false;
23
+ const end = descendants(paragraph, 'bookmarkEnd').find((bookmark)=>work_ooxml_package_attribute(bookmark, 'id') === id);
24
+ return Boolean(end && precedes(start, field.start) && precedes(field.end, end));
25
+ }
26
+ function precedes(candidate, target) {
27
+ return Boolean(candidate.compareDocumentPosition(target) & Node.DOCUMENT_POSITION_FOLLOWING);
28
+ }
4
29
  function docxFieldInstructions(root) {
5
30
  return docxFieldOccurrences(root).map((field)=>field.instruction);
6
31
  }
@@ -133,31 +158,6 @@ function fieldRange(field, indexes) {
133
158
  function containsRange(outer, inner) {
134
159
  return outer.start >= 0 && inner.start >= 0 && outer.start < inner.start && outer.end >= inner.end;
135
160
  }
136
- function docxCaptionSequenceKind(instruction) {
137
- const identifier = /^\s*SEQ\s+([^\s\\]+)/i.exec(instruction)?.[1]?.toLowerCase();
138
- if (!identifier) return null;
139
- if ('table' === identifier || '表' === identifier) return 'table';
140
- if ('figure' === identifier || 'fig' === identifier || '图' === identifier) return 'figure';
141
- return null;
142
- }
143
- function docxCaptionReferenceTarget(instruction) {
144
- return /^\s*REF\s+([^\s\\]+)/i.exec(instruction)?.[1] ?? null;
145
- }
146
- function docxCaptionBookmark(paragraph, field) {
147
- const bookmarks = descendants(paragraph, 'bookmarkStart').filter((bookmark)=>!/^_(?:GoBack|Toc)/i.test(work_ooxml_package_attribute(bookmark, 'name') ?? ''));
148
- const enclosing = bookmarks.filter((bookmark)=>bookmarkContains(paragraph, bookmark, field));
149
- if (enclosing.length) return enclosing.at(-1);
150
- return bookmarks.filter((bookmark)=>precedes(bookmark, field.start)).at(-1) ?? bookmarks[0];
151
- }
152
- function bookmarkContains(paragraph, start, field) {
153
- const id = work_ooxml_package_attribute(start, 'id');
154
- if (!id) return false;
155
- const end = descendants(paragraph, 'bookmarkEnd').find((bookmark)=>work_ooxml_package_attribute(bookmark, 'id') === id);
156
- return Boolean(end && precedes(start, field.start) && precedes(field.end, end));
157
- }
158
- function precedes(candidate, target) {
159
- return Boolean(candidate.compareDocumentPosition(target) & Node.DOCUMENT_POSITION_FOLLOWING);
160
- }
161
161
  function isDocxEquationLikeRoot(element) {
162
162
  return 'oMath' === element.localName || 'oMathPara' === element.localName;
163
163
  }
@@ -4793,157 +4793,448 @@ function textNodes(root) {
4793
4793
  while(walker.nextNode())nodes.push(walker.currentNode);
4794
4794
  return nodes;
4795
4795
  }
4796
- const PAGE_BORDER_EDGE_SET = new Set(DOCUMENT_PAGE_BORDER_EDGES);
4797
- const PAGE_BORDER_ATTRIBUTES = new Set([
4798
- 'zOrder',
4799
- 'display',
4800
- 'offsetFrom'
4801
- ]);
4802
- const PAGE_BORDER_DISPLAYS = new Set([
4803
- 'allPages',
4804
- 'firstPage',
4805
- 'notFirstPage'
4806
- ]);
4807
- const PAGE_BORDER_OFFSETS = new Set([
4808
- 'page',
4809
- 'text'
4796
+ const work_docx_paragraph_shading_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
4797
+ const work_docx_paragraph_shading_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
4798
+ const work_docx_paragraph_shading_import_XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/';
4799
+ const PARAGRAPH_SHADING_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_SHADING_\d+__/g;
4800
+ const work_docx_paragraph_shading_import_WORD_THEME_COLORS = new Set([
4801
+ 'dark1',
4802
+ 'light1',
4803
+ 'dark2',
4804
+ 'light2',
4805
+ 'accent1',
4806
+ 'accent2',
4807
+ 'accent3',
4808
+ 'accent4',
4809
+ 'accent5',
4810
+ 'accent6',
4811
+ 'hyperlink',
4812
+ 'followedHyperlink',
4813
+ 'none',
4814
+ 'background1',
4815
+ 'text1',
4816
+ 'background2',
4817
+ 'text2'
4810
4818
  ]);
4811
- const PAGE_BORDER_Z_ORDERS = new Set([
4812
- 'front',
4813
- 'back'
4819
+ const SHADING_ATTRIBUTES = new Set([
4820
+ 'val',
4821
+ 'color',
4822
+ 'themeColor',
4823
+ 'themeTint',
4824
+ 'themeShade',
4825
+ 'fill',
4826
+ 'themeFill',
4827
+ 'themeFillTint',
4828
+ 'themeFillShade'
4814
4829
  ]);
4815
- function inspectDocxPageBorders(sectionProperties, themeSource) {
4816
- if ('sectPr' !== sectionProperties.localName || !DOCX_WORDPROCESSING_NAMESPACES.has(sectionProperties.namespaceURI ?? '')) return {
4817
- status: 'absent',
4818
- invalidCount: 0,
4819
- spoofedCount: 0
4820
- };
4821
- const named = work_ooxml_package_directChildren(sectionProperties, 'pgBorders');
4822
- const candidates = named.filter((element)=>element.namespaceURI === sectionProperties.namespaceURI);
4823
- const spoofedContainers = named.length - candidates.length;
4824
- if (!candidates.length) return {
4825
- status: 'absent',
4826
- invalidCount: 0,
4827
- spoofedCount: spoofedContainers
4828
- };
4829
- if (1 !== candidates.length) return {
4830
- status: 'invalid',
4831
- invalidCount: candidates.length,
4832
- spoofedCount: spoofedContainers
4833
- };
4834
- const container = candidates[0];
4835
- if (!container) return {
4836
- status: 'invalid',
4837
- invalidCount: 1,
4838
- spoofedCount: spoofedContainers
4839
- };
4840
- const attributes = wordContainerAttributes(container);
4841
- if (!attributes || hasNonWhitespaceText(container)) return {
4842
- status: 'invalid',
4843
- invalidCount: 1,
4844
- spoofedCount: spoofedContainers
4845
- };
4846
- const display = pageBorderDisplay(attributes.get('display'));
4847
- const offsetFrom = pageBorderOffset(attributes.get('offsetFrom'));
4848
- const zOrder = pageBorderZOrder(attributes.get('zOrder'));
4849
- if (null === display || null === offsetFrom || null === zOrder) return {
4850
- status: 'invalid',
4851
- invalidCount: 1,
4852
- spoofedCount: spoofedContainers
4853
- };
4830
+ function markDocxParagraphShading(document, styleSource, themeSource, tableStyleSource) {
4831
+ const paragraphs = [];
4832
+ const styles = resolveDocxParagraphStyleResolver(styleSource);
4833
+ const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
4854
4834
  const theme = work_docx_theme_resolveDocxThemeResolver(themeSource);
4855
- const edges = {};
4856
4835
  let invalidCount = 0;
4857
- let spoofedCount = spoofedContainers;
4858
- let previousIndex = -1;
4859
- const seen = new Set();
4860
- for (const child of work_ooxml_package_directChildren(container)){
4861
- if (child.namespaceURI !== container.namespaceURI) {
4862
- spoofedCount += 1;
4863
- continue;
4864
- }
4865
- const edge = child.localName;
4866
- const index = DOCUMENT_PAGE_BORDER_EDGES.indexOf(edge);
4867
- if (!PAGE_BORDER_EDGE_SET.has(edge) || index < previousIndex || seen.has(edge)) return {
4868
- status: 'invalid',
4869
- invalidCount: invalidCount + 1,
4870
- spoofedCount
4871
- };
4872
- previousIndex = index;
4873
- seen.add(edge);
4874
- const border = parseDocxBorderElement(child, theme);
4875
- if (border) edges[edge] = border;
4876
- else {
4877
- invalidCount += 1;
4878
- edges[edge] = {
4879
- style: 'nil'
4880
- };
4881
- }
4836
+ let spoofedCount = 0;
4837
+ for (const paragraph of descendants(document, 'p')){
4838
+ if (!work_docx_paragraph_shading_import_isWordElement(paragraph)) continue;
4839
+ const properties = work_docx_paragraph_shading_import_wordDirectChildren(paragraph, 'pPr')[0];
4840
+ const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
4841
+ const resolved = resolveParagraphShading(sources, theme);
4842
+ invalidCount += resolved.invalidCount;
4843
+ spoofedCount += resolved.spoofedCount;
4844
+ if (!resolved.shading) continue;
4845
+ const marker = `__A3S_WORK_PARAGRAPH_SHADING_${paragraphs.length + 1}__`;
4846
+ const writableProperties = properties ?? work_docx_paragraph_shading_import_insertParagraphProperties(document, paragraph);
4847
+ work_docx_paragraph_shading_import_insertParagraphMarker(document, paragraph, writableProperties, marker);
4848
+ paragraphs.push({
4849
+ marker,
4850
+ shading: resolved.shading
4851
+ });
4882
4852
  }
4883
- const pageBorders = normalizeDocumentPageBorders({
4884
- ...display ? {
4885
- display
4886
- } : {},
4887
- ...offsetFrom ? {
4888
- offsetFrom
4889
- } : {},
4890
- ...zOrder ? {
4891
- zOrder
4892
- } : {},
4893
- edges
4894
- });
4895
- if (!pageBorders) return {
4896
- status: 'invalid',
4897
- invalidCount: invalidCount + 1,
4898
- spoofedCount
4899
- };
4900
4853
  return {
4901
- status: 'valid',
4902
- pageBorders,
4854
+ paragraphs,
4903
4855
  invalidCount,
4904
4856
  spoofedCount
4905
4857
  };
4906
4858
  }
4907
- function parseDocxPageBorders(sectionProperties, themeSource) {
4908
- const inspected = inspectDocxPageBorders(sectionProperties, themeSource);
4909
- if ('absent' === inspected.status) return;
4910
- return 'valid' === inspected.status ? inspected.pageBorders : null;
4911
- }
4912
- function wordContainerAttributes(element) {
4913
- const result = new Map();
4914
- for (const attribute of Array.from(element.attributes)){
4915
- if (work_docx_page_borders_import_isNamespaceDeclaration(attribute)) continue;
4916
- const name = xmlAttributeLocalName(attribute);
4917
- if (xmlAttributeNamespace(element, attribute) !== element.namespaceURI || !PAGE_BORDER_ATTRIBUTES.has(name) || result.has(name)) return null;
4918
- result.set(name, attribute.value);
4919
- }
4920
- return result;
4921
- }
4922
- function pageBorderDisplay(value) {
4923
- if (void 0 === value) return;
4924
- const normalized = value.trim();
4925
- return PAGE_BORDER_DISPLAYS.has(normalized) ? normalized : null;
4926
- }
4927
- function pageBorderOffset(value) {
4928
- if (void 0 === value) return;
4929
- const normalized = value.trim();
4930
- return PAGE_BORDER_OFFSETS.has(normalized) ? normalized : null;
4931
- }
4932
- function pageBorderZOrder(value) {
4933
- if (void 0 === value) return;
4934
- const normalized = value.trim();
4935
- return PAGE_BORDER_Z_ORDERS.has(normalized) ? normalized : null;
4936
- }
4937
- function hasNonWhitespaceText(element) {
4938
- return Array.from(element.childNodes).some((node)=>node.nodeType === Node.TEXT_NODE && node.textContent?.trim());
4859
+ function applyImportedDocxParagraphShadingMarkers(document, markers) {
4860
+ const shadingByMarker = new Map(markers.paragraphs.map(({ marker, shading })=>[
4861
+ marker,
4862
+ shading
4863
+ ]));
4864
+ for (const node of work_docx_paragraph_shading_import_textNodes(document.body))if (node.data.includes('__A3S_WORK_PARAGRAPH_SHADING_')) node.data = node.data.replace(PARAGRAPH_SHADING_MARKER_PATTERN, (marker)=>{
4865
+ const shading = shadingByMarker.get(marker);
4866
+ const block = shading ? work_docx_paragraph_shading_import_closestParagraphBlock(node.parentElement, node) : null;
4867
+ if (block && shading) applyParagraphShading(block, shading);
4868
+ return '';
4869
+ });
4870
+ document.body.normalize();
4939
4871
  }
4940
- function work_docx_page_borders_import_isNamespaceDeclaration(attribute) {
4941
- return attribute.namespaceURI === work_docx_settings_xml_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:');
4872
+ function hasImportedDocxParagraphShadingMarkers(markers) {
4873
+ return markers.paragraphs.length > 0;
4942
4874
  }
4943
- const STRICT_WORDPROCESSING_NAMESPACE = 'http://purl.oclc.org/ooxml/wordprocessingml/main';
4944
- const UNIVERSAL_TWIP_RATIOS = {
4945
- mm: [
4946
- 7200n,
4875
+ function parseDocxParagraphShadingElement(element, themeSource) {
4876
+ const attributes = work_docx_paragraph_shading_import_wordLeafAttributes(element, SHADING_ATTRIBUTES);
4877
+ const pattern = attributes?.get('val')?.trim();
4878
+ if (!attributes || !DOCUMENT_PARAGRAPH_SHADING_PATTERNS.has(pattern)) return null;
4879
+ const theme = work_docx_theme_resolveDocxThemeResolver(themeSource);
4880
+ const color = parseShadingColor(attributes, theme, {
4881
+ value: 'color',
4882
+ theme: 'themeColor',
4883
+ tint: 'themeTint',
4884
+ shade: 'themeShade'
4885
+ });
4886
+ const fill = parseShadingColor(attributes, theme, {
4887
+ value: 'fill',
4888
+ theme: 'themeFill',
4889
+ tint: 'themeFillTint',
4890
+ shade: 'themeFillShade'
4891
+ });
4892
+ if (null === color || null === fill) return null;
4893
+ return {
4894
+ pattern: pattern,
4895
+ ...color ? {
4896
+ color
4897
+ } : {},
4898
+ ...fill ? {
4899
+ fill
4900
+ } : {}
4901
+ };
4902
+ }
4903
+ function parseDirectDocxParagraphShading(properties, themeSource) {
4904
+ const candidates = work_ooxml_package_directChildren(properties, 'shd').filter(work_docx_paragraph_shading_import_isWordElement);
4905
+ if (!candidates.length) return;
4906
+ if (1 !== candidates.length) return null;
4907
+ const candidate = candidates[0];
4908
+ return candidate ? parseDocxParagraphShadingElement(candidate, themeSource) : void 0;
4909
+ }
4910
+ function resolveDocxParagraphShadingFromSources(propertySources, themeSource) {
4911
+ return resolveParagraphShading(propertySources, work_docx_theme_resolveDocxThemeResolver(themeSource)).shading;
4912
+ }
4913
+ function resolveParagraphShading(propertySources, theme) {
4914
+ let shading = null;
4915
+ let specified = false;
4916
+ let invalidCount = 0;
4917
+ let spoofedCount = 0;
4918
+ for (const properties of propertySources){
4919
+ const named = work_ooxml_package_directChildren(properties, 'shd');
4920
+ spoofedCount += named.filter((child)=>!work_docx_paragraph_shading_import_isWordElement(child)).length;
4921
+ const candidates = named.filter(work_docx_paragraph_shading_import_isWordElement);
4922
+ if (!candidates.length) continue;
4923
+ specified = true;
4924
+ if (1 !== candidates.length) {
4925
+ invalidCount += candidates.length;
4926
+ shading = {
4927
+ pattern: 'nil'
4928
+ };
4929
+ continue;
4930
+ }
4931
+ const candidate = candidates[0];
4932
+ const parsed = candidate ? parseDocxParagraphShadingElement(candidate, theme) : null;
4933
+ if (!parsed) {
4934
+ invalidCount += 1;
4935
+ shading = {
4936
+ pattern: 'nil'
4937
+ };
4938
+ continue;
4939
+ }
4940
+ shading = parsed;
4941
+ }
4942
+ return {
4943
+ shading: specified ? shading : null,
4944
+ invalidCount,
4945
+ spoofedCount
4946
+ };
4947
+ }
4948
+ function parseShadingColor(attributes, theme, names) {
4949
+ const rawValue = attributes.get(names.value)?.trim();
4950
+ const rawTheme = attributes.get(names.theme)?.trim();
4951
+ const rawTint = attributes.get(names.tint)?.trim();
4952
+ const rawShade = attributes.get(names.shade)?.trim();
4953
+ if (void 0 === rawValue && void 0 === rawTheme && void 0 === rawTint && void 0 === rawShade) return;
4954
+ const value = 'auto' === rawValue ? 'auto' : rawValue && /^[0-9a-f]{6}$/i.test(rawValue) ? `#${rawValue.toLowerCase()}` : null;
4955
+ const themeName = rawTheme && work_docx_paragraph_shading_import_WORD_THEME_COLORS.has(rawTheme) ? rawTheme : null;
4956
+ const tint = work_docx_paragraph_shading_import_byteHex(rawTint);
4957
+ const shade = work_docx_paragraph_shading_import_byteHex(rawShade);
4958
+ if (void 0 !== rawValue && !value || void 0 !== rawTheme && !themeName || void 0 !== rawTint && !tint || void 0 !== rawShade && !shade || (!themeName || 'none' === themeName) && (tint || shade)) return null;
4959
+ if (themeName && 'none' !== themeName) {
4960
+ const resolved = docxThemeColor(theme, themeName, tint, tint ? void 0 : shade);
4961
+ const fallback = value && 'auto' !== value ? value : null;
4962
+ const semanticValue = resolved ? `#${resolved}` : fallback;
4963
+ if (!semanticValue) return null;
4964
+ return {
4965
+ value: semanticValue,
4966
+ theme: work_docx_paragraph_shading_import_themeReference(themeName, semanticValue, tint, shade)
4967
+ };
4968
+ }
4969
+ if (!value) return null;
4970
+ return {
4971
+ value,
4972
+ ...'none' === themeName && 'auto' !== value ? {
4973
+ theme: work_docx_paragraph_shading_import_themeReference(themeName, value, tint, shade)
4974
+ } : {}
4975
+ };
4976
+ }
4977
+ function work_docx_paragraph_shading_import_themeReference(theme, resolved, tint, shade) {
4978
+ return {
4979
+ theme,
4980
+ resolved,
4981
+ ...tint ? {
4982
+ tint
4983
+ } : {},
4984
+ ...shade ? {
4985
+ shade
4986
+ } : {}
4987
+ };
4988
+ }
4989
+ function work_docx_paragraph_shading_import_byteHex(value) {
4990
+ if (void 0 === value) return;
4991
+ return /^[0-9a-f]{2}$/i.test(value) ? value.toUpperCase() : void 0;
4992
+ }
4993
+ function work_docx_paragraph_shading_import_wordLeafAttributes(element, allowed) {
4994
+ if (!work_docx_paragraph_shading_import_isWordElement(element) || work_ooxml_package_directChildren(element).length || Array.from(element.childNodes).some((node)=>node.nodeType === Node.TEXT_NODE && node.textContent?.trim())) return null;
4995
+ const result = new Map();
4996
+ for (const attribute of Array.from(element.attributes)){
4997
+ if (attribute.namespaceURI === work_docx_paragraph_shading_import_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:')) continue;
4998
+ const name = xmlAttributeLocalName(attribute);
4999
+ if (xmlAttributeNamespace(element, attribute) !== element.namespaceURI || !allowed.has(name) || result.has(name)) return null;
5000
+ result.set(name, attribute.value);
5001
+ }
5002
+ return result;
5003
+ }
5004
+ function applyParagraphShading(element, shading) {
5005
+ for (const [name, value] of Object.entries(documentParagraphShadingDomAttributes(shading))){
5006
+ if ('style' !== name) {
5007
+ element.setAttribute(name, value);
5008
+ continue;
5009
+ }
5010
+ for (const declaration of value.split(';')){
5011
+ const separator = declaration.indexOf(':');
5012
+ if (!(separator < 0)) element.style.setProperty(declaration.slice(0, separator).trim(), declaration.slice(separator + 1).trim());
5013
+ }
5014
+ }
5015
+ }
5016
+ function work_docx_paragraph_shading_import_insertParagraphMarker(document, paragraph, properties, marker) {
5017
+ const namespace = paragraph.namespaceURI ?? work_docx_paragraph_shading_import_WORD_NAMESPACE;
5018
+ const prefix = paragraph.prefix ?? 'w';
5019
+ const run = document.createElementNS(namespace, `${prefix}:r`);
5020
+ const text = document.createElementNS(namespace, `${prefix}:t`);
5021
+ text.setAttributeNS(work_docx_paragraph_shading_import_XML_NAMESPACE, 'xml:space', 'preserve');
5022
+ text.textContent = marker;
5023
+ run.append(text);
5024
+ paragraph.insertBefore(run, properties.nextSibling);
5025
+ }
5026
+ function work_docx_paragraph_shading_import_insertParagraphProperties(document, paragraph) {
5027
+ const namespace = paragraph.namespaceURI ?? work_docx_paragraph_shading_import_WORD_NAMESPACE;
5028
+ const prefix = paragraph.prefix ?? 'w';
5029
+ const properties = document.createElementNS(namespace, `${prefix}:pPr`);
5030
+ paragraph.insertBefore(properties, paragraph.firstChild);
5031
+ return properties;
5032
+ }
5033
+ function work_docx_paragraph_shading_import_wordDirectChildren(parent, name) {
5034
+ return work_ooxml_package_directChildren(parent, name).filter(work_docx_paragraph_shading_import_isWordElement);
5035
+ }
5036
+ function work_docx_paragraph_shading_import_isWordElement(element) {
5037
+ return DOCX_WORDPROCESSING_NAMESPACES.has(element.namespaceURI ?? '');
5038
+ }
5039
+ function work_docx_paragraph_shading_import_closestParagraphBlock(element, markerNode) {
5040
+ const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote, li');
5041
+ if (!(block instanceof HTMLElement)) return null;
5042
+ return 'li' === block.tagName.toLowerCase() ? work_docx_paragraph_shading_import_wrapListItemParagraph(block, markerNode) : block;
5043
+ }
5044
+ function work_docx_paragraph_shading_import_wrapListItemParagraph(item, markerNode) {
5045
+ let anchor = markerNode;
5046
+ while(anchor.parentNode && anchor.parentNode !== item)anchor = anchor.parentNode;
5047
+ const children = Array.from(item.childNodes);
5048
+ const anchorIndex = children.indexOf(anchor);
5049
+ if (anchorIndex < 0) return item;
5050
+ let start = anchorIndex;
5051
+ let end = anchorIndex;
5052
+ while(start > 0 && !work_docx_paragraph_shading_import_isListItemBlock(children[start - 1]))start -= 1;
5053
+ while(end + 1 < children.length && !work_docx_paragraph_shading_import_isListItemBlock(children[end + 1]))end += 1;
5054
+ const paragraph = item.ownerDocument.createElement('p');
5055
+ const grouped = children.slice(start, end + 1);
5056
+ item.insertBefore(paragraph, grouped[0] ?? null);
5057
+ paragraph.append(...grouped);
5058
+ return paragraph;
5059
+ }
5060
+ function work_docx_paragraph_shading_import_isListItemBlock(node) {
5061
+ if (!(node instanceof HTMLElement)) return false;
5062
+ return [
5063
+ 'blockquote',
5064
+ 'div',
5065
+ 'h1',
5066
+ 'h2',
5067
+ 'h3',
5068
+ 'h4',
5069
+ 'h5',
5070
+ 'h6',
5071
+ 'hr',
5072
+ 'ol',
5073
+ 'p',
5074
+ 'pre',
5075
+ 'table',
5076
+ 'ul'
5077
+ ].includes(node.tagName.toLowerCase());
5078
+ }
5079
+ function work_docx_paragraph_shading_import_textNodes(root) {
5080
+ const document = root.ownerDocument;
5081
+ const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
5082
+ const nodes = [];
5083
+ if (!walker) return nodes;
5084
+ while(walker.nextNode())nodes.push(walker.currentNode);
5085
+ return nodes;
5086
+ }
5087
+ const PAGE_BORDER_EDGE_SET = new Set(DOCUMENT_PAGE_BORDER_EDGES);
5088
+ const PAGE_BORDER_ATTRIBUTES = new Set([
5089
+ 'zOrder',
5090
+ 'display',
5091
+ 'offsetFrom'
5092
+ ]);
5093
+ const PAGE_BORDER_DISPLAYS = new Set([
5094
+ 'allPages',
5095
+ 'firstPage',
5096
+ 'notFirstPage'
5097
+ ]);
5098
+ const PAGE_BORDER_OFFSETS = new Set([
5099
+ 'page',
5100
+ 'text'
5101
+ ]);
5102
+ const PAGE_BORDER_Z_ORDERS = new Set([
5103
+ 'front',
5104
+ 'back'
5105
+ ]);
5106
+ function inspectDocxPageBorders(sectionProperties, themeSource) {
5107
+ if ('sectPr' !== sectionProperties.localName || !DOCX_WORDPROCESSING_NAMESPACES.has(sectionProperties.namespaceURI ?? '')) return {
5108
+ status: 'absent',
5109
+ invalidCount: 0,
5110
+ spoofedCount: 0
5111
+ };
5112
+ const named = work_ooxml_package_directChildren(sectionProperties, 'pgBorders');
5113
+ const candidates = named.filter((element)=>element.namespaceURI === sectionProperties.namespaceURI);
5114
+ const spoofedContainers = named.length - candidates.length;
5115
+ if (!candidates.length) return {
5116
+ status: 'absent',
5117
+ invalidCount: 0,
5118
+ spoofedCount: spoofedContainers
5119
+ };
5120
+ if (1 !== candidates.length) return {
5121
+ status: 'invalid',
5122
+ invalidCount: candidates.length,
5123
+ spoofedCount: spoofedContainers
5124
+ };
5125
+ const container = candidates[0];
5126
+ if (!container) return {
5127
+ status: 'invalid',
5128
+ invalidCount: 1,
5129
+ spoofedCount: spoofedContainers
5130
+ };
5131
+ const attributes = wordContainerAttributes(container);
5132
+ if (!attributes || hasNonWhitespaceText(container)) return {
5133
+ status: 'invalid',
5134
+ invalidCount: 1,
5135
+ spoofedCount: spoofedContainers
5136
+ };
5137
+ const display = pageBorderDisplay(attributes.get('display'));
5138
+ const offsetFrom = pageBorderOffset(attributes.get('offsetFrom'));
5139
+ const zOrder = pageBorderZOrder(attributes.get('zOrder'));
5140
+ if (null === display || null === offsetFrom || null === zOrder) return {
5141
+ status: 'invalid',
5142
+ invalidCount: 1,
5143
+ spoofedCount: spoofedContainers
5144
+ };
5145
+ const theme = work_docx_theme_resolveDocxThemeResolver(themeSource);
5146
+ const edges = {};
5147
+ let invalidCount = 0;
5148
+ let spoofedCount = spoofedContainers;
5149
+ let previousIndex = -1;
5150
+ const seen = new Set();
5151
+ for (const child of work_ooxml_package_directChildren(container)){
5152
+ if (child.namespaceURI !== container.namespaceURI) {
5153
+ spoofedCount += 1;
5154
+ continue;
5155
+ }
5156
+ const edge = child.localName;
5157
+ const index = DOCUMENT_PAGE_BORDER_EDGES.indexOf(edge);
5158
+ if (!PAGE_BORDER_EDGE_SET.has(edge) || index < previousIndex || seen.has(edge)) return {
5159
+ status: 'invalid',
5160
+ invalidCount: invalidCount + 1,
5161
+ spoofedCount
5162
+ };
5163
+ previousIndex = index;
5164
+ seen.add(edge);
5165
+ const border = parseDocxBorderElement(child, theme);
5166
+ if (border) edges[edge] = border;
5167
+ else {
5168
+ invalidCount += 1;
5169
+ edges[edge] = {
5170
+ style: 'nil'
5171
+ };
5172
+ }
5173
+ }
5174
+ const pageBorders = normalizeDocumentPageBorders({
5175
+ ...display ? {
5176
+ display
5177
+ } : {},
5178
+ ...offsetFrom ? {
5179
+ offsetFrom
5180
+ } : {},
5181
+ ...zOrder ? {
5182
+ zOrder
5183
+ } : {},
5184
+ edges
5185
+ });
5186
+ if (!pageBorders) return {
5187
+ status: 'invalid',
5188
+ invalidCount: invalidCount + 1,
5189
+ spoofedCount
5190
+ };
5191
+ return {
5192
+ status: 'valid',
5193
+ pageBorders,
5194
+ invalidCount,
5195
+ spoofedCount
5196
+ };
5197
+ }
5198
+ function parseDocxPageBorders(sectionProperties, themeSource) {
5199
+ const inspected = inspectDocxPageBorders(sectionProperties, themeSource);
5200
+ if ('absent' === inspected.status) return;
5201
+ return 'valid' === inspected.status ? inspected.pageBorders : null;
5202
+ }
5203
+ function wordContainerAttributes(element) {
5204
+ const result = new Map();
5205
+ for (const attribute of Array.from(element.attributes)){
5206
+ if (work_docx_page_borders_import_isNamespaceDeclaration(attribute)) continue;
5207
+ const name = xmlAttributeLocalName(attribute);
5208
+ if (xmlAttributeNamespace(element, attribute) !== element.namespaceURI || !PAGE_BORDER_ATTRIBUTES.has(name) || result.has(name)) return null;
5209
+ result.set(name, attribute.value);
5210
+ }
5211
+ return result;
5212
+ }
5213
+ function pageBorderDisplay(value) {
5214
+ if (void 0 === value) return;
5215
+ const normalized = value.trim();
5216
+ return PAGE_BORDER_DISPLAYS.has(normalized) ? normalized : null;
5217
+ }
5218
+ function pageBorderOffset(value) {
5219
+ if (void 0 === value) return;
5220
+ const normalized = value.trim();
5221
+ return PAGE_BORDER_OFFSETS.has(normalized) ? normalized : null;
5222
+ }
5223
+ function pageBorderZOrder(value) {
5224
+ if (void 0 === value) return;
5225
+ const normalized = value.trim();
5226
+ return PAGE_BORDER_Z_ORDERS.has(normalized) ? normalized : null;
5227
+ }
5228
+ function hasNonWhitespaceText(element) {
5229
+ return Array.from(element.childNodes).some((node)=>node.nodeType === Node.TEXT_NODE && node.textContent?.trim());
5230
+ }
5231
+ function work_docx_page_borders_import_isNamespaceDeclaration(attribute) {
5232
+ return attribute.namespaceURI === work_docx_settings_xml_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:');
5233
+ }
5234
+ const STRICT_WORDPROCESSING_NAMESPACE = 'http://purl.oclc.org/ooxml/wordprocessingml/main';
5235
+ const UNIVERSAL_TWIP_RATIOS = {
5236
+ mm: [
5237
+ 7200n,
4947
5238
  127n
4948
5239
  ],
4949
5240
  cm: [
@@ -5258,408 +5549,117 @@ function inspectDocxPaperSource(sectionProperties) {
5258
5549
  if ('valid' !== inspected.status || !inspected.element) return {
5259
5550
  status: inspected.status,
5260
5551
  invalidCount: inspected.invalidCount,
5261
- spoofedCount: inspected.spoofedCount
5262
- };
5263
- const attributes = wordAttributes(inspected.element, PAPER_SOURCE_ATTRIBUTE_SET);
5264
- if (!attributes) return invalidPaperSource(inspected.spoofedCount);
5265
- const source = {};
5266
- for (const key of [
5267
- 'first',
5268
- 'other'
5269
- ]){
5270
- const value = attributes.get(key);
5271
- if (void 0 === value) continue;
5272
- const parsed = parseBoundedDocxInteger(value, {
5273
- minimum: 0,
5274
- maximum: 65535
5275
- });
5276
- if (null === parsed) return invalidPaperSource(inspected.spoofedCount);
5277
- source[key] = parsed;
5278
- }
5279
- const paperSource = normalizeDocumentPaperSource(source);
5280
- return paperSource ? {
5281
- status: 'valid',
5282
- paperSource,
5283
- invalidCount: 0,
5284
- spoofedCount: inspected.spoofedCount
5285
- } : invalidPaperSource(inspected.spoofedCount);
5286
- }
5287
- function parseDocxPageGeometry(sectionProperties, fallback) {
5288
- const inspected = inspectDocxPageSize(sectionProperties);
5289
- if ('valid' === inspected.status) return inspected.pageGeometry;
5290
- if ('invalid' === inspected.status) return null;
5291
- return normalizeDocumentPageGeometry(fallback) ?? void 0;
5292
- }
5293
- function parseDocxPaperSource(sectionProperties, fallback) {
5294
- const inspected = inspectDocxPaperSource(sectionProperties);
5295
- if ('valid' === inspected.status) return inspected.paperSource;
5296
- if ('invalid' === inspected.status) return null;
5297
- return normalizeDocumentPaperSource(fallback) ?? void 0;
5298
- }
5299
- function inspectLeaf(parent, localName) {
5300
- const named = work_ooxml_package_directChildren(parent, localName);
5301
- const candidates = named.filter((element)=>element.namespaceURI === parent.namespaceURI);
5302
- const spoofedCount = named.length - candidates.length;
5303
- if (!candidates.length) return {
5304
- status: 'absent',
5305
- invalidCount: 0,
5306
- spoofedCount
5307
- };
5308
- if (1 !== candidates.length) return {
5309
- status: 'invalid',
5310
- invalidCount: candidates.length,
5311
- spoofedCount
5312
- };
5313
- const element = candidates[0];
5314
- if (!element || work_docx_page_size_import_hasElementOrNonWhitespaceContent(element)) return {
5315
- status: 'invalid',
5316
- invalidCount: 1,
5317
- spoofedCount
5318
- };
5319
- return {
5320
- status: 'valid',
5321
- element,
5322
- invalidCount: 0,
5323
- spoofedCount
5324
- };
5325
- }
5326
- function wordAttributes(element, allowed) {
5327
- const result = new Map();
5328
- for (const item of Array.from(element.attributes)){
5329
- if (work_docx_page_size_import_isNamespaceDeclaration(item)) continue;
5330
- const name = xmlAttributeLocalName(item);
5331
- if (xmlAttributeNamespace(element, item) !== element.namespaceURI || !allowed.has(name) || result.has(name)) return null;
5332
- result.set(name, item.value.trim());
5333
- }
5334
- return result;
5335
- }
5336
- function isWordSection(element) {
5337
- return 'sectPr' === element.localName && DOCX_WORDPROCESSING_NAMESPACES.has(element.namespaceURI ?? '');
5338
- }
5339
- function work_docx_page_size_import_hasElementOrNonWhitespaceContent(element) {
5340
- return Array.from(element.childNodes).some((node)=>node.nodeType === Node.ELEMENT_NODE || (node.nodeType === Node.TEXT_NODE || node.nodeType === Node.CDATA_SECTION_NODE) && Boolean(node.textContent?.trim()));
5341
- }
5342
- function work_docx_page_size_import_isNamespaceDeclaration(attribute) {
5343
- return attribute.namespaceURI === work_docx_settings_xml_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:');
5344
- }
5345
- function absentPageSize() {
5346
- return {
5347
- status: 'absent',
5348
- invalidCount: 0,
5349
- spoofedCount: 0
5350
- };
5351
- }
5352
- function invalidPageSize(spoofedCount) {
5353
- return {
5354
- status: 'invalid',
5355
- invalidCount: 1,
5356
- spoofedCount
5357
- };
5358
- }
5359
- function absentPaperSource() {
5360
- return {
5361
- status: 'absent',
5362
- invalidCount: 0,
5363
- spoofedCount: 0
5364
- };
5365
- }
5366
- function invalidPaperSource(spoofedCount) {
5367
- return {
5368
- status: 'invalid',
5369
- invalidCount: 1,
5370
- spoofedCount
5371
- };
5372
- }
5373
- const work_docx_paragraph_shading_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
5374
- const work_docx_paragraph_shading_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
5375
- const work_docx_paragraph_shading_import_XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/';
5376
- const PARAGRAPH_SHADING_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_SHADING_\d+__/g;
5377
- const work_docx_paragraph_shading_import_WORD_THEME_COLORS = new Set([
5378
- 'dark1',
5379
- 'light1',
5380
- 'dark2',
5381
- 'light2',
5382
- 'accent1',
5383
- 'accent2',
5384
- 'accent3',
5385
- 'accent4',
5386
- 'accent5',
5387
- 'accent6',
5388
- 'hyperlink',
5389
- 'followedHyperlink',
5390
- 'none',
5391
- 'background1',
5392
- 'text1',
5393
- 'background2',
5394
- 'text2'
5395
- ]);
5396
- const SHADING_ATTRIBUTES = new Set([
5397
- 'val',
5398
- 'color',
5399
- 'themeColor',
5400
- 'themeTint',
5401
- 'themeShade',
5402
- 'fill',
5403
- 'themeFill',
5404
- 'themeFillTint',
5405
- 'themeFillShade'
5406
- ]);
5407
- function markDocxParagraphShading(document, styleSource, themeSource, tableStyleSource) {
5408
- const paragraphs = [];
5409
- const styles = resolveDocxParagraphStyleResolver(styleSource);
5410
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
5411
- const theme = work_docx_theme_resolveDocxThemeResolver(themeSource);
5412
- let invalidCount = 0;
5413
- let spoofedCount = 0;
5414
- for (const paragraph of descendants(document, 'p')){
5415
- if (!work_docx_paragraph_shading_import_isWordElement(paragraph)) continue;
5416
- const properties = work_docx_paragraph_shading_import_wordDirectChildren(paragraph, 'pPr')[0];
5417
- const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
5418
- const resolved = resolveParagraphShading(sources, theme);
5419
- invalidCount += resolved.invalidCount;
5420
- spoofedCount += resolved.spoofedCount;
5421
- if (!resolved.shading) continue;
5422
- const marker = `__A3S_WORK_PARAGRAPH_SHADING_${paragraphs.length + 1}__`;
5423
- const writableProperties = properties ?? work_docx_paragraph_shading_import_insertParagraphProperties(document, paragraph);
5424
- work_docx_paragraph_shading_import_insertParagraphMarker(document, paragraph, writableProperties, marker);
5425
- paragraphs.push({
5426
- marker,
5427
- shading: resolved.shading
5428
- });
5429
- }
5430
- return {
5431
- paragraphs,
5432
- invalidCount,
5433
- spoofedCount
5434
- };
5435
- }
5436
- function applyImportedDocxParagraphShadingMarkers(document, markers) {
5437
- const shadingByMarker = new Map(markers.paragraphs.map(({ marker, shading })=>[
5438
- marker,
5439
- shading
5440
- ]));
5441
- for (const node of work_docx_paragraph_shading_import_textNodes(document.body))if (node.data.includes('__A3S_WORK_PARAGRAPH_SHADING_')) node.data = node.data.replace(PARAGRAPH_SHADING_MARKER_PATTERN, (marker)=>{
5442
- const shading = shadingByMarker.get(marker);
5443
- const block = shading ? work_docx_paragraph_shading_import_closestParagraphBlock(node.parentElement, node) : null;
5444
- if (block && shading) applyParagraphShading(block, shading);
5445
- return '';
5446
- });
5447
- document.body.normalize();
5448
- }
5449
- function hasImportedDocxParagraphShadingMarkers(markers) {
5450
- return markers.paragraphs.length > 0;
5451
- }
5452
- function parseDocxParagraphShadingElement(element, themeSource) {
5453
- const attributes = work_docx_paragraph_shading_import_wordLeafAttributes(element, SHADING_ATTRIBUTES);
5454
- const pattern = attributes?.get('val')?.trim();
5455
- if (!attributes || !DOCUMENT_PARAGRAPH_SHADING_PATTERNS.has(pattern)) return null;
5456
- const theme = work_docx_theme_resolveDocxThemeResolver(themeSource);
5457
- const color = parseShadingColor(attributes, theme, {
5458
- value: 'color',
5459
- theme: 'themeColor',
5460
- tint: 'themeTint',
5461
- shade: 'themeShade'
5462
- });
5463
- const fill = parseShadingColor(attributes, theme, {
5464
- value: 'fill',
5465
- theme: 'themeFill',
5466
- tint: 'themeFillTint',
5467
- shade: 'themeFillShade'
5468
- });
5469
- if (null === color || null === fill) return null;
5470
- return {
5471
- pattern: pattern,
5472
- ...color ? {
5473
- color
5474
- } : {},
5475
- ...fill ? {
5476
- fill
5477
- } : {}
5552
+ spoofedCount: inspected.spoofedCount
5478
5553
  };
5554
+ const attributes = wordAttributes(inspected.element, PAPER_SOURCE_ATTRIBUTE_SET);
5555
+ if (!attributes) return invalidPaperSource(inspected.spoofedCount);
5556
+ const source = {};
5557
+ for (const key of [
5558
+ 'first',
5559
+ 'other'
5560
+ ]){
5561
+ const value = attributes.get(key);
5562
+ if (void 0 === value) continue;
5563
+ const parsed = parseBoundedDocxInteger(value, {
5564
+ minimum: 0,
5565
+ maximum: 65535
5566
+ });
5567
+ if (null === parsed) return invalidPaperSource(inspected.spoofedCount);
5568
+ source[key] = parsed;
5569
+ }
5570
+ const paperSource = normalizeDocumentPaperSource(source);
5571
+ return paperSource ? {
5572
+ status: 'valid',
5573
+ paperSource,
5574
+ invalidCount: 0,
5575
+ spoofedCount: inspected.spoofedCount
5576
+ } : invalidPaperSource(inspected.spoofedCount);
5479
5577
  }
5480
- function parseDirectDocxParagraphShading(properties, themeSource) {
5481
- const candidates = work_ooxml_package_directChildren(properties, 'shd').filter(work_docx_paragraph_shading_import_isWordElement);
5482
- if (!candidates.length) return;
5483
- if (1 !== candidates.length) return null;
5484
- const candidate = candidates[0];
5485
- return candidate ? parseDocxParagraphShadingElement(candidate, themeSource) : void 0;
5578
+ function parseDocxPageGeometry(sectionProperties, fallback) {
5579
+ const inspected = inspectDocxPageSize(sectionProperties);
5580
+ if ('valid' === inspected.status) return inspected.pageGeometry;
5581
+ if ('invalid' === inspected.status) return null;
5582
+ return normalizeDocumentPageGeometry(fallback) ?? void 0;
5486
5583
  }
5487
- function resolveDocxParagraphShadingFromSources(propertySources, themeSource) {
5488
- return resolveParagraphShading(propertySources, work_docx_theme_resolveDocxThemeResolver(themeSource)).shading;
5584
+ function parseDocxPaperSource(sectionProperties, fallback) {
5585
+ const inspected = inspectDocxPaperSource(sectionProperties);
5586
+ if ('valid' === inspected.status) return inspected.paperSource;
5587
+ if ('invalid' === inspected.status) return null;
5588
+ return normalizeDocumentPaperSource(fallback) ?? void 0;
5489
5589
  }
5490
- function resolveParagraphShading(propertySources, theme) {
5491
- let shading = null;
5492
- let specified = false;
5493
- let invalidCount = 0;
5494
- let spoofedCount = 0;
5495
- for (const properties of propertySources){
5496
- const named = work_ooxml_package_directChildren(properties, 'shd');
5497
- spoofedCount += named.filter((child)=>!work_docx_paragraph_shading_import_isWordElement(child)).length;
5498
- const candidates = named.filter(work_docx_paragraph_shading_import_isWordElement);
5499
- if (!candidates.length) continue;
5500
- specified = true;
5501
- if (1 !== candidates.length) {
5502
- invalidCount += candidates.length;
5503
- shading = {
5504
- pattern: 'nil'
5505
- };
5506
- continue;
5507
- }
5508
- const candidate = candidates[0];
5509
- const parsed = candidate ? parseDocxParagraphShadingElement(candidate, theme) : null;
5510
- if (!parsed) {
5511
- invalidCount += 1;
5512
- shading = {
5513
- pattern: 'nil'
5514
- };
5515
- continue;
5516
- }
5517
- shading = parsed;
5518
- }
5519
- return {
5520
- shading: specified ? shading : null,
5521
- invalidCount,
5590
+ function inspectLeaf(parent, localName) {
5591
+ const named = work_ooxml_package_directChildren(parent, localName);
5592
+ const candidates = named.filter((element)=>element.namespaceURI === parent.namespaceURI);
5593
+ const spoofedCount = named.length - candidates.length;
5594
+ if (!candidates.length) return {
5595
+ status: 'absent',
5596
+ invalidCount: 0,
5522
5597
  spoofedCount
5523
5598
  };
5524
- }
5525
- function parseShadingColor(attributes, theme, names) {
5526
- const rawValue = attributes.get(names.value)?.trim();
5527
- const rawTheme = attributes.get(names.theme)?.trim();
5528
- const rawTint = attributes.get(names.tint)?.trim();
5529
- const rawShade = attributes.get(names.shade)?.trim();
5530
- if (void 0 === rawValue && void 0 === rawTheme && void 0 === rawTint && void 0 === rawShade) return;
5531
- const value = 'auto' === rawValue ? 'auto' : rawValue && /^[0-9a-f]{6}$/i.test(rawValue) ? `#${rawValue.toLowerCase()}` : null;
5532
- const themeName = rawTheme && work_docx_paragraph_shading_import_WORD_THEME_COLORS.has(rawTheme) ? rawTheme : null;
5533
- const tint = work_docx_paragraph_shading_import_byteHex(rawTint);
5534
- const shade = work_docx_paragraph_shading_import_byteHex(rawShade);
5535
- if (void 0 !== rawValue && !value || void 0 !== rawTheme && !themeName || void 0 !== rawTint && !tint || void 0 !== rawShade && !shade || (!themeName || 'none' === themeName) && (tint || shade)) return null;
5536
- if (themeName && 'none' !== themeName) {
5537
- const resolved = docxThemeColor(theme, themeName, tint, tint ? void 0 : shade);
5538
- const fallback = value && 'auto' !== value ? value : null;
5539
- const semanticValue = resolved ? `#${resolved}` : fallback;
5540
- if (!semanticValue) return null;
5541
- return {
5542
- value: semanticValue,
5543
- theme: work_docx_paragraph_shading_import_themeReference(themeName, semanticValue, tint, shade)
5544
- };
5545
- }
5546
- if (!value) return null;
5547
- return {
5548
- value,
5549
- ...'none' === themeName && 'auto' !== value ? {
5550
- theme: work_docx_paragraph_shading_import_themeReference(themeName, value, tint, shade)
5551
- } : {}
5599
+ if (1 !== candidates.length) return {
5600
+ status: 'invalid',
5601
+ invalidCount: candidates.length,
5602
+ spoofedCount
5603
+ };
5604
+ const element = candidates[0];
5605
+ if (!element || work_docx_page_size_import_hasElementOrNonWhitespaceContent(element)) return {
5606
+ status: 'invalid',
5607
+ invalidCount: 1,
5608
+ spoofedCount
5552
5609
  };
5553
- }
5554
- function work_docx_paragraph_shading_import_themeReference(theme, resolved, tint, shade) {
5555
5610
  return {
5556
- theme,
5557
- resolved,
5558
- ...tint ? {
5559
- tint
5560
- } : {},
5561
- ...shade ? {
5562
- shade
5563
- } : {}
5611
+ status: 'valid',
5612
+ element,
5613
+ invalidCount: 0,
5614
+ spoofedCount
5564
5615
  };
5565
5616
  }
5566
- function work_docx_paragraph_shading_import_byteHex(value) {
5567
- if (void 0 === value) return;
5568
- return /^[0-9a-f]{2}$/i.test(value) ? value.toUpperCase() : void 0;
5569
- }
5570
- function work_docx_paragraph_shading_import_wordLeafAttributes(element, allowed) {
5571
- if (!work_docx_paragraph_shading_import_isWordElement(element) || work_ooxml_package_directChildren(element).length || Array.from(element.childNodes).some((node)=>node.nodeType === Node.TEXT_NODE && node.textContent?.trim())) return null;
5617
+ function wordAttributes(element, allowed) {
5572
5618
  const result = new Map();
5573
- for (const attribute of Array.from(element.attributes)){
5574
- if (attribute.namespaceURI === work_docx_paragraph_shading_import_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:')) continue;
5575
- const name = xmlAttributeLocalName(attribute);
5576
- if (xmlAttributeNamespace(element, attribute) !== element.namespaceURI || !allowed.has(name) || result.has(name)) return null;
5577
- result.set(name, attribute.value);
5619
+ for (const item of Array.from(element.attributes)){
5620
+ if (work_docx_page_size_import_isNamespaceDeclaration(item)) continue;
5621
+ const name = xmlAttributeLocalName(item);
5622
+ if (xmlAttributeNamespace(element, item) !== element.namespaceURI || !allowed.has(name) || result.has(name)) return null;
5623
+ result.set(name, item.value.trim());
5578
5624
  }
5579
5625
  return result;
5580
5626
  }
5581
- function applyParagraphShading(element, shading) {
5582
- for (const [name, value] of Object.entries(documentParagraphShadingDomAttributes(shading))){
5583
- if ('style' !== name) {
5584
- element.setAttribute(name, value);
5585
- continue;
5586
- }
5587
- for (const declaration of value.split(';')){
5588
- const separator = declaration.indexOf(':');
5589
- if (!(separator < 0)) element.style.setProperty(declaration.slice(0, separator).trim(), declaration.slice(separator + 1).trim());
5590
- }
5591
- }
5592
- }
5593
- function work_docx_paragraph_shading_import_insertParagraphMarker(document, paragraph, properties, marker) {
5594
- const namespace = paragraph.namespaceURI ?? work_docx_paragraph_shading_import_WORD_NAMESPACE;
5595
- const prefix = paragraph.prefix ?? 'w';
5596
- const run = document.createElementNS(namespace, `${prefix}:r`);
5597
- const text = document.createElementNS(namespace, `${prefix}:t`);
5598
- text.setAttributeNS(work_docx_paragraph_shading_import_XML_NAMESPACE, 'xml:space', 'preserve');
5599
- text.textContent = marker;
5600
- run.append(text);
5601
- paragraph.insertBefore(run, properties.nextSibling);
5602
- }
5603
- function work_docx_paragraph_shading_import_insertParagraphProperties(document, paragraph) {
5604
- const namespace = paragraph.namespaceURI ?? work_docx_paragraph_shading_import_WORD_NAMESPACE;
5605
- const prefix = paragraph.prefix ?? 'w';
5606
- const properties = document.createElementNS(namespace, `${prefix}:pPr`);
5607
- paragraph.insertBefore(properties, paragraph.firstChild);
5608
- return properties;
5627
+ function isWordSection(element) {
5628
+ return 'sectPr' === element.localName && DOCX_WORDPROCESSING_NAMESPACES.has(element.namespaceURI ?? '');
5609
5629
  }
5610
- function work_docx_paragraph_shading_import_wordDirectChildren(parent, name) {
5611
- return work_ooxml_package_directChildren(parent, name).filter(work_docx_paragraph_shading_import_isWordElement);
5630
+ function work_docx_page_size_import_hasElementOrNonWhitespaceContent(element) {
5631
+ return Array.from(element.childNodes).some((node)=>node.nodeType === Node.ELEMENT_NODE || (node.nodeType === Node.TEXT_NODE || node.nodeType === Node.CDATA_SECTION_NODE) && Boolean(node.textContent?.trim()));
5612
5632
  }
5613
- function work_docx_paragraph_shading_import_isWordElement(element) {
5614
- return DOCX_WORDPROCESSING_NAMESPACES.has(element.namespaceURI ?? '');
5633
+ function work_docx_page_size_import_isNamespaceDeclaration(attribute) {
5634
+ return attribute.namespaceURI === work_docx_settings_xml_XMLNS_NAMESPACE || 'xmlns' === attribute.name || attribute.name.startsWith('xmlns:');
5615
5635
  }
5616
- function work_docx_paragraph_shading_import_closestParagraphBlock(element, markerNode) {
5617
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote, li');
5618
- if (!(block instanceof HTMLElement)) return null;
5619
- return 'li' === block.tagName.toLowerCase() ? work_docx_paragraph_shading_import_wrapListItemParagraph(block, markerNode) : block;
5636
+ function absentPageSize() {
5637
+ return {
5638
+ status: 'absent',
5639
+ invalidCount: 0,
5640
+ spoofedCount: 0
5641
+ };
5620
5642
  }
5621
- function work_docx_paragraph_shading_import_wrapListItemParagraph(item, markerNode) {
5622
- let anchor = markerNode;
5623
- while(anchor.parentNode && anchor.parentNode !== item)anchor = anchor.parentNode;
5624
- const children = Array.from(item.childNodes);
5625
- const anchorIndex = children.indexOf(anchor);
5626
- if (anchorIndex < 0) return item;
5627
- let start = anchorIndex;
5628
- let end = anchorIndex;
5629
- while(start > 0 && !work_docx_paragraph_shading_import_isListItemBlock(children[start - 1]))start -= 1;
5630
- while(end + 1 < children.length && !work_docx_paragraph_shading_import_isListItemBlock(children[end + 1]))end += 1;
5631
- const paragraph = item.ownerDocument.createElement('p');
5632
- const grouped = children.slice(start, end + 1);
5633
- item.insertBefore(paragraph, grouped[0] ?? null);
5634
- paragraph.append(...grouped);
5635
- return paragraph;
5643
+ function invalidPageSize(spoofedCount) {
5644
+ return {
5645
+ status: 'invalid',
5646
+ invalidCount: 1,
5647
+ spoofedCount
5648
+ };
5636
5649
  }
5637
- function work_docx_paragraph_shading_import_isListItemBlock(node) {
5638
- if (!(node instanceof HTMLElement)) return false;
5639
- return [
5640
- 'blockquote',
5641
- 'div',
5642
- 'h1',
5643
- 'h2',
5644
- 'h3',
5645
- 'h4',
5646
- 'h5',
5647
- 'h6',
5648
- 'hr',
5649
- 'ol',
5650
- 'p',
5651
- 'pre',
5652
- 'table',
5653
- 'ul'
5654
- ].includes(node.tagName.toLowerCase());
5650
+ function absentPaperSource() {
5651
+ return {
5652
+ status: 'absent',
5653
+ invalidCount: 0,
5654
+ spoofedCount: 0
5655
+ };
5655
5656
  }
5656
- function work_docx_paragraph_shading_import_textNodes(root) {
5657
- const document = root.ownerDocument;
5658
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
5659
- const nodes = [];
5660
- if (!walker) return nodes;
5661
- while(walker.nextNode())nodes.push(walker.currentNode);
5662
- return nodes;
5657
+ function invalidPaperSource(spoofedCount) {
5658
+ return {
5659
+ status: 'invalid',
5660
+ invalidCount: 1,
5661
+ spoofedCount
5662
+ };
5663
5663
  }
5664
5664
  const work_docx_paragraph_alignment_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
5665
5665
  const work_docx_paragraph_alignment_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';