docxmlater 1.3.0 → 1.3.2

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 (81) hide show
  1. package/README.md +228 -31
  2. package/dist/core/BaseManager.d.ts +21 -0
  3. package/dist/core/BaseManager.d.ts.map +1 -0
  4. package/dist/core/BaseManager.js +86 -0
  5. package/dist/core/BaseManager.js.map +1 -0
  6. package/dist/core/Document.d.ts +22 -1
  7. package/dist/core/Document.d.ts.map +1 -1
  8. package/dist/core/Document.js +453 -15
  9. package/dist/core/Document.js.map +1 -1
  10. package/dist/core/DocumentParser.d.ts +4 -0
  11. package/dist/core/DocumentParser.d.ts.map +1 -1
  12. package/dist/core/DocumentParser.js +649 -103
  13. package/dist/core/DocumentParser.js.map +1 -1
  14. package/dist/elements/Comment.d.ts +1 -0
  15. package/dist/elements/Comment.d.ts.map +1 -1
  16. package/dist/elements/Comment.js +3 -0
  17. package/dist/elements/Comment.js.map +1 -1
  18. package/dist/elements/Field.d.ts +12 -0
  19. package/dist/elements/Field.d.ts.map +1 -1
  20. package/dist/elements/Field.js +34 -1
  21. package/dist/elements/Field.js.map +1 -1
  22. package/dist/elements/FieldHelpers.d.ts +7 -0
  23. package/dist/elements/FieldHelpers.d.ts.map +1 -0
  24. package/dist/elements/FieldHelpers.js +62 -0
  25. package/dist/elements/FieldHelpers.js.map +1 -0
  26. package/dist/elements/Hyperlink.d.ts +1 -0
  27. package/dist/elements/Hyperlink.d.ts.map +1 -1
  28. package/dist/elements/Hyperlink.js +6 -1
  29. package/dist/elements/Hyperlink.js.map +1 -1
  30. package/dist/elements/Paragraph.d.ts +6 -0
  31. package/dist/elements/Paragraph.d.ts.map +1 -1
  32. package/dist/elements/Paragraph.js +48 -2
  33. package/dist/elements/Paragraph.js.map +1 -1
  34. package/dist/elements/Run.d.ts +15 -3
  35. package/dist/elements/Run.d.ts.map +1 -1
  36. package/dist/elements/Run.js +122 -54
  37. package/dist/elements/Run.js.map +1 -1
  38. package/dist/elements/Section.d.ts +9 -0
  39. package/dist/elements/Section.d.ts.map +1 -1
  40. package/dist/elements/Section.js +21 -0
  41. package/dist/elements/Section.js.map +1 -1
  42. package/dist/elements/Table.d.ts +0 -1
  43. package/dist/elements/Table.d.ts.map +1 -1
  44. package/dist/elements/Table.js +18 -21
  45. package/dist/elements/Table.js.map +1 -1
  46. package/dist/elements/TableCell.d.ts +0 -1
  47. package/dist/elements/TableCell.d.ts.map +1 -1
  48. package/dist/elements/TableCell.js +4 -16
  49. package/dist/elements/TableCell.js.map +1 -1
  50. package/dist/elements/TableOfContents.d.ts +5 -0
  51. package/dist/elements/TableOfContents.d.ts.map +1 -1
  52. package/dist/elements/TableOfContents.js +31 -4
  53. package/dist/elements/TableOfContents.js.map +1 -1
  54. package/dist/formatting/NumberingLevel.d.ts +2 -0
  55. package/dist/formatting/NumberingLevel.d.ts.map +1 -1
  56. package/dist/formatting/NumberingLevel.js +8 -0
  57. package/dist/formatting/NumberingLevel.js.map +1 -1
  58. package/dist/index.d.ts +3 -1
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +17 -3
  61. package/dist/index.js.map +1 -1
  62. package/dist/utils/diagnostics.d.ts +20 -0
  63. package/dist/utils/diagnostics.d.ts.map +1 -0
  64. package/dist/utils/diagnostics.js +80 -0
  65. package/dist/utils/diagnostics.js.map +1 -0
  66. package/dist/utils/formatting.d.ts +7 -0
  67. package/dist/utils/formatting.d.ts.map +1 -0
  68. package/dist/utils/formatting.js +93 -0
  69. package/dist/utils/formatting.js.map +1 -0
  70. package/dist/utils/validation.d.ts +1 -0
  71. package/dist/utils/validation.d.ts.map +1 -1
  72. package/dist/utils/validation.js +12 -0
  73. package/dist/utils/validation.js.map +1 -1
  74. package/dist/xml/XMLBuilder.d.ts +25 -0
  75. package/dist/xml/XMLBuilder.d.ts.map +1 -1
  76. package/dist/xml/XMLBuilder.js +72 -0
  77. package/dist/xml/XMLBuilder.js.map +1 -1
  78. package/dist/xml/XMLParser.d.ts.map +1 -1
  79. package/dist/xml/XMLParser.js +15 -0
  80. package/dist/xml/XMLParser.js.map +1 -1
  81. package/package.json +1 -1
