Skip to content

Commit bc1b62d

Browse files
Copilotsirrealyouknowriadellatrix
authored
Fix HTML Block: Hide CSS/JS tabs and show warning for users without unfiltered_html capability (#73749)
Co-authored-by: sirreal <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: ellatrix <[email protected]>
1 parent 30a94a2 commit bc1b62d

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

packages/block-library/src/html/modal.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Modal,
99
Button,
1010
Flex,
11+
Notice,
1112
privateApis as componentsPrivateApis,
1213
__experimentalHStack as HStack,
1314
__experimentalGrid as Grid,
@@ -48,8 +49,9 @@ export default function HTMLEditModal( {
4849
};
4950
}, [] );
5051

51-
// Show JS tab if user has permission OR if block contains JavaScript
52-
const shouldShowJsTab = canUserUseUnfilteredHTML || js.trim() !== '';
52+
// Determine if we should show a warning about CSS/JS content being stripped
53+
const hasRestrictedContent =
54+
! canUserUseUnfilteredHTML && ( css.trim() || js.trim() );
5355

5456
if ( ! isOpen ) {
5557
return null;
@@ -68,11 +70,13 @@ export default function HTMLEditModal( {
6870
setIsDirty( true );
6971
};
7072
const handleUpdate = () => {
73+
// For users without unfiltered_html capability, strip CSS and JS content
74+
// to prevent kses from leaving broken content
7175
setAttributes( {
7276
content: serializeContent( {
7377
html: editedHtml,
74-
css: editedCss,
75-
js: editedJs,
78+
css: canUserUseUnfilteredHTML ? editedCss : '',
79+
js: canUserUseUnfilteredHTML ? editedJs : '',
7680
} ),
7781
} );
7882
setIsDirty( false );
@@ -130,8 +134,10 @@ export default function HTMLEditModal( {
130134
<div>
131135
<Tabs.TabList>
132136
<Tabs.Tab tabId="html">HTML</Tabs.Tab>
133-
<Tabs.Tab tabId="css">CSS</Tabs.Tab>
134-
{ shouldShowJsTab && (
137+
{ canUserUseUnfilteredHTML && (
138+
<Tabs.Tab tabId="css">CSS</Tabs.Tab>
139+
) }
140+
{ canUserUseUnfilteredHTML && (
135141
<Tabs.Tab tabId="js">
136142
{ __( 'JavaScript' ) }
137143
</Tabs.Tab>
@@ -148,6 +154,17 @@ export default function HTMLEditModal( {
148154
/>
149155
</div>
150156
</HStack>
157+
{ hasRestrictedContent && (
158+
<Notice
159+
status="warning"
160+
isDismissible={ false }
161+
className="block-library-html__modal-notice"
162+
>
163+
{ __(
164+
'This block contains CSS or JavaScript that will be removed when you save because you do not have permission to use unfiltered HTML.'
165+
) }
166+
</Notice>
167+
) }
151168
<HStack
152169
alignment="stretch"
153170
justify="flex-start"
@@ -168,20 +185,22 @@ export default function HTMLEditModal( {
168185
className="block-library-html__modal-editor"
169186
/>
170187
</Tabs.TabPanel>
171-
<Tabs.TabPanel
172-
tabId="css"
173-
focusable={ false }
174-
className="block-library-html__modal-tab"
175-
>
176-
<PlainText
177-
value={ editedCss }
178-
onChange={ handleCssChange }
179-
placeholder={ __( 'Write CSS…' ) }
180-
aria-label={ __( 'CSS' ) }
181-
className="block-library-html__modal-editor"
182-
/>
183-
</Tabs.TabPanel>
184-
{ shouldShowJsTab && (
188+
{ canUserUseUnfilteredHTML && (
189+
<Tabs.TabPanel
190+
tabId="css"
191+
focusable={ false }
192+
className="block-library-html__modal-tab"
193+
>
194+
<PlainText
195+
value={ editedCss }
196+
onChange={ handleCssChange }
197+
placeholder={ __( 'Write CSS…' ) }
198+
aria-label={ __( 'CSS' ) }
199+
className="block-library-html__modal-editor"
200+
/>
201+
</Tabs.TabPanel>
202+
) }
203+
{ canUserUseUnfilteredHTML && (
185204
<Tabs.TabPanel
186205
tabId="js"
187206
focusable={ false }

0 commit comments

Comments
 (0)