diff --git a/phpunit/directives/wp-bind.php b/phpunit/directives/attributes/wp-bind.php similarity index 64% rename from phpunit/directives/wp-bind.php rename to phpunit/directives/attributes/wp-bind.php index 1e6c4122..4b74df24 100644 --- a/phpunit/directives/wp-bind.php +++ b/phpunit/directives/attributes/wp-bind.php @@ -3,11 +3,11 @@ * wp-bind directive test. */ -require_once __DIR__ . '/../../src/directives/wp-bind.php'; +require_once __DIR__ . '/../../../src/directives/attributes/wp-bind.php'; -require_once __DIR__ . '/../../src/directives/class-wp-directive-context.php'; +require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; -require_once __DIR__ . '/../../../gutenberg/lib/experimental/html/index.php'; +require_once __DIR__ . '/../../../../gutenberg/lib/experimental/html/index.php'; /** * Tests for the wp-bind directive. @@ -46,18 +46,4 @@ public function test_directive_ignores_empty_bound_attribute() { $this->assertNull( $tags->get_attribute( 'src' ) ); $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-bind directive changed context' ); } - - public function test_directive_ignores_wp_bind_tag() { - $markup = ''; - $tags = new WP_HTML_Tag_Processor( $markup ); - $tags->next_tag(); - - $context_before = new WP_Directive_Context( array( 'myblock' => array( 'imageSource' => './wordpress.png' ) ) ); - $context = $context_before; - process_wp_bind( $tags, $context ); - - $this->assertSame( $markup, $tags->get_updated_html() ); - $this->assertNull( $tags->get_attribute( 'src' ) ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-bind directive changed context' ); - } } diff --git a/phpunit/directives/wp-class.php b/phpunit/directives/attributes/wp-class.php similarity index 81% rename from phpunit/directives/wp-class.php rename to phpunit/directives/attributes/wp-class.php index 22494e24..e8fef2b4 100644 --- a/phpunit/directives/wp-class.php +++ b/phpunit/directives/attributes/wp-class.php @@ -3,11 +3,11 @@ * wp-class directive test. */ -require_once __DIR__ . '/../../src/directives/wp-class.php'; +require_once __DIR__ . '/../../../src/directives/attributes/wp-class.php'; -require_once __DIR__ . '/../../src/directives/class-wp-directive-context.php'; +require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; -require_once __DIR__ . '/../../../gutenberg/lib/experimental/html/index.php'; +require_once __DIR__ . '/../../../../gutenberg/lib/experimental/html/index.php'; /** * Tests for the wp-class directive. @@ -98,18 +98,4 @@ public function test_directive_ignores_empty_class_name() { $this->assertStringNotContainsString( 'red', $tags->get_attribute( 'class' ) ); $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-class directive changed context' ); } - - public function test_directive_ignores_wp_class_tag() { - $markup = 'Test'; - $tags = new WP_HTML_Tag_Processor( $markup ); - $tags->next_tag(); - - $context_before = new WP_Directive_Context( array( 'myblock' => array( 'isRed' => true ) ) ); - $context = $context_before; - process_wp_class( $tags, $context ); - - $this->assertSame( $markup, $tags->get_updated_html() ); - $this->assertStringNotContainsString( 'red', $tags->get_attribute( 'class' ) ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-class directive changed context' ); - } } diff --git a/phpunit/directives/attributes/wp-context.php b/phpunit/directives/attributes/wp-context.php new file mode 100644 index 00000000..f3460cb2 --- /dev/null +++ b/phpunit/directives/attributes/wp-context.php @@ -0,0 +1,43 @@ +markTestSkipped( 'Need to implement the wp-context attribute directive processor first.' ); + + $context = new WP_Directive_Context( + array( + 'myblock' => array( 'open' => false ), + 'otherblock' => array( 'somekey' => 'somevalue' ), + ) + ); + + $markup = '
'; + $tags = new WP_HTML_Tag_Processor( $markup ); + $tags->next_tag(); + + process_wp_context_attribute( $tags, $context ); + + $this->assertSame( + array( + 'myblock' => array( 'open' => true ), + 'otherblock' => array( 'somekey' => 'somevalue' ), + ), + $context->get_context() + ); + } +} diff --git a/phpunit/directives/wp-style.php b/phpunit/directives/attributes/wp-style.php similarity index 64% rename from phpunit/directives/wp-style.php rename to phpunit/directives/attributes/wp-style.php index 37749bd6..99be8e9a 100644 --- a/phpunit/directives/wp-style.php +++ b/phpunit/directives/attributes/wp-style.php @@ -3,11 +3,11 @@ * wp-style directive test. */ -require_once __DIR__ . '/../../src/directives/wp-style.php'; +require_once __DIR__ . '/../../../src/directives/attributes/wp-style.php'; -require_once __DIR__ . '/../../src/directives/class-wp-directive-context.php'; +require_once __DIR__ . '/../../../src/directives/class-wp-directive-context.php'; -require_once __DIR__ . '/../../../gutenberg/lib/experimental/html/index.php'; +require_once __DIR__ . '/../../../../gutenberg/lib/experimental/html/index.php'; /** * Tests for the wp-style directive. @@ -46,18 +46,4 @@ public function test_directive_ignores_empty_style() { $this->assertStringNotContainsString( 'color: green;', $tags->get_attribute( 'style' ) ); $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-style directive changed context' ); } - - public function test_directive_ignores_wp_style_tag() { - $markup = 'Test
'; - $tags = new WP_HTML_Tag_Processor( $markup ); - $tags->next_tag(); - - $context_before = new WP_Directive_Context( array( 'myblock' => array( 'color' => 'green' ) ) ); - $context = $context_before; - process_wp_style( $tags, $context ); - - $this->assertSame( $markup, $tags->get_updated_html() ); - $this->assertStringNotContainsString( 'color: green;', $tags->get_attribute( 'style' ) ); - $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-style directive changed context' ); - } } diff --git a/phpunit/directives/wp-context.php b/phpunit/directives/tags/wp-context.php similarity index 72% rename from phpunit/directives/wp-context.php rename to phpunit/directives/tags/wp-context.php index 84e6dd62..c2623477 100644 --- a/phpunit/directives/wp-context.php +++ b/phpunit/directives/tags/wp-context.php @@ -1,21 +1,21 @@ next_tag(); - process_wp_context( $tags, $context ); + process_wp_context_tag( $tags, $context ); $this->assertSame( array( @@ -52,7 +52,7 @@ public function test_directive_resets_context_correctly_upon_closing_wp_context_ $tags = new WP_HTML_Tag_Processor( $markup ); $tags->next_tag( array( 'tag_closers' => 'visit' ) ); - process_wp_context( $tags, $context ); + process_wp_context_tag( $tags, $context ); $this->assertSame( array( 'my-key' => 'original-value' ), @@ -61,6 +61,8 @@ public function test_directive_resets_context_correctly_upon_closing_wp_context_ } public function test_directive_merges_context_correctly_upon_wp_context_attribute_on_opening_tag() { + $this->markTestSkipped( 'Need to implement the wp-context attribute directive processor first.' ); + $context = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ), @@ -72,7 +74,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( $tags, $context ); + process_wp_context_tag( $tags, $context ); $this->assertSame( array( diff --git a/src/directives/wp-bind.php b/src/directives/attributes/wp-bind.php similarity index 70% rename from src/directives/wp-bind.php rename to src/directives/attributes/wp-bind.php index 4fce3690..628e7110 100644 --- a/src/directives/wp-bind.php +++ b/src/directives/attributes/wp-bind.php @@ -1,20 +1,12 @@ is_tag_closer() ) { return; } - /** - * A `wp-bind` *tag* doesn't really make sense. - * What would be the point of e.g. `? - */ - if ( 'WP-BIND' === $tags->get_tag() ) { - return; - } - $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-bind:' ); foreach ( $prefixed_attributes as $attr ) { diff --git a/src/directives/wp-class.php b/src/directives/attributes/wp-class.php similarity index 73% rename from src/directives/wp-class.php rename to src/directives/attributes/wp-class.php index b6af6505..0e27d3eb 100644 --- a/src/directives/wp-class.php +++ b/src/directives/attributes/wp-class.php @@ -1,20 +1,12 @@ is_tag_closer() ) { return; } - /** - * A `wp-class` *tag* doesn't really make sense. - * What would be the point of e.g. `? - */ - if ( 'WP-CLASS' === $tags->get_tag() ) { - return; - } - $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-class:' ); foreach ( $prefixed_attributes as $attr ) { diff --git a/src/directives/wp-style.php b/src/directives/attributes/wp-style.php similarity index 78% rename from src/directives/wp-style.php rename to src/directives/attributes/wp-style.php index 6441d0a6..2d227029 100644 --- a/src/directives/wp-style.php +++ b/src/directives/attributes/wp-style.php @@ -1,20 +1,12 @@ is_tag_closer() ) { return; } - /** - * A `wp-style` *tag* doesn't really make sense. - * What would be the point of e.g. `? - */ - if ( 'WP-STYLE' === $tags->get_tag() ) { - return; - } - $prefixed_attributes = $tags->get_attribute_names_with_prefix( 'wp-style:' ); foreach ( $prefixed_attributes as $attr ) { diff --git a/src/directives/tags/wp-context.php b/src/directives/tags/wp-context.php new file mode 100644 index 00000000..bf2b2a30 --- /dev/null +++ b/src/directives/tags/wp-context.php @@ -0,0 +1,19 @@ +is_tag_closer() ) { + $context->rewind_context(); + return; + } + + $value = $tags->get_attribute( 'data' ); + if ( null === $value ) { + // No wp-context directive. + return; + } + + $new_context = json_decode( $value, true ); + // TODO: Error handling. + + $context->set_context( $new_context ); +} diff --git a/src/directives/wp-context.php b/src/directives/wp-context.php deleted file mode 100644 index cd6e4ca0..00000000 --- a/src/directives/wp-context.php +++ /dev/null @@ -1,24 +0,0 @@ -get_tag() ) { - if ( $tags->is_tag_closer() ) { - $context->rewind_context(); - return; - } - $value = $tags->get_attribute( 'data' ); - } else { - // TODO: Implement rewinding context upon matching closing tag. - $value = $tags->get_attribute( 'wp-context' ); - } - - if ( null === $value ) { - // No wp-context directive. - return; - } - - $new_context = json_decode( $value, true ); - // TODO: Error handling. - - $context->set_context( $new_context ); -} diff --git a/wp-directives.php b/wp-directives.php index d4d034fe..a72ce368 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -37,12 +37,13 @@ function () { require_once __DIR__ . '/../gutenberg/lib/experimental/html/index.php'; -require_once __DIR__ . '/src/directives/wp-context.php'; -require_once __DIR__ . '/src/directives/wp-context.php'; -require_once __DIR__ . '/src/directives/wp-bind.php'; -require_once __DIR__ . '/src/directives/wp-class.php'; require_once __DIR__ . '/src/directives/class-wp-directive-context.php'; +require_once __DIR__ . '/src/directives/attributes/wp-bind.php'; +require_once __DIR__ . '/src/directives/attributes/wp-class.php'; +require_once __DIR__ . '/src/directives/attributes/wp-style.php'; +require_once __DIR__ . '/src/directives/tags/wp-context.php'; + function wp_directives_loader() { // Load the Admin page. require_once plugin_dir_path( __FILE__ ) . '/src/admin/admin-page.php'; @@ -207,11 +208,15 @@ function bhe_inner_blocks( $parsed_block, $source_block, $parent_block ) { function wp_process_directives( $block_content ) { // TODO: Add some directive/components registration mechanism. - $directives = array( - 'wp-context' => 'process_wp_context', - 'wp-bind' => 'process_wp_bind', - 'wp-class' => 'process_wp_class', - 'wp-style' => 'process_wp_style', + $tag_directives = array( + 'wp-context' => 'process_wp_context_tag', + ); + + $attribute_directives = array( + // 'wp-context' => 'process_wp_context_attribute', // TODO + 'wp-bind' => 'process_wp_bind', + 'wp-class' => 'process_wp_class', + 'wp-style' => 'process_wp_style', ); $tags = new WP_HTML_Tag_Processor( $block_content ); @@ -219,17 +224,18 @@ function wp_process_directives( $block_content ) { $context = new WP_Directive_Context; while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { $tag_name = strtolower( $tags->get_tag() ); - if ( array_key_exists( $tag_name, $directives ) ) { - call_user_func( $directives[ $tag_name ], $tags, $context ); + 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). - foreach ( $directives as $directive => $directive_processor ) { - $attributes = $tags->get_attribute_names_with_prefix( $directive ); - if ( empty( $attributes ) ) { + $attributes = $tags->get_attribute_names_with_prefix( 'wp-' ); + + foreach ( $attributes as $attribute ) { + if ( ! array_key_exists( $attribute, $attribute_directives ) ) { continue; } - call_user_func( $directive_processor, $tags, $context ); + call_user_func( $attribute_directives[ $attribute ], $tags, $context ); } } }