Skip to content

Commit

Permalink
Merge pull request #228 from KevinBatdorf/better-handle-encoding-options
Browse files Browse the repository at this point in the history
Add fallback approach to opt-in uri encoding
  • Loading branch information
KevinBatdorf authored Jul 31, 2023
2 parents fc50e1e + 4dd3d7f commit 7bbed14
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ Themes are rendered inside the editor as you type or make changes, so the code b

- Fix: Theme previews now respect the tabSize setting
- Fix: Added escapeHTML wrapper on code to code pro transform function
- Fix: Added some clarify on encoding, and added better edge cases handling
- Fix: Fixed an issue with the decoding with some sequences that would cause a block invalidation error.

= 1.22.0 - 2023-07-22 =
Expand Down
24 changes: 22 additions & 2 deletions src/editor/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,31 @@ export const Edit = ({
useDefaults({ attributes, setAttributes });

const decode = useCallback(
(code: string) => (useDecodeURI ? code : decodeEntities(code)),
(code: string) => {
if (useDecodeURI) {
try {
return decodeURIComponent(code);
} catch (e) {
// Covers sequences that fail the above
return code;
}
}
return decodeEntities(code);
},
[useDecodeURI],
);
const encode = useCallback(
(code: string) => (useDecodeURI ? code : escapeHTML(code)),
(code: string) => {
if (useDecodeURI) {
try {
return encodeURIComponent(code);
} catch (e) {
// Covers sequences that fail the above
return code;
}
}
return escapeHTML(code);
},
[useDecodeURI],
);

Expand Down
7 changes: 5 additions & 2 deletions src/editor/controls/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,12 @@ export const SidebarControls = ({
<BaseControl id="code-block-pro-decode-uri">
<CheckboxControl
data-cy="use-decode-uri"
label={__('Allow HTML Entites', 'code-block-pro')}
label={__(
'Encode special characters',
'code-block-pro',
)}
help={__(
'Select this to allow HTML entities such as &lt; and &gt; to be displayed. You may need to re-add the code after changing this',
'Select this to allow HTML entities such as &lt; and &gt; and others to be displayed. You may need to re-add the code after changing this',
'code-block-pro',
)}
checked={attributes.useDecodeURI}
Expand Down
2 changes: 1 addition & 1 deletion src/front/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const handleCopyButton = () => {
event.preventDefault();
const b = target?.closest('span');
const code = b?.dataset?.encoded
? decodeURIComponent(b?.dataset?.code)
? decodeURIComponent(decodeURIComponent(b?.dataset?.code))
: b?.dataset?.code;
copy(code ?? '', {
format: 'text/plain',
Expand Down

0 comments on commit 7bbed14

Please sign in to comment.