Skip to content

Fix: data loss when parsing Delta to Document - #7

Merged
CatHood0 merged 3 commits into
Mainfrom
fix_data_losses
Jul 24, 2025
Merged

Fix: data loss when parsing Delta to Document#7
CatHood0 merged 3 commits into
Mainfrom
fix_data_losses

Conversation

@CatHood0

@CatHood0 CatHood0 commented Jul 24, 2025

Copy link
Copy Markdown
Owner

Description

The insert method in the Document class fails to properly handle transitions between paragraphs, leading to unintended data loss—specifically, content before the first newline is discarded.

The original implementation relies on an outdated API behavior, where it only checks if the last paragraph is empty or ends with an empty line before merging content. This logic is insufficient, as it doesn't account for cases where a paragraph break should be enforced (e.g., when shouldBreakToNext is true).

Problematic Code:

if (lastParagraph != null) {
  if (lastParagraph.isEmpty || (lastParagraph.last?.isEmpty ?? false)) {
    if (lastParagraph.isSealed) {
      lastParagraph.unseal();
    }
    lastParagraph.insertAll(paragraph.lines);
    lastParagraph.setType(paragraph.type);
    lastParagraph.blockAttributes = lastParagraph.blockAttributes;
    if ((lastParagraph.isBlock ||
          lastParagraph.isEmbed ||
          lastParagraph.isNewLine) &&
         !lastParagraph.isSealed) {
      lastParagraph.seal(sealLines: true);
    }
    updateLast(paragraph);
    return;
  }
  paragraphs.add(paragraph);
}

Flaws:

  • Does not respect paragraph-breaking conditions (shouldBreakToNext).
  • Incorrectly reuses lastParagraph.blockAttributes instead of the new paragraph's attributes.
  • May improperly merge content when it should start a new paragraph.

Solution:

The fix involves better utilization of the existing API with updated logic:

  1. Check for required paragraph breaks (shouldBreakToNext) before merging.
  2. Properly handle empty paragraphs by inserting new content correctly.
  3. Ensure block attributes are taken from the new paragraph, not the old one.
if (lastParagraph != null) {
  if (lastParagraph.shouldBreakToNext) {
    lastParagraph.unseal();
    lastParagraph
      ..removeLastLineIfNeeded()
      ..seal();
    updateLast(lastParagraph);
  }

  if (lastParagraph.isEmpty) {
    lastParagraph.unseal();
    lastParagraph.insertAll(paragraph.lines);
    lastParagraph.setType(paragraph.type);
    lastParagraph.blockAttributes = paragraph.blockAttributes;
    if ((lastParagraph.isBlock ||
            lastParagraph.isEmbed ||
            lastParagraph.isNewLine) &&
        !lastParagraph.isSealed) {
      lastParagraph.seal(sealLines: true);
    }
    updateLast(lastParagraph);
    return;
  }
}

paragraphs.add(paragraph);

Related issues

Type change

  • Feature: New functionality without breaking existing features.
  • 🛠️ Bug fix: Resolves an issue without altering current behavior.
  • 🧹 Refactor: Code reorganization, no behavior change.
  • Breaking: Alters existing functionality and requires updates.
  • 🧪 Tests: New or modified tests
  • 📝 Documentation: Updates or additions to documentation.
  • 🗑️ Chore: Routine tasks, or maintenance.
  • Build configuration change: Build/configuration changes.

@CatHood0 CatHood0 self-assigned this Jul 24, 2025
@CatHood0 CatHood0 added bug Something isn't working documentation Improvements or additions to documentation labels Jul 24, 2025
@CatHood0
CatHood0 merged commit 199b601 into Main Jul 24, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue: Data Loss When Parsing Document

1 participant