Skip to content

Fix handling of raw transforms that return multiple blocks#28371

Merged
talldan merged 1 commit intoWordPress:masterfrom
mirka:fix/raw-transform-array
Jan 21, 2021
Merged

Fix handling of raw transforms that return multiple blocks#28371
talldan merged 1 commit intoWordPress:masterfrom
mirka:fix/raw-transform-array

Conversation

@mirka
Copy link
Copy Markdown
Member

@mirka mirka commented Jan 21, 2021

Fixes #28370

Description

Flattens the return array in raw-handling/html-to-blocks.js so that we don't pass a deeply nested array to the consumer handlers (rawHandler, pasteHandler), which are only shallowly flattened.

How has this been tested?

  • test/integration/blocks-raw-handling.test.js (not sure if the added test is necessary)
  • Test cases like the one in the original issue work as expected.

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@talldan talldan added [Feature] Paste [Type] Bug An existing feature does not function as intended labels Jan 21, 2021
Copy link
Copy Markdown
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell from looking at the history this has never worked, despite the docs explicitly saying it should. Thanks for fixing and adding the test.

@talldan talldan merged commit 48e53ad into WordPress:master Jan 21, 2021
@github-actions github-actions bot added this to the Gutenberg 9.9 milestone Jan 21, 2021
@mirka mirka deleted the fix/raw-transform-array branch January 21, 2021 11:26
doc.body.innerHTML = html;

return Array.from( doc.body.children ).map( ( node ) => {
return Array.from( doc.body.children ).flatMap( ( node ) => {
Copy link
Copy Markdown
Contributor

@fluiddot fluiddot Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 I tested these changes in the native version and unfortunately the built-in version of flatMap is not 100% supported (Android particularly).

Would it be possible to use the version from the lodash library instead? Do you foresee any inconvenience of using that version instead of the built-in?

Thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking! 🙏

Using the lodash one instead should be fine, but I'm wondering whether our Babel config is intentionally leaving it unhandled. Could it have been an oversight in the v7 migration?

I'd think it would be safer if we relied on Babel to handle it, unless there's a project-wide preference to use lodash replacements for built-in functions. I'm not really familiar with the goings-on here though. Do you know?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Noting one more instance, in case we're going the lodash route.)

return innerBlocks.flatMap( ( block, index ) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the lodash one instead should be fine, but I'm wondering whether our Babel config is intentionally leaving it unhandled. Could it have been an oversight in the v7 migration?

To be honest I'm not sure, the adoption of built-in flatMap is quite high (91.18% as checked in caniuse.com) and since it has been only used in one place maybe we haven't spotted this issue yet.

(Noting one more instance, in case we're going the lodash route.)

Good catch! That part should be also incompatible with the native version but most likely it hasn't raised any issue because is not used (I need to check it).

I found interesting that this place was the only one where the built-in flatMap was being used so far, maybe as you commented, there's a project-wide preference to use lodash.
I'm not familiar neither with this since I'm working more in the native side but I'll ask for help.

Copy link
Copy Markdown
Contributor

@youknowriad youknowriad Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polyfills are included separate in WordPress in a wp-polyfill package. I think that package is probably not included in native, should it be included?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we would need to import and inject that package when generating the JS bundle for React native. Could you point me out where I can get that package? Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its setup is a bit complex, it's mainly composed of babel-polypill see https://github.com/WordPress/wordpress-develop/blob/master/package.json#L81

but it has other dependencies that are only loaded if the browser doesn't support them https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/script-loader.php#L122-L136

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @youknowriad for the info!

I'm going to try adding the core-js dependency to the react-native-editor package (the entry point for the native version) and import the polyfill for flatMap from core-js/features/array/flat-map as states in the Babel documentation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally I've included the polyfill by adding core-js to the native package in this PR. In the future if we need to add new polyfills we'll have an entry point there.

Thanks @mirka and @youknowriad for checking this!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix @fluiddot 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Paste [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raw transforms that return an array of blocks don't get handled correctly

4 participants