fix(docx): balance single-quoted field char runs (#392)
TOKEN_RE only matched double-quoted fldCharType, so single-quoted fields unbalanced on save. Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
2c25b86402
commit
0e83b97e6f
@@ -7,7 +7,8 @@
|
||||
* written: stray separate/end runs go, and a begin left open is closed at the
|
||||
* end of its own paragraph so the field keeps its code and first result line.
|
||||
*/
|
||||
const TOKEN_RE = /<w:fldChar\b[^>]*\bw:fldCharType="(begin|separate|end)"|<w:p\b[^>]*>|<\/w:p>/g
|
||||
const TOKEN_RE =
|
||||
/<w:fldChar\b[^>]*\bw:fldCharType=(?:"(begin|separate|end)"|'(begin|separate|end)')|<w:p\b[^>]*>|<\/w:p>/g
|
||||
|
||||
interface Edit {
|
||||
start: number
|
||||
@@ -45,7 +46,7 @@ export function balanceFieldChars(bodyXml: string): string {
|
||||
if (!token.endsWith('/>')) depth++
|
||||
continue
|
||||
}
|
||||
const type = m[1]
|
||||
const type = m[1] ?? m[2]
|
||||
if (type === 'begin') {
|
||||
open.push({ depth, paraEnd: null })
|
||||
} else if (type === 'end' && open.length > 0) {
|
||||
|
||||
@@ -59,4 +59,14 @@ describe('balanceFieldChars', () => {
|
||||
const body = '<w:p/>' + p(begin + instr + sep + t('Alpha'))
|
||||
expect(balanceFieldChars(body)).toBe('<w:p/>' + p(begin + instr + sep + t('Alpha') + end))
|
||||
})
|
||||
|
||||
it('balances single-quoted fldCharType runs', () => {
|
||||
const qbegin = "<w:r><w:fldChar w:fldCharType='begin'/></w:r>"
|
||||
const qen = "<w:r><w:fldChar w:fldCharType='end'/></w:r>"
|
||||
const body = p(qbegin + instr + sep + t('Alpha')) + p(t('Beta'))
|
||||
const out = balanceFieldChars(body)
|
||||
expect(out).toContain('Alpha')
|
||||
expect(out).toContain('w:fldCharType="end"')
|
||||
expect(balanceFieldChars(p(t('x')) + p(qen))).toBe(p(t('x')) + p(''))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user