-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block editor: iframe: load inline styles (#33389)
- Loading branch information
1 parent
fd70931
commit e03c496
Showing
12 changed files
with
219 additions
and
10 deletions.
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
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,52 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Test Iframed Inline Styles | ||
* Plugin URI: https://github.com/WordPress/gutenberg | ||
* Author: Gutenberg Team | ||
* | ||
* @package gutenberg-test-iframed-inline-styles | ||
*/ | ||
|
||
add_action( | ||
'setup_theme', | ||
function() { | ||
add_theme_support( 'block-templates' ); | ||
} | ||
); | ||
|
||
add_action( | ||
'init', | ||
function() { | ||
wp_register_script( | ||
'iframed-inline-styles-editor-script', | ||
plugin_dir_url( __FILE__ ) . 'iframed-inline-styles/editor.js', | ||
array( | ||
'wp-blocks', | ||
'wp-block-editor', | ||
'wp-element', | ||
), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'iframed-inline-styles/editor.js' ) | ||
); | ||
wp_register_style( | ||
'iframed-inline-styles-style', | ||
plugin_dir_url( __FILE__ ) . 'iframed-inline-styles/style.css', | ||
array(), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'iframed-inline-styles/style.css' ) | ||
); | ||
wp_add_inline_style( 'iframed-inline-styles-style', '.wp-block-test-iframed-inline-styles{padding:20px}' ); | ||
register_block_type_from_metadata( __DIR__ . '/iframed-inline-styles' ); | ||
} | ||
); | ||
|
||
add_action( | ||
'enqueue_block_editor_assets', | ||
function() { | ||
wp_enqueue_style( | ||
'iframed-inline-styles-compat-style', | ||
plugin_dir_url( __FILE__ ) . 'iframed-inline-styles/compat-style.css', | ||
array(), | ||
filemtime( plugin_dir_path( __FILE__ ) . 'iframed-inline-styles/compat-style.css' ) | ||
); | ||
wp_add_inline_style( 'iframed-inline-styles-compat-style', '.wp-block-test-iframed-inline-styles{border-width:2px}' ); | ||
} | ||
); |
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/iframed-inline-styles/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 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/iframed-inline-styles", | ||
"title": "Iframed Inline Styles", | ||
"category": "text", | ||
"icon": "smiley", | ||
"description": "", | ||
"supports": { | ||
"html": false | ||
}, | ||
"textdomain": "iframed-inline-styles", | ||
"editorScript": "iframed-inline-styles-editor-script", | ||
"editorStyle": "file:./editor.css", | ||
"style": "iframed-inline-styles-style" | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/e2e-tests/plugins/iframed-inline-styles/compat-style.css
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,4 @@ | ||
/* Random rule with `wp-block` in the selector. */ | ||
.wp-block-test-iframed-inline-styles { | ||
display: block; | ||
} |
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,6 @@ | ||
/** | ||
* The following styles get applied inside the editor only. | ||
*/ | ||
.wp-block-test-iframed-inline-styles { | ||
border: 1px dotted #f00; | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/iframed-inline-styles/editor.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,15 @@ | ||
( ( { wp: { element, blocks, blockEditor } } ) => { | ||
const { createElement: el } = element; | ||
const { registerBlockType } = blocks; | ||
const { useBlockProps } = blockEditor; | ||
|
||
registerBlockType( 'test/iframed-inline-styles', { | ||
apiVersion: 2, | ||
edit: function Edit() { | ||
return el( 'div', useBlockProps(), 'Edit' ); | ||
}, | ||
save: function Save() { | ||
return el( 'div', useBlockProps.save(), 'Save' ); | ||
}, | ||
} ); | ||
} )( window ); |
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,9 @@ | ||
/** | ||
* The following styles get applied both on the front of your site and in the | ||
* editor. | ||
*/ | ||
.wp-block-test-iframed-inline-styles { | ||
background-color: #21759b; | ||
color: #fff; | ||
padding: 2px; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
packages/e2e-tests/specs/editor/plugins/__snapshots__/iframed-inline-styles.test.js.snap
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,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`iframed inline styles should load inline styles in iframe 1`] = ` | ||
"<!-- wp:test/iframed-inline-styles --> | ||
<div class=\\"wp-block-test-iframed-inline-styles\\">Save</div> | ||
<!-- /wp:test/iframed-inline-styles -->" | ||
`; |
2 changes: 1 addition & 1 deletion
2
packages/e2e-tests/specs/editor/plugins/__snapshots__/iframed-masonry-block.test.js.snap
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
69 changes: 69 additions & 0 deletions
69
packages/e2e-tests/specs/editor/plugins/iframed-inline-styles.test.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,69 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
activatePlugin, | ||
createNewPost, | ||
deactivatePlugin, | ||
insertBlock, | ||
getEditedPostContent, | ||
openDocumentSettingsSidebar, | ||
clickButton, | ||
canvas, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
async function getComputedStyle( context, property ) { | ||
return await context.evaluate( ( prop ) => { | ||
const container = document.querySelector( | ||
'.wp-block-test-iframed-inline-styles' | ||
); | ||
return window.getComputedStyle( container )[ prop ]; | ||
}, property ); | ||
} | ||
|
||
describe( 'iframed inline styles', () => { | ||
beforeEach( async () => { | ||
await activatePlugin( 'gutenberg-test-iframed-inline-styles' ); | ||
await createNewPost( { postType: 'page' } ); | ||
} ); | ||
|
||
afterEach( async () => { | ||
await deactivatePlugin( 'gutenberg-test-iframed-inline-styles' ); | ||
} ); | ||
|
||
it( 'should load inline styles in iframe', async () => { | ||
await insertBlock( 'Iframed Inline Styles' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
expect( await getComputedStyle( page, 'padding' ) ).toBe( '20px' ); | ||
expect( await getComputedStyle( page, 'border-width' ) ).toBe( '2px' ); | ||
|
||
await openDocumentSettingsSidebar(); | ||
await clickButton( 'Page' ); | ||
await clickButton( 'Template' ); | ||
await clickButton( 'New' ); | ||
await page.keyboard.press( 'Tab' ); | ||
await page.keyboard.press( 'Tab' ); | ||
await page.keyboard.type( 'Iframed Test' ); | ||
await clickButton( 'Create' ); | ||
await page.waitForSelector( 'iframe[name="editor-canvas"]' ); | ||
await canvas().waitForSelector( | ||
'.wp-block-test-iframed-inline-styles' | ||
); | ||
|
||
// Inline styles of properly enqueued stylesheet should load. | ||
expect( await getComputedStyle( canvas(), 'padding' ) ).toBe( '20px' ); | ||
|
||
// Inline styles of stylesheet loaded with the compatibility layer | ||
// should load. | ||
expect( await getComputedStyle( canvas(), 'border-width' ) ).toBe( | ||
'2px' | ||
); | ||
|
||
expect( console ).toHaveErrored( | ||
`Stylesheet iframed-inline-styles-compat-style-css was not properly added. | ||
For blocks, use the block API's style (https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#style) or editorStyle (https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#editor-style). | ||
For themes, use add_editor_style (https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#editor-styles). <link rel="stylesheet" id="iframed-inline-styles-compat-style-css" href="http://localhost:8889/wp-content/plugins/gutenberg-test-plugins/iframed-inline-styles/compat-style.css?ver=1626189899" media="all">` | ||
); | ||
} ); | ||
} ); |
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