How to Copy-Paste YouTube Comments With Formatting in Firefox

TL;DR: Copy more. Extending your selection outside the comment body will prevent the bug from triggering.

For the last little while, I’d been suffering from an annoying bug where, if I tried to copy and paste YouTube comments, I’d lose all the line breaks and have one big ugly wall of text.

I finally decided to go to Bugzilla and report it, only to discover that someone else had reported it two years ago and there appeared to be no progress, so I decided to see if I could figure out what was going on… and not only did I narrow it down to something actionable, I realized why I had only started to suffer from it much more recently.

YouTube serves up its comments in a custom HTML element named <yt-formatted-string> and, in the DOM inspector, it looked like the element contained exactly the problem text I was getting when I tried to copy and paste.

At first, this had me worried, as visions of custom rendering and esoteric bugs danced through my head but, as I continued to poke around, I noticed two things:

First, it actually did have the line breaks… but as raw, plaintext newlines (\n in views showed all characters). That prompted a suspicion, which revealed the second thing…

They’re using white-space: pre-wrap to ask the browser to render that “plaintext” as it’s meant to be.

It was then I had a bit of a “No. It couldn’t be that simple.” moment.

Sure enough, when I popped over to jsBin and added these active ingredients, I was able to reproduce the bug:

<style>
  div { white-space: pre-wrap; }
</style>
[...]
<div>
  some

  text

  with

  newlines
</div>

Firefox has a bug with copying and pasting any text that’s set to white-space: pre-wrap;!

…but then why didn’t I trigger it before? I actually discovered that completely by accident, when I got a bit sloppy and impatient. If you begin your text selection outside the pre-wrapped element, then it copies properly!

For YouTube, I used to copy the entire comment, including the header, in one go, and then edit out all the cruft that got picked up in between the username and the content, which protected me from the bug.

In hindsight, that does make sense. This isn’t the only circumstance I’ve run into where Firefox may not give the clipboard what you expect if you begin and end your selection in the right place. (I’ve also seen it happen when copy-pasting fragments of <p> tags into my fanfiction quotes bin, which is from a normal website into a contenteditable element in a form, also being rendered by Firefox.)

EDIT: And I’ve now tracked down and reported that other bug too.

CC BY-SA 4.0 How to Copy-Paste YouTube Comments With Formatting in Firefox by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Web Wandering & Opinion. Bookmark the permalink.

2 Responses to How to Copy-Paste YouTube Comments With Formatting in Firefox

  1. Jasper says:

    BRAVO! That’s OUTSTANDING!

    That issue plagued me for the longest time, the only reason it really doesn’t much anymore is because I have not had the leisure time to use Firefox, and I’ve even resorted to using other browsers at times; and my limited usage involves much less of a need to use the clipboard.

    Whenever I’ve visited Bugzilla to file a bug report, not only has someone always already done so. It was usually done LONG ago, and there has been little to NO progress, or even discussion on the matter. Usually just the one person posting their findings & frustration until they lose hope and give up! At least, that’s the way it seems to me.
    And since it is free & open-source, that means the developers are free to work on whatever features or issues they wish. And it’s always a lot more fun to add a cool new feature than to try to resolve a bug in someone else’s code, especially if the bug isn’t serious and/or you can avoid it or at least minimize its effect.
    Free & open-source also means, if it really bothers you that much; at least you’re free to fix it yourself! LOL (which, of course, I am not even remotely capable of doing…)
    I’m shocked by what I see when I make the mistake of loading that site; it’s very upsetting and frustrating. So, I completely avoid it. It’s not as if any action of mine with spawn any results that would be helpful or appreciated by me anyway. If nobody cares enough to fix a bug (or at least begin researching/working on it) within a year, it’s not going to happen! Unless it is found to have some kind of much more profound, detrimental effects which can not be ignored.
    I know my attitude may sound pessimistic and even hopeless; however, it actually is not. But it does reflect the acceptance of conceding that these things are beyond my control and sphere of influence.

    • I very much know your pain on that front but, to be fair to Firefox, there are two problems they have to deal with which other open-source projects don’t:

      1. They’re not the ones setting the pace. If they don’t keep focusing on the fancy new features, then they’ll lose even more market share to Google Chrome. (Even Microsoft is throwing in the towel. They’re planning to change Edge from having its own engine to using Chrome’s.)
      2. From before it was even Firefox, the extension API has been “let extensions monkey-patch arbitrary browser internals”, which has severely hamstrung attempts to clean up and improve the code. WebExtensions was Mozilla finally admitting that such a design was untenable after they’d already lost far too much market share to Chrome.

      I’m hopeful for what the combination of WebExtensions and rewriting components in Rust will do for Firefox going forward.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.