Skip to content

Commit

Permalink
Replace remaining custom deep cloning with 'structuredClone' (WordPre…
Browse files Browse the repository at this point in the history
…ss#67707)

* Replace remaining custom deep cloning with 'structuredClone'
* Polyfill structuredClone for jsdom

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
  • Loading branch information
4 people authored and yogeshbhutkar committed Dec 18, 2024
1 parent 722ba4f commit 7d3fa75
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
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;

0 comments on commit 7d3fa75

Please sign in to comment.