Skip to content

[Windows] Pasting formatted text from Word on Windows sometimes throws FormatException #2650

@d-ukhanov

Description

@d-ukhanov

Have you checked for an existing issue?

Flutter Quill Version

11.4.2

Steps to Reproduce

  1. On Windows, open Microsoft Word and copy the formatted text.
  2. Focus a QuillEditor and paste (Ctrl+V).

Expected results

Text should be pasted with formatting.

Actual results

For the same Word content the behavior is inconsistent:

  1. Sometimes formatted text is pasted correctly.
  2. Other times nothing is inserted and the following error is logged to the console.

Additional Context

Screenshots / Video demonstration Word text: Image

When paste work:

Image

When don't work (with my workaround in onClipboardPaste):

Image
Logs
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: FormatException: Missing extension byte (at offset 46628)
#0      _Utf8Decoder.convertSingle (dart:convert-patch/convert_patch.dart:1859:7)
#1      Utf8Decoder.convert (dart:convert/utf.dart:350:37)
#2      Utf8Codec.decode (dart:convert/utf.dart:63:20)
#3      Utf8Pointer.toDartString (package:ffi/src/utf8.dart:48:17)
#4      QuillNativeBridgeWindows.getClipboardHtml (package:quill_native_bridge_windows/quill_native_bridge_windows.dart:87:44)
#5      QuillNativeBridge.getClipboardHtml (package:quill_native_bridge/quill_native_bridge.dart:64:51)
#6      DefaultClipboardService.getHtmlText (package:flutter_quill/src/editor_toolbar_controller_shared/clipboard/default_clipboard_service.dart:19:47)
<asynchronous suspension>
#7      QuillControllerRichPaste.pasteHTML.getHTML (package:flutter_quill/src/controller/clipboard/quill_controller_rich_paste.dart:25:33)
<asynchronous suspension>
#8      QuillControllerRichPaste.pasteHTML (package:flutter_quill/src/controller/clipboard/quill_controller_rich_paste.dart:36:22)
<asynchronous suspension>
#9      QuillController.clipboardPaste (package:flutter_quill/src/controller/quill_controller.dart:572:32)
<asynchronous suspension>
#10     QuillRawEditorState.pasteText (package:flutter_quill/src/editor/raw_editor/raw_editor_state.dart:145:9)
<asynchronous suspension>
Workarounds
  1. Disable external rich paste:
QuillControllerConfig(
  clipboardConfig: QuillClipboardConfig(enableExternalRichPaste: false),
)
  1. Catch the FormatException and fallback to plain text (example used in my app):
QuillClipboardConfig(
  onClipboardPaste: () async {
    try {
      final html = await QuillNativeBridge().getClipboardHtml();
      if (html != null && html.trim().isNotEmpty) return false;
      return false;
    } on FormatException catch (e, st) {
      // log and fallback to plain text
      final data = await Clipboard.getData(Clipboard.kTextPlain);
      final plain = data?.text ?? '';
      if (plain.isEmpty) return true;
      final sel = controller.selection;
      final index = sel.start.clamp(0, controller.document.length);
      final replaceLen =
          (sel.end - sel.start).clamp(0, controller.document.length - index);
      controller.replaceText(
        index,
        replaceLen,
        plain,
        TextSelection.collapsed(offset: index + plain.length),
      );
      return true;
    }
  },
)

A .docx file that can be used for testing:

test.docx

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions