-
Notifications
You must be signed in to change notification settings - Fork 93
fix: only include inline pPr on export #2287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VladaHarbour
wants to merge
5
commits into
main
Choose a base branch
from
sd-1919_filter-inherited-props
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3fea8ba
fix: only include inline pPr on export
VladaHarbour 3375489
fix: address review comments
VladaHarbour 755a5a3
fix: make unit tests pass
VladaHarbour cc32510
fix: support collaboration changes
VladaHarbour 91bad8b
chore: resolve conflicts
VladaHarbour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/super-editor/src/core/super-converter/export-helpers/run-properties-export.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // @ts-check | ||
| /** | ||
| * Helpers for exporting w:rPr so we only output overrides relative to paragraph/style | ||
| * (inherited props are already in styles.xml). | ||
| */ | ||
| import { translator as wRPrTranslator } from '@converter/v3/handlers/w/rpr'; | ||
|
|
||
| const STYLES_KEY = 'word/styles.xml'; | ||
|
|
||
| /** | ||
| * Get the merged run properties for a paragraph style from styles.xml (including basedOn chain). | ||
| * @param {Object} docx - Converted XML (e.g. converter.convertedXml) | ||
| * @param {string} styleId - Paragraph style id (e.g. from w:pStyle) | ||
| * @param {import('@translator').SCEncoderConfig} [params] - Params for encoding (docx for theme etc.) | ||
| * @returns {Object} Run properties object from the style, or {} if not found | ||
| */ | ||
| export function getParagraphStyleRunPropertiesFromStylesXml(docx, styleId, params) { | ||
| const stylesPart = docx?.[STYLES_KEY]; | ||
| if (!stylesPart?.elements?.[0]?.elements) return {}; | ||
|
|
||
| const styleElements = stylesPart.elements[0].elements.filter((el) => el.name === 'w:style'); | ||
| const styleById = new Map(styleElements.map((el) => [el.attributes?.['w:styleId'], el])); | ||
|
|
||
| const chain = []; | ||
| let currentId = styleId; | ||
| const seen = new Set(); | ||
|
|
||
| while (currentId && !seen.has(currentId)) { | ||
| seen.add(currentId); | ||
| const styleTag = styleById.get(currentId); | ||
| if (!styleTag) break; | ||
| const rPr = styleTag.elements?.find((el) => el.name === 'w:rPr'); | ||
| if (rPr?.elements?.length) chain.push(rPr); | ||
| const basedOn = styleTag.elements?.find((el) => el.name === 'w:basedOn'); | ||
| currentId = basedOn?.attributes?.['w:val']; | ||
| } | ||
|
|
||
| if (chain.length === 0) return {}; | ||
|
|
||
| // Chain is derived → base (walk from current to basedOn). Reverse so we merge base first, then derived (derived overrides base). | ||
| const byName = {}; | ||
| chain.reverse().forEach((rPr) => { | ||
| (rPr.elements || []).forEach((el) => { | ||
| if (el?.name) byName[el.name] = el; | ||
| }); | ||
| }); | ||
| const mergedRPr = { | ||
| name: 'w:rPr', | ||
| elements: Object.values(byName), | ||
| }; | ||
|
|
||
| const encodeParams = { ...params, docx: params.docx ?? docx, nodes: [mergedRPr] }; | ||
| const encoded = wRPrTranslator.encode(encodeParams); | ||
| return encoded ?? {}; | ||
| } |
126 changes: 126 additions & 0 deletions
126
packages/super-editor/src/core/super-converter/export-helpers/run-properties-export.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
|
|
||
| vi.mock('@converter/v3/handlers/w/rpr', () => ({ | ||
| translator: { | ||
| encode: vi.fn(() => ({})), | ||
| }, | ||
| })); | ||
|
|
||
| const { getParagraphStyleRunPropertiesFromStylesXml } = await import('./run-properties-export.js'); | ||
| const { translator: wRPrTranslator } = await import('@converter/v3/handlers/w/rpr'); | ||
|
|
||
| function makeStylesDocx(styles) { | ||
| return { | ||
| 'word/styles.xml': { | ||
| elements: [ | ||
| { | ||
| name: 'w:styles', | ||
| type: 'element', | ||
| elements: styles, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function makeStyle({ styleId, basedOn, rPrElements = [] }) { | ||
| const elements = []; | ||
| if (basedOn) { | ||
| elements.push({ | ||
| name: 'w:basedOn', | ||
| type: 'element', | ||
| attributes: { 'w:val': basedOn }, | ||
| }); | ||
| } | ||
| if (rPrElements.length) { | ||
| elements.push({ | ||
| name: 'w:rPr', | ||
| type: 'element', | ||
| elements: rPrElements, | ||
| }); | ||
| } | ||
| return { | ||
| name: 'w:style', | ||
| type: 'element', | ||
| attributes: { 'w:styleId': styleId }, | ||
| elements, | ||
| }; | ||
| } | ||
|
|
||
| describe('getParagraphStyleRunPropertiesFromStylesXml', () => { | ||
| beforeEach(() => { | ||
| wRPrTranslator.encode.mockReset(); | ||
| }); | ||
|
|
||
| it('returns empty object when styles part is missing or has no styles', () => { | ||
| expect(getParagraphStyleRunPropertiesFromStylesXml({}, 'Heading1', {})).toEqual({}); | ||
| expect( | ||
| getParagraphStyleRunPropertiesFromStylesXml( | ||
| { | ||
| 'word/styles.xml': { | ||
| elements: [{ name: 'w:styles', type: 'element', elements: [] }], | ||
| }, | ||
| }, | ||
| 'Heading1', | ||
| {}, | ||
| ), | ||
| ).toEqual({}); | ||
| expect(wRPrTranslator.encode).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('merges basedOn chain from base to derived so derived overrides base', () => { | ||
| const baseStyle = makeStyle({ | ||
| styleId: 'Base', | ||
| rPrElements: [ | ||
| { name: 'w:color', type: 'element', attributes: { 'w:val': '0000FF' } }, | ||
| { name: 'w:b', type: 'element', attributes: {} }, | ||
| ], | ||
| }); | ||
| const derivedStyle = makeStyle({ | ||
| styleId: 'Heading1', | ||
| basedOn: 'Base', | ||
| rPrElements: [ | ||
| { name: 'w:color', type: 'element', attributes: { 'w:val': 'FF0000' } }, | ||
| { name: 'w:i', type: 'element', attributes: {} }, | ||
| ], | ||
| }); | ||
|
|
||
| const docx = makeStylesDocx([baseStyle, derivedStyle]); | ||
|
|
||
| wRPrTranslator.encode.mockImplementation(({ nodes }) => ({ fromNodes: nodes })); | ||
|
|
||
| const result = getParagraphStyleRunPropertiesFromStylesXml(docx, 'Heading1', { docx: { theme: 'x' } }); | ||
|
|
||
| expect(wRPrTranslator.encode).toHaveBeenCalledTimes(1); | ||
| const encodeArg = wRPrTranslator.encode.mock.calls[0][0]; | ||
|
|
||
| expect(encodeArg.docx).toEqual({ theme: 'x' }); | ||
| expect(Array.isArray(encodeArg.nodes)).toBe(true); | ||
| expect(encodeArg.nodes).toHaveLength(1); | ||
|
|
||
| const mergedRPr = encodeArg.nodes[0]; | ||
| expect(mergedRPr.name).toBe('w:rPr'); | ||
|
|
||
| const elementNames = (mergedRPr.elements || []).map((el) => el.name); | ||
| expect(elementNames).toEqual(expect.arrayContaining(['w:b', 'w:i', 'w:color'])); | ||
|
|
||
| const colorElement = mergedRPr.elements.find((el) => el.name === 'w:color'); | ||
| expect(colorElement?.attributes?.['w:val']).toBe('FF0000'); | ||
|
|
||
| expect(result).toEqual({ fromNodes: expect.any(Array) }); | ||
| }); | ||
|
|
||
| it('falls back to docx argument when params.docx is not provided', () => { | ||
| const style = makeStyle({ | ||
| styleId: 'Normal', | ||
| rPrElements: [{ name: 'w:b', type: 'element', attributes: {} }], | ||
| }); | ||
| const docx = makeStylesDocx([style]); | ||
|
|
||
| getParagraphStyleRunPropertiesFromStylesXml(docx, 'Normal', {}); | ||
|
|
||
| expect(wRPrTranslator.encode).toHaveBeenCalledTimes(1); | ||
| const encodeArg = wRPrTranslator.encode.mock.calls[0][0]; | ||
| expect(encodeArg.docx).toBe(docx); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing for paragraphs — old docs hit this and lose their default formatting on save.