From 303a423e174ac3ea58655b15d437517f29eb5b60 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 17 May 2022 23:08:51 +0100 Subject: [PATCH] Add: Raw handling to the new list block. (#39954) --- packages/block-library/src/list/v2/migrate.js | 2 +- .../block-library/src/list/v2/transforms.js | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/list/v2/migrate.js b/packages/block-library/src/list/v2/migrate.js index f9cd6d6419fb04..d0ed33e8f3c440 100644 --- a/packages/block-library/src/list/v2/migrate.js +++ b/packages/block-library/src/list/v2/migrate.js @@ -8,7 +8,7 @@ import { omit } from 'lodash'; */ import { createBlock } from '@wordpress/blocks'; -function createListBlockFromDOMElement( listElement ) { +export function createListBlockFromDOMElement( listElement ) { const listAttributes = { ordered: 'OL' === listElement.tagName, start: listElement.getAttribute( 'start' ) diff --git a/packages/block-library/src/list/v2/transforms.js b/packages/block-library/src/list/v2/transforms.js index 5070bc7c473d11..176a9d74377ed7 100644 --- a/packages/block-library/src/list/v2/transforms.js +++ b/packages/block-library/src/list/v2/transforms.js @@ -9,6 +9,32 @@ import { toHTMLString, } from '@wordpress/rich-text'; +/** + * Internal dependencies + */ +import { createListBlockFromDOMElement } from './migrate'; + +function getListContentSchema( { phrasingContentSchema } ) { + const listContentSchema = { + ...phrasingContentSchema, + ul: {}, + ol: { attributes: [ 'type', 'start', 'reversed' ] }, + }; + + // Recursion is needed. + // Possible: ul > li > ul. + // Impossible: ul > ul. + [ 'ul', 'ol' ].forEach( ( tag ) => { + listContentSchema[ tag ].children = { + li: { + children: listContentSchema, + }, + }; + } ); + + return listContentSchema; +} + const transforms = { from: [ { @@ -82,6 +108,15 @@ const transforms = { ); }, } ) ), + { + type: 'raw', + selector: 'ol,ul', + schema: ( args ) => ( { + ol: getListContentSchema( args ).ol, + ul: getListContentSchema( args ).ul, + } ), + transform: createListBlockFromDOMElement, + }, ], to: [ ...[ 'core/paragraph', 'core/heading' ].map( ( block ) => ( {