Skip to content
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

- `GradientPicker`: Add `selectedSlug` prop for slug-based selection and pass the selected preset's slug to `onChange`, so two presets sharing a gradient keep their identity ([#80554](https://github.com/WordPress/gutenberg/pull/80554)).
- `SandBox`: Add `allowPopups` prop to opt into `allow-popups` in the iframe's sandbox attribute ([#69617](https://github.com/WordPress/gutenberg/pull/69617)).
- `SandBox`: Add `allowForms` prop to opt into `allow-forms` in the iframe's sandbox attribute ([#76471](https://github.com/WordPress/gutenberg/pull/76471)).
- Validated form controls: Only move focus to the invalid control for trusted `invalid` events (form submission, `reportValidity()`). Consumers can now dispatch a synthetic `invalid` event to reveal a control's error message without disturbing the user's place in the form ([#80685](https://github.com/WordPress/gutenberg/pull/80685)).

### Bug Fixes
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/sandbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,15 @@ function IsolatedSandBox( {
onFocus,
tabIndex,
allowPopups = false,
allowForms = false,
}: SandBoxContentProps ) {
const ref = useRef< HTMLIFrameElement >( null );
const [ width, setWidth ] = useState( 0 );
const [ height, setHeight ] = useState( 0 );

const sandbox = clsx( 'allow-scripts', 'allow-presentation', {
'allow-popups': allowPopups,
'allow-forms': allowForms,
} );

const srcDoc = useMemo(
Expand Down Expand Up @@ -393,6 +395,7 @@ function SameOriginSandBox( {
onFocus,
tabIndex,
allowPopups = false,
allowForms = false,
}: SandBoxContentProps ) {
const ref = useRef< HTMLIFrameElement >( null );
const [ width, setWidth ] = useState( 0 );
Expand All @@ -404,6 +407,7 @@ function SameOriginSandBox( {
'allow-presentation',
{
'allow-popups': allowPopups,
'allow-forms': allowForms,
}
);

Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/sandbox/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ const Template: StoryFn< typeof SandBox > = ( args ) => <SandBox { ...args } />;
export const Default = Template.bind( {} );
Default.args = {
html: '<p>Arbitrary HTML content</p>',
allowForms: false,
};

export const WithForm = Template.bind( {} );
WithForm.args = {
html: '<form action="#"><label for="name">Name</label><input id="name" type="text" /><button type="submit">Submit</button></form>',
allowForms: true,
};
21 changes: 21 additions & 0 deletions packages/components/src/sandbox/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ describe( 'SandBox', () => {
);
} );

it( 'should not include allow-forms by default', () => {
render( <SandBox html="<p>Hello</p>" title="No Forms" /> );

const iframe = screen.getByTitle< HTMLIFrameElement >( 'No Forms' );

expect( iframe.getAttribute( 'sandbox' ) ).not.toContain(
'allow-forms'
);
} );

it( 'should include allow-forms when allowForms is set', () => {
render( <SandBox html="<p>Hello</p>" title="Forms" allowForms /> );

const iframe = screen.getByTitle< HTMLIFrameElement >( 'Forms' );

expect( iframe ).toHaveAttribute(
'sandbox',
'allow-scripts allow-presentation allow-forms'
);
} );

it( 'should set srcdoc with the provided html content', () => {
render( <SandBox html="<p>Hello</p>" title="Test Title" /> );

Expand Down
10 changes: 10 additions & 0 deletions packages/components/src/sandbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export type SandBoxProps = {
* @default false
*/
allowPopups?: boolean;
/**
* Whether to include `allow-forms` in the iframe's sandbox attribute.
* When true, content inside the iframe is allowed to submit forms.
*
* Enable this for previews whose content includes forms that should be
* submittable.
*
* @default false
*/
allowForms?: boolean;
/**
* The HTML to render in the body of the iframe document.
*
Expand Down
1 change: 1 addition & 0 deletions storybook/components-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@
- style
- name: SandBox
props:
- allowForms
- allowPopups
- allowSameOrigin
- html
Expand Down
Loading