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

Commit

Permalink
Add basic wp-bind code
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 20, 2022
1 parent e4228c1 commit 751027c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions phpunit/directives/wp-bind.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* wp-bind directive test.
*/

require_once __DIR__ . '/../../src/directives/wp-bind.php';

require_once __DIR__ . '/../../src/html/index.php';

/**
* Tests for the wp-bind directive.
*
* @group directives
* @covers process_wp_bind
*/
class Tests_Directives_WpBind extends WP_UnitTestCase {
public function test_directive() {
$markup = '<img wp-bind:src="context.myblock.imageSource" />';
$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(
'<img src="./wordpress.png" wp-bind:src="context.myblock.imageSource" />',
$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' );
}
}
19 changes: 19 additions & 0 deletions src/directives/wp-bind.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once __DIR__ . '/utils.php';

function process_wp_bind( &$tags, &$context ) {
$prefixed_attributes = $tags->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 );
}
}

0 comments on commit 751027c

Please sign in to comment.