-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Interactivity API: Prevent empty namespace or different namespaces from killing the runtime #61409
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
71283b3
Prevent empty namespace or different namespaces from killing the runtime
cbravobernal 26e321a
Update changelog
cbravobernal 1c37013
Move changelog
cbravobernal ac6d7b7
Update with isdebug, rephrase error
cbravobernal 6491f6b
Prevent warning duplication
cbravobernal 1640064
Remove not needed check
cbravobernal 605eeac
Move warn to same resolve function
cbravobernal 414e15b
Add some tests
cbravobernal f587e3d
Remove not needed warn
cbravobernal 3679c6b
Fix typos
cbravobernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/namespace/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-namespace/directive-bind", | ||
"title": "E2E Interactivity tests - directive bind", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScriptModule": "file:./view.js", | ||
"render": "file:./render.php" | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/namespace/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-bind`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
?> | ||
|
||
<div data-wp-interactive=""> | ||
<a data-wp-bind--href="state.url" data-testid="empty namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive="namespace"> | ||
<a data-wp-bind--href="state.url" data-testid="correct namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive="{}"> | ||
<a data-wp-bind--href="state.url" data-testid="object namespace"></a> | ||
</div> | ||
|
||
<div data-wp-interactive> | ||
<a data-wp-bind--href="other::state.url" data-testid="other namespace"></a> | ||
</div> |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/plugins/interactive-blocks/namespace/view.asset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array( 'dependencies' => array( '@wordpress/interactivity' ) ); |
16 changes: 16 additions & 0 deletions
16
packages/e2e-tests/plugins/interactive-blocks/namespace/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store } from '@wordpress/interactivity'; | ||
|
||
store( 'namespace', { | ||
state: { | ||
url: '/some-url', | ||
}, | ||
} ); | ||
|
||
store( 'other', { | ||
state: { | ||
url: '/other-store-url', | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const logged = new Set(); | ||
|
||
export const warn = ( message ) => { | ||
// @ts-expect-error | ||
if ( typeof SCRIPT_DEBUG !== 'undefined' && SCRIPT_DEBUG === true ) { | ||
if ( logged.has( message ) ) { | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn( message ); | ||
|
||
// Adding a stack trace to the warning message to help with debugging. | ||
try { | ||
throw Error( message ); | ||
} catch ( e ) { | ||
// Do nothing. | ||
} | ||
michalczaplinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logged.add( message ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'Namespaces', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test-namespace/directive-bind' ); | ||
} ); | ||
|
||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test-namespace/directive-bind' ) ); | ||
} ); | ||
|
||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'Empty string as namespace should not work', async ( { page } ) => { | ||
const el = page.getByTestId( 'empty namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A string as namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'correct namespace' ); | ||
await expect( el ).toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'An empty object as namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'object namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A wrong namespace should not break the runtime', async ( { | ||
page, | ||
} ) => { | ||
const el = page.getByTestId( 'object namespace' ); | ||
await expect( el ).not.toHaveAttribute( 'href', '/some-url' ); | ||
const correct = page.getByTestId( 'correct namespace' ); | ||
await expect( correct ).toHaveAttribute( 'href', '/some-url' ); | ||
} ); | ||
|
||
test( 'A different store namespace should work', async ( { page } ) => { | ||
const el = page.getByTestId( 'other namespace' ); | ||
await expect( el ).toHaveAttribute( 'href', '/other-store-url' ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flagging as something I'll need to update in #61486