Plugins incompatibility: File Highlight and Copy to Clipboard
See original GitHub issueInformation:
- Prism version: [1.16.0]
- Environment: [Browser]
Does the latest version of Prism from the download page also have this issue? Yes.
Description
The plugin Copy to Clipboard
works well if the code is sourced between the <pre><code>
tags. On the other hand, if the code was sourced using the File Highlight
plugin, e.g.
<pre data-src="/path/to/asset" class="language-shell" data-download-link="" data-download-link-label="download"></pre>
… then Copy to Clipboard
fails. Instead of copying the code, it simply copies Loading…
, which interestingly, is what initially appears when File Highlight
is still loading the code from the file.
Example Here is a sample webpage that shows this incompatibility: https://www.majlovesreg.one/adding-brotli-to-a-built-nginx-instance.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Copy Anything to Clipboard – WordPress plugin
Copy Anything to Clipboard is the #1 WordPress plugin with 90,000+ downloads. You can use plugin to copy anything including: Copy Blockquote; Copy...
Read more >copy2clipboard Plugin - DokuWiki
Description. This extension add a clipboard button to <code> and <file> blocks that copies text to the clipboard. This ...
Read more >Interact with the clipboard - Mozilla - MDN Web Docs
This article describes some limitations, but be sure to review the compatibility tables for each method before using them to ensure that the...
Read more >Implementing copy-to-clipboard in React with Clipboard API
Improve user experience by implementing a copy-to-clipboard button ... This is a lot easier than having to select the entire line of text, ......
Read more >Clipboard — WorldEdit 7.2 documentation
WorldEdit has a powerful clipboard function that allows you to copy an area, paste it, and even save it to and load it...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Interestingly, your website works flawlessly in the best of all browsers: IE11.
Alright, after a few hours of debugging your website, I finally found the answer! Yaaaaay
TLDR;
Don’t load the script containing Prism asynchronously until this is fixed.
The long version
File Highlight and Copy to Clipboard are not incompatible. In fact, the plugin page of Copy to Clipboard uses File Highlight to load the code which is then can then be copied. (You can’t see the copy button because of an issue of Toolbar and the Prism page design (will be fixed soon). In the meantime, setting
right: 10em
fordiv.code-toolbar > .toolbar
in the dev tools of your browser will make the button visible.)The problem is that File Highlight has to executed after the initial
Prism.highlightAll
because the FH will insert acode
element with the text Loading… inside allpre
s with adata-src
attribute. If FH were to be executed beforePrism.highlightAll
, all of the Loading…-code
-elements would be highlighted as well (which is not intended) only to highlighted again after the code has been loaded. One of the consequences of this double-highlighting is that all plugins are run twice on thecode
element. Most plugins check for multiple executions on the same element, so this usually isn’t a problem but it is in this case because the text content of thecode
elements changed from Loading… to whatever code has been loaded. This is the reason why the copied text was Loading…: The Copy to Clipboard plugin also detects these multiple executions on the same element.Then why was File Highlight executed before
Prism.highlightAll
in the first place? Events. This is the code which adds FH’s callback and this adds thePrism.highlightAll
callback. If the Prism script is loaded synchronously, both FH andhighlightAll
use theDOMContentLoaded
event. Because Prism Core is always loaded before FH, the FH listener will be executed after thehighlightAll
listener, so no problem. But if the Prism script is loaded asynchronously, theDOMContentLoaded
event might have already been fired, so we userequestAnimationFrame
(orsetTimeout
) instead. The problem here is that just becausedocument.readyState !== "loading"
doesn’t mean that theDOMContentLoaded
event has been fired because there is theinteractive
ready state. So what happens is thathighlightAll
usesrequestAnimationFrame
and gets executed before FH which usesDOMContentLoaded
.Depending on how you look at it, the bug is either that File Highlight doesn’t use the same async logic as Prism Core or that the async logic of Core doesn’t quite work.
Thoughts? @mAAdhaTTah @Golmote.
Btw. #959 added Prism Core’s async logic.