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

🐛 Run clipboard matchers against plain text pastes #4002

Closed

Conversation

alecgibson
Copy link
Contributor

This is a reimplementation of #3530

At the moment, if the clipboard pastes text/plain content and no text/html content, the Clipboard.convert() function will completely skip the matching logic.

This is surprising when registering text node clipboard matchers.

This change updates the convert() function to match the plain text against the plain text matchers, just like we do with HTML.

Note that these matchers will run before applying the formats for "paste and match style", so they won't match an element matchers for the target formatting (which I think should be expected anyway).

This is a reimplementation of slab#3530

At the moment, if the clipboard pastes `text/plain` content and no
`text/html` content, the `Clipboard.convert()` function will completely
skip the matching logic.

This is surprising when registering text node clipboard matchers.

This change updates the `convert()` function to match the plain text
against the plain text matchers, just like we do with HTML.

Note that these matchers will run _before_ applying the formats for
["paste and match style"][1], so they won't match an element matchers
for the target formatting (which I think should be expected anyway).

[1]: slab#3927
@luin
Copy link
Member

luin commented Feb 12, 2024

It fails when text contains \n as it converts it to spaces. Added test cases: #4005

Come to think about it, I'm not sure if it's technically correct to say a plain text is a Node.TEXT_NODE, as it's not a part of an HTML document at all.

I think we can keep this part as is, and for users who want to change the behavior can override the convert method and apply their handlers. WDYT?

@alecgibson
Copy link
Contributor Author

I guess that's a fair comment that it's not technically yet a TEXT_NODE (even if it's about to become one...).

I think I personally find it a bit surprising (hence the PR), and it's a shame there's no entrypoint to just handle pasted "text" (eg if you want to turn ... into ; linkify URLs like in the unit test; strip unwanted characters like weird whitespace; etc.). I think it leads to inconsistent pasting behaviour if consumers aren't careful.

In future I think I'd argue for either renaming Node.TEXT_NODE -> 'text', or at least adding a 'text' matcher type that would match both TEXT_NODE, and plain text pastes.

@alecgibson alecgibson closed this Feb 12, 2024
@alecgibson alecgibson deleted the plain-text-clipboard-matchers branch February 12, 2024 08:34
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.

2 participants