@@ -46,6 +46,7 @@ const HeaderFooterManager_1 = require("../elements/HeaderFooterManager");
46
46
  const TableOfContents_1 = require("../elements/TableOfContents");
47
47
  const TableOfContentsElement_1 = require("../elements/TableOfContentsElement");
48
48
  const Bookmark_1 = require("../elements/Bookmark");
49
+ const StructuredDocumentTag_1 = require("../elements/StructuredDocumentTag");
49
50
  const BookmarkManager_1 = require("../elements/BookmarkManager");
50
51
  const Revision_1 = require("../elements/Revision");
51
52
  const RevisionManager_1 = require("../elements/RevisionManager");
@@ -221,6 +222,52 @@ class Document {
221
222
  getTables() {
222
223
  return this.bodyElements.filter((el) => el instanceof Table_1.Table);
223
224
  }
225
+ getAllParagraphs() {
226
+ const result = [];
227
+ for (const element of this.bodyElements) {
228
+ if (element instanceof Paragraph_1.Paragraph) {
229
+ result.push(element);
230
+ }
231
+ else if (element instanceof Table_1.Table) {
232
+ for (const row of element.getRows()) {
233
+ for (const cell of row.getCells()) {
234
+ result.push(...cell.getParagraphs());
235
+ }
236
+ }
237
+ }
238
+ else if (element instanceof StructuredDocumentTag_1.StructuredDocumentTag) {
239
+ for (const content of element.getContent()) {
240
+ if (content instanceof Paragraph_1.Paragraph) {
241
+ result.push(content);
242
+ }
243
+ else if (content instanceof Table_1.Table) {
244
+ for (const row of content.getRows()) {
245
+ for (const cell of row.getCells()) {
246
+ result.push(...cell.getParagraphs());
247
+ }
248
+ }
249
+ }
250
+ }
251
+ }
252
+ }
253
+ return result;
254
+ }
255
+ getAllTables() {
256
+ const result = [];
257
+ for (const element of this.bodyElements) {
258
+ if (element instanceof Table_1.Table) {
259
+ result.push(element);
260
+ }
261
+ else if (element instanceof StructuredDocumentTag_1.StructuredDocumentTag) {
262
+ for (const content of element.getContent()) {
263
+ if (content instanceof Table_1.Table) {
264
+ result.push(content);
265
+ }
266
+ }
267
+ }
268
+ }
269
+ return result;
270
+ }
224
271
  getTableOfContentsElements() {
225
272
  return this.bodyElements.filter((el) => el instanceof TableOfContentsElement_1.TableOfContentsElement);
226
273
  }
@@ -598,6 +645,13 @@ class Document {
598
645
  }
599
646
  return tables.length;
600
647
  }
