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

Commit

Permalink
SSR: Remove wp-context tag directive (#165)
Browse files Browse the repository at this point in the history
* Remove wp-context tag directive
* Rename process_wp_context_attribute to process_wp_context
* Remove logic for tag directives from wp_process_directives
* Fix unit test
  • Loading branch information
ockham committed Mar 2, 2023
1 parent 22065df commit 5508ec9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 136 deletions.
6 changes: 3 additions & 3 deletions phpunit/directives/attributes/wp-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Tests for the wp-context attribute directive.
*
* @group directives
* @covers process_wp_context_attribute
* @covers process_wp_context
*/
class Tests_Directives_Attributes_WpContext extends WP_UnitTestCase {
public function test_directive_merges_context_correctly_upon_wp_context_attribute_on_opening_tag() {
Expand All @@ -28,7 +28,7 @@ public function test_directive_merges_context_correctly_upon_wp_context_attribut
$tags = new WP_HTML_Tag_Processor( $markup );
$tags->next_tag();

process_wp_context_attribute( $tags, $context );
process_wp_context( $tags, $context );

$this->assertSame(
array(
Expand All @@ -52,7 +52,7 @@ public function test_directive_resets_context_correctly_upon_closing_tag() {
$tags = new WP_HTML_Tag_Processor( $markup );
$tags->next_tag( array( 'tag_closers' => 'visit' ) );

process_wp_context_tag( $tags, $context );
process_wp_context( $tags, $context );

$this->assertSame(
array( 'my-key' => 'original-value' ),
Expand Down
62 changes: 0 additions & 62 deletions phpunit/directives/tags/wp-context.php

This file was deleted.

8 changes: 4 additions & 4 deletions phpunit/directives/wp-process-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ function( $p ) {
)
);

$attribute_directives = array(
$directives = array(
'foo-test' => array( $test_helper, 'process_foo_test' ),
);

$markup = '<div>Example: <div foo-test="abc"><img><span>This is a test></span><div>Here is a nested div</div></div></div>';
$tags = new WP_HTML_Tag_Processor( $markup );
wp_process_directives( $tags, 'foo-', array(), $attribute_directives );
wp_process_directives( $tags, 'foo-', $directives );
}

public function test_directives_with_colon_processed_correctly() {
$test_helper = $this->createMock( Helper_Class::class );
$test_helper->expects( $this->atLeastOnce() )
->method( 'process_foo_test' );

$attribute_directives = array(
$directives = array(
'foo-test' => array( $test_helper, 'process_foo_test' ),
);

$markup = '<div foo-test:value="abc"></div>';
$tags = new WP_HTML_Tag_Processor( $markup );
wp_process_directives( $tags, 'foo-', array(), $attribute_directives );
wp_process_directives( $tags, 'foo-', $directives );
}
}
2 changes: 1 addition & 1 deletion src/directives/attributes/wp-context.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function process_wp_context_attribute( $tags, $context ) {
function process_wp_context( $tags, $context ) {
if ( $tags->is_tag_closer() ) {
$context->rewind_context();
return;
Expand Down
19 changes: 0 additions & 19 deletions src/directives/tags/wp-context.php

This file was deleted.

75 changes: 35 additions & 40 deletions src/directives/wp-process-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,54 @@

require_once __DIR__ . '/class-wp-directive-context.php';

function wp_process_directives( $tags, $prefix, $tag_directives, $attribute_directives ) {
function wp_process_directives( $tags, $prefix, $directives ) {
$context = new WP_Directive_Context;

$tag_stack = array();
while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
$tag_name = strtolower( $tags->get_tag() );
if ( array_key_exists( $tag_name, $tag_directives ) ) {
call_user_func( $tag_directives[ $tag_name ], $tags, $context );
} else {
// Components can't have directives (unless we change our mind about this).

// Is this a tag that closes the latest opening tag?
if ( $tags->is_tag_closer() ) {
if ( 0 === count( $tag_stack ) ) {
continue;
}
// Is this a tag that closes the latest opening tag?
if ( $tags->is_tag_closer() ) {
if ( 0 === count( $tag_stack ) ) {
continue;
}

list( $latest_opening_tag_name, $attributes ) = end( $tag_stack );
if ( $latest_opening_tag_name === $tag_name ) {
array_pop( $tag_stack );
list( $latest_opening_tag_name, $attributes ) = end( $tag_stack );
if ( $latest_opening_tag_name === $tag_name ) {
array_pop( $tag_stack );

// If the matching opening tag didn't have any attribute directives,
// we move on.
if ( 0 === count( $attributes ) ) {
continue;
}
// If the matching opening tag didn't have any attribute directives,
// we move on.
if ( 0 === count( $attributes ) ) {
continue;
}
} else {
// Helper that removes the part after the colon before looking
// for the directive processor inside `$attribute_directives`.
$get_directive_type = function ( $attr ) {
return strtok( $attr, ':' );
};
}
} else {
// Helper that removes the part after the colon before looking
// for the directive processor inside `$attribute_directives`.
$get_directive_type = function ( $attr ) {
return strtok( $attr, ':' );
};

$attributes = $tags->get_attribute_names_with_prefix( $prefix );
$attributes = array_map( $get_directive_type, $attributes );
$attributes = array_intersect( $attributes, array_keys( $attribute_directives ) );
$attributes = $tags->get_attribute_names_with_prefix( $prefix );
$attributes = array_map( $get_directive_type, $attributes );
$attributes = array_intersect( $attributes, array_keys( $directives ) );

// If this is an open tag, and if it either has attribute directives,
// or if we're inside a tag that does, take note of this tag and its attribute
// directives so we can call its directive processor once we encounter the
// matching closing tag.
if (
! is_html_void_element( $tags->get_tag() ) &&
( 0 !== count( $attributes ) || 0 !== count( $tag_stack ) )
) {
$tag_stack[] = array( $tag_name, $attributes );
}
// If this is an open tag, and if it either has attribute directives,
// or if we're inside a tag that does, take note of this tag and its attribute
// directives so we can call its directive processor once we encounter the
// matching closing tag.
if (
! is_html_void_element( $tags->get_tag() ) &&
( 0 !== count( $attributes ) || 0 !== count( $tag_stack ) )
) {
$tag_stack[] = array( $tag_name, $attributes );
}
}

foreach ( $attributes as $attribute ) {
call_user_func( $attribute_directives[ $attribute ], $tags, $context );
}
foreach ( $attributes as $attribute ) {
call_user_func( $directives[ $attribute ], $tags, $context );
}
}

Expand Down
10 changes: 3 additions & 7 deletions wp-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,15 @@ function bhe_inner_blocks( $parsed_block, $source_block, $parent_block ) {

function process_directives_in_block( $block_content ) {
// TODO: Add some directive/components registration mechanism.
$tag_directives = array(
'wp-context' => 'process_wp_context_tag',
);

$attribute_directives = array(
'wp-context' => 'process_wp_context_attribute',
$directives = array(
'wp-context' => 'process_wp_context',
'wp-bind' => 'process_wp_bind',
'wp-class' => 'process_wp_class',
'wp-style' => 'process_wp_style',
);

$tags = new WP_HTML_Tag_Processor( $block_content );
$tags = wp_process_directives( $tags, 'wp-', $tag_directives, $attribute_directives );
$tags = wp_process_directives( $tags, 'wp-', $directives );
return $tags->get_updated_html();
}
add_filter(
Expand Down

0 comments on commit 5508ec9

Please sign in to comment.