mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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).
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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()", () => {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user