Skip to content

fix(style-engine): resolve built-in Word heading styles when missing from styles.xml#2806

Open
gpardhivvarma wants to merge 2 commits intosuperdoc-dev:mainfrom
gpardhivvarma:fix/builtin-heading-styles
Open

fix(style-engine): resolve built-in Word heading styles when missing from styles.xml#2806
gpardhivvarma wants to merge 2 commits intosuperdoc-dev:mainfrom
gpardhivvarma:fix/builtin-heading-styles

Conversation

@gpardhivvarma
Copy link
Copy Markdown
Contributor

Summary

Closes #2805

  • Add fallback to Word's built-in heading defaults (Heading1–Heading6) in resolveStyleChain() when a referenced style is not defined in the document's styles.xml
  • Apply the same fallback in the basedOn chain walk so custom styles inheriting from built-in headings resolve correctly
  • Add 9 unit tests covering fallback resolution, precedence, basedOn inheritance, and integration through resolveRunProperties and resolveParagraphProperties

Problem

Documents created with python-docx's add_heading() reference Word's built-in heading styles (Heading1Heading9) without adding explicit <w:style> definitions to styles.xml. This is valid OOXML — Word has ~260 built-in styles that implicitly exist. SuperDoc's resolveStyleChain() returned {} for these, causing headings to render as unstyled body text.

Changes

File Change
style-engine/src/ooxml/builtin-styles.ts NEW — static BUILTIN_STYLE_DEFAULTS map with Heading1–6 properties matching Word defaults (font sizes, spacing, outline levels, theme fonts, colors)
style-engine/src/ooxml/index.ts 2-line fix: ?? fallback to built-in defaults in both initial lookup and basedOn chain walk
style-engine/src/ooxml/index.test.ts 9 new tests

How it works

// Before: missing styles return empty
const styleDef = params.translatedLinkedStyles?.styles?.[styleId];

// After: fall back to built-in defaults
const styleDef = params.translatedLinkedStyles?.styles?.[styleId] ?? BUILTIN_STYLE_DEFAULTS[styleId];

Document-defined styles always win (?? only activates when the left operand is undefined). Built-in headings set basedOn: 'Normal', so the existing chain walk picks up the document's Normal style for font family and body defaults.

Test plan

  • bun test in style-engine — 137/137 passing
  • 9 new tests: run properties, paragraph properties, precedence, basedOn inheritance, outline levels, basedOn chain with built-in parents, resolveRunProperties + resolveParagraphProperties integration
  • Manual verification with python-docx add_heading() document

…from styles.xml

Add fallback to Word's built-in heading defaults (Heading1–Heading6) in
resolveStyleChain() when a referenced style is not explicitly defined in
the document's styles.xml. Also apply fallback in the basedOn chain walk
so custom styles inheriting from built-in headings resolve correctly.

Documents created with python-docx's add_heading() reference built-in
styles without adding definitions — this is valid OOXML but previously
rendered as unstyled body text.

Closes superdoc-dev#2805
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99418295be

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Addresses Codex review feedback — extend BUILTIN_STYLE_DEFAULTS to
cover all nine heading levels. Heading7-9 use muted text color (595959)
instead of the accent color used by Heading1-6, matching Word's visual
hierarchy.
@gpardhivvarma
Copy link
Copy Markdown
Contributor Author

Addressed the Codex feedback — pushed a follow-up commit adding Heading7–Heading9.

Note: Title, Subtitle, ListParagraph, TOC1TOC9 are also referenced across the codebase and could hit the same missing-style issue. Adding all of them would bloat this PR — the headings are the most impactful fix. Happy to add the others in a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: built-in Word heading styles not rendered when missing from styles.xml

1 participant