Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Move attribute to wrapping tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 21, 2023
1 parent 43fec8e commit da195d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions phpunit/directives/attributes/wp-show.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function test_directive_wraps_content_in_template_if_when_is_false() {
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
process_wp_show( $tags, $context );

$updated_markup = '<template><div data-wp-show="context.myblock.open">I should not be shown!</div></template>';
$updated_markup = '<template data-wp-show="context.myblock.open"><div >I should not be shown!</div></template>';

$this->assertSame( $updated_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' );
Expand All @@ -64,7 +64,7 @@ public function test_directive_wraps_void_tag_in_template_if_when_is_false() {
$tags->next_tag( array( 'tag_closers' => 'visit' ) );
process_wp_show( $tags, $context );

$updated_markup = '<template><img data-wp-show="context.myblock.open"></template>';
$updated_markup = '<template data-wp-show="context.myblock.open"><img ></template>';

$this->assertSame( $updated_markup, $tags->get_updated_html() );
$this->assertSame( $context_before->get_context(), $context->get_context(), 'data-wp-show directive changed context' );
Expand Down
6 changes: 5 additions & 1 deletion src/directives/attributes/wp-show.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function process_wp_show( $tags, $context ) {

$show = evaluate( $value, $context->get_context() );
if ( ! $show ) {
$tags->wrap_in_tag( 'TEMPLATE' );
$wrapper_bookmark = $tags->wrap_in_tag( 'TEMPLATE' );
$tags->seek( $wrapper_bookmark );
$tags->set_attribute( 'data-wp-show', $value );
$tags->next_tag();
$tags->remove_attribute( 'data-wp-show' );
}
}
14 changes: 12 additions & 2 deletions src/directives/class-wp-directive-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public function get_balanced_tag_bookmarks() {
* calling the function.
*
* @param string $tag An HTML tag, specified in uppercase (e.g. "DIV").
* @return bool Whether the operation was successful.
* @return string|false The name of a bookmark pointing to the wrapping tag opener
* if successful; false otherwise.
*
* @todo Allow passing in tags with attributes, e.g. <template id="abc">?
*/
Expand Down Expand Up @@ -179,7 +180,16 @@ public function wrap_in_tag( $tag ) {
$this->seek( $start_name ); // Return to original position.
$this->release_bookmark( $start_name );

return true;
$i = 0;
while ( array_key_exists( $tag . $i, $this->bookmarks ) ) {
++$i;
}
$bookmark_name = $tag . $i;
$this->bookmarks[ $bookmark_name ] = new WP_HTML_Span(
$start,
$start + strlen( $tag ) + 2
);
return $bookmark_name;
}

/**
Expand Down

0 comments on commit da195d7

Please sign in to comment.