Skip to content

Commit 2cf5e3f

Browse files
fix(layout): preserve converter title page refs
1 parent b4194f1 commit 2cf5e3f

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

packages/layout-engine/layout-bridge/src/headerFooterUtils.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,20 @@ export function buildMultiSectionIdentifier(
266266
sectionMetadata: SectionMetadata[],
267267
pageStyles?: { alternateHeaders?: boolean },
268268
converterIds?: {
269-
headerIds?: { default?: string | null; first?: string | null; even?: string | null; odd?: string | null };
270-
footerIds?: { default?: string | null; first?: string | null; even?: string | null; odd?: string | null };
269+
headerIds?: {
270+
default?: string | null;
271+
first?: string | null;
272+
even?: string | null;
273+
odd?: string | null;
274+
titlePg?: boolean;
275+
};
276+
footerIds?: {
277+
default?: string | null;
278+
first?: string | null;
279+
even?: string | null;
280+
odd?: string | null;
281+
titlePg?: boolean;
282+
};
271283
},
272284
): MultiSectionHeaderFooterIdentifier {
273285
const identifier = defaultMultiSectionIdentifier();
@@ -299,12 +311,11 @@ export function buildMultiSectionIdentifier(
299311
});
300312
}
301313

302-
// Track per-section titlePg from section metadata (w:titlePg element in OOXML)
303-
// Note: The presence of a 'first' header/footer reference does NOT mean titlePg is enabled.
304-
// The w:titlePg element must be present in sectPr to use first page headers/footers.
305-
// Track per-section titlePg from section metadata (w:titlePg element in OOXML)
306-
// Store explicit false so later sections don't inherit section 0's value.
307-
identifier.sectionTitlePg.set(idx, section.titlePg === true);
314+
// Track per-section titlePg from section metadata (w:titlePg element in OOXML).
315+
// The presence of a 'first' header/footer reference does NOT mean titlePg is enabled.
316+
if (Object.prototype.hasOwnProperty.call(section, 'titlePg')) {
317+
identifier.sectionTitlePg.set(idx, section.titlePg === true);
318+
}
308319
}
309320

310321
// Set legacy fields from section 0 for backward compatibility
@@ -322,7 +333,7 @@ export function buildMultiSectionIdentifier(
322333
// Only fill in null values - don't override existing refs from section metadata
323334
// Also fall back to converter's titlePg if not set from section metadata
324335
if (converterIds?.headerIds) {
325-
if (!identifier.titlePg && (converterIds.headerIds as { titlePg?: boolean }).titlePg) {
336+
if (!identifier.titlePg && converterIds.headerIds.titlePg) {
326337
identifier.titlePg = true;
327338
}
328339
identifier.headerIds.default = identifier.headerIds.default ?? converterIds.headerIds.default ?? null;
@@ -331,7 +342,7 @@ export function buildMultiSectionIdentifier(
331342
identifier.headerIds.odd = identifier.headerIds.odd ?? converterIds.headerIds.odd ?? null;
332343
}
333344
if (converterIds?.footerIds) {
334-
if (!identifier.titlePg && (converterIds.footerIds as { titlePg?: boolean }).titlePg) {
345+
if (!identifier.titlePg && converterIds.footerIds.titlePg) {
335346
identifier.titlePg = true;
336347
}
337348
identifier.footerIds.default = identifier.footerIds.default ?? converterIds.footerIds.default ?? null;

packages/layout-engine/layout-bridge/test/headerFooterUtils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,23 @@ describe('headerFooterUtils', () => {
401401
).toBe('converter-f-default');
402402
});
403403

404+
it('should preserve converter titlePg fallback for section 0 variant selection', () => {
405+
const sectionMetadata: SectionMetadata[] = [
406+
{
407+
sectionIndex: 0,
408+
headerRefs: { first: null },
409+
},
410+
];
411+
412+
const identifier = buildMultiSectionIdentifier(sectionMetadata, undefined, {
413+
headerIds: { first: 'converter-h-first', titlePg: true },
414+
});
415+
416+
expect(
417+
getHeaderFooterIdForPage({ number: 1, fragments: [], sectionIndex: 0 }, identifier, { kind: 'header' }),
418+
).toBe('converter-h-first');
419+
});
420+
404421
it('should NOT override existing section metadata with converter IDs', () => {
405422
const sectionMetadata: SectionMetadata[] = [
406423
{

0 commit comments

Comments
 (0)