Skip to content

Commit 05be5ef

Browse files
committed
Properly escape shortcodes with attributes
1 parent 95f6f7c commit 05be5ef

5 files changed

Lines changed: 73 additions & 4 deletions

File tree

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.5
11+
* Version: 1.27.6
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

cypress/e2e/buttons.cy.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,37 @@ context('Copy button', () => {
174174
});
175175
});
176176

177+
it('Copy button copies audio and video shortcodes with attributes', () => {
178+
const text = '[audio src="fake.mp3"]\n[video src="fake.mp4"]';
179+
cy.setLanguage('plaintext');
180+
cy.addCode(text);
181+
182+
cy.previewCurrentPage();
183+
cy.get('.wp-block-kevinbatdorf-code-block-pro [aria-label="Copy"]')
184+
.should('exist')
185+
.realClick();
186+
cy.window().then((win) => {
187+
win.navigator.clipboard.readText().then((clipText) => {
188+
expect(clipText).to.equal(text);
189+
});
190+
});
191+
192+
cy.go('back');
193+
cy.focusBlock('code-block-pro');
194+
cy.openSideBarPanel('Extra Settings');
195+
cy.get('[data-cy="use-escape-shortcodes"]').uncheck();
196+
197+
cy.previewCurrentPage();
198+
cy.get('.wp-block-kevinbatdorf-code-block-pro [aria-label="Copy"]')
199+
.should('exist')
200+
.realClick();
201+
cy.window().then((win) => {
202+
win.navigator.clipboard.readText().then((clipText) => {
203+
expect(clipText).to.not.equal(text);
204+
});
205+
});
206+
});
207+
177208
// Doesn't seem to work 🤷
178209
// it.only('Copies code on keypress', () => {
179210
// const text = 'const foo = "bar";';

cypress/e2e/extra-settings.cy.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,40 @@ context('Extra Settings', () => {
8989
.invoke('html')
9090
.should('contain', '<a href="http://foo">foo</a>'); // Renders
9191
});
92+
93+
it('Escapes WordPress audio and video shortcodes with attributes', () => {
94+
cy.openSideBarPanel('Extra Settings');
95+
96+
cy.setLanguage('plaintext');
97+
cy.addCode('[audio src="fake.mp3"]\n[video src="fake.mp4"]');
98+
cy.findBlock('code-block-pro', '> div > div > pre')
99+
.invoke('html')
100+
.should('contain', '[audio src="fake.mp3"]')
101+
.and('contain', '[video src="fake.mp4"]');
102+
103+
cy.previewCurrentPage();
104+
cy.get('.wp-block-kevinbatdorf-code-block-pro pre.shiki')
105+
.should('exist')
106+
.invoke('html')
107+
.should('contain', '[audio src="fake.mp3"]')
108+
.and('contain', '[video src="fake.mp4"]');
109+
110+
cy.go('back');
111+
cy.focusBlock('code-block-pro');
112+
cy.openSideBarPanel('Extra Settings');
113+
cy.get('[data-cy="use-escape-shortcodes"]').uncheck();
114+
cy.findBlock('code-block-pro', '> div > div > pre')
115+
.invoke('html')
116+
.should('contain', '[audio src="fake.mp3"]')
117+
.and('contain', '[video src="fake.mp4"]')
118+
.and('not.contain', 'wp-embedded-audio')
119+
.and('not.contain', 'wp-embedded-video');
120+
121+
cy.previewCurrentPage();
122+
cy.get('.wp-block-kevinbatdorf-code-block-pro pre.shiki')
123+
.should('exist')
124+
.invoke('html')
125+
.should('contain', 'wp-embedded-audio')
126+
.and('contain', 'wp-embedded-video');
127+
});
92128
});

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.5
5+
Stable tag: 1.27.6
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.6 - 2025-06-29 =
317+
- Fixes a bug where shortcodes with attributes would not be properly escaped.
318+
316319
= 1.27.5 - 2025-06-21 =
317320
- Changes the default value for escaping wp shortcodes to true. Can't see why anyone would want this any other way by default.
318321
- Prevents shortcodes from rendering in the copy button

src/util/code.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const decode = (code: string, { useDecodeURI }: Partial<Attributes>) => {
2929
};
3030

3131
export const escapeShortcodes = (content: string) =>
32-
// eslint-disable-next-line no-control-regex
33-
content.replaceAll(/\[([^<>&/[\]\x00-\x20=]+)\]/g, (match) =>
32+
content.replaceAll(/\[([^[\]]+)\]/g, (match) =>
3433
match.replace('[', '&#91;').replace(']', '&#93;'),
3534
);

0 commit comments

Comments
 (0)