Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace remaining custom deep cloning with 'structuredClone' #67707

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ function pickStyleKeys( treeToPickFrom ) {
// clone the style objects so that `getFeatureDeclarations` can remove consumed keys from it
const clonedEntries = pickedEntries.map( ( [ key, style ] ) => [
key,
JSON.parse( JSON.stringify( style ) ),
structuredClone( style ),
] );
return Object.fromEntries( clonedEntries );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function omitStyle( style, paths, preserveReference = false ) {

let newStyle = style;
if ( ! preserveReference ) {
newStyle = JSON.parse( JSON.stringify( style ) );
newStyle = structuredClone( style );
}

if ( ! Array.isArray( paths ) ) {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/config/global-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { TextDecoder, TextEncoder } from 'node:util';
import { Blob as BlobPolyfill, File as FilePolyfill } from 'node:buffer';
import 'core-js/stable/structured-clone';

jest.mock( '@wordpress/compose', () => {
return {
Expand Down Expand Up @@ -49,3 +50,6 @@ if ( ! global.TextEncoder ) {
// Override jsdom built-ins with native node implementation.
global.Blob = BlobPolyfill;
global.File = FilePolyfill;

// Polyfill structuredClone for jsdom.
global.structuredClone = structuredClone;
Copy link
Member

Choose a reason for hiding this comment

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

@tyxla writes:

So you can't just use the underlying Node.js modules like this (please correct me if I'm missing something).

The jsdom environment creates a new global object for the environment, but it still runs in Node.js. If native structuredClone is available in Node.js, then you should be able to "forward" it from the Node.js global to the jsdom global.

Could the global.structuredClone = structuredClone assignment work without importing the CoreJS polyfill? If the structuredClone reference uses the Node.js globalThis, it should work.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried that initially, but unfortunately, it doesn't work.

Copy link
Member

Choose a reason for hiding this comment

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

OK then let's do the CoreJS version.

Loading