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

Commit 751027c

Browse files
committed
Add basic wp-bind code
1 parent e4228c1 commit 751027c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

phpunit/directives/wp-bind.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* wp-bind directive test.
4+
*/
5+
6+
require_once __DIR__ . '/../../src/directives/wp-bind.php';
7+
8+
require_once __DIR__ . '/../../src/html/index.php';
9+
10+
/**
11+
* Tests for the wp-bind directive.
12+
*
13+
* @group directives
14+
* @covers process_wp_bind
15+
*/
16+
class Tests_Directives_WpBind extends WP_UnitTestCase {
17+
public function test_directive() {
18+
$markup = '<img wp-bind:src="context.myblock.imageSource" />';
19+
$tags = new WP_HTML_Processor( $markup );
20+
$tags->next_tag();
21+
22+
$context_before = array( 'myblock' => array( 'imageSource' => './wordpress.png' ) );
23+
$context = $context_before;
24+
process_wp_bind( $tags, $context );
25+
26+
$this->assertSame(
27+
'<img src="./wordpress.png" wp-bind:src="context.myblock.imageSource" />',
28+
$tags->get_updated_html()
29+
);
30+
// $this->assertSame( './wordpress.png', $tags->get_attribute( 'src' ) ); // FIXME: Doesn't currently work.
31+
$this->assertSame( $context_before, $context, 'wp-bind directive changed context' );
32+
}
33+
}

src/directives/wp-bind.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once __DIR__ . '/utils.php';
4+
5+
function process_wp_bind( &$tags, &$context ) {
6+
$prefixed_attributes = $tags->get_attributes_by_prefix( 'wp-bind:' );
7+
8+
foreach( $prefixed_attributes as $name => $expr ) {
9+
$attr_parts = explode( ':', $name );
10+
if ( count( $attr_parts ) < 2 ) {
11+
continue;
12+
}
13+
$bound_attr = $attr_parts[1];
14+
15+
// TODO: Properly parse $value.
16+
$value = get_from_context( $expr, $context );
17+
$tags->set_attribute( $bound_attr, $value );
18+
}
19+
}

0 commit comments

Comments
 (0)