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

Interactivity API: Prevent the use of components in wp-text #57879

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -34,4 +34,18 @@
Toggle Context Text
</button>
</div>
<div>
<span
data-wp-text="state.component"
data-testid="show state component"
></span>
<span
data-wp-text="state.number"
data-testid="show state number"
></span>
<span
data-wp-text="state.boolean"
data-testid="show state boolean"
></span>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
* WordPress dependencies
*/
import { store, getContext } from '@wordpress/interactivity';
import { store, getContext, createElement } from '@wordpress/interactivity';

const { state } = store( 'directive-context', {
state: {
text: 'Text 1',
component: () => (createElement( 'div', {}, state.text )),
number: 1,
boolean: true
},
actions: {
toggleStateText() {
Expand Down
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

### Enhancements

- Allow only strings for `wp-text`. ([#57879](https://github.com/WordPress/gutenberg/pull/57879))
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

### New Features

- Add the `data-wp-run` directive along with the `useInit` and `useWatch` hooks. ([57805](https://github.com/WordPress/gutenberg/pull/57805))
Expand Down
10 changes: 9 additions & 1 deletion packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,15 @@ export default () => {
// data-wp-text
directive( 'text', ( { directives: { text }, element, evaluate } ) => {
const entry = text.find( ( { suffix } ) => suffix === 'default' );
element.props.children = evaluate( entry );
try {
if ( typeof evaluate( entry ) === 'object' ) {
element.props.children = null;
return;
}
element.props.children = evaluate( entry ).toString();
} catch ( e ) {
element.props.children = null;
}
cbravobernal marked this conversation as resolved.
Show resolved Hide resolved
} );

// data-wp-slot
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/specs/interactivity/directives-text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ test.describe( 'data-wp-text', () => {
await page.getByTestId( 'toggle context text' ).click();
await expect( el ).toHaveText( 'Text 1' );
} );

test( 'Transforms results into strings', async ( { page } ) => {
const elObject = page.getByTestId( 'show state component' );
await expect( elObject ).toBeHidden();
const elNumber = page.getByTestId( 'show state number' );
await expect( elNumber ).toHaveText( '1' );
const elBool = page.getByTestId( 'show state boolean' );
await expect( elBool ).toHaveText( 'true' );
} );
} );
Loading