-
Notifications
You must be signed in to change notification settings - Fork 132
feat: preserve w:view setting through DOCX round-trip #2190
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
Merged
caio-pizzol
merged 4 commits into
superdoc-dev:main
from
gpardhivvarma:feat/preserve-w-view-setting
Feb 27, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62580c7
feat: preserve <w:view> setting through DOCX round-trip
gpardhivvarma dffbc5d
fix: preserve w:view element position in settings.xml schema order
gpardhivvarma 959859f
fix: reset viewSetting before parsing to prevent stale state on conve…
gpardhivvarma 24e340c
fix: add clarifying comments for w:view export placement
gpardhivvarma 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
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
242 changes: 242 additions & 0 deletions
242
packages/super-editor/src/tests/import-export/view-setting-roundtrip.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,242 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { parseXmlToJson } from '@converter/v2/docxHelper.js'; | ||
| import { prepareFootnotesXmlForExport } from '@converter/v2/exporter/footnotesExporter.js'; | ||
| import { carbonCopy } from '@core/utilities/carbonCopy.js'; | ||
|
|
||
| const minimalStylesXml = parseXmlToJson( | ||
| '<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' + | ||
| '<w:docDefaults>' + | ||
| '<w:rPrDefault><w:rPr/></w:rPrDefault>' + | ||
| '<w:pPrDefault><w:pPr/></w:pPrDefault>' + | ||
| '</w:docDefaults>' + | ||
| '<w:style w:type="paragraph" w:styleId="Normal">' + | ||
| '<w:name w:val="Normal"/>' + | ||
| '<w:qFormat/>' + | ||
| '<w:pPr/>' + | ||
| '<w:rPr/>' + | ||
| '</w:style>' + | ||
| '</w:styles>', | ||
| ); | ||
|
|
||
| const makeSettingsXml = (innerXml = '') => | ||
| parseXmlToJson( | ||
| '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' + innerXml + '</w:settings>', | ||
| ); | ||
|
|
||
| const makeDocumentXml = () => | ||
| parseXmlToJson( | ||
| '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' + | ||
| '<w:body><w:p><w:r><w:t>Test</w:t></w:r></w:p></w:body>' + | ||
| '</w:document>', | ||
| ); | ||
|
|
||
| const findViewInSettings = (settingsJson) => { | ||
| const root = settingsJson?.elements?.[0]; | ||
| return root?.elements?.find((el) => el?.name === 'w:view') || null; | ||
| }; | ||
|
|
||
| describe('w:view setting roundtrip', () => { | ||
| describe('import', () => { | ||
| it('imports w:view with val="web" from settings.xml', async () => { | ||
| const settingsXml = makeSettingsXml('<w:view w:val="web"/>'); | ||
| const docx = { | ||
| 'word/document.xml': makeDocumentXml(), | ||
| 'word/settings.xml': settingsXml, | ||
| 'word/styles.xml': minimalStylesXml, | ||
| }; | ||
|
|
||
| const { createDocumentJson } = await import('@converter/v2/importer/docxImporter.js'); | ||
| const converter = { headers: {}, footers: {}, headerIds: {}, footerIds: {}, docHiglightColors: new Set() }; | ||
| const editor = { options: {}, emit: () => {} }; | ||
|
|
||
| createDocumentJson(docx, converter, editor); | ||
|
|
||
| expect(converter.viewSetting).toBeDefined(); | ||
| expect(converter.viewSetting.val).toBe('web'); | ||
| expect(converter.viewSetting.originalXml).toBeDefined(); | ||
| expect(converter.viewSetting.originalXml.name).toBe('w:view'); | ||
| }); | ||
|
|
||
| it('imports w:view with val="print" from settings.xml', async () => { | ||
| const settingsXml = makeSettingsXml('<w:view w:val="print"/>'); | ||
| const docx = { | ||
| 'word/document.xml': makeDocumentXml(), | ||
| 'word/settings.xml': settingsXml, | ||
| 'word/styles.xml': minimalStylesXml, | ||
| }; | ||
|
|
||
| const { createDocumentJson } = await import('@converter/v2/importer/docxImporter.js'); | ||
| const converter = { headers: {}, footers: {}, headerIds: {}, footerIds: {}, docHiglightColors: new Set() }; | ||
| const editor = { options: {}, emit: () => {} }; | ||
|
|
||
| createDocumentJson(docx, converter, editor); | ||
|
|
||
| expect(converter.viewSetting).toBeDefined(); | ||
| expect(converter.viewSetting.val).toBe('print'); | ||
| }); | ||
|
|
||
| it('leaves viewSetting null when settings.xml has no w:view', async () => { | ||
| const settingsXml = makeSettingsXml('<w:compat/>'); | ||
| const docx = { | ||
| 'word/document.xml': makeDocumentXml(), | ||
| 'word/settings.xml': settingsXml, | ||
| 'word/styles.xml': minimalStylesXml, | ||
| }; | ||
|
|
||
| const { createDocumentJson } = await import('@converter/v2/importer/docxImporter.js'); | ||
| const converter = { | ||
| headers: {}, | ||
| footers: {}, | ||
| headerIds: {}, | ||
| footerIds: {}, | ||
| docHiglightColors: new Set(), | ||
| viewSetting: null, | ||
| }; | ||
| const editor = { options: {}, emit: () => {} }; | ||
|
|
||
| createDocumentJson(docx, converter, editor); | ||
|
|
||
| expect(converter.viewSetting).toBeNull(); | ||
| }); | ||
|
|
||
| it('leaves viewSetting null when settings.xml is missing entirely', async () => { | ||
| const docx = { | ||
| 'word/document.xml': makeDocumentXml(), | ||
| 'word/styles.xml': minimalStylesXml, | ||
| }; | ||
|
|
||
| const { createDocumentJson } = await import('@converter/v2/importer/docxImporter.js'); | ||
| const converter = { | ||
| headers: {}, | ||
| footers: {}, | ||
| headerIds: {}, | ||
| footerIds: {}, | ||
| docHiglightColors: new Set(), | ||
| viewSetting: null, | ||
| }; | ||
| const editor = { options: {}, emit: () => {} }; | ||
|
|
||
| createDocumentJson(docx, converter, editor); | ||
|
|
||
| expect(converter.viewSetting).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('export', () => { | ||
| it('preserves w:view val="web" through export', () => { | ||
| const viewXml = { type: 'element', name: 'w:view', attributes: { 'w:val': 'web' }, elements: [] }; | ||
| const converter = { viewSetting: { val: 'web', originalXml: carbonCopy(viewXml) } }; | ||
| const convertedXml = { 'word/settings.xml': makeSettingsXml('<w:compat/>') }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const viewEl = findViewInSettings(updatedXml['word/settings.xml']); | ||
| expect(viewEl).toBeDefined(); | ||
| expect(viewEl.attributes['w:val']).toBe('web'); | ||
| }); | ||
|
|
||
| it('preserves w:view val="print" through export', () => { | ||
| const viewXml = { type: 'element', name: 'w:view', attributes: { 'w:val': 'print' }, elements: [] }; | ||
| const converter = { viewSetting: { val: 'print', originalXml: carbonCopy(viewXml) } }; | ||
| const convertedXml = { 'word/settings.xml': makeSettingsXml('') }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const viewEl = findViewInSettings(updatedXml['word/settings.xml']); | ||
| expect(viewEl).toBeDefined(); | ||
| expect(viewEl.attributes['w:val']).toBe('print'); | ||
| }); | ||
|
|
||
| it('does not add w:view when converter has no viewSetting', () => { | ||
| const converter = { viewSetting: null }; | ||
| const convertedXml = { 'word/settings.xml': makeSettingsXml('<w:compat/>') }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const viewEl = findViewInSettings(updatedXml['word/settings.xml']); | ||
| expect(viewEl).toBeNull(); | ||
| }); | ||
|
|
||
| it('preserves other settings.xml elements alongside w:view', () => { | ||
| const viewXml = { type: 'element', name: 'w:view', attributes: { 'w:val': 'web' }, elements: [] }; | ||
| const converter = { viewSetting: { val: 'web', originalXml: carbonCopy(viewXml) } }; | ||
| const convertedXml = { | ||
| 'word/settings.xml': makeSettingsXml('<w:compat/><w:defaultTabStop w:val="720"/>'), | ||
| }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const root = updatedXml['word/settings.xml']?.elements?.[0]; | ||
| const compat = root?.elements?.find((el) => el?.name === 'w:compat'); | ||
| const tabStop = root?.elements?.find((el) => el?.name === 'w:defaultTabStop'); | ||
| const viewEl = root?.elements?.find((el) => el?.name === 'w:view'); | ||
|
|
||
| expect(compat).toBeDefined(); | ||
| expect(tabStop).toBeDefined(); | ||
| expect(viewEl).toBeDefined(); | ||
| expect(viewEl.attributes['w:val']).toBe('web'); | ||
| }); | ||
|
|
||
| it('replaces existing w:view rather than duplicating', () => { | ||
| const viewXml = { type: 'element', name: 'w:view', attributes: { 'w:val': 'normal' }, elements: [] }; | ||
| const converter = { viewSetting: { val: 'normal', originalXml: carbonCopy(viewXml) } }; | ||
| const convertedXml = { | ||
| 'word/settings.xml': makeSettingsXml('<w:view w:val="print"/><w:compat/>'), | ||
| }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const root = updatedXml['word/settings.xml']?.elements?.[0]; | ||
| const viewElements = root?.elements?.filter((el) => el?.name === 'w:view') || []; | ||
|
|
||
| expect(viewElements.length).toBe(1); | ||
| expect(viewElements[0].attributes['w:val']).toBe('normal'); | ||
| }); | ||
|
|
||
| it('preserves w:view position in element order', () => { | ||
| const viewXml = { type: 'element', name: 'w:view', attributes: { 'w:val': 'web' }, elements: [] }; | ||
| const converter = { viewSetting: { val: 'web', originalXml: carbonCopy(viewXml) } }; | ||
| const convertedXml = { | ||
| 'word/settings.xml': makeSettingsXml('<w:compat/><w:view w:val="print"/><w:defaultTabStop w:val="720"/>'), | ||
| }; | ||
|
|
||
| const { updatedXml } = prepareFootnotesXmlForExport({ | ||
| footnotes: [], | ||
| editor: {}, | ||
| converter, | ||
| convertedXml, | ||
| }); | ||
|
|
||
| const root = updatedXml['word/settings.xml']?.elements?.[0]; | ||
| const names = root?.elements?.map((el) => el?.name); | ||
|
|
||
| expect(names).toEqual(['w:compat', 'w:view', 'w:defaultTabStop']); | ||
| expect(root.elements[1].attributes['w:val']).toBe('web'); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.