From 1e85f2b3f72058e987ac0933516fe20c4589b3a4 Mon Sep 17 00:00:00 2001 From: David Aguilera Date: Wed, 22 May 2019 17:28:02 +0200 Subject: [PATCH 1/2] Fixes bug that prevents code block from being re-edited --- packages/block-library/src/code/index.js | 7 +++++++ packages/block-library/src/code/utils.js | 26 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/block-library/src/code/index.js b/packages/block-library/src/code/index.js index 1430dc7f27ad7..dde3dbc3def33 100644 --- a/packages/block-library/src/code/index.js +++ b/packages/block-library/src/code/index.js @@ -23,6 +23,13 @@ export const settings = { supports: { html: false, }, + attributes: { + content: { + type: 'string', + source: 'html', + selector: 'pre code', + }, + }, transforms, edit, save, diff --git a/packages/block-library/src/code/utils.js b/packages/block-library/src/code/utils.js index fb8557a95b18e..6acd9543700db 100644 --- a/packages/block-library/src/code/utils.js +++ b/packages/block-library/src/code/utils.js @@ -12,6 +12,7 @@ import { flow } from 'lodash'; export function escape( content ) { return flow( escapeAmpersands, + escapeTagDelimiters, escapeOpeningSquareBrackets, escapeProtocolInIsolatedUrls )( content || '' ); @@ -27,6 +28,7 @@ export function unescape( content ) { return flow( unescapeProtocolInIsolatedUrls, unescapeOpeningSquareBrackets, + unescapeTagDelimiters, unescapeAmpersands )( content || '' ); } @@ -54,6 +56,30 @@ function unescapeAmpersands( content ) { return content.replace( /&/g, '&' ); } +/** + * Returns the given content with all < and > characters converted into + * their HTML entity counterparts: < and > + * + * @param {string} content The content of a code block. + * @return {string} The given content with all < and > characters converted + * into their HTML entity counterparts: < and > + */ +function escapeTagDelimiters( content ) { + return content.replace( //g, '>' ); +} + +/** + * Returns the given content with all < and > HTML entities converted + * into < and >, respectively. + * + * @param {string} content The content of a code block. + * @return {string} The given content with all < and > HTML entities + * converted into < and >, respectively + */ +function unescapeTagDelimiters( content ) { + return content.replace( /</g, '<' ).replace( />/g, '>' ); +} + /** * Returns the given content with all opening shortcode characters converted * into their HTML entity counterpart (i.e. [ => [). For instance, a From 1ac7cfafd547231fe9920938f59c275e8bcbb97f Mon Sep 17 00:00:00 2001 From: David Aguilera Date: Thu, 23 May 2019 09:06:51 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Unescapes=20`<`=20and=20`>`=20characters=20?= =?UTF-8?q?(so=20that=20=E2=80=9Cload=E2=80=9D=20works),=20but=20doesn't?= =?UTF-8?q?=20actually=20escape=20them=20(if=20anybody=20does,=20it's=20Gu?= =?UTF-8?q?tenberg=20itself,=20implicitly,=20when=20saving)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/block-library/src/code/block.json | 2 +- packages/block-library/src/code/index.js | 7 ------- packages/block-library/src/code/test/utils.js | 5 +++++ packages/block-library/src/code/utils.js | 13 ------------- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/packages/block-library/src/code/block.json b/packages/block-library/src/code/block.json index d39e5f213bbe6..354d30b04fd6d 100644 --- a/packages/block-library/src/code/block.json +++ b/packages/block-library/src/code/block.json @@ -4,7 +4,7 @@ "attributes": { "content": { "type": "string", - "source": "text", + "source": "html", "selector": "code" } } diff --git a/packages/block-library/src/code/index.js b/packages/block-library/src/code/index.js index dde3dbc3def33..1430dc7f27ad7 100644 --- a/packages/block-library/src/code/index.js +++ b/packages/block-library/src/code/index.js @@ -23,13 +23,6 @@ export const settings = { supports: { html: false, }, - attributes: { - content: { - type: 'string', - source: 'html', - selector: 'pre code', - }, - }, transforms, edit, save, diff --git a/packages/block-library/src/code/test/utils.js b/packages/block-library/src/code/test/utils.js index 4926eef16d528..4dbf5e1c40e42 100644 --- a/packages/block-library/src/code/test/utils.js +++ b/packages/block-library/src/code/test/utils.js @@ -38,6 +38,11 @@ describe( 'core/code', () => { expect( text ).toBe( '&' ); } ); + it( 'should unescape escaped less than and greater than characters', () => { + const text = unescape( '<button>Click</button>' ); + expect( text ).toBe( '' ); + } ); + it( 'should unescape escaped opening square brackets', () => { const text = unescape( '[shortcode][/shortcode]' ); expect( text ).toBe( '[shortcode][/shortcode]' ); diff --git a/packages/block-library/src/code/utils.js b/packages/block-library/src/code/utils.js index 6acd9543700db..c5f2040db9048 100644 --- a/packages/block-library/src/code/utils.js +++ b/packages/block-library/src/code/utils.js @@ -12,7 +12,6 @@ import { flow } from 'lodash'; export function escape( content ) { return flow( escapeAmpersands, - escapeTagDelimiters, escapeOpeningSquareBrackets, escapeProtocolInIsolatedUrls )( content || '' ); @@ -56,18 +55,6 @@ function unescapeAmpersands( content ) { return content.replace( /&/g, '&' ); } -/** - * Returns the given content with all < and > characters converted into - * their HTML entity counterparts: < and > - * - * @param {string} content The content of a code block. - * @return {string} The given content with all < and > characters converted - * into their HTML entity counterparts: < and > - */ -function escapeTagDelimiters( content ) { - return content.replace( //g, '>' ); -} - /** * Returns the given content with all < and > HTML entities converted * into < and >, respectively.