Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pasting HTML with newlines #3479

Closed
wants to merge 1 commit into from
Closed

Conversation

alecgibson
Copy link
Contributor

At the moment, if you paste the following HTML, the newline between the
<span> elements is incorrectly discarded.

<span>foo</span>
<span>bar</span>

This will currently insert foobar into Quill, when we'd expect
foo bar, since newlines should treated as spaces between inline
elements (such as <span>).

This change updates the matchText() clipboard matcher to check if the
text node is between inline elements or not before early-returning.

@alecgibson
Copy link
Contributor Author

A possible alternative solution could be:

function convertNewlinesToSpaces(root) {
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
  let node = walker.nextNode();
  while (node) {
    if (!isPre(node)) node.textContent = node.textContent.replace(/\n/g, ' ');
    node = walker.nextNode();
  }
}

But that feels potentially a bit "aggressive"

At the moment, if you paste the following HTML, the newline between the
`<span>` elements is incorrectly discarded.

```html
<span>foo</span>
<span>bar</span>
```

This will currently insert `foobar` into Quill, when we'd expect
`foo bar`, since newlines should [treated as spaces][1] between inline
elements (such as `<span>`).

This change updates the `matchText()` clipboard matcher to check if the
text node is between inline elements or not before early-returning.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace
@luin
Copy link
Member

luin commented Jan 31, 2024

Cherry-picked to #3983

@luin luin closed this Jan 31, 2024
@alecgibson alecgibson deleted the clipboard-fix branch February 9, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants