Table and HTML paste handlers both insert content for the same paste event

Đang mở Phù hợp với người mới
#129 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, markdown
Lĩnh vực
frontend, web-dev

Hướng nghiên cứu

Đọc src/index.ts để theo dõi thứ tự subscribe() cài đặt các trình xử lý, sau đó kiểm tra src/paste-markdown-table.ts và src/paste-markdown-html.ts để xem hành vi của chúng khi có sự kiện dán. Tái hiện việc chèn trùng lặp bằng ví dụ tối giản của issue và kiểm tra cách mỗi trình xử lý xử lý một sự kiện đã bị ngăn chặn. Công việc hoàn tất khi một sự kiện dán chỉ chèn nội dung một lần; báo cáo không nêu tên tệp kiểm thử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

A single paste event containing an HTML table and a link can insert the selection twice: once through the table handler and again through the HTML/link handler.

This was reported when copying from BlockNote to GitHub: https://github.com/TypeCellOS/BlockNote/issues/3305. However, the duplication can be reproduced with this package alone and with ordinary text/plain content—no BlockNote dependency or Markdown in the plain-text representation is necessary.

Verification

Reproduced in a browser using the published @github/[email protected] package:

  1. The table handler inserts its result and calls preventDefault() and stopPropagation().
  2. The HTML handler subsequently runs on the same textarea with event.defaultPrevented === true and inserts another representation of the selection.
  3. Skipping handlers when event.defaultPrevented is already true prevents the second insertion.

Both handlers are registered by subscribe(), and the duplication also reproduces using subscribe() instead of installing the two handlers individually.

This verifies the published library's behavior; it is not a separate verification of GitHub.com's currently deployed bundle.

Minimal reproduction

Save the following as an HTML file and open it in a browser with network access. Click Run. The results show the unmodified behavior and a guarded control. The control wraps listener registration only to demonstrate the effect of checking defaultPrevented; it is not intended as the proposed implementation.

<!doctype html>
<meta charset="utf-8">
<button id="run">Run</button>
<pre id="results"></pre>
<script type="module">
  import {installTable, installHTML} from 'https://esm.sh/@github/[email protected]?bundle';

  function run(guard) {
    const field = document.createElement('textarea');
    document.body.append(field);
    field.focus();
    const calls = [];
    const original = field.addEventListener;
    field.addEventListener = function(type, listener, options) {
      if (type !== 'paste') return original.call(this, type, listener, options);
      return original.call(this, type, function(event) {
        calls.push({defaultPreventedAtEntry: event.defaultPrevented, skipped: guard && event.defaultPrevented});
        if (guard && event.defaultPrevented) return;
        listener.call(this, event);
      }, options);
    };
    installTable(field);
    installHTML(field);
    const data = new DataTransfer();
    data.setData('text/html', '<p><a href="https://example.com/test">Test</a></p><table><tr><th>A</th><th>B</th></tr><tr><td>C</td><td>D</td></tr></table>');
    // Ordinary plain text, deliberately NOT Markdown.
    data.setData('text/plain', 'Test\nA\tB\nC\tD');
    field.dispatchEvent(new ClipboardEvent('paste', {
      clipboardData: data,
      bubbles: true,
      cancelable: true
    }));
    const result = {guard, calls, output: field.value};
    field.remove();
    return result;
  }

  document.querySelector('#run').addEventListener('click', function() {
    document.querySelector('#results').textContent = JSON.stringify([run(false), run(true)], null, 2);
  });
</script>
Actual result without guard

Handler entry states:

[
  {"defaultPreventedAtEntry": false, "skipped": false},
  {"defaultPreventedAtEntry": true, "skipped": false}
]

Textarea contents:

<p><a href="https://example.com/test">Test</a></p>

A | B
-- | --
C | D

[Test](https://example.com/test)
A	B
C	D

With the guard enabled, the second handler sees defaultPrevented === true and is skipped, removing the duplicate insertion.

Expected behavior

Only one handler should insert content for a paste event. Handlers should coordinate ownership of the event, for example by respecting event.defaultPrevented before processing an already-handled paste.

stopPropagation() does not stop other listeners on the same element, and preventDefault() does not prevent those listeners from running either.

Relevant source

At commit 86be75f8b746fcfc40cb1a9850f7124980c9253a:

Related but separate issues

  • https://github.com/github/paste-markdown/issues/96 covers nested Markdown links when text/plain already contains Markdown. This report is specifically about duplicate insertion, which also occurs with unformatted plain text.
  • The table handler retaining surrounding HTML is another concern; preventing duplicate insertion alone does not fix that behavior.
  • https://github.com/opf/openproject/pull/25964 documents a downstream workaround using data-paste-markdown-skip on tables.
Ngôn ngữ chính
JavaScript
Star
232
Fork
48
Merge trung bình
17 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của github/paste-markdown

Tất cả issue của github/paste-markdown

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.