Skip to content

Commit ea309e0

Browse files
Add figcaption escaping for image and video blocks (#745)
* add figcaption escaping for video and image blocks. * update image block test and add video block test
1 parent 13cb16c commit ea309e0

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

includes/create-theme/theme-locale.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ private static function get_text_replacement_patterns_for_html( $block_name ) {
101101
return array( '/(<pre[^>]*>)(.*?)(<\/pre>)/' );
102102
case 'core/button':
103103
return array( '/(<a[^>]*>)(.*?)(<\/a>)/' );
104-
case 'core/image':
105-
case 'core/cover':
106-
case 'core/media-text':
107-
return array( '/alt="(.*?)"/' );
108104
case 'core/quote':
109105
case 'core/pullquote':
110106
return array(
@@ -117,6 +113,16 @@ private static function get_text_replacement_patterns_for_html( $block_name ) {
117113
'/(<th[^>]*>)(.*?)(<\/th>)/',
118114
'/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/',
119115
);
116+
case 'core/video':
117+
return array( '/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/' );
118+
case 'core/image':
119+
return array(
120+
'/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/',
121+
'/(alt=")(.*?)(")/',
122+
);
123+
case 'core/cover':
124+
case 'core/media-text':
125+
return array( '/(alt=")(.*?)(")/' );
120126
default:
121127
return null;
122128
}
@@ -158,19 +164,7 @@ public static function escape_text_content_of_blocks( $blocks ) {
158164
case 'core/quote':
159165
case 'core/pullquote':
160166
case 'core/table':
161-
$replace_content_callback = function ( $content, $pattern ) {
162-
if ( empty( $content ) ) {
163-
return;
164-
}
165-
return preg_replace_callback(
166-
$pattern,
167-
function( $matches ) {
168-
return $matches[1] . self::escape_text_content( $matches[2] ) . $matches[3];
169-
},
170-
$content
171-
);
172-
};
173-
break;
167+
case 'core/video':
174168
case 'core/image':
175169
case 'core/cover':
176170
case 'core/media-text':
@@ -181,7 +175,11 @@ function( $matches ) {
181175
return preg_replace_callback(
182176
$pattern,
183177
function( $matches ) {
184-
return 'alt="' . self::escape_attribute( $matches[1] ) . '"';
178+
// If the pattern is for attribute like alt="".
179+
if ( str_ends_with( $matches[1], '="' ) ) {
180+
return $matches[1] . self::escape_attribute( $matches[2] ) . $matches[3];
181+
}
182+
return $matches[1] . self::escape_text_content( $matches[2] ) . $matches[3];
185183
},
186184
$content
187185
);

tests/CbtThemeLocale/escapeTextContentOfBlocks.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ public function data_test_escape_text_content_of_blocks() {
125125

126126
'image' => array(
127127
'block_markup' =>
128-
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} -->
129-
<figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="Windows of a building in Nuremberg, Germany"/></figure>
128+
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->
129+
<figure class="wp-block-image size-large"><img src="http://example.org/image.webp" alt="image alt text" class="wp-image-151"/><figcaption class="wp-element-caption">Image caption</figcaption></figure>
130130
<!-- /wp:image -->',
131131
'expected_markup' =>
132-
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none","className":"is-style-rounded"} -->
133-
<figure class="wp-block-image size-large is-style-rounded"><img src="http://localhost/wp1/wp-content/themes/twentytwentyfour/assets/images/windows.webp" alt="<?php esc_attr_e(\'Windows of a building in Nuremberg, Germany\', \'test-locale-theme\');?>"/></figure>
132+
'<!-- wp:image {"sizeSlug":"large","linkDestination":"none"} -->
133+
<figure class="wp-block-image size-large"><img src="http://example.org/image.webp" alt="<?php esc_attr_e(\'image alt text\', \'test-locale-theme\');?>" class="wp-image-151"/><figcaption class="wp-element-caption"><?php esc_html_e(\'Image caption\', \'test-locale-theme\');?></figcaption></figure>
134134
<!-- /wp:image -->',
135135
),
136136

@@ -186,6 +186,15 @@ public function data_test_escape_text_content_of_blocks() {
186186
<!-- /wp:table -->',
187187
),
188188

189+
'video' => array(
190+
'block_markup' => '<!-- wp:video -->
191+
<figure class="wp-block-video"><video controls src="http://example.org/video.mp4"></video><figcaption class="wp-element-caption">Video caption test</figcaption></figure>
192+
<!-- /wp:video -->',
193+
'expected_markup' =>
194+
'<!-- wp:video -->
195+
<figure class="wp-block-video"><video controls src="http://example.org/video.mp4"></video><figcaption class="wp-element-caption"><?php esc_html_e(\'Video caption test\', \'test-locale-theme\');?></figcaption></figure>
196+
<!-- /wp:video -->',
197+
),
189198
);
190199
}
191200
}

0 commit comments

Comments
 (0)