From 751027ca44d0505750e0d576fda2ef90d633173a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 20 Dec 2022 16:32:44 +0100 Subject: [PATCH] Add basic wp-bind code --- phpunit/directives/wp-bind.php | 33 +++++++++++++++++++++++++++++++++ src/directives/wp-bind.php | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 phpunit/directives/wp-bind.php create mode 100644 src/directives/wp-bind.php diff --git a/phpunit/directives/wp-bind.php b/phpunit/directives/wp-bind.php new file mode 100644 index 00000000..719eae37 --- /dev/null +++ b/phpunit/directives/wp-bind.php @@ -0,0 +1,33 @@ +'; + $tags = new WP_HTML_Processor( $markup ); + $tags->next_tag(); + + $context_before = array( 'myblock' => array( 'imageSource' => './wordpress.png' ) ); + $context = $context_before; + process_wp_bind( $tags, $context ); + + $this->assertSame( + '', + $tags->get_updated_html() + ); + // $this->assertSame( './wordpress.png', $tags->get_attribute( 'src' ) ); // FIXME: Doesn't currently work. + $this->assertSame( $context_before, $context, 'wp-bind directive changed context' ); + } +} diff --git a/src/directives/wp-bind.php b/src/directives/wp-bind.php new file mode 100644 index 00000000..94d0a953 --- /dev/null +++ b/src/directives/wp-bind.php @@ -0,0 +1,19 @@ +get_attributes_by_prefix( 'wp-bind:' ); + + foreach( $prefixed_attributes as $name => $expr ) { + $attr_parts = explode( ':', $name ); + if ( count( $attr_parts ) < 2 ) { + continue; + } + $bound_attr = $attr_parts[1]; + + // TODO: Properly parse $value. + $value = get_from_context( $expr, $context ); + $tags->set_attribute( $bound_attr, $value ); + } +} \ No newline at end of file