-
Notifications
You must be signed in to change notification settings - Fork 59
Process inner html of blocks when escaping text content #719
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
Changes from 6 commits
5724b49
723a88f
a9ca35a
0460dc5
194764c
45c312c
678a8ef
bf8ff55
d55f322
3f0adfa
4504111
7a75758
c665286
730565a
bf603a9
6b5fe21
1e317ce
273378e
e3497d3
d6e9041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,72 @@ private static function escape_text_content( $string ) { | |
|
||
$string = addcslashes( $string, "'" ); | ||
|
||
// Process the string to avoid escaping inner HTML markup. | ||
$p = new WP_HTML_Tag_Processor( $string ); | ||
|
||
$text = ''; | ||
$tokens = array(); | ||
while ( $p->next_token() ) { | ||
$token_type = $p->get_token_type(); | ||
$token_name = strtolower( $p->get_token_name() ); | ||
$is_tag_closer = $p->is_tag_closer(); | ||
|
||
if ( '#tag' === $token_type ) { | ||
// Add a placeholder for the token. | ||
$text .= '%s'; | ||
if ( $is_tag_closer ) { | ||
$tokens[] = "</{$token_name}>"; | ||
} else { | ||
// Depending on the HTML tag, we may need to process attributes so they are correctly added to the placeholder. | ||
switch ( $token_name ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing I would like feedback on is whether this is too fragile, complicated, or tedious an approach to maintain. I plan to add a few more unit tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're good to rely on the output of That's only my opinion though 😅 Was there anything you were specifically concerned about with this approach? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think just needing to add cases for specific tags and if the attributes changes, it may break. But the options for adding inline HTML / formatting inside blocks are rather limited, so maybe it's okay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to deconstruct the attributes and reassemble them? I'm worried about dropping attributes or other parts of the markup that might be added by plugins or filters. For example, could we replace <a href="<?php echo esc_url( 'https://wordpress.org' ) >?"><?php echo esc_html__( 'WordPress' ) ?></a> but maintain any other attributes added to the I'm not really worried about escaping every possible attribute, I think the main thing is to make sure the translated strings are escaped as a form of user input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's a good point. I could not find a way with the HTML Tag Processor to just carry over the whole tag token and all its attributes. But I refactored the approach so that all the attributes should be carried over, rather than processing the attributes based on the type of tag: 678a8ef#diff-fc11ad66397ed68725a8fe74f3031f104368f850b5b94370123482ec9e719e1bR54-R70 |
||
// Handle links. | ||
case 'a': | ||
$href = esc_url( $p->get_attribute( 'href' ) ); | ||
$target = empty( esc_attr( $p->get_attribute( 'target' ) ) ) ? '' : ' target="_blank"'; | ||
$rel = empty( esc_attr( $p->get_attribute( 'rel' ) ) ) ? '' : ' rel="nofollow noopener noreferrer"'; | ||
$tokens[] = "<a href=\"{$href}\"{$target}{$rel}>"; | ||
break; | ||
// Handle inline images. | ||
case 'img': | ||
$src = esc_url( $p->get_attribute( 'src' ) ); | ||
$style = esc_attr( $p->get_attribute( 'style' ) ); | ||
$alt = esc_attr( $p->get_attribute( 'alt' ) ); | ||
|
||
CBT_Theme_Media::add_media_to_local( array( $src ) ); | ||
$relative_src = CBT_Theme_Media::get_media_folder_path_from_url( $src ) . basename( $src ); | ||
$tokens[] = "<img style=\"{$style}\" src=\"' . esc_url( get_stylesheet_directory_uri() ) . '{$relative_src}\" alt=\"{$alt}\">"; | ||
break; | ||
// Handle highlights. | ||
case 'mark': | ||
$style = esc_attr( $p->get_attribute( 'style' ) ); | ||
$class = esc_attr( $p->get_attribute( 'class' ) ); | ||
$tokens[] = "<mark style=\"{$style}\" class=\"{$class}\">"; | ||
break; | ||
jffng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Otherwise, just add the tag opener. | ||
default: | ||
$tokens[] = "<{$token_name}>"; | ||
break; | ||
} | ||
} | ||
} else { | ||
// If it's not a tag, just add the text content. | ||
$text .= esc_html( $p->get_modifiable_text() ); | ||
} | ||
} | ||
// If tokens is not empty, format the string using sprintf. | ||
if ( ! empty( $tokens ) ) { | ||
// Format the string, replacing the placeholders with the formatted tokens. | ||
return "<?php /* Translators: %s are html tags */ echo sprintf( esc_html__( '$text', '" . wp_get_theme()->get( 'TextDomain' ) . "' ), " . implode( | ||
jffng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
', ', | ||
array_map( | ||
function( $token ) { | ||
return "'$token'"; | ||
}, | ||
$tokens | ||
) | ||
) . ' ); ?>'; | ||
} | ||
creativecoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return "<?php esc_html_e('" . $string . "', '" . wp_get_theme()->get( 'TextDomain' ) . "');?>"; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.