@docx4j/core-ts 0.3.1 → 0.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 (30) hide show
  1. package/dist/model/content/Body.mjs +2 -1
  2. package/dist/model/content/ContentControl.d.mts +5 -1
  3. package/dist/model/content/ContentControl.mjs +18 -5
  4. package/dist/model/content/Paragraph.d.mts +11 -11
  5. package/dist/model/content/Paragraph.mjs +30 -43
  6. package/dist/model/content/Range.mjs +3 -2
  7. package/dist/model/content/stylesFacade.mjs +7 -4
  8. package/dist/model/content/tracking.d.mts +14 -0
  9. package/dist/model/content/tracking.mjs +19 -10
  10. package/dist/model/customxml/XmlMapping.d.mts +20 -4
  11. package/dist/model/customxml/XmlMapping.mjs +44 -6
  12. package/dist/model/customxml/bindings.d.mts +9 -2
  13. package/dist/model/customxml/bindings.mjs +18 -2
  14. package/dist/model/customxml/index.d.mts +1 -1
  15. package/dist/model/customxml/index.mjs +1 -1
  16. package/dist/model/customxml/insert.d.mts +10 -0
  17. package/dist/model/customxml/insert.mjs +13 -0
  18. package/dist/model/fonts/RunFontSelector.d.mts +8 -1
  19. package/dist/model/fonts/RunFontSelector.mjs +6 -5
  20. package/dist/model/properties/PropertyResolver.d.mts +110 -10
  21. package/dist/model/properties/PropertyResolver.mjs +300 -31
  22. package/dist/model/properties/index.d.mts +3 -0
  23. package/dist/model/properties/index.mjs +2 -0
  24. package/dist/model/properties/table.d.mts +155 -0
  25. package/dist/model/properties/table.mjs +315 -0
  26. package/dist/model/properties/tableStyleConditions.d.mts +101 -0
  27. package/dist/model/properties/tableStyleConditions.mjs +296 -0
  28. package/dist/parts/wml/placeholderStyle.d.mts +3 -0
  29. package/dist/parts/wml/placeholderStyle.mjs +13 -0
  30. package/package.json +2 -2
