question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Copy To Clipboard Adds Linebreak At End Of String In Firefox

See original GitHub issue

I’ve found a bug and checked that …

  • … the problem doesn’t occur with the default MkDocs template
  • … the problem is not in any of my customizations (CSS, JS, template)
  • … the documentation does not mention anything about my problem
  • … there are no open or closed issues that are related to my problem

Description

In Firefox, the copy to clipboard button on code blocks adds a linebreak/return character at the end of the string causing terminal commands to immediately execute when pasted.

Expected behavior

Clicking the copy to clipboard button for a terminal command code block and then pasting that command into a terminal should not execute the command until the enter key is pressed. This expected behavior is what happens in Chrome, Safari, and Edge (Chromium).

Actual behavior

A command copied via the copy to clipboard button is immediately executed when pasted into a terminal because it has a trailing linebreak/return character at the end of the copied string.

Steps to reproduce the bug

  1. Open Firefox
  2. Navigate to https://squidfunk.github.io/mkdocs-material/getting-started/
  3. Click the copy to clipboard button on the pip install mkdocs-material code block
  4. Paste the command into a terminal
  5. Command is immediately executed instead of waiting for the return key-press

Package versions & Project configuration

I’ve omitted these since the issue is reproducible on the MkDocs for Material docs site: https://squidfunk.github.io/mkdocs-material Happy to provide more information if needed.

I fully understand if this is a low priority issue due to it only happening in Firefox. I started trying to figure out why it’s happening but I’m a bit stuck on figuring out how to get mkdocs to serve up the source maps for the js bundles and without that, I’m not making much headway looking through the minified js.

System information

  • OS: Mac OS Catalina (10.15.7)

  • Browser: Firefox 81

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
zekefarwellcommented, Nov 1, 2020

You’re absolutely right about the python markdown parser emitting a trailing newline character. The dev tools inspector reformats html so that sort of thing isn’t always obvious. Very clear when looking at the raw html source though. I’ll open issues with python markdown and/or clipboard.js.

1reaction
squidfunkcommented, Nov 1, 2020

I’ve investigated this problem and it appears to me that it’s Firefox-specific:

If I select the whole line with the cursor, no newline is added

Ohne Titel

If I select the whole line with the cursor and move the cursor down by a little, a newline is added

Ohne Titel 2

If you look closely, the cursor is at the beginning of the next line, however, the command is not executed. This does not happen in Chrome.

If I press the copy-to-clipboard button, a newline is added – same behavior

Ohne Titel 3

If we inspect the HTML of the code block, there’s indeed a newline added to the end of it:

Bildschirmfoto 2020-11-01 um 10 48 49

The exact same thing happens on the official MkDocs documentation – you can inspect the first code block under this section. If I remove the newline from the code block, it renders just as fine. For this reason, there are two possibilities to solve this:

  1. Fix the Markdown Parser in Python Markdown to not emit trailing newlines.
  2. Fix Clipboard.js to strip the newline in Firefox

None of that can be done as part of this project, which is why I’m closing this issue. I’m sorry we cannot do anything about it, but it only makes sense to solve this upstream.

Read more comments on GitHub >

github_iconTop Results From Across the Web

preserve line breaks in text copy | Firefox Support Forum
It sounds like the line breaks are getting lost. Here are two suggestions to try: (1) Select a little before and a little...
Read more >
Firefox adding newlines to clipboard content
When displaying three <div> elements inline in Firefox, and then copying the outputted text to the clipboard, newlines are added between ...
Read more >
r/firefox - How to copy paste "pre-wrap" text while retaining ...
Pasting the text into word retains everything aside from the line breaks If I "paste without formatting" or Ctrl-Shift-V, only the line ......
Read more >
Why does xclip-copied text pasted into Gmail on Firefox ...
I could copy and paste from various sources, but somehow anything from xclip or nvim-qt (a NeoVim GUI) resulted in collapsing line breaks...
Read more >
Copy As Plain Text :: Reviews :: Add-ons for Firefox
https://addons.mozilla.org/firefox/addon/copy-plaintext/ ... No such luck....clipboard washer has the option to remove ALL line breaks, not just repeated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found