News for hackers. Stuff that matters.
Re:A contentEditable, pasted garbage and caret placement wal... by akersten on Friday July 23, 2021 @09:55PM (#27937776)
So when stuff is pasted in from Word or whatever other weird Rich Text Enterprise application - what's actually doing the heavy lifting of translating all that \\rtf\\section\\block\\blockend\\secondend garbage into HTML? Is it the `sanitize-html` library here? Is it the browser? My first intuition would be "pasting a bunch of formatted stuff from Wordpad into an HTML document surely doesn't result in anything reasonable, right?" But then seeing the direct jump from "alright, stuff was pasted in, now we only want to allow these HTML tags" makes me think somewhere in here some magic happened to convert it.

I think that's probably the most interesting part here. What happens for images? Do they get converted into an <img src="data:base-64;..." type thing?

Re:A contentEditable, pasted garbage and caret placement wal... by ljm on Friday July 23, 2021 @10:14PM (#27937854)
A native app will copy a selection into the clipboard in several ways, basically accounting for a selection of common MIME-types.

So when you copy an image from Finder in MacOS, if you paste it into a terminal you get the file path and if you paste it into a comment on github you get the contents of the file as a binary blob.

When pasting, an application is essentially querying the clipboard for a representation of the data it can use, and if there is none, it either defaults to something or fails.

In a well designed program, all of the translating and parsing is done before sending to the clipboard. If you copy text from MS Word and paste it into a content editable field in a website, you might expect some consistency.

Re:A contentEditable, pasted garbage and caret placement wal... by iudqnolq on Friday July 23, 2021 @10:46PM (#27937996)
I believe at least on windows this bit isn't always true

> In a well designed program, all of the translating and parsing is done before sending to the clipboard.

To avoid unnecessary work a program can opt to delay rendering until it's pasted or the program is closed. This is why excel will sometimes ask you if you want to keep your clipboard on save.

Re:A contentEditable, pasted garbage and caret placement wal... by TazeTSchnitzel on Saturday July 24, 2021 @07:27AM (#27939959)
Based on behaviour I've observed on Linux before, I think X11 might only use delayed rendering (at least for non-text stuff), and so pasting from a closed application just hangs the recipient.
Re:A contentEditable, pasted garbage and caret placement wal... by tW4r on Saturday July 24, 2021 @12:42AM (#27938451)
Is there any way to force which gets chosen? I love being able to copy and image and just paste it to WhatsApp/Messenger/Whichever other client, but if I do copy an image from the internet it’s very hit or miss if it shows up as an image when I paste, or a link. If I download the image, and then copy the downloaded one, it always shows up as an image
Re:A contentEditable, pasted garbage and caret placement wal... by TazeTSchnitzel on Saturday July 24, 2021 @07:26AM (#27939957)
I ended up working around this by copying into GIMP and copying from there, but of course that's a suboptimal workaround. There's surely some small utility application for this.
Re:A contentEditable, pasted garbage and caret placement wal... by reportgunner on Saturday July 24, 2021 @08:23AM (#27940293)
The snipping tool.
Re:A contentEditable, pasted garbage and caret placement wal... by dmitryminkovsky on Friday July 23, 2021 @11:47PM (#27938240)
> If you copy text from MS Word and paste it into a content editable field in a website, you might expect some consistency

Hard-fought consistency... https://github.com/ProseMirror/prosemirror-schema-basic/blob...

Re:A contentEditable, pasted garbage and caret placement wal... by iudqnolq on Friday July 23, 2021 @10:12PM (#27937847)
When you copy to the clipboard (or sometimes lazily at time of paste) the content is represented in multiple forms. One of them is pseudo-html.
Re:A contentEditable, pasted garbage and caret placement wal... by askonomm on Saturday July 24, 2021 @10:42AM (#27941347)
When pasting from Word, the contentEditable will gladly accept any formatting that came with the paste (e.g whatever strongs, bolds, italics, links, headings, tables, what-not you may have in your clipboard), and so the sanitize-html NPM library essentially strips out the elements I do not want for UX consistency. Since I'm building this contentEditable just for a singular paragraph as part of a larger block-based editor, I only want it to contain what a regular paragraph would (e.g bold, italic, link), but nothing else.

So, all-in-all, the clipboard contains still everything, the sanitize-html library would strip things from it that you wouldn't want, so that you're left with only the formatting you do want. As for images, since I'm stripping them out as well, there will not be any images. By default I'm not sure if they end up as base-64 (I'm leaning to believe that rather they would get local URL's like file://...).

Re:A contentEditable, pasted garbage and caret placement wal... by ectopod on Saturday July 24, 2021 @07:46AM (#27940077)
Rich Text Format is the default representation for formatted text on the Windows clipboard. Any application that can paste rich text should accept it. When you paste directly into a content-editable the browser should convert the rtf into html.

Most modern apps will also copy an html fragment to the clipboard so the browser could use that directly.

According to stackoverflow, when you read from the clipboard in javascript the browser gives you access to its synthesised html version of the rtf content via ClipboardItem.getType. That's what you would expect, but I haven't tried it.