@@ -0,0 +1,296 @@
1
+ import { log } from './log.mjs';
2
+ import { isEmptyPPr, isEmptyRPr } from './styleUtil.mjs';
3
+ /**
4
+ * The order in which a table style's conditional formats are applied to a cell, later
5
+ * overriding earlier (ECMA-376-1 17.7.6).
6
+ */
7
+ export const PRECEDENCE = [
8
+ 'wholeTable',
9
+ 'band1Vert', 'band2Vert',
10
+ 'band1Horz', 'band2Horz',
11
+ 'firstCol', 'lastCol',
12
+ 'firstRow', 'lastRow',
13
+ 'nwCell', 'neCell',
14
+ 'swCell', 'seCell',
15
+ ];
16
+ /** The conditions `w:cnfStyle` carries on a row: the row-axis bits. */
17
+ const ROW_AXIS = new Set(['firstRow', 'lastRow', 'band1Horz', 'band2Horz']);
18
+ /** The conditions `w:cnfStyle` carries on a cell: the column-axis bits. */
19
+ const COLUMN_AXIS = new Set(['firstCol', 'lastCol', 'band1Vert', 'band2Vert']);
20
+ /**
21
+ * What Word assumes where a table states no `w:tblLook` at all: its own default, `04A0` - first
22
+ * row, first column and row banding on, column banding off.
23
+ */
24
+ export const DEFAULT_LOOK = Object.freeze({
25
+ firstRow: true, lastRow: false, firstColumn: true, lastColumn: false, hBand: true, vBand: false,
26
+ });
27
+ function on(v) {
28
+ return v === '1' || v === 'true' || v === 'on';
29
+ }
30
+ /**
31
+ * The look a `w:tblLook` states. The six attributes win where any of them is present; otherwise
32
+ * the legacy `w:val` bitmask (0x0020 firstRow, 0x0040 lastRow, 0x0080 firstColumn, 0x0100
33
+ * lastColumn, 0x0200 noHBand, 0x0400 noVBand); none, or an empty element, is {@link DEFAULT_LOOK}.
34
+ */
35
+ export function lookOf(tblLook) {
36
+ if (tblLook === undefined)
37
+ return DEFAULT_LOOK;
38
+ if (tblLook.firstRow !== undefined || tblLook.lastRow !== undefined
39
+ || tblLook.firstColumn !== undefined || tblLook.lastColumn !== undefined
40
+ || tblLook.noHBand !== undefined || tblLook.noVBand !== undefined) {
41
+ return {
42
+ firstRow: on(tblLook.firstRow), lastRow: on(tblLook.lastRow),
43
+ firstColumn: on(tblLook.firstColumn), lastColumn: on(tblLook.lastColumn),
44
+ hBand: !on(tblLook.noHBand), vBand: !on(tblLook.noVBand),
45
+ };
46
+ }
47
+ const val = tblLook.val;
48
+ if (val === undefined || val.trim().length === 0)
49
+ return DEFAULT_LOOK;
50
+ const text = val.trim();
51
+ // Integer.parseInt(s, 16): an optional sign, then hex digits only
52
+ if (!/^[+-]?[0-9a-fA-F]+$/.test(text)) {
53
+ log.warn(`Unreadable w:tblLook/@w:val '${val}'; using Word's default`);
54
+ return DEFAULT_LOOK;
55
+ }
56
+ const bits = parseInt(text, 16);
57
+ return {
58
+ firstRow: (bits & 0x0020) !== 0, lastRow: (bits & 0x0040) !== 0,
59
+ firstColumn: (bits & 0x0080) !== 0, lastColumn: (bits & 0x0100) !== 0,
60
+ hBand: (bits & 0x0200) === 0, vBand: (bits & 0x0400) === 0,
61
+ };
62
+ }
63
+ /** Whether a look lets the condition through at all. */
64
+ export function lookAllows(look, t) {
65
+ switch (t) {
66
+ case 'wholeTable': return true;
67
+ case 'firstRow': return look.firstRow;
68
+ case 'lastRow': return look.lastRow;
69
+ case 'firstCol': return look.firstColumn;
70
+ case 'lastCol': return look.lastColumn;
71
+ case 'band1Horz':
72
+ case 'band2Horz': return look.hBand;
73
+ case 'band1Vert':
74
+ case 'band2Vert': return look.vBand;
75
+ case 'nwCell': return look.firstRow && look.firstColumn;
76
+ case 'neCell': return look.firstRow && look.lastColumn;
77
+ case 'swCell': return look.lastRow && look.firstColumn;
78
+ case 'seCell': return look.lastRow && look.lastColumn;
79
+ default: return false;
80
+ }
81
+ }
82
+ function atLeastOne(v) {
83
+ return v === undefined || !(v >= 1) ? 1 : Math.trunc(v);
84
+ }
85
+ /**
86
+ * `w:tblStyleRowBandSize`, at least 1 where it is stated, and 0 where it is not: Word bands no
87
+ * row of a table whose style chain and own `w:tblPr` state no band size, whatever its `w:tblLook`
88
+ * and the style's band conditions (measured, docx4j CR-030 probes T9 and T10, D11: Word's re-save
89
+ * writes no band bit, and its PDF has no band formatting; a size stated by the table alone is
90
+ * enough). Until docx4j 0349796f9 an absent size was taken as 1.
91
+ */
92
+ export function rowBandSize(tblPr) {
93
+ return (tblPr === null || tblPr === void 0 ? void 0 : tblPr.tblStyleRowBandSize) === undefined ? 0 : atLeastOne(tblPr.tblStyleRowBandSize.val);
94
+ }
95
+ /**
96
+ * `w:tblStyleColBandSize`, as {@link rowBandSize} for the columns: 0, no vertical banding, where
97
+ * it is not stated. (By the rows' rule; the columns themselves were not probed in Word.)
98
+ */
99
+ export function colBandSize(tblPr) {
100
+ return (tblPr === null || tblPr === void 0 ? void 0 : tblPr.tblStyleColBandSize) === undefined ? 0 : atLeastOne(tblPr.tblStyleColBandSize.val);
101
+ }
102
+ /** The bit order of a `w:cnfStyle` value (ECMA-376-1 17.18.6). */
103
+ const CNF_BITS = [
104
+ 'firstRow', 'lastRow', 'firstCol', 'lastCol',
105
+ 'band1Vert', 'band2Vert', 'band1Horz', 'band2Horz',
106
+ 'nwCell', 'neCell', 'swCell', 'seCell',
107
+ ];
108
+ /**
109
+ * The conditions a `w:cnfStyle` bitmask names: twelve characters, firstRow, lastRow,
110
+ * firstColumn, lastColumn, oddVBand, evenVBand, oddHBand, evenHBand, then the corners nw, ne,
111
+ * sw, se. A short string is read as far as it goes; none is the empty set.
112
+ */
113
+ export function fromCnf(cnf) {
114
+ const out = new Set();
115
+ const val = typeof cnf === 'string' ? cnf : cnf === null || cnf === void 0 ? void 0 : cnf.val;
116
+ if (val === undefined || val === null)
117
+ return out;
118
+ const s = String(val).trim();
119
+ for (let i = 0; i < CNF_BITS.length && i < s.length; i++) {
120
+ if (s.charAt(i) === '1')
121
+ out.add(CNF_BITS[i]);
122
+ }
123
+ return out;
124
+ }
125
+ /** The `w:cnfStyle` of a row, or undefined where the row states none. */
126
+ export function rowCnf(trPr) {
127
+ var _a, _b;
128
+ for (const item of ((_a = trPr === null || trPr === void 0 ? void 0 : trPr.cnfStyleOrDivIdOrGridBefore) !== null && _a !== void 0 ? _a : [])) {
129
+ if (((_b = item === null || item === void 0 ? void 0 : item.name) === null || _b === void 0 ? void 0 : _b.localPart) === 'cnfStyle' && typeof item.value === 'object' && item.value !== null) {
130
+ return item.value;
131
+ }
132
+ }
133
+ return undefined;
134
+ }
135
+ function addCorners(s) {
136
+ const fr = s.has('firstRow');
137
+ const lr = s.has('lastRow');
138
+ const fc = s.has('firstCol');
139
+ const lc = s.has('lastCol');
140
+ if (fr && fc)
141
+ s.add('nwCell');
142
+ if (fr && lc)
143
+ s.add('neCell');
144
+ if (lr && fc)
145
+ s.add('swCell');
146
+ if (lr && lc)
147
+ s.add('seCell');
148
+ }
149
+ /** Remove the conditions the look does not allow (in place), and return the set. */
150
+ export function gate(conditions, look = DEFAULT_LOOK) {
151
+ for (const t of [...conditions])
152
+ if (!lookAllows(look, t))
153
+ conditions.delete(t);
154
+ return conditions;
155
+ }
156
+ /**
157
+ * The conditions a cell is under by its position alone, with no `w:cnfStyle` to go by.
158
+ *
159
+ * Banding counts from the first row (column) which is not under a condition of its own: where
160
+ * the look has `firstRow` on, row 1 is the first banded row; a last row (column) the look gives
161
+ * its own condition is left out of the bands too. A band is `w:tblStyleRowBandSize` rows deep,
162
+ * and a size of 0 (none stated anywhere, see {@link rowBandSize}) bands nothing.
163
+ * A cell spanning several columns is placed by its first column, and is in the last column
164
+ * where its span reaches it.
165
+ */
166
+ export function atPosition(look, rowBand, colBand, row, rowCount, col, colSpan, colCount) {
167
+ const out = new Set();
168
+ const first = look.firstRow && row === 0;
169
+ const last = look.lastRow && rowCount > 0 && row === rowCount - 1;
170
+ if (first)
171
+ out.add('firstRow');
172
+ if (last)
173
+ out.add('lastRow');
174
+ if (look.hBand && rowBand > 0 && !first && !last) {
175
+ const offset = look.firstRow ? 1 : 0;
176
+ const band = Math.trunc((row - offset) / Math.max(1, rowBand));
177
+ out.add(band % 2 === 0 ? 'band1Horz' : 'band2Horz');
178
+ }
179
+ const span = Math.max(1, colSpan);
180
+ const firstCol = look.firstColumn && col === 0;
181
+ const lastCol = look.lastColumn && colCount > 0 && col + span - 1 >= colCount - 1;
182
+ if (firstCol)
183
+ out.add('firstCol');
184
+ if (lastCol)
185
+ out.add('lastCol');
186
+ if (look.vBand && colBand > 0 && !firstCol && !lastCol) {
187
+ const offset = look.firstColumn ? 1 : 0;
188
+ const band = Math.trunc((col - offset) / Math.max(1, colBand));
189
+ out.add(band % 2 === 0 ? 'band1Vert' : 'band2Vert');
190
+ }
191
+ addCorners(out);
192
+ return out;
193
+ }
194
+ function axis(bits, para, keep) {
195
+ const out = new Set();
196
+ for (const t of bits)
197
+ if (keep.has(t))
198
+ out.add(t);
199
+ for (const t of para)
200
+ if (keep.has(t))
201
+ out.add(t);
202
+ return out;
203
+ }
204
+ /**
205
+ * The conditions a cell (or a paragraph in it) is under: the `w:cnfStyle` caches where the
206
+ * document has them, the position where it has not, gated by the look (docx4j
207
+ * `TableStyleConditions.resolve`).
208
+ *
209
+ * Word writes the row-axis bits on the `w:tr`, the column-axis bits on the `w:tc` and the
210
+ * row-axis bits again on each `w:p`, and never the corner bits. So the row's cache, where there
211
+ * is one, decides the row axis, the cell's the column axis, a paragraph's bits are added on
212
+ * whichever axis they name, and the corners are derived from the result. An axis with no cache
213
+ * falls back to {@link atPosition}. Every bit is then gated by the look, so a stale cache cannot
214
+ * switch on a format the table has turned off.
215
+ */
216
+ export function resolve(look, rowBand, colBand, row, rowCount, col, colSpan, colCount, rowCnfStyle, cellCnf, paraCnf) {
217
+ const computed = atPosition(look, rowBand, colBand, row, rowCount, col, colSpan, colCount);
218
+ const para = fromCnf(paraCnf);
219
+ const out = new Set();
220
+ const rowBits = axis(fromCnf(rowCnfStyle), para, ROW_AXIS);
221
+ if (rowCnfStyle !== undefined || (paraCnf !== undefined && rowBits.size > 0)) {
222
+ for (const t of rowBits)
223
+ out.add(t);
224
+ }
225
+ else {
226
+ for (const t of computed)
227
+ if (ROW_AXIS.has(t))
228
+ out.add(t);
229
+ }
230
+ const colBits = axis(fromCnf(cellCnf), para, COLUMN_AXIS);
231
+ if (cellCnf !== undefined || (paraCnf !== undefined && colBits.size > 0)) {
232
+ for (const t of colBits)
233
+ out.add(t);
234
+ }
235
+ else {
236
+ for (const t of computed)
237
+ if (COLUMN_AXIS.has(t))
238
+ out.add(t);
239
+ }
240
+ gate(out, look);
241
+ addCorners(out);
242
+ return out;
243
+ }
244
+ /**
245
+ * The row-axis conditions a row is under - first row, last row, or a horizontal band - for the
246
+ * row's own properties: its `w:cnfStyle` where it has one, else its position, gated by the look.
247
+ */
248
+ export function rowConditions(look, rowBand, row, rowCount, rowCnfStyle) {
249
+ const from = rowCnfStyle === undefined
250
+ ? atPosition(look, rowBand, 1, row, rowCount, 1, 1, 3)
251
+ : fromCnf(rowCnfStyle);
252
+ const out = new Set();
253
+ for (const t of from)
254
+ if (ROW_AXIS.has(t))
255
+ out.add(t);
256
+ return gate(out, look);
257
+ }
258
+ /**
259
+ * The style's `w:tblStylePr` entry for a condition, or undefined. Pass the merged table style
260
+ * (`PropertyResolver.getTableStyleChain`) to see inherited conditions.
261
+ */
262
+ export function lookup(tableStyle, type) {
263
+ var _a;
264
+ return (_a = tableStyle === null || tableStyle === void 0 ? void 0 : tableStyle.tblStylePr) === null || _a === void 0 ? void 0 : _a.find((pr) => pr !== undefined && pr !== null && pr.type === type);
265
+ }
266
+ /**
267
+ * The style's `w:tblStylePr` entries which apply under these conditions, in the order they are
268
+ * to be applied ({@link PRECEDENCE}); a `wholeTable` entry, where the style has one, always
269
+ * applies and comes first.
270
+ */
271
+ export function applicable(tableStyle, conditions) {
272
+ const out = [];
273
+ if ((tableStyle === null || tableStyle === void 0 ? void 0 : tableStyle.tblStylePr) === undefined || tableStyle.tblStylePr.length === 0)
274
+ return out;
275
+ for (const t of PRECEDENCE) {
276
+ if (t !== 'wholeTable' && (conditions === undefined || !conditions.has(t)))
277
+ continue;
278
+ const pr = lookup(tableStyle, t);
279
+ if (pr !== undefined)
280
+ out.push(pr);
281
+ }
282
+ return out;
283
+ }
284
+ /** Whether the entry has any paragraph or run formatting to contribute. */
285
+ export function formatsText(pr) {
286
+ return pr !== undefined && (!isEmptyPPr(pr.pPr) || !isEmptyRPr(pr.rPr));
287
+ }
288
+ /**
289
+ * A short name for a set of conditions, in precedence order - `firstCol-firstRow-nwCell` - the
290
+ * form docx4j's goldens and synthetic style ids use; the empty string for none.
291
+ */
292
+ export function key(conditions) {
293
+ if (conditions === undefined || conditions.size === 0)
294
+ return '';
295
+ return PRECEDENCE.filter((t) => t !== 'wholeTable' && conditions.has(t)).join('-');
296
+ }
@@ -0,0 +1,3 @@
1
+ import type * as wml from '@docx4j/generated-objects-ts/modules/org_docx4j_wml';
2
+ /** A fresh copy of the definition, each call. */
3
+ export declare function placeholderStyle(): wml.Style;
@@ -0,0 +1,13 @@
1
+ import * as wmlFactory from '@docx4j/generated-objects-ts/factory/org_docx4j_wml';
2
+ import { PLACEHOLDER_STYLE } from '../../model/customxml/bindings.mjs';
3
+ /** A fresh copy of the definition, each call. */
4
+ export function placeholderStyle() {
5
+ return wmlFactory.createStyle({
6
+ type: 'character', styleId: PLACEHOLDER_STYLE,
7
+ name: wmlFactory.createStyleName({ val: 'Placeholder Text' }),
8
+ basedOn: wmlFactory.createStyleBasedOn({ val: 'DefaultParagraphFont' }),
9
+ uiPriority: wmlFactory.createStyleUiPriority({ val: 99 }),
10
+ unhideWhenUsed: wmlFactory.createBooleanDefaultTrue(),
11
+ rPr: wmlFactory.createRPr({ color: wmlFactory.createColor({ val: '666666' }) }),
12
+ });
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docx4j/core-ts",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "docx4j-core for TypeScript: Open Packaging (docx/pptx/xlsx zip and flat OPC), typed parts and relationships, style, numbering and font resolution over @docx4j/generated-objects-ts. Node, browsers, Word add-ins.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -79,7 +79,7 @@
79
79
  },
80
80
  "license": "Apache-2.0",
81
81
  "dependencies": {
82
- "@docx4j/generated-objects-ts": "^0.3.0",
82
+ "@docx4j/generated-objects-ts": "^0.3.1",
83
83
  "fflate": "^0.8.2"
84
84
  },
85
85
  "devDependencies": {