fix(docx): decode XML entities exactly once in parsed run text (#89)

fast-xml-parser already resolves the five named entities in text and
attribute values, so calling decodeEntities() on parser output decoded
them a second time: a document whose visible text literally contains
"&lt;" or "&amp;" turned into "<" / "&" at parse time, and editing the
paragraph then wrote the corrupted text back to word/document.xml.

Split out decodeNumericCharRefs() — numeric character references are
the only thing the parser leaves unresolved — and use it at the call
sites that read text or attributes off the parse tree. Call sites that
decode raw XML slices keep the full decode.

Fixes #88

Co-authored-by: codeAnqiang-ma <273298913+codeAnqiang-ma@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
anchor
2026-08-13 15:55:35 +08:00
committed by GitHub
co-authored by codeAnqiang-ma Cursor
parent dc4d7e5927
commit 945c370fb9
2 changed files with 82 additions and 20 deletions
+33 -20
View File
@@ -1858,7 +1858,7 @@ function extractTextboxes(
for (const r of childrenOf(p)) {
if (nameOf(r) !== 'a:r') continue
const t = findChild(r, 'a:t')
const text = t ? decodeEntities(textOf(t)) : ''
const text = t ? decodeNumericCharRefs(textOf(t)) : ''
if (text === '') continue
const run: Run = { text }
const rPr = findChild(r, 'a:rPr')
@@ -2056,7 +2056,9 @@ const SIMPLE_INLINE_FIELD_RE = /^\s*(DATE|TIME|CREATEDATE|SAVEDATE|NUMPAGES|FILE
/** HYPERLINK "url" (optional \o "tip"): the only field form folded into an editable link run;
* any other switch (\l bookmark, \t frame...) keeps the protected-passthrough path */
function convertibleHyperlink(instr: string): { href: string; tooltip?: string } | null {
const m = /^\s*HYPERLINK\s+"([^"\\]+)"\s*(?:\\o\s+"([^"]*)"\s*)?$/.exec(decodeEntities(instr))
const m = /^\s*HYPERLINK\s+"([^"\\]+)"\s*(?:\\o\s+"([^"]*)"\s*)?$/.exec(
decodeNumericCharRefs(instr),
)
if (!m) return null
return { href: m[1], ...(m[2] ? { tooltip: m[2] } : {}) }
}
@@ -2285,7 +2287,7 @@ function rubyPartText(rubyNode: XNode, part: 'w:rubyBase' | 'w:rt'): string {
for (const r of childrenOf(partNode)) {
if (nameOf(r) !== 'w:r') continue
for (const c of childrenOf(r)) {
if (nameOf(c) === 'w:t') text += decodeEntities(textOf(c))
if (nameOf(c) === 'w:t') text += decodeNumericCharRefs(textOf(c))
}
}
return text
@@ -2367,7 +2369,7 @@ function buildRun(
let text = ''
for (const child of childrenOf(rNode)) {
const name = nameOf(child)
if (name === 'w:t' || name === 'w:delText') text += decodeEntities(textOf(child))
if (name === 'w:t' || name === 'w:delText') text += decodeNumericCharRefs(textOf(child))
else if (name === 'w:tab') text += '\t'
// In-paragraph page breaks (w:br w:type="page") are encoded as \f and preserved; column/soft breaks become \n
else if (name === 'w:br') text += attrsOf(child)['w:type'] === 'page' ? '\f' : '\n'
@@ -3355,20 +3357,31 @@ function mathTokens(xml: string): string[] {
return tokens
}
/**
* Numeric character references only (`&#65;` / `&#xF0B7;`), which fast-xml-parser
* leaves alone. Text and attribute values read off the parse tree have already had
* the five named entities resolved, so they must NOT be run through the named-entity
* replacements again: a document whose visible text is literally `&lt;` is stored as
* `&amp;lt;`, and a second decode would turn it into `<`.
*/
function decodeNumericCharRefs(text: string): string {
return text.replace(
/&#(?:x([0-9a-f]+)|([0-9]+));/gi,
(entity, hex: string | undefined, decimal: string | undefined) => {
const codePoint = parseInt(hex ?? decimal ?? '', hex ? 16 : 10)
return Number.isFinite(codePoint) &&
codePoint >= 0 &&
codePoint <= 0x10ffff &&
!(codePoint >= 0xd800 && codePoint <= 0xdfff)
? String.fromCodePoint(codePoint)
: entity
},
)
}
/** Full decode, for raw XML slices that never passed through the parser. */
function decodeEntities(text: string): string {
return text
.replace(
/&#(?:x([0-9a-f]+)|([0-9]+));/gi,
(entity, hex: string | undefined, decimal: string | undefined) => {
const codePoint = parseInt(hex ?? decimal ?? '', hex ? 16 : 10)
return Number.isFinite(codePoint) &&
codePoint >= 0 &&
codePoint <= 0x10ffff &&
!(codePoint >= 0xd800 && codePoint <= 0xdfff)
? String.fromCodePoint(codePoint)
: entity
},
)
return decodeNumericCharRefs(text)
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
@@ -3880,7 +3893,7 @@ function extractLockedCanvas(xml: string, ctx: BuildContext): DiagramDisplay | n
const parts: string[] = []
for (const r of findChildren(p, 'a:r')) {
const t = findChild(r, 'a:t')
if (t) parts.push(decodeEntities(textOf(t)))
if (t) parts.push(decodeNumericCharRefs(textOf(t)))
const rPr = findChild(r, 'a:rPr')
if (rPr && sizePt === undefined) {
const sz = parseInt(attrsOf(rPr)['sz'] ?? '', 10)
@@ -4054,7 +4067,7 @@ async function extractDiagramDrawing(
const parts: string[] = []
for (const r of findChildren(p, 'a:r')) {
const t = findChild(r, 'a:t')
if (t) parts.push(decodeEntities(textOf(t)))
if (t) parts.push(decodeNumericCharRefs(textOf(t)))
const rPr = findChild(r, 'a:rPr')
if (rPr && sizePt === undefined) {
const sz = parseInt(attrsOf(rPr)['sz'] ?? '', 10)
@@ -4586,7 +4599,7 @@ function parseNumberingLevel(lvlNode: XNode): NumberingLevel {
const start = parseInt(attrsOf(findChild(lvlNode, 'w:start') ?? {})['w:val'] ?? '0', 10)
const level: NumberingLevel = {
numFmt: attrsOf(findChild(lvlNode, 'w:numFmt') ?? {})['w:val'] ?? 'decimal',
lvlText: decodeEntities(attrsOf(findChild(lvlNode, 'w:lvlText') ?? {})['w:val'] ?? ''),
lvlText: decodeNumericCharRefs(attrsOf(findChild(lvlNode, 'w:lvlText') ?? {})['w:val'] ?? ''),
start: Number.isFinite(start) ? start : 0,
}
const lvlPPr = findChild(lvlNode, 'w:pPr')
@@ -0,0 +1,49 @@
import JSZip from 'jszip'
import { describe, expect, it } from 'vitest'
import { parseDocx, saveDocx } from '../src/index'
import { buildDocx } from './helpers/build-docx'
/**
* fast-xml-parser resolves the five named XML entities on its own, so text read off
* the parse tree only needs numeric character references resolved on top of it.
* Decoding the named entities a second time rewrites a document's own literal
* "&lt;" / "&amp;" text into "<" / "&", and regenerating the paragraph then persists
* that damage to word/document.xml.
*/
describe('entity decoding in run text', () => {
// XML 1.0 stores literal entity text with the ampersand escaped; Word displays
// this run as: Escape &amp; as &lt;b&gt; ok
const STORED = 'Escape &amp;amp; as &amp;lt;b&amp;gt; ok'
const DISPLAYED = 'Escape &amp; as &lt;b&gt; ok'
it('keeps text that is literally an entity reference', async () => {
const bytes = await buildDocx({
bodyXml: `<w:p><w:r><w:t xml:space="preserve">${STORED}</w:t></w:r></w:p>`,
})
const doc = await parseDocx(bytes)
expect(doc.blocks[0].runs!.map((r) => r.text).join('')).toBe(DISPLAYED)
})
it('still resolves a real numeric character reference beside literal entity text', async () => {
const bytes = await buildDocx({
bodyXml: '<w:p><w:r><w:t xml:space="preserve">&amp;lt; then &#x2713;</w:t></w:r></w:p>',
})
const doc = await parseDocx(bytes)
expect(doc.blocks[0].runs!.map((r) => r.text).join('')).toBe('&lt; then ✓')
})
it('does not rewrite an untouched run when a sibling run is edited', async () => {
const bytes = await buildDocx({
bodyXml:
`<w:p><w:r><w:t xml:space="preserve">${STORED} </w:t></w:r>` +
'<w:r><w:rPr><w:b/></w:rPr><w:t>tail</w:t></w:r></w:p>',
})
const doc = await parseDocx(bytes)
const runs = doc.blocks[0].runs!.map((r) => (r.bold ? { ...r, text: 'edited' } : r))
const saved = await saveDocx(doc, [{ kind: 'generated', block: { type: 'paragraph', runs } }])
const zip = await JSZip.loadAsync(saved)
const xml = await zip.file('word/document.xml')!.async('string')
expect(xml).toContain(`${STORED} `)
expect(xml).toContain('edited')
})
})