Skip to content

Commit

Permalink
Parser: In BasicText parser, replace entire html elements or attributes.
Browse files Browse the repository at this point in the history
This prevents nested or broken replacements when there are multiples of the same string, or if a string appears as a substring of another string.
  • Loading branch information
ryelle committed Jul 7, 2023
1 parent 8f9a77e commit aecc15c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
22 changes: 16 additions & 6 deletions env/export-content/includes/parsers/BasicText.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@ public function replace_strings( array $block, array $replacements ) : array {
$xpath = new \DOMXPath( $dom );

$text_nodes = $xpath->query( $xpath_query );
$strings = array();

// Only update text matches that are found outside of HTML tags.
// This approach does not use $dom->saveHTML because innerContent includes
// unclosed HTML tags, and saveHTML adds extra closed tags.
// Get a unique list of things to replace.
foreach ( $text_nodes as $text ) {
if ( trim( $text->nodeValue ) && isset( $replacements[ $text->nodeValue ] ) ) {
$regex = '#(<([^>]*)>)?' . preg_quote( $text->nodeValue, '#' ) . '(<([^>]*)>)?#s';
$inner_content = preg_replace( $regex, '${1}' . $replacements[ $text->nodeValue ] . '${3}', $inner_content );
if ( ! in_array( $text->nodeValue, $strings ) ) {
$strings[] = $text->nodeValue;
}
}

foreach ( $strings as $string ) {
if ( trim( $string ) && isset( $replacements[ $string ] ) ) {
// Replace only things inside HTML tags or attributes, to prevent nested replacements.
// For example, if we have "Jetpack" and "Use Jetpack!", this prevents the latter
// string from becoming `<?php _e( 'Use <?php _e( 'Jetpack', 'wporg' );!', 'wporg' ).
// There might be edge cases where the string is not wrapped in tags/quotes, but I
// haven't seen one in testing.
$regex = '#(<([^>]*)>|=")' . preg_quote( $string, '#' ) . '(<([^>]*)>|")#s';
$inner_content = preg_replace( $regex, '${1}' . $replacements[ $string ] . '${3}', $inner_content );
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions env/export-content/tests/block-parser-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function data_block_content_strings() {
"<!-- wp:list -->\n<ul><!-- wp:list-item -->\n<li>APIs:<!-- wp:list -->\n<ul>\n<!-- wp:list-item -->\n<li>Fonts API</li>\n<!-- /wp:list-item -->\n<!-- wp:list-item -->\n<li>Interactivity API</li>\n<!-- /wp:list-item -->\n<!-- wp:list-item -->\n<li>Block API</li>\n<!-- /wp:list-item --></ul>\n<!-- /wp:list --></li>\n<!-- /wp:list-item -->\n</ul>\n<!-- /wp:list -->",
[ 'APIs:', 'Fonts API', 'Interactivity API', 'Block API' ],
],
[
"<!-- wp:table -->\n<figure class=\"wp-block-table\"><table><thead><tr><th>Cookie</th><th>Logged-in Users Only?</th></tr></thead><tbody><tr><th>welcome-{blog_id}</th><td>No</td></tr><tr><th>showComments</th><td>No</td></tr></tbody></table></figure>\n<!-- /wp:table -->",
[ 'Cookie', 'Logged-in Users Only?', 'welcome-{blog_id}', 'No', 'showComments' ],
],
];
}

Expand Down Expand Up @@ -126,6 +130,11 @@ public function data_block_content_i18n() {
"<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><!-- wp:paragraph -->\m<p>I'm interested in running the open-source WordPress &lt;https://wordpress.org/&gt; web software and I was wondering if my account supported the following:</p>\n<!-- /wp:paragraph --></blockquote>\n<!-- /wp:quote -->",
"<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><!-- wp:paragraph -->\m<p><?php _e( 'I&#039;m interested in running the open-source WordPress &lt;https://wordpress.org/&gt; web software and I was wondering if my account supported the following:', 'wporg' ); ?></p>\n<!-- /wp:paragraph --></blockquote>\n<!-- /wp:quote -->",
],
[
// Block with repeated strings.
"<!-- wp:table -->\n<figure class=\"wp-block-table\"><table><thead><tr><th>Cookie</th><th>Logged-in Users Only?</th></tr></thead><tbody><tr><th>welcome-{blog_id}</th><td>No</td></tr><tr><th>showComments</th><td>No</td></tr></tbody></table></figure>\n<!-- /wp:table -->",
"<!-- wp:table -->\n<figure class=\"wp-block-table\"><table><thead><tr><th><?php _e( 'Cookie', 'wporg' ); ?></th><th><?php _e( 'Logged-in Users Only?', 'wporg' ); ?></th></tr></thead><tbody><tr><th><?php _e( 'welcome-{blog_id}', 'wporg' ); ?></th><td><?php _e( 'No', 'wporg' ); ?></td></tr><tr><th><?php _e( 'showComments', 'wporg' ); ?></th><td><?php _e( 'No', 'wporg' ); ?></td></tr></tbody></table></figure>\n<!-- /wp:table -->",
],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<!-- /wp:paragraph -->

<!-- wp:table {"className":"is-style-stripes"} -->
<figure class="wp-block-table is-style-stripes"><table><thead><tr><th><strong><?php _e( 'Version', 'wporg' ); ?></strong></th><th><strong><?php _e( 'Planned', 'wporg' ); ?></strong></th></tr></thead><tbody><tr><td><?php _e( '6.3 ', 'wporg' ); ?><a href="https://core.trac.wordpress.org/tickets/major"><?php _e( 'Trac', 'wporg' ); ?></a></td><td><a href="https://make.wordpress.org/core/6-3/"><?php _e( 'August 2023', 'wporg' ); ?></a></td></tr><tr><td><?php _e( '6.4', 'wporg' ); ?></td><td><?php _e( 'November 2023', 'wporg' ); ?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php _e( 'Planned', 'wporg' ); ?> WordPress releases</figcaption></figure>
<figure class="wp-block-table is-style-stripes"><table><thead><tr><th><strong><?php _e( 'Version', 'wporg' ); ?></strong></th><th><strong><?php _e( 'Planned', 'wporg' ); ?></strong></th></tr></thead><tbody><tr><td><?php _e( '6.3 ', 'wporg' ); ?><a href="https://core.trac.wordpress.org/tickets/major"><?php _e( 'Trac', 'wporg' ); ?></a></td><td><a href="https://make.wordpress.org/core/6-3/"><?php _e( 'August 2023', 'wporg' ); ?></a></td></tr><tr><td><?php _e( '6.4', 'wporg' ); ?></td><td><?php _e( 'November 2023', 'wporg' ); ?></td></tr></tbody></table><figcaption class="wp-element-caption"><?php _e( 'Planned WordPress releases', 'wporg' ); ?></figcaption></figure>
<!-- /wp:table -->

<!-- wp:paragraph -->
Expand Down

0 comments on commit aecc15c

Please sign in to comment.