-
Notifications
You must be signed in to change notification settings - Fork 42
Description
Summary
Text styles (italic, bold, underline, etc.) are lost when the styled text is within an RTF group {...}. This affects common RTF patterns like {\i text} or {\b text}.
Steps to Reproduce
const rtfParser = require('rtf-parser');
const rtf = String.raw`{\rtf1\ansi\deff0{\fonttbl{\f0 Times;}}
\pard Normal text {\i1 italic text} more normal\par}`;
rtfParser.string(rtf, (err, doc) => {
// The text "italic text" should have style.italic = true
// but it's missing because the style gets overwritten
});Expected Behavior
The text "italic text" should have style.italic = true.
Actual Behavior
The text "italic text" has no italic style - it appears as plain text.
Root Cause
In rtf-interpreter.js (lines 101-104), when a group ends, content items are moved to the parent by calling doc.addContent(item). However, addContent() in rtf-group.js (line 29) overwrites the node's style with the parent's style:
addContent (node) {
node.style = Object.assign({}, this.getStyle()) // Overwrites existing style!
...
}This means:
- Text "italic" is added to child group with
{italic: true} - When group ends, item is moved to parent via
addContent() addContent()overwrites style with parent's style{}- Italic formatting is lost
Proposed Fix
In rtf-interpreter.js, change lines 102-104 from:
for (const item of endingGroup.content) {
doc.addContent(item)
}To:
doc.content.push(...endingGroup.content)This pushes items directly without re-processing styles, since styles were already correctly applied when the content was added to the child group.
Affected Applications
This bug affects RTF from:
- Scrivener (uses
{\i1 text}format) - Microsoft Word (uses grouped formatting)
- Most RTF producers that use standard grouping for formatting
I have a fix ready and tested - happy to submit a PR if you're interested.