fix(docx): decode numeric char refs in notes and text patches (#319)

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-13 16:41:14 +08:00
committed by GitHub
co-authored by Aniruddha Adak
parent ae6c94df44
commit 601ba438d7
4 changed files with 21 additions and 16 deletions
+2 -7
View File
@@ -1,3 +1,4 @@
import { decodeEntities } from './parse-xml-text'
import { patchParagraphTexts } from './text-patch'
import type { NoteInfo, NoteRun } from './types'
import { escapeXmlAttr, escapeXmlText } from './xml-utils'
@@ -200,13 +201,7 @@ function notePlainText(xml: string): string {
const re = /<w:t(?:\s[^>]*)?>([\s\S]*?)<\/w:t>/g
let m: RegExpExecArray | null
while ((m = re.exec(xml)) !== null) texts.push(m[1])
return texts
.join('')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&amp;/g, '&')
return decodeEntities(texts.join(''))
}
/** default separator entries required by Word when the part is created fresh */
+1 -9
View File
@@ -1,3 +1,4 @@
import { decodeEntities } from './parse-xml-text'
import { escapeXmlText } from './xml-utils'
/**
@@ -126,15 +127,6 @@ function decodedTextOf(paraXml: string): string {
.join('')
}
function decodeEntities(s: string): string {
return s
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&amp;/g, '&')
}
function patchOneParagraph(paraXml: string, newText: string, skipLeading: boolean): string | null {
const slices = tSlices(paraXml)
if (slices.length === 0) return null
+13
View File
@@ -368,4 +368,17 @@ describe('rich-text footnote display runs', () => {
{ text: ' tail', color: '1F4E79', sizeHalfPoints: 16 },
])
})
it('decodes numeric char refs in note display text like the body path does', async () => {
const footnotesXml =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<w:footnotes xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' +
'<w:footnote w:id="1"><w:p><w:r><w:footnoteRef/></w:r>' +
'<w:r><w:t>A &#8212; B</w:t></w:r></w:p></w:footnote>' +
'</w:footnotes>'
const { parseNotesXml } = await import('../src/notes')
const notes = parseNotesXml(footnotesXml, 'footnote')
expect(notes).toHaveLength(1)
expect(notes[0]!.text).toBe('A — B')
})
})
@@ -65,6 +65,11 @@ describe('patchParagraphTexts', () => {
expect(out).toContain('<w:rPr><w:i/></w:rPr>')
expect(out).toContain('斜体脚注改')
})
it('leaves entity-encoded text byte-identical when nothing changed', () => {
const entry = '<w:comment w:id="1"><w:p><w:r><w:t>A &#8212; B</w:t></w:r></w:p></w:comment>'
expect(patchParagraphTexts(entry, 'A — B')).toBe(entry)
})
})
describe('rich-text comment/footnote edits use the surgical patch', () => {