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
6 changes: 4 additions & 2 deletions lib/block-supports/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ function gutenberg_apply_block_supports( $block_content, $block ) {

// Apply new styles and classes.
if ( ! empty( $new_classes ) ) {
$block_root->setAttribute( 'class', esc_attr( implode( ' ', $new_classes ) ) );
// `DOMElement::setAttribute` handles attribute value escaping.
$block_root->setAttribute( 'class', implode( ' ', $new_classes ) );
}

if ( ! empty( $new_styles ) ) {
$block_root->setAttribute( 'style', esc_attr( implode( '; ', $new_styles ) . ';' ) );
// `DOMElement::setAttribute` handles attribute value escaping.
$block_root->setAttribute( 'style', implode( '; ', $new_styles ) . ';' );
}

return $dom->saveHtml( $block_root );
Expand Down
44 changes: 44 additions & 0 deletions phpunit/class-block-supported-styles-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -817,4 +817,48 @@ function ( $errno = 0, $errstr = '' ) use ( &$errors ) {

$this->assertEmpty( $errors, 'Libxml errors should be dropped.' );
}

/**
* Ensures block attributes are output correctly.
*
* Some blocks saved with valid attributes were broken after the block was rendered. Ensure that
* block attributes are escaped correctly and safely.
*/
public function test_render_block_attribute() {
$this->register_block_type( 'core/example', array( 'render_callback' => true ) );

$block = array(
'blockName' => 'core/example',
'attrs' => array(),
);

// Tests of shape [ [ $input, $expected_result ], … ].
$tests = array(

// Valid single quotes in double-quoted attribute.
array(
'<div style="background-image:url(\'https://example.com/image.png?example=query&amp;args\')"></div>',
'<div style="background-image: url(\'https://example.com/image.png?example=query&amp;args\');" class="wp-block-example"></div>',
),

// Valid double quotes in single-quoted attribute.
array(
'<div style=\'background-image:url("https://example.com/image.png?example=query&amp;args")\'></div>',
'<div style=\'background-image: url("https://example.com/image.png?example=query&amp;args");\' class="wp-block-example"></div>',
),

// Encode attributes.
array(
'<div style="&quot;><script>alert(1)</script>"></div>',
'<div style=\'"&gt;&lt;script&gt;alert(1)&lt;/script&gt;;\' class="wp-block-example"></div>',
),
);

foreach ( $tests as $test ) {
$input = $test[0];
$expected = $test[1];
$result = apply_filters( 'render_block', $input, $block );
$this->assertEquals( $expected, $result );
}
}
}