Skip to content

Bug: Text styles lost when using grouped formatting #41

@david-cilluffo

Description

@david-cilluffo

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:

  1. Text "italic" is added to child group with {italic: true}
  2. When group ends, item is moved to parent via addContent()
  3. addContent() overwrites style with parent's style {}
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions