Fix handling of raw transforms that return multiple blocks#28371
Fix handling of raw transforms that return multiple blocks#28371talldan merged 1 commit intoWordPress:masterfrom mirka:fix/raw-transform-array
Conversation
talldan
left a comment
There was a problem hiding this comment.
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.
| doc.body.innerHTML = html; | ||
|
|
||
| return Array.from( doc.body.children ).map( ( node ) => { | ||
| return Array.from( doc.body.children ).flatMap( ( node ) => { |
There was a problem hiding this comment.
👋 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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
(Noting one more instance, in case we're going the lodash route.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Fixes #28370
Description
Flattens the return array in
raw-handling/html-to-blocks.jsso 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)Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: