Skip to content

Commit

Permalink
Interactivity API: Only add proxies to plain objects inside the store (
Browse files Browse the repository at this point in the history
…#59039)

* Only proxify plain objects

* Do not support objects without prototypes

* Rename store-tag to interactivity-data

* Add test for non-plain objects in state

* Update `isObject` implementation

* Update changelog

---------

Co-authored-by: c4rl0sbr4v0 <cbravobernal@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
  • Loading branch information
3 people authored Feb 20, 2024
1 parent d79c286 commit 55b032f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "test/store",
"title": "E2E Interactivity tests - store definition",
"category": "text",
"icon": "heart",
"description": "",
"supports": {
"interactivity": true
},
"textdomain": "e2e-interactivity",
"viewScript": "store-view",
"render": "file:./render.php"
}
17 changes: 17 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* HTML for testing the directive `data-wp-bind`.
*
* @package gutenberg-test-interactive-blocks
*/

wp_enqueue_script_module( 'store-view' );
?>

<div data-wp-interactive="test/store">
<div
data-testid="non-plain object"
data-wp-text="state.isNotProxified"
data-wp-init="callbacks.init"
></div>
</div>
20 changes: 20 additions & 0 deletions packages/e2e-tests/plugins/interactive-blocks/store/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { store, getElement } from '@wordpress/interactivity';


const { state } = store( 'test/store', {
state: {
get isNotProxified() {
const { ref } = getElement();
return state.elementRef === ref;
}
},
callbacks: {
init() {
const { ref } = getElement();
state.elementRef = ref; // HTMLElement
}
}
} )
4 changes: 4 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Only add proxies to plain objects inside the store. ([#59039](https://github.com/WordPress/gutenberg/pull/59039))

## 5.0.0 (2024-02-09)

### New Features
Expand Down
4 changes: 2 additions & 2 deletions packages/interactivity/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
resetNamespace,
} from './hooks';

const isObject = ( item: unknown ): boolean =>
!! item && typeof item === 'object' && ! Array.isArray( item );
const isObject = ( item: unknown ): item is Record< string, unknown > =>
item && typeof item === 'object' && item.constructor === Object;

const deepMerge = ( target: any, source: any ) => {
if ( isObject( target ) && isObject( source ) ) {
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/specs/interactivity/store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Internal dependencies
*/
import { test, expect } from './fixtures';

test.describe( 'data-wp-bind', () => {
test.beforeAll( async ( { interactivityUtils: utils } ) => {
await utils.activatePlugins();
await utils.addPostWithBlock( 'test/store' );
} );

test.beforeEach( async ( { interactivityUtils: utils, page } ) => {
await page.goto( utils.getLink( 'test/store' ) );
} );

test.afterAll( async ( { interactivityUtils: utils } ) => {
await utils.deactivatePlugins();
await utils.deleteAllPosts();
} );

test( 'non-plain objects are not proxified', async ( { page } ) => {
const el = page.getByTestId( 'non-plain object' );
await expect( el ).toHaveText( 'true' );
} );
} );

1 comment on commit 55b032f

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 55b032f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7979057621
📝 Reported issues:

Please sign in to comment.