648
+ applyBordersToAllTables(border) {
649
+ const tables = this.getAllTables();
650
+ for (const table of tables) {
651
+ table.setAllBorders(border);
652
+ }
653
+ return tables.length;
654
+ }
601
655
  applyTableFormattingBySize(singleCellShading, multiCellFirstRowShading) {
602
656
  const tables = this.getTables();
603
657
  let singleCellCount = 0;
@@ -872,6 +926,341 @@ class Document {
872
926
  normalizeAllListIndentation() {
873
927
  return this.numberingManager.normalizeAllListIndentation();
874
928
  }
929
+ applyStandardListFormatting() {
930
+ const instances = this.numberingManager.getAllInstances();
931
+ let count = 0;
932
+ for (const instance of instances) {
933
+ const abstractNumId = instance.getAbstractNumId();
934
+ const abstractNum = this.numberingManager.getAbstractNumbering(abstractNumId);
935
+ if (!abstractNum)
936
+ continue;
937
+ const level0 = abstractNum.getLevel(0);
938
+ if (!level0 || level0.getFormat() !== 'bullet')
939
+ continue;
940
+ for (let levelIndex = 0; levelIndex < 9; levelIndex++) {
941
+ const numLevel = abstractNum.getLevel(levelIndex);
942
+ if (!numLevel)
943
+ continue;
944
+ const bullet = levelIndex % 2 === 0 ? '•' : '○';
945
+ numLevel.setText(bullet);
946
+ numLevel.setFont('Arial');
947
+ numLevel.setFontSize(24);
948
+ numLevel.setLeftIndent(720 * (levelIndex + 1));
949
+ numLevel.setHangingIndent(360);
950
+ }
951
+ this.applyFormattingToListParagraphs(instance.getNumId());
952
+ count++;
953
+ }
954
+ return count;
955
+ }
956
+ applyFormattingToListParagraphs(numId) {
957
+ const paragraphs = this.getAllParagraphs();
958
+ for (const para of paragraphs) {
959
+ const numbering = para.getNumbering();
960
+ if (numbering?.numId === numId) {
961
+ const runs = para.getRuns();
962
+ for (const run of runs) {
963
+ run.setFont('Verdana', 12);
964
+ }
965
+ para.setSpaceBefore(0);
966
+ para.setSpaceAfter(60);
967
+ para.setContextualSpacing(true);
968
+ para.formatting.indentation = undefined;
969
+ }
970
+ }
971
+ }
972
+ isParagraphInTable(para) {
973
+ const allTables = this.getAllTables();
974
+ for (const table of allTables) {
975
+ for (const row of table.getRows()) {
976
+ for (const cell of row.getCells()) {
977
+ const cellParas = cell.getParagraphs();
978
+ for (const cellPara of cellParas) {
979
+ if (cellPara === para) {
980
+ return { inTable: true, cell };
981
+ }
982
+ }
983
+ }
984
+ }
985
+ }
986
+ return { inTable: false };
987
+ }
988
+ wrapParagraphInTable(para, options) {
989
+ const paraIndex = this.bodyElements.indexOf(para);
990
+ if (paraIndex === -1) {
991
+ throw new Error('Paragraph not found in document body elements');
992
+ }
993
+ const table = new Table_1.Table(1, 1);
994
+ const cell = table.getCell(0, 0);
995
+ if (!cell) {
996
+ throw new Error('Failed to get cell from newly created table');
997
+ }
998
+ this.bodyElements.splice(paraIndex, 1);
999
+ cell.addParagraph(para);
1000
+ if (options.shading) {
1001
+ cell.setShading({ fill: options.shading });
1002
+ }
1003
+ if (options.marginTop !== undefined || options.marginBottom !== undefined ||
1004
+ options.marginLeft !== undefined || options.marginRight !== undefined) {
1005
+ cell.setMargins({
1006
+ top: options.marginTop ?? 100,
1007
+ bottom: options.marginBottom ?? 100,
1008
+ left: options.marginLeft ?? 100,
1009
+ right: options.marginRight ?? 100
1010
+ });
1011
+ }
1012
+ if (options.tableWidthPercent !== undefined) {
1013
+ table.setWidth(options.tableWidthPercent);
1014
+ table.setWidthType('pct');
1015
+ }
1016
+ this.bodyElements.splice(paraIndex, 0, table);
1017
+ return table;
1018
+ }
1019
+ applyCustomStylesToDocument() {
1020
+ const counts = { heading1: 0, heading2: 0, normal: 0 };
1021
+ const header1Style = Style_1.Style.create({
1022
+ styleId: 'CustomHeader1',
1023
+ name: 'Custom Header 1',
1024
+ type: 'paragraph',
1025
+ basedOn: 'Normal',
1026
+ runFormatting: {
1027
+ font: 'Verdana',
1028
+ size: 18,
1029
+ bold: true,
1030
+ color: '000000'
1031
+ },
1032
+ paragraphFormatting: {
1033
+ alignment: 'left',
1034
+ spacing: {
1035
+ before: 0,
1036
+ after: 240
1037
+ }
1038
+ }
1039
+ });
1040
+ const header2Style = Style_1.Style.create({
1041
+ styleId: 'CustomHeader2',
1042
+ name: 'Custom Header 2',
1043
+ type: 'paragraph',
1044
+ basedOn: 'Normal',
1045
+ runFormatting: {
1046
+ font: 'Verdana',
1047
+ size: 14,
1048
+ bold: true,
1049
+ color: '000000'
1050
+ },
1051
+ paragraphFormatting: {
1052
+ alignment: 'left',
1053
+ spacing: {
1054
+ before: 120,
1055
+ after: 120
1056
+ }
1057
+ }
1058
+ });
1059
+ const normalStyle = Style_1.Style.create({
1060
+ styleId: 'CustomNormal',
1061
+ name: 'Custom Normal',
1062
+ type: 'paragraph',
1063
+ basedOn: 'Normal',
1064
+ runFormatting: {
1065
+ font: 'Verdana',
1066
+ size: 12,
1067
+ color: '000000'
1068
+ },
1069
+ paragraphFormatting: {
1070
+ alignment: 'left',
1071
+ spacing: {
1072
+ before: 60,
1073
+ after: 60
1074
+ }
1075
+ }
1076
+ });
1077
+ this.addStyle(header1Style);
1078
+ this.addStyle(header2Style);
1079
+ this.addStyle(normalStyle);
1080
+ const allParagraphs = this.getAllParagraphs();
1081
+ for (const para of allParagraphs) {
1082
+ const currentStyle = para.getStyle();
1083
+ if (currentStyle === 'Heading1' || currentStyle === 'Heading 1') {
1084
+ para.setStyle('CustomHeader1');
1085
+ counts.heading1++;
1086
+ }
1087
+ else if (currentStyle === 'Heading2' || currentStyle === 'Heading 2') {
1088
+ para.setStyle('CustomHeader2');
1089
+ const { inTable, cell } = this.isParagraphInTable(para);
1090
+ if (inTable && cell) {
1091
+ cell.setShading({ fill: 'BFBFBF' });
1092
+ cell.setMargins({ top: 0, bottom: 0, left: 101, right: 101 });
1093
+ const table = this.getAllTables().find(t => {
1094
+ for (const row of t.getRows()) {
1095
+ for (const c of row.getCells()) {
1096
+ if (c === cell)
1097
+ return true;
1098
+ }
1099
+ }
1100
+ return false;
1101
+ });
1102
+ if (table) {
1103
+ table.setWidth(5000);
1104
+ table.setWidthType('pct');
1105
+ }
1106
+ }
1107
+ else {
1108
+ this.wrapParagraphInTable(para, {
1109
+ shading: 'BFBFBF',
1110
+ marginTop: 0,
1111
+ marginBottom: 0,
1112
+ marginLeft: 101,
1113
+ marginRight: 101,
1114
+ tableWidthPercent: 5000
1115
+ });
1116
+ }
1117
+ counts.heading2++;
1118
+ }
1119
+ else if (currentStyle === 'Normal' || currentStyle === undefined) {
1120
+ para.setStyle('CustomNormal');
1121
+ counts.normal++;
1122
+ }
1123
+ }
1124
+ return counts;
1125
+ }
1126
+ applyCustomFormattingToExistingStyles() {
1127
+ const results = { heading1: false, heading2: false, normal: false };
1128
+ const heading1 = this.stylesManager.getStyle('Heading1');
1129
+ const heading2 = this.stylesManager.getStyle('Heading2');
1130
+ const normal = this.stylesManager.getStyle('Normal');
1131
+ if (heading1) {
1132
+ heading1.setRunFormatting({
1133
+ font: 'Verdana',
1134
+ size: 18,
1135
+ bold: true,
1136
+ color: '000000'
1137
+ });
1138
+ heading1.setParagraphFormatting({
1139
+ alignment: 'left',
1140
+ spacing: { before: 0, after: 240, line: 240, lineRule: 'auto' }
1141
+ });
1142
+ results.heading1 = true;
1143
+ }
1144
+ if (heading2) {
1145
+ heading2.setRunFormatting({
1146
+ font: 'Verdana',
1147
+ size: 14,
1148
+ bold: true,
1149
+ color: '000000'
1150
+ });
1151
+ heading2.setParagraphFormatting({
1152
+ alignment: 'left',
1153
+ spacing: { before: 120, after: 120, line: 240, lineRule: 'auto' }
1154
+ });
1155
+ results.heading2 = true;
1156
+ }
1157
+ if (normal) {
1158
+ normal.setRunFormatting({
1159
+ font: 'Verdana',
1160
+ size: 12,
1161
+ color: '000000'
1162
+ });
1163
+ normal.setParagraphFormatting({
1164
+ alignment: 'left',
1165
+ spacing: { before: 60, after: 60, line: 240, lineRule: 'auto' }
1166
+ });
1167
+ results.normal = true;
1168
+ }
1169
+ const processedParagraphs = new Set();
1170
+ const allParas = this.getAllParagraphs();
1171
+ for (const para of allParas) {
1172
+ if (processedParagraphs.has(para)) {
1173
+ continue;
1174
+ }
1175
+ const styleId = para.getStyle();
1176
+ if (styleId === 'Heading1' && heading1) {
1177
+ para.clearDirectFormattingConflicts(heading1);
1178
+ for (const run of para.getRuns()) {
1179
+ run.setItalic(false);
1180
+ run.setUnderline(false);
1181
+ }
1182
+ if (para.formatting.paragraphMarkRunProperties) {
1183
+ delete para.formatting.paragraphMarkRunProperties.italic;
1184
+ delete para.formatting.paragraphMarkRunProperties.underline;
1185
+ }
1186
+ processedParagraphs.add(para);
1187
+ }
1188
+ else if (styleId === 'Heading2' && heading2) {
1189
+ const hasContent = para.getRuns().some(run => run.getText().trim().length > 0);
1190
+ if (!hasContent) {
1191
+ processedParagraphs.add(para);
1192
+ continue;
1193
+ }
1194
+ para.clearDirectFormattingConflicts(heading2);
1195
+ for (const run of para.getRuns()) {
1196
+ run.setItalic(false);
1197
+ run.setUnderline(false);
1198
+ }
1199
+ if (para.formatting.paragraphMarkRunProperties) {
1200
+ delete para.formatting.paragraphMarkRunProperties.italic;
1201
+ delete para.formatting.paragraphMarkRunProperties.underline;
1202
+ }
1203
+ const { inTable, cell } = this.isParagraphInTable(para);
1204
+ if (inTable && cell) {
1205
+ cell.setShading({ fill: 'BFBFBF' });
1206
+ cell.setMargins({ top: 0, bottom: 0, left: 101, right: 101 });
1207
+ const table = this.getAllTables().find(t => {
1208
+ for (const row of t.getRows()) {
1209
+ for (const c of row.getCells()) {
1210
+ if (c === cell)
1211
+ return true;
1212
+ }
1213
+ }
1214
+ return false;
1215
+ });
1216
+ if (table) {
1217
+ table.setWidth(5000);
1218
+ table.setWidthType('pct');
1219
+ }
1220
+ }
1221
+ else {
1222
+ this.wrapParagraphInTable(para, {
1223
+ shading: 'BFBFBF',
1224
+ marginTop: 0,
1225
+ marginBottom: 0,
1226
+ marginLeft: 101,
1227
+ marginRight: 101,
1228
+ tableWidthPercent: 5000
1229
+ });
1230
+ }
1231
+ processedParagraphs.add(para);
1232
+ }
1233
+ else if ((styleId === 'Normal' || styleId === undefined) && normal) {
1234
+ para.clearDirectFormattingConflicts(normal);
1235
+ for (const run of para.getRuns()) {
1236
+ run.setItalic(false);
1237
+ run.setUnderline(false);
1238
+ }
1239
+ if (para.formatting.paragraphMarkRunProperties) {
1240
+ delete para.formatting.paragraphMarkRunProperties.italic;
1241
+ delete para.formatting.paragraphMarkRunProperties.underline;
1242
+ }
1243
+ processedParagraphs.add(para);
1244
+ }
1245
+ }
1246
+ const hyperlinks = this.getHyperlinks();
1247
+ for (const { hyperlink, paragraph } of hyperlinks) {
1248
+ const text = hyperlink.getText().trim();
1249
+ if (text === 'Top of the Document' || text === 'Top of Document') {
1250
+ paragraph.setAlignment('right');
1251
+ paragraph.setSpaceBefore(0);
1252
+ paragraph.setSpaceAfter(0);
1253
+ }
1254
+ }
1255
+ this.updateAllHyperlinkColors('0000FF');
1256
+ const tocElements = this.getTableOfContentsElements();
1257
+ for (const tocElement of tocElements) {
1258
+ const toc = tocElement.getTableOfContents();
1259
+ toc.setShowPageNumbers(false);
1260
+ toc.setHideInWebLayout(true);
1261
+ }
1262
+ return results;
1263
+ }
875
1264
  getSection() {
876
1265
  return this.section;
877
1266
  }
@@ -1676,33 +2065,80 @@ class Document {
1676
2065
  }
1677
2066
  return false;
1678
2067
  }
1679
- insertParagraphAt(index, paragraph) {
2068
+ validateParagraph(paragraph) {
2069
+ if (!(paragraph instanceof Paragraph_1.Paragraph)) {
2070
+ throw new Error('insertParagraphAt: parameter must be a Paragraph instance');
2071
+ }
2072
+ const paraId = paragraph.getFormatting().paraId;
2073
+ if (paraId) {
2074
+ const existingIds = this.bodyElements
2075
+ .filter((el) => el instanceof Paragraph_1.Paragraph)
2076
+ .map(p => p.getFormatting().paraId)
2077
+ .filter(id => id === paraId);
2078
+ if (existingIds.length > 0) {
2079
+ throw new Error(`Duplicate paragraph ID detected: ${paraId}. Each paragraph must have a unique ID.`);
2080
+ }
2081
+ }
2082
+ const style = paragraph.getFormatting().style;
2083
+ if (style && !this.stylesManager.hasStyle(style)) {
2084
+ console.warn(`Style "${style}" not found in document. Paragraph will fall back to Normal style.`);
2085
+ }
2086
+ const numbering = paragraph.getFormatting().numbering;
2087
+ if (numbering && !this.numberingManager.hasInstance(numbering.numId)) {
2088
+ console.warn(`Numbering ID ${numbering.numId} not found in document. List formatting will not display.`);
2089
+ }
2090
+ }
2091
+ validateTable(table) {
2092
+ if (!(table instanceof Table_1.Table)) {
2093
+ throw new Error('insertTableAt: parameter must be a Table instance');
2094
+ }
2095
+ const rows = table.getRows();
2096
+ if (rows.length === 0) {
2097
+ throw new Error('insertTableAt: table must have at least one row');
2098
+ }
2099
+ const firstRow = rows[0];
2100
+ if (firstRow && firstRow.getCells().length === 0) {
2101
+ throw new Error('insertTableAt: table rows must have at least one cell');
2102
+ }
2103
+ const tableStyle = table.getFormatting().style;
2104
+ if (tableStyle && !this.stylesManager.hasStyle(tableStyle)) {
2105
+ console.warn(`Table style "${tableStyle}" not found in document. Table will use default formatting.`);
2106
+ }
2107
+ }
2108
+ validateToc(toc) {
2109
+ if (!(toc instanceof TableOfContentsElement_1.TableOfContentsElement)) {
2110
+ throw new Error('insertTocAt: parameter must be a TableOfContentsElement instance');
2111
+ }
2112
+ const hasHeadings = ['Heading1', 'Heading2', 'Heading3', 'Heading4', 'Heading5', 'Heading6', 'Heading7', 'Heading8', 'Heading9']
2113
+ .some(style => this.stylesManager.hasStyle(style));
2114
+ if (!hasHeadings) {
2115
+ console.warn('No heading styles found in document. Table of Contents may not display entries correctly.');
2116
+ }
2117
+ }
2118
+ normalizeIndex(index) {
1680
2119
  if (index < 0) {
1681
- index = 0;
2120
+ return 0;
1682
2121
  }
1683
2122
  else if (index > this.bodyElements.length) {
1684
- index = this.bodyElements.length;
2123
+ return this.bodyElements.length;
1685
2124
  }
2125
+ return index;
2126
+ }
2127
+ insertParagraphAt(index, paragraph) {
2128
+ this.validateParagraph(paragraph);
2129
+ index = this.normalizeIndex(index);
1686
2130
  this.bodyElements.splice(index, 0, paragraph);
1687
2131
  return this;
1688
2132
  }
1689
2133
  insertTableAt(index, table) {
1690
- if (index < 0) {
1691
- index = 0;
1692
- }
1693
- else if (index > this.bodyElements.length) {
1694
- index = this.bodyElements.length;
1695
- }
2134
+ this.validateTable(table);
2135
+ index = this.normalizeIndex(index);
1696
2136
  this.bodyElements.splice(index, 0, table);
1697
2137
  return this;
1698
2138
  }
1699
2139
  insertTocAt(index, toc) {
1700
- if (index < 0) {
1701
- index = 0;
1702
- }
1703
- else if (index > this.bodyElements.length) {
1704
- index = this.bodyElements.length;
1705
- }
2140
+ this.validateToc(toc);
2141
+ index = this.normalizeIndex(index);
1706
2142
  this.bodyElements.splice(index, 0, toc);
1707
2143
  return this;
1708
2144
  }
@@ -1710,6 +2146,7 @@ class Document {
1710
2146
  if (index >= 0 && index < this.bodyElements.length) {
1711
2147
  const element = this.bodyElements[index];
1712
2148
  if (element instanceof Paragraph_1.Paragraph) {
2149
+ this.validateParagraph(paragraph);
1713
2150
  this.bodyElements[index] = paragraph;
1714
2151
  return true;
1715
2152
  }
@@ -1720,6 +2157,7 @@ class Document {
1720
2157
  if (index >= 0 && index < this.bodyElements.length) {
1721
2158
  const element = this.bodyElements[index];
1722
2159
  if (element instanceof Table_1.Table) {
2160
+ this.validateTable(table);
1723
2161
  this.bodyElements[index] = table;
1724
2162
  return true;
1725
2163
  }