Skip to content

Commit 7662c51

Browse files
committed
Prevent smart quotes etc from affecting copied code
1 parent f260c27 commit 7662c51

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

code-block-pro.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author URI: https://code-block-pro.com/?utm_campaign=plugin&utm_source=author-uri
99
* Requires at least: 6.0
1010
* Requires PHP: 7.0
11-
* Version: 1.27.2
11+
* Version: 1.27.3
1212
* License: GPL-2.0-or-later
1313
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
1414
* Text Domain: code-block-pro

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Contributors: kbat82, dcooney, a169kai
33
Tags: block, code, syntax, highlighter, php
44
Tested up to: 6.8
5-
Stable tag: 1.27.2
5+
Stable tag: 1.27.3
66
License: GPL-2.0-or-later
77
License URI: https://www.gnu.org/licenses/gpl-2.0.html
88

@@ -313,6 +313,9 @@ Themes are rendered inside the editor as you type or make changes, so the code b
313313

314314
== Changelog ==
315315

316+
= 1.27.3 - 2025-05-15 =
317+
- Prevents smart quotes, typographic dashes, and other non-ASCII characters from affecting copied code snippets.
318+
316319
= 1.27.2 - 2025-05-15 =
317320
- Refactors the copy button to use a textarea for copying
318321
- Removed the WP outline style on the copy button and pre (now only when tab focused)

src/editor/components/buttons/copy/CopyButton.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export const CopyButton = ({ attributes }: { attributes: Attributes }) => {
7070
<textarea
7171
className="code-block-pro-copy-button-textarea"
7272
aria-hidden="true"
73-
readOnly>
74-
{codeToCopy}
75-
</textarea>
73+
readOnly
74+
value={codeToCopy}
75+
/>
7676
) : null}
7777
<Component text={copyButtonString} />
7878
</span>

src/front/front.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import copy from 'copy-to-clipboard';
22

33
const containerClass = '.wp-block-kevinbatdorf-code-block-pro';
44

5+
const sanitize = (str) =>
6+
str
7+
.replace(/[\u2018\u2019]/g, "'") // single quotes
8+
.replace(/[\u201C\u201D]/g, '"') // double quotes
9+
.replace(/[\u2013\u2014]/g, '-') // en/em dash
10+
.replace(/\u2026/g, '...') // ellipsis
11+
.replace(/[\u2039\u203A\u00AB\u00BB]/g, (c) =>
12+
c === '‹' || c === '«' ? '<' : '>',
13+
)
14+
.replace(/\u00A0/g, ' '); // non-breaking space
15+
516
const handleCopyButton = () => {
617
const buttons = Array.from(
718
document.querySelectorAll(
@@ -22,7 +33,9 @@ const handleCopyButton = () => {
2233
const code = b?.dataset?.encoded
2334
? decodeURIComponent(decodeURIComponent(source))
2435
: source;
25-
const content = window.cbpCopyOverride?.(code, button) ?? code;
36+
const content = sanitize(
37+
window.cbpCopyOverride?.(code, button) ?? code,
38+
);
2639
copy(content ?? '', {
2740
format: 'text/plain',
2841
onCopy: (code) => {

0 commit comments

Comments
 (0)