Skip to content

Commit 312988e

Browse files
author
Gabe Webb
committed
Update how we're doing things. Add console logs to test.
1 parent b19c4a0 commit 312988e

File tree

3 files changed

+84
-89
lines changed

3 files changed

+84
-89
lines changed

src/properties/section-properties.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('Section column formatting for equally sized columns', () => {
5959
numberOfColumns: 3,
6060
equalWidth: true,
6161
separator: false,
62-
columnSpacing: twip(720)
62+
columns: []
6363
}
6464
}
6565
);
@@ -70,7 +70,7 @@ describe('Section column formatting for equally sized columns', () => {
7070
describe('Section column formatting for differently sized columns', () => {
7171
test(
7272
`<w:sectPr ${ALL_NAMESPACE_DECLARATIONS}>
73-
<w:cols w:num="3" w:equalwidth="0" w:sep="0">
73+
<w:cols w:num="3" w:equalwidth="0" w:sep="1">
7474
<w:col w:w="1440" w:space="720"/>
7575
<w:col w:w="1440" w:space="720" />
7676
<w:col w:w="2880" />
@@ -82,9 +82,9 @@ describe('Section column formatting for differently sized columns', () => {
8282
equalWidth: false,
8383
separator: true,
8484
columns: [
85-
{columnSize: twip(1440), columnSpacing: twip(720)},
86-
{columnSize: twip(1440), columnSpacing: twip(720)},
87-
{columnSize: twip(1440), columnSpacing: null}
85+
{columnWidth: twip(1440), columnSpace: twip(720)},
86+
{columnWidth: twip(1440), columnSpace: twip(720)},
87+
{columnWidth: twip(2880)}
8888
]
8989
}
9090
}

src/properties/section-properties.ts

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { create } from '../utilities/dom.ts';
1+
import { create, update, serialize } from '../utilities/dom.ts';
22
import { Length } from '../utilities/length.ts';
33
import { QNS } from '../utilities/namespaces.ts';
4-
import { evaluateXPathToMap } from '../utilities/xquery.ts';
4+
import * as slimdom from 'https://esm.sh/[email protected]?pin=v121';
5+
import { evaluateXPathToArray, evaluateXPathToFirstNode, evaluateXPathToMap } from '../utilities/xquery.ts';
56

67
/**
78
* All the formatting options that can be given on a text run (inline text).
@@ -17,8 +18,7 @@ export type SectionProperties = {
1718
numberOfColumns: null | number;
1819
equalWidth: null | boolean;
1920
separator: null | boolean;
20-
columnSpacing?: null | Length;
21-
columns?: {columnSize: Length, columnSpacing: Length | null }[]
21+
columns?: {columnWidth: Length | null, columnSpace?: Length | null}[] | null;
2222
};
2323

2424
/**
@@ -65,70 +65,59 @@ export type SectionProperties = {
6565
export function sectionPropertiesFromNode(node?: Node | null): SectionProperties {
6666
if (!node) {
6767
return {};
68-
};
69-
const data = evaluateXPathToMap<SectionProperties>(
70-
`let $propsMap := map {
71-
"headers": map {
72-
"first": ./${QNS.w}headerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
73-
"even": ./${QNS.w}headerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
74-
"odd": ./${QNS.w}headerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
75-
},
76-
"footers": map {
77-
"first": ./${QNS.w}footerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
78-
"even": ./${QNS.w}footerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
79-
"odd": ./${QNS.w}footerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
80-
},
81-
"columns": map {
82-
"numberOfColumns": ./${QNS.w}cols/@${QNS.w}num/number(),
83-
"separator": docxml:st-on-off(./${QNS.w}cols/@${QNS.w}sep),
84-
"equalWidth": docxml:st-on-off(./${QNS.w}cols/@${QNS.w}equalWidth)
85-
},
86-
"pageWidth": docxml:length(${QNS.w}pgSz/@${QNS.w}w, 'twip'),
87-
"pageHeight": docxml:length(${QNS.w}pgSz/@${QNS.w}h, 'twip'),
88-
"pageOrientation": ./${QNS.w}pgSz/@${QNS.w}orient/string(),
89-
"pageMargin": map {
90-
"top": docxml:length(./${QNS.w}pgMar/@${QNS.w}top, 'twip'),
91-
"right": docxml:length(./${QNS.w}pgMar/@${QNS.w}right, 'twip'),
92-
"bottom": docxml:length(./${QNS.w}pgMar/@${QNS.w}bottom, 'twip'),
93-
"left": docxml:length(./${QNS.w}pgMar/@${QNS.w}left, 'twip'),
94-
"header": docxml:length(./${QNS.w}pgMar/@${QNS.w}header, 'twip'),
95-
"footer": docxml:length(./${QNS.w}pgMar/@${QNS.w}footer, 'twip'),
96-
"gutter": docxml:length(./${QNS.w}pgMar/@${QNS.w}gutter, 'twip')
97-
},
98-
"isTitlePage": exists(./${QNS.w}titlePg) and (not(./${QNS.w}titlePg/@${QNS.w}val) or docxml:st-on-off(./${QNS.w}titlePg/@${QNS.w}val))
99-
}
100-
let $colSpacing := if (exists(./${QNS.w}cols/@${QNS.w}space)) then (docxml:length(./${QNS.w}cols/@${QNS.w}space, 'twip')) else ()
101-
let $cols := ./${QNS.w}cols/${QNS.w}col
102-
let $colMap :=
103-
if (exists($cols))
104-
then (
105-
for-each(
106-
$cols,
107-
function($c) {
108-
map {
109-
"columnsSize": docxml:length($c/@${QNS.w}w, 'twip'),
110-
"columnSpacing": docxml:length($c/@${QNS.w}space, 'twip')
111-
}
112-
}
113-
)
114-
)
115-
else()
68+
};
11669

117-
return (
118-
if (exists($colSpacing))
119-
then (
120-
map:put($propsMap('columns'), 'columnSpacing', $colSpacing)
121-
)
122-
else(),
123-
map:put($propsMap('columns'), 'columns', $colMap)
124-
)`,
70+
const data = evaluateXPathToMap<SectionProperties>(
71+
`map {
72+
"headers": map {
73+
"first": ./${QNS.w}headerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
74+
"even": ./${QNS.w}headerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
75+
"odd": ./${QNS.w}headerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
76+
},
77+
"footers": map {
78+
"first": ./${QNS.w}footerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
79+
"even": ./${QNS.w}footerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
80+
"odd": ./${QNS.w}footerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
81+
},
82+
"columns": map {
83+
"numberOfColumns": ./${QNS.w}cols/@${QNS.w}num/number(),
84+
"separator": docxml:st-on-off(./${QNS.w}cols/@${QNS.w}sep),
85+
"equalWidth": docxml:st-on-off(./${QNS.w}cols/@${QNS.w}equalwidth),
86+
"columns": array{}
87+
},
88+
"pageWidth": docxml:length(${QNS.w}pgSz/@${QNS.w}w, 'twip'),
89+
"pageHeight": docxml:length(${QNS.w}pgSz/@${QNS.w}h, 'twip'),
90+
"pageOrientation": ./${QNS.w}pgSz/@${QNS.w}orient/string(),
91+
"pageMargin": map {
92+
"top": docxml:length(./${QNS.w}pgMar/@${QNS.w}top, 'twip'),
93+
"right": docxml:length(./${QNS.w}pgMar/@${QNS.w}right, 'twip'),
94+
"bottom": docxml:length(./${QNS.w}pgMar/@${QNS.w}bottom, 'twip'),
95+
"left": docxml:length(./${QNS.w}pgMar/@${QNS.w}left, 'twip'),
96+
"header": docxml:length(./${QNS.w}pgMar/@${QNS.w}header, 'twip'),
97+
"footer": docxml:length(./${QNS.w}pgMar/@${QNS.w}footer, 'twip'),
98+
"gutter": docxml:length(./${QNS.w}pgMar/@${QNS.w}gutter, 'twip')
99+
},
100+
"isTitlePage": exists(./${QNS.w}titlePg) and (not(./${QNS.w}titlePg/@${QNS.w}val) or docxml:st-on-off(./${QNS.w}titlePg/@${QNS.w}val))
101+
}`,
125102
node,
126103
);
104+
105+
evaluateXPathToArray(`array{./${QNS.w}cols/${QNS.w}col/map{
106+
"columnWidth": if (@${QNS.w}w) then docxml:length(@${QNS.w}w, 'twip') else (),
107+
"columnSpace": if (@${QNS.w}space) then docxml:length(@${QNS.w}space, 'twip') else ()
108+
}}`, node).forEach(({columnWidth, columnSpace}) => data.columns?.columns?.push(
109+
{columnWidth: columnWidth || null, columnSpace: columnSpace || null}
110+
));
111+
112+
// console.log(data);
127113
return data;
128114
}
129115

130116
export function sectionPropertiesToNode(data: SectionProperties = {}): Node {
131-
const query = create(
117+
118+
// console.log(data.columns?.columns?.forEach((col) => { console.log(col.columnWidth)}))
119+
120+
const newNode = create(
132121
`element ${QNS.w}sectPr {
133122
if (exists($headers('first'))) then element ${QNS.w}headerReference {
134123
attribute ${QNS.r}id { $headers('first') },
@@ -154,23 +143,10 @@ export function sectionPropertiesToNode(data: SectionProperties = {}): Node {
154143
attribute ${QNS.r}id { $footers('odd') },
155144
attribute ${QNS.w}type { 'default' }
156145
} else (),
157-
if (exists($columns)) then element ${QNS.w}cols {
146+
if (exists($columns) and $columns('numberOfColumns') > 0) then element ${QNS.w}cols {
158147
attribute ${QNS.w}sep { $columns('separator') },
159-
attribute ${QNS.w}equalWidth { $columns('equalWidth') },
160-
attribute ${QNS.w}num { $columns('numberOfColumns') },
161-
if (exists($columns('columnSpacing')))
162-
then (
163-
attribute ${QNS.w}columnSpacing { $columns('columnSpacing')}
164-
)
165-
else (),
166-
if (exists($columns('columns')))
167-
then (
168-
element columns {
169-
attribute ${QNS.w}w { round($columns('columns')('columnSize')('twip')) },
170-
attribute ${QNS.w}space { round($columns('columns')('columnSpacing')('twip')) }
171-
}
172-
)
173-
else()
148+
attribute ${QNS.w}equalwidth { $columns('equalWidth') },
149+
attribute ${QNS.w}num { $columns('numberOfColumns') }
174150
} else (),
175151
if (exists($pageWidth) or exists($pageHeight) or $pageOrientation) then element ${QNS.w}pgSz {
176152
if (exists($pageWidth)) then attribute ${QNS.w}w {
@@ -218,16 +194,33 @@ export function sectionPropertiesToNode(data: SectionProperties = {}): Node {
218194
columns: {
219195
numberOfColumns: data.columns?.numberOfColumns,
220196
separator: data.columns?.separator,
221-
equalWidth: data.columns?.equalWidth,
222-
columnSpacing: data.columns?.columnSpacing,
223-
columns: data.columns?.columns,
224-
},
197+
equalWidth: data.columns?.equalWidth },
225198
pageWidth: data.pageWidth || null,
226199
pageHeight: data.pageHeight || null,
227200
pageMargin: data.pageMargin || null,
228201
pageOrientation: data.pageOrientation || null,
229202
isTitlePage: data.isTitlePage || null,
230-
},
231-
);
232-
return query;
203+
}
204+
);
205+
data.columns?.columns?.forEach((col) => {
206+
// console.log("COL");
207+
if (col.columnWidth != undefined) {
208+
update(
209+
newNode,
210+
`let $cols := ./${QNS.w}sectPr/${QNS.w}cols
211+
return (
212+
if (exists($cols))
213+
then (
214+
insert node element ${QNS.w}col {
215+
attribute ${QNS.w}w { round(${col.columnWidth?.twip.toString() }) },
216+
attribute ${QNS.w}space { round(${col.columnSpace?.twip.toString() }) }
217+
} as last into $cols
218+
) else ()
219+
)
220+
`
221+
)
222+
}
223+
});
224+
225+
return newNode;
233226
}

src/utilities/tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export function createXmlRoundRobinTest<ObjectShape extends { [key: string]: unk
125125
): void {
126126
const value = p1[prop as keyof typeof p1];
127127
const expectation = e1[prop as keyof typeof e1];
128+
console.log("P1: ", p1);
129+
console.log("P2: ", p2);
128130
const reparsed = p2[prop as keyof typeof p2];
129131

130132
if (expectation && typeof expectation === 'object') {

0 commit comments

Comments
 (0)