Skip to content

Commit

Permalink
Update docs and remove superfluous escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jan 25, 2024
1 parent ba8ff86 commit 85185a4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/wp-includes/html-api/class-wp-html-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WP_HTML_Template extends WP_HTML_Tag_Processor {
*
* - `...named_arg` when found within an HTML tag will lookup `named_arg` in the arguments array
* and, if it's an array, will set the attribute on the tag for each key/value pair whose value
* is a string. The
* is a string, boolean, or `null`.
*
* ## Notes.
*
Expand Down Expand Up @@ -90,6 +90,7 @@ public static function render( $template, $args = array() ) {
$type = $processor->get_token_type();
$text = $processor->get_modifiable_text();

// Replace placeholders that are found inside #text nodes.
if ( '#funky-comment' === $type && strlen( $text ) > 0 && '%' === $text[0] ) {
$name = substr( $text, 1 );
$value = isset( $args[ $name ] ) && is_string( $args[ $name ] ) ? $args[ $name ] : null;
Expand All @@ -102,6 +103,7 @@ public static function render( $template, $args = array() ) {
continue;
}

// For every tag, scan the attributes to look for placeholders.
if ( '#tag' === $type ) {
foreach ( $processor->get_attribute_names_with_prefix( '' ) ?? array() as $attribute_name ) {
if ( str_starts_with( $attribute_name, '...' ) ) {
Expand All @@ -122,6 +124,7 @@ public static function render( $template, $args = array() ) {
continue;
}

// Replace entire attributes if their content is exclusively a placeholder, e.g. `title="</%title>"`.
$full_match = null;
if ( preg_match( '~^</%([^>]+)>$~', $value, $full_match ) ) {
$name = $full_match[1];
Expand All @@ -133,7 +136,7 @@ public static function render( $template, $args = array() ) {
} elseif ( true === $value ) {
$processor->set_attribute( $attribute_name, true );
} elseif ( is_string( $value ) ) {
$processor->set_attribute( $attribute_name, esc_attr( $args[ $name ] ) );
$processor->set_attribute( $attribute_name, $args[ $name ] );
} else {
$processor->remove_attribute( $attribute_name );
}
Expand All @@ -144,6 +147,7 @@ public static function render( $template, $args = array() ) {
continue;
}

// Replace placeholders embedded in otherwise-static attribute values, e.g. `title="Post: </%title>"`.
$new_value = preg_replace_callback(
'~</%([^>]+)>~',
static function ( $matches ) use ( $args ) {
Expand Down

0 comments on commit 85185a4

Please sign in to comment.