From 626cb0d15aa7423f9713dd88c7d791ae35329ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= <4710635+ellatrix@users.noreply.github.com> Date: Wed, 10 Aug 2022 09:08:58 +0200 Subject: [PATCH] Quote: fix raw transform handler (#43093) --- packages/block-library/src/quote/transforms.js | 10 +++------- packages/blocks/src/api/raw-handling/html-to-blocks.js | 8 +++++--- packages/blocks/src/api/raw-handling/index.js | 2 +- packages/blocks/src/api/raw-handling/paste-handler.js | 2 +- test/integration/blocks-raw-handling.test.js | 1 + .../integration/fixtures/documents/slack-quote-in.html | 1 + .../fixtures/documents/slack-quote-out.html | 5 +++++ 7 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 test/integration/fixtures/documents/slack-quote-in.html create mode 100644 test/integration/fixtures/documents/slack-quote-out.html diff --git a/packages/block-library/src/quote/transforms.js b/packages/block-library/src/quote/transforms.js index 18716d3b11308..6f922d1794e19 100644 --- a/packages/block-library/src/quote/transforms.js +++ b/packages/block-library/src/quote/transforms.js @@ -1,11 +1,7 @@ /** * WordPress dependencies */ -import { - createBlock, - parseWithAttributeSchema, - rawHandler, -} from '@wordpress/blocks'; +import { createBlock, parseWithAttributeSchema } from '@wordpress/blocks'; const transforms = { from: [ @@ -59,7 +55,7 @@ const transforms = { }, } ), selector: 'blockquote', - transform: ( node ) => { + transform: ( node, handler ) => { return createBlock( 'core/quote', // Don't try to parse any `cite` out of this content. @@ -68,7 +64,7 @@ const transforms = { // * If the cite is nested in the quoted text, it's wrong to // remove it. {}, - rawHandler( { + handler( { HTML: node.innerHTML, mode: 'BLOCKS', } ) diff --git a/packages/blocks/src/api/raw-handling/html-to-blocks.js b/packages/blocks/src/api/raw-handling/html-to-blocks.js index e31905ba82c9b..18630a9abdce4 100644 --- a/packages/blocks/src/api/raw-handling/html-to-blocks.js +++ b/packages/blocks/src/api/raw-handling/html-to-blocks.js @@ -10,11 +10,13 @@ import { getRawTransforms } from './get-raw-transforms'; * top-level tag. The HTML should be filtered to not have any text between * top-level tags and formatted in a way that blocks can handle the HTML. * - * @param {string} html HTML to convert. + * @param {string} html HTML to convert. + * @param {Function} handler The handler calling htmlToBlocks: either rawHandler + * or pasteHandler. * * @return {Array} An array of blocks. */ -export function htmlToBlocks( html ) { +export function htmlToBlocks( html, handler ) { const doc = document.implementation.createHTMLDocument( '' ); doc.body.innerHTML = html; @@ -36,7 +38,7 @@ export function htmlToBlocks( html ) { const { transform, blockName } = rawTransform; if ( transform ) { - return transform( node ); + return transform( node, handler ); } return createBlock( diff --git a/packages/blocks/src/api/raw-handling/index.js b/packages/blocks/src/api/raw-handling/index.js index d534cfaf14bb0..afd68a4a9e1e7 100644 --- a/packages/blocks/src/api/raw-handling/index.js +++ b/packages/blocks/src/api/raw-handling/index.js @@ -71,7 +71,7 @@ export function rawHandler( { HTML = '' } ) { piece = deepFilterHTML( piece, filters, blockContentSchema ); piece = normaliseBlocks( piece ); - return htmlToBlocks( piece ); + return htmlToBlocks( piece, rawHandler ); } ) .flat() .filter( Boolean ); diff --git a/packages/blocks/src/api/raw-handling/paste-handler.js b/packages/blocks/src/api/raw-handling/paste-handler.js index eecf70e3e99ea..aa9f99037a39e 100644 --- a/packages/blocks/src/api/raw-handling/paste-handler.js +++ b/packages/blocks/src/api/raw-handling/paste-handler.js @@ -209,7 +209,7 @@ export function pasteHandler( { // Allows us to ask for this information when we get a report. console.log( 'Processed HTML piece:\n\n', piece ); - return htmlToBlocks( piece ); + return htmlToBlocks( piece, pasteHandler ); } ) .flat() .filter( Boolean ); diff --git a/test/integration/blocks-raw-handling.test.js b/test/integration/blocks-raw-handling.test.js index 6e3f0e63aba94..14f38348bc68a 100644 --- a/test/integration/blocks-raw-handling.test.js +++ b/test/integration/blocks-raw-handling.test.js @@ -345,6 +345,7 @@ describe( 'Blocks raw handling', () => { 'wordpress', 'gutenberg', 'shortcode-matching', + 'slack-quote', ].forEach( ( type ) => { // eslint-disable-next-line jest/valid-title it( type, () => { diff --git a/test/integration/fixtures/documents/slack-quote-in.html b/test/integration/fixtures/documents/slack-quote-in.html new file mode 100644 index 0000000000000..6e18f3e462f13 --- /dev/null +++ b/test/integration/fixtures/documents/slack-quote-in.html @@ -0,0 +1 @@ +
Test with link.\ No newline at end of file diff --git a/test/integration/fixtures/documents/slack-quote-out.html b/test/integration/fixtures/documents/slack-quote-out.html new file mode 100644 index 0000000000000..b5f9f82483d78 --- /dev/null +++ b/test/integration/fixtures/documents/slack-quote-out.html @@ -0,0 +1,5 @@ + +
++ \ No newline at end of fileTest with link.
+