-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Dynamically add CSS class to Paragraph block #71207
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2330b2e
Dynamically add CSS classe to Paragraph block
t-hamano b086591
Fix unit test
t-hamano 804703b
Fix e2e tests
t-hamano d1a577f
Update PHPDoc
t-hamano 77aa2a1
Simplify HTML Tag Processor logic
t-hamano 8b1ec0c
Merge branch 'trunk' into dynamically-add-css-class-to-paragraph
t-hamano d8ae682
Use render_block hook
t-hamano bfba47f
Register block on server side
t-hamano 9b8d796
Fix unit test
t-hamano 5091d56
Add data_prefix to dataprovider function name
t-hamano e3ca87f
Add param PHPDoc
t-hamano 990a58f
Removed unnecessary @cover PHPDoc
t-hamano 7c2e0f0
Merge branch 'trunk' into dynamically-add-css-class-to-paragraph
t-hamano 68dde11
Remove unnecessary file
t-hamano 72aa10b
Remove redundant blank lines
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,48 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/paragraph` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Append the `wp-block-paragraph` class before rendering the stored | ||
| * `core/paragraph` block contents. | ||
| * | ||
| * For example, the following block content: | ||
| * <p class="align-left">Hello World</p> | ||
| * | ||
| * Would be transformed to: | ||
| * <p class="align-left wp-block-paragraph">Hello World</p> | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $block_content The block content. | ||
| * | ||
| * @return string Filtered block content. | ||
| */ | ||
| function block_core_paragraph_add_class( $block_content ) { | ||
| if ( ! $block_content ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $processor = new WP_HTML_Tag_Processor( $block_content ); | ||
|
|
||
| if ( $processor->next_tag( 'p' ) ) { | ||
| $processor->add_class( 'wp-block-paragraph' ); | ||
| } | ||
|
|
||
| return $processor->get_updated_html(); | ||
| } | ||
|
|
||
| add_filter( 'render_block_core/paragraph', 'block_core_paragraph_add_class' ); | ||
|
|
||
| /** | ||
| * Registers the `core/paragraph` block on server. | ||
| * | ||
| * @since 7.0.0 | ||
| */ | ||
| function register_block_core_paragraph() { | ||
| register_block_type_from_metadata( __DIR__ . '/paragraph' ); | ||
| } | ||
| add_action( 'init', 'register_block_core_paragraph' ); |
This file contains hidden or 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 hidden or 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,90 @@ | ||
| <?php | ||
| /** | ||
| * Paragraph block rendering tests. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Blocks | ||
| */ | ||
|
|
||
| /** | ||
| * Tests for the Paragraph block. | ||
| * | ||
| * @group blocks | ||
| */ | ||
| class Render_Block_Paragraph_Test extends WP_UnitTestCase { | ||
|
|
||
|
|
||
| /** | ||
| * @dataProvider data_css_class_examples | ||
| * | ||
| * @param string $input The input HTML. | ||
| * @param string $expected_result The expected result HTML. | ||
| */ | ||
| public function test_block_core_paragraph_render_appends_css_class_to_a_vanilla_element( $input, $expected_result ) { | ||
| $parsed_blocks = parse_blocks( '<!-- wp:paragraph -->' . $input . '<!-- /wp:paragraph -->' ); | ||
| $block = new WP_Block( $parsed_blocks[0] ); | ||
| $actual = $block->render(); | ||
| $this->assertEquals( $expected_result, $actual ); | ||
| } | ||
|
|
||
| public function data_css_class_examples() { | ||
| return array( | ||
| 'should add a class name to a vanilla p element' => array( | ||
| '<p>Hello World</p>', | ||
| '<p class="wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should not add a class name to a div element' => array( | ||
| '<div>Hello World</div>', | ||
| '<div>Hello World</div>', | ||
| ), | ||
| 'should not add a class name to a span element' => array( | ||
| '<span>Hello World</span>', | ||
| '<span>Hello World</span>', | ||
| ), | ||
| 'should not add a class name to a heading element' => array( | ||
| '<h2>Hello World</h2>', | ||
| '<h2>Hello World</h2>', | ||
| ), | ||
| 'should add a class name even when the class attribute is already defined' => array( | ||
| '<p class="is-align-right">Hello World</p>', | ||
| '<p class="is-align-right wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should handle single quotes' => array( | ||
| "<p class='is-align-right'>Hello World</p>", | ||
| '<p class="is-align-right wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should handle single quotes with double quotes inside' => array( | ||
| "<p class='\" is-align-right'>Hello World</p>", | ||
| '<p class="" is-align-right wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should not add a class name even when it is already defined' => array( | ||
| '<p class="is-align-right wp-block-paragraph">Hello World</p>', | ||
| '<p class="is-align-right wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should add a class name even when there are other HTML attributes present' => array( | ||
| '<p style="display: block">Hello World</p>', | ||
| '<p class="wp-block-paragraph" style="display: block">Hello World</p>', | ||
| ), | ||
| 'should add a class name even when the class attribute is already defined and has many entries' => array( | ||
| '<p class="is-align-right custom classes">Hello World</p>', | ||
| '<p class="is-align-right custom classes wp-block-paragraph">Hello World</p>', | ||
| ), | ||
| 'should not add a class name to a nested p' => array( | ||
| '<p class="is-align-right custom classes"><p>Hello World</p></p>', | ||
| '<p class="is-align-right custom classes wp-block-paragraph"><p>Hello World</p></p>', | ||
| ), | ||
| 'should not add a class name to a nested p when the parent has another attribute' => array( | ||
| '<p style="display: block" class="is-align-right"><p>Hello World</p></p>', | ||
| '<p style="display: block" class="is-align-right wp-block-paragraph"><p>Hello World</p></p>', | ||
| ), | ||
| 'should add a class name even when the class attribute is surrounded by other attributes' => array( | ||
| '<p style="display: block" class="is-align-right" data-class="corner case!"><p>Hello World</p></p>', | ||
| '<p style="display: block" class="is-align-right wp-block-paragraph" data-class="corner case!"><p>Hello World</p></p>', | ||
| ), | ||
| 'should add a class name without getting confused when there is a tricky data-class attribute present' => array( | ||
| '<p data-class="corner case!" style="display: block" class="is-align-right"><p>Hello World</p></p>', | ||
| '<p data-class="corner case!" style="display: block" class="is-align-right wp-block-paragraph"><p>Hello World</p></p>', | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.