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

wymeditor: Adds paste listener to strip superfluous code #345

Merged
merged 1 commit into from
Oct 6, 2020

Conversation

laurensmartina
Copy link
Contributor

Fixes #66

@laurensmartina
Copy link
Contributor Author

Based on the #314 by @RoukePouw and processed comments by @matthijskooijman

@matthijskooijman
Copy link
Contributor

In #66, I wrote:

One thing to consider: If this catches all pastes, that might be counter-productive when pasting within the wymeditor (e.g. if you want to move a bit of test with markup within a page or article). If that is currently also pasted through a textarea, stripping all markup, that might break things like creating a homepage or newsletter (though you can always paste raw HTML maybe).

I don't think this is addressed in this PR, right? But I think it should be somehow adressed?

@laurensmartina
Copy link
Contributor Author

In #66, I wrote:

One thing to consider: If this catches all pastes, that might be counter-productive when pasting within the wymeditor (e.g. if you want to move a bit of test with markup within a page or article). If that is currently also pasted through a textarea, stripping all markup, that might break things like creating a homepage or newsletter (though you can always paste raw HTML maybe).

I don't think this is addressed in this PR, right? But I think it should be somehow adressed?

Good point! I've raised question as well and talked to @giplt about this. His reaction, and I agree, is that is more preferable to implement this feature now, with the downside that all pastes are stripped and later fix it so that it only targets Word-like code.

@matthijskooijman
Copy link
Contributor

A "copy" event handler exists: https://developer.mozilla.org/en-US/docs/Web/API/Element/copy_event which can probably manually retrieve the selected content, customize it to mark it as "from inside hypha" so that the paste event handler can recognize it and skip the format-stripping.

One way I thought we could mark the data as being "from inside hypha" could be to tweak the "format" on the clipboard. The clipboard typically contains multiple versions (e.g. "text/plain" and "text/html"), and I hoped we could just add an "application/hypha-html" or something in addition to the default types, with identical content to the "text/html" version. However, reading the spec, I think that the formats that can be written from "untrusted" scripts is limited: https://w3c.github.io/clipboard-apis/#writing-to-clipboard.

So I guess adding some wrapper or marker inside the "text/html" version should be done instead. This marker should ideally be invisible and unobtrusive, for the case that this HTML is pasted elsewhere.

We should probably also try to generate a "text/plain" version, to allow copy-pasting from Wymeditor into a plain textarea or so.

Note that this makes *all* pastes text-only and also prevents
copy-pasting within the same editor, or between editors. This should be
solved later.

There also seems to be a corner case in the `wym.paste` function where
the text is interpreted as HTML rather than text (i.e. any HTML tags in
the text content, which must have been escaped as e.g. `>` in the
original clipboard contents, would become actual HTML again after the
paste). This only occurs when inserting into the outer body tag, so when
no other container is selected (which is pretty hard to achieve, except
when the editor is completely empty). This also seems to occur with the
existing "Paste from word" feature as well. Since this is an existing
and rare problem, it is left to be solved later.

This fixes the main part of #66
@matthijskooijman
Copy link
Contributor

Rebased and updated the commit message, merging next.

@matthijskooijman
Copy link
Contributor

I created #348 for some followup improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

copy/paste from word without superfluous code
2 participants