diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 869b3cf2e41f4f..9ae15cb596843b 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -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 diff --git a/packages/components/src/sandbox/index.tsx b/packages/components/src/sandbox/index.tsx index ece32df2ec80b4..1de1e5c8b4f7cf 100644 --- a/packages/components/src/sandbox/index.tsx +++ b/packages/components/src/sandbox/index.tsx @@ -268,6 +268,7 @@ function IsolatedSandBox( { onFocus, tabIndex, allowPopups = false, + allowForms = false, }: SandBoxContentProps ) { const ref = useRef< HTMLIFrameElement >( null ); const [ width, setWidth ] = useState( 0 ); @@ -275,6 +276,7 @@ function IsolatedSandBox( { const sandbox = clsx( 'allow-scripts', 'allow-presentation', { 'allow-popups': allowPopups, + 'allow-forms': allowForms, } ); const srcDoc = useMemo( @@ -393,6 +395,7 @@ function SameOriginSandBox( { onFocus, tabIndex, allowPopups = false, + allowForms = false, }: SandBoxContentProps ) { const ref = useRef< HTMLIFrameElement >( null ); const [ width, setWidth ] = useState( 0 ); @@ -404,6 +407,7 @@ function SameOriginSandBox( { 'allow-presentation', { 'allow-popups': allowPopups, + 'allow-forms': allowForms, } ); diff --git a/packages/components/src/sandbox/stories/index.story.tsx b/packages/components/src/sandbox/stories/index.story.tsx index 77ff4cbfa83b21..9617436b0f7ee6 100644 --- a/packages/components/src/sandbox/stories/index.story.tsx +++ b/packages/components/src/sandbox/stories/index.story.tsx @@ -36,4 +36,11 @@ const Template: StoryFn< typeof SandBox > = ( args ) => ; export const Default = Template.bind( {} ); Default.args = { html: '

Arbitrary HTML content

', + allowForms: false, +}; + +export const WithForm = Template.bind( {} ); +WithForm.args = { + html: '
', + allowForms: true, }; diff --git a/packages/components/src/sandbox/test/index.tsx b/packages/components/src/sandbox/test/index.tsx index 0c216a820c04a6..c103ca14d7b0e4 100644 --- a/packages/components/src/sandbox/test/index.tsx +++ b/packages/components/src/sandbox/test/index.tsx @@ -70,6 +70,27 @@ describe( 'SandBox', () => { ); } ); + it( 'should not include allow-forms by default', () => { + render( ); + + 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( ); + + 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( ); diff --git a/packages/components/src/sandbox/types.ts b/packages/components/src/sandbox/types.ts index 9c7e7454771df2..4b8e532d07e499 100644 --- a/packages/components/src/sandbox/types.ts +++ b/packages/components/src/sandbox/types.ts @@ -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. * diff --git a/storybook/components-manifest.yml b/storybook/components-manifest.yml index 1813f92f5f1f2a..d1a62f1cc31d59 100644 --- a/storybook/components-manifest.yml +++ b/storybook/components-manifest.yml @@ -803,6 +803,7 @@ - style - name: SandBox props: + - allowForms - allowPopups - allowSameOrigin - html