Skip to content

Commit

Permalink
List v2: fix migration when nested list is invalid (#44822)
Browse files Browse the repository at this point in the history
* List v2: fix migration when nested list is invalid

* Reduce test case
  • Loading branch information
ellatrix authored Oct 28, 2022
1 parent 69fc362 commit 3c9a020
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 11 deletions.
14 changes: 3 additions & 11 deletions packages/block-library/src/list/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { createBlock, rawHandler } from '@wordpress/blocks';

export function createListBlockFromDOMElement( listElement ) {
const listAttributes = {
Expand Down Expand Up @@ -70,15 +70,7 @@ export function migrateToListV2( attributes ) {
list.setAttribute( 'type', type );
}

const listBlock = createListBlockFromDOMElement( list );
const [ listBlock ] = rawHandler( { HTML: list.outerHTML } );

const { values: omittedValues, ...restAttributes } = attributes;

return [
{
...restAttributes,
...listBlock.attributes,
},
listBlock.innerBlocks,
];
return [ listBlock.attributes, listBlock.innerBlocks ];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:list -->
<ul><li><strong>Item</strong><ul><li><strong>Item</strong><ul><li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li></ul></li></ul></li></ul>
<!-- /wp:list -->
112 changes: 112 additions & 0 deletions test/integration/fixtures/blocks/core__list__deprecated-v1-nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
[
{
"name": "core/list",
"isValid": true,
"attributes": {
"ordered": false,
"values": ""
},
"innerBlocks": [
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<strong>Item</strong>"
},
"innerBlocks": [
{
"name": "core/list",
"isValid": true,
"attributes": {
"ordered": false,
"values": ""
},
"innerBlocks": [
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<strong>Item</strong>"
},
"innerBlocks": [
{
"name": "core/list",
"isValid": true,
"attributes": {
"ordered": false,
"values": ""
},
"innerBlocks": [
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a>"
},
"innerBlocks": []
}
]
}
]
},
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<strong>Item</strong>"
},
"innerBlocks": [
{
"name": "core/list",
"isValid": true,
"attributes": {
"ordered": false,
"values": ""
},
"innerBlocks": [
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a>"
},
"innerBlocks": []
}
]
}
]
},
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<strong>Item</strong>"
},
"innerBlocks": [
{
"name": "core/list",
"isValid": true,
"attributes": {
"ordered": false,
"values": ""
},
"innerBlocks": [
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": "<a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a>"
},
"innerBlocks": []
}
]
}
]
}
]
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"blockName": "core/list",
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<ul><li><strong>Item</strong><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul></li></ul>\n",
"innerContent": [
"\n<ul><li><strong>Item</strong><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul><ul><li><strong>Item</strong><ul><li><a rel=\"noreferrer noopener\" href=\"#\" target=\"_blank\">Item</a></li></ul></li></ul></li></ul>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Item</strong><!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Item</strong><!-- wp:list -->
<ul><!-- wp:list-item -->
<li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Item</strong><!-- wp:list -->
<ul><!-- wp:list-item -->
<li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Item</strong><!-- wp:list -->
<ul><!-- wp:list-item -->
<li><a rel="noreferrer noopener" href="#" target="_blank">Item</a></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

0 comments on commit 3c9a020

Please sign in to comment.