From 2243d74c225c5e004c3fea9235756639718e4e1a Mon Sep 17 00:00:00 2001 From: Dolan Date: Thu, 26 Nov 2026 17:36:05 +0800 Subject: [PATCH] fix: write one w:pStyle for a paragraph with both heading and style (#3940) A paragraph with both `heading` and `style` wrote a w:pStyle for each, and CT_PPrBase has one, so the Open XML SDK validator rejected the document. Word applies the last of the two, `style`, so `style` now takes the place of `heading`, and Word shows the paragraph as it did. The layout module and the table of contents' heading entries read the first w:pStyle, the heading, and now read the style Word shows. The ListParagraph that a bullet or numbered paragraph gets without a style of its own goes in the same if/else. Co-Authored-By: Claude Opus 5.5 --- docs/usage/paragraph.md | 2 ++ src/file/paragraph/paragraph.spec.ts | 28 +++++++++++++++++++++++++++ src/file/paragraph/properties.spec.ts | 27 +++++++++++++++++++++++++- src/file/paragraph/properties.ts | 28 ++++++++++++--------------- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/docs/usage/paragraph.md b/docs/usage/paragraph.md index 3e8714e3f3..73994f816f 100644 --- a/docs/usage/paragraph.md +++ b/docs/usage/paragraph.md @@ -198,6 +198,8 @@ const doc = new Document({ }); ``` +A paragraph has one style, so a paragraph with a `style` too is in that style, not the heading's, as Word shows it. + ## Border Add borders to a `Paragraph`. Good for making the `Paragraph` stand out. Border top and border bottom can be used as a horizontal rule (also known as horizontal line). diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index a33d6b4569..ee6aeaadd8 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -1117,6 +1117,34 @@ describe("Paragraph", () => { ], }); }); + + it("should write only the style for a paragraph with a heading too, as Word applied the last of the two", () => { + const paragraph = new Paragraph({ + heading: HeadingLevel.HEADING_1, + style: "myFancyStyle", + }); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [ + { + "w:pPr": [{ "w:pStyle": { _attr: { "w:val": "myFancyStyle" } } }], + }, + ], + }); + }); + + it("should write only the style for a bullet paragraph with a heading too", () => { + const paragraph = new Paragraph({ + heading: HeadingLevel.HEADING_1, + style: "myFancyStyle", + bullet: { level: 0 }, + }); + const tree = new Formatter().format(paragraph); + expect(tree["w:p"][0]["w:pPr"]).to.deep.equal([ + { "w:pStyle": { _attr: { "w:val": "myFancyStyle" } } }, + { "w:numPr": [{ "w:ilvl": { _attr: { "w:val": 0 } } }, { "w:numId": { _attr: { "w:val": 1 } } }] }, + ]); + }); }); describe("#indent", () => { diff --git a/src/file/paragraph/properties.spec.ts b/src/file/paragraph/properties.spec.ts index f0bd1e5493..08af2c47c3 100644 --- a/src/file/paragraph/properties.spec.ts +++ b/src/file/paragraph/properties.spec.ts @@ -10,7 +10,7 @@ import { SectionProperties } from "../document/body/section-properties/section-p import { DocumentWrapper } from "../document-wrapper"; import type { File } from "../file"; import { FontWrapper } from "../fonts/font-wrapper"; -import { AlignmentType, TabStopType } from "./formatting"; +import { AlignmentType, HeadingLevel, TabStopType } from "./formatting"; import { FrameAnchorType } from "./frame"; import { ParagraphProperties } from "./properties"; @@ -588,6 +588,31 @@ describe("ParagraphProperties", () => { ], }); }); + + it("should write one style in the properties a revision keeps, with both a heading and a style", () => { + const properties = new ParagraphProperties({ + heading: HeadingLevel.HEADING_2, + revision: { + id: 1, + author: "Firstname Lastname", + date: "123", + heading: HeadingLevel.HEADING_1, + style: "myFancyStyle", + }, + }); + const tree = new Formatter().format(properties); + expect(tree).to.deep.equal({ + "w:pPr": [ + { "w:pStyle": { _attr: { "w:val": "Heading2" } } }, + { + "w:pPrChange": [ + { _attr: { "w:author": "Firstname Lastname", "w:date": "123", "w:id": 1 } }, + { "w:pPr": [{ "w:pStyle": { _attr: { "w:val": "myFancyStyle" } } }] }, + ], + }, + ], + }); + }); }); describe("#removeSectionProperties()", () => { diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts index 087d0a1c28..a7e182f8e5 100644 --- a/src/file/paragraph/properties.ts +++ b/src/file/paragraph/properties.ts @@ -138,7 +138,10 @@ export type IParagraphStylePropertiesOptions = { } & ILevelParagraphStylePropertiesOptions; export type IParagraphPropertiesOptionsBase = { - /** Heading level (Heading1, Heading2, etc.) - applies predefined heading style */ + /** + * Heading level (Heading1, Heading2, etc.) - applies predefined heading style. A paragraph has one style, so with a + * `style` too, the paragraph is in `style` + */ readonly heading?: (typeof HeadingLevel)[keyof typeof HeadingLevel]; /** * Whether to lay the paragraph out right-to-left, for languages such as Arabic and Hebrew. @@ -150,7 +153,7 @@ export type IParagraphPropertiesOptionsBase = { readonly pageBreakBefore?: boolean; /** Custom tab stop positions and alignments */ readonly tabStops?: readonly TabStopDefinition[]; - /** Style ID to apply to this paragraph */ + /** Style ID to apply to this paragraph. A paragraph has one style, so this takes the place of `heading` */ readonly style?: string; /** * Bullet list configuration. The paragraph is given Word's built-in `ListParagraph` style, unless it has a `style` or a @@ -402,22 +405,15 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent { return this; } - if (options.heading) { - this.push(createParagraphStyle(options.heading)); - } - - if (implicitListParagraphStyle) { - if (options.bullet && !options.style && !options.heading) { - this.push(createParagraphStyle("ListParagraph")); - } - - if (options.numbering && !options.numbering.custom && !options.style && !options.heading) { - this.push(createParagraphStyle("ListParagraph")); - } - } - + // A paragraph has one style: CT_PPrBase has one w:pStyle. With both `style` and `heading`, Word applied the last of + // the two docx wrote, `style`, so `style` takes the place of `heading`. A list paragraph with neither is given + // ListParagraph if (options.style) { this.push(createParagraphStyle(options.style)); + } else if (options.heading) { + this.push(createParagraphStyle(options.heading)); + } else if (implicitListParagraphStyle && (options.bullet || (options.numbering && !options.numbering.custom))) { + this.push(createParagraphStyle("ListParagraph")); } if (options.keepNext !== undefined) {