Skip to content

Commit 002b84e

Browse files
committed
Add tokens to blocks
1 parent 431bb42 commit 002b84e

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/BlockParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function getIterator(): Generator
5858
namespace: $token->namespace,
5959
name: $token->name,
6060
attributes: $token->attributes,
61+
token: $token,
6162
);
6263
} elseif ($token instanceof BlockOpening && $this->stackDepth++ === 0) {
6364
$opening = $token;
@@ -77,6 +78,8 @@ public function getIterator(): Generator
7778
$openedAt,
7879
$token->startsAt - $openedAt,
7980
),
81+
opening: $opening,
82+
closing: $token,
8083
);
8184
}
8285
}

src/Blocks/Block.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Recoded\WordPressBlockParser\Blocks;
44

5+
use Recoded\WordPressBlockParser\Tokens\BlockClosing;
6+
use Recoded\WordPressBlockParser\Tokens\BlockOpening;
7+
58
final class Block
69
{
710
/**
@@ -11,13 +14,17 @@ final class Block
1114
* @param string $name
1215
* @param array<string, mixed> $attributes
1316
* @param string $content
17+
* @param \Recoded\WordPressBlockParser\Tokens\BlockOpening $opening
18+
* @param \Recoded\WordPressBlockParser\Tokens\BlockClosing $closing
1419
* @return void
1520
*/
1621
public function __construct(
1722
public readonly string $namespace,
1823
public readonly string $name,
1924
public readonly array $attributes,
2025
public readonly string $content,
26+
public readonly BlockOpening $opening,
27+
public readonly BlockClosing $closing,
2128
) {
2229
//
2330
}

src/Blocks/SelfClosingBlock.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Recoded\WordPressBlockParser\Blocks;
44

5+
use Recoded\WordPressBlockParser\Tokens\SelfClosingBlock as SelfClosingBlockToken;
6+
57
final class SelfClosingBlock
68
{
79
/**
@@ -10,12 +12,14 @@ final class SelfClosingBlock
1012
* @param string $namespace
1113
* @param string $name
1214
* @param array<string, mixed> $attributes
15+
* @param \Recoded\WordPressBlockParser\Tokens\SelfClosingBlock $token
1316
* @return void
1417
*/
1518
public function __construct(
1619
public readonly string $namespace,
1720
public readonly string $name,
1821
public readonly array $attributes,
22+
public readonly SelfClosingBlockToken $token,
1923
) {
2024
//
2125
}

tests/BlockParserTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Recoded\WordPressBlockParser\BlockParser;
77
use Recoded\WordPressBlockParser\Blocks\Block;
88
use Recoded\WordPressBlockParser\Blocks\SelfClosingBlock;
9+
use Recoded\WordPressBlockParser\Tokens\BlockClosing;
10+
use Recoded\WordPressBlockParser\Tokens\BlockOpening;
11+
use Recoded\WordPressBlockParser\Tokens\SelfClosingBlock as SelfClosingBlockToken;
912

1013
final class BlockParserTest extends TestCase
1114
{
@@ -30,11 +33,31 @@ public function test_it_parses_blocks(): void
3033
Test
3134
<!-- /wp:paragraph -->
3235
',
36+
opening: new BlockOpening(
37+
namespace: 'core/',
38+
name: 'paragraph',
39+
attributes: [],
40+
startsAt: 0,
41+
length: 21,
42+
),
43+
closing: new BlockClosing(
44+
namespace: 'core/',
45+
name: 'paragraph',
46+
startsAt: 84,
47+
length: 22,
48+
),
3349
),
3450
new SelfClosingBlock(
3551
namespace: 'core/',
3652
name: 'paragraph',
3753
attributes: [],
54+
token: new SelfClosingBlockToken(
55+
namespace: 'core/',
56+
name: 'paragraph',
57+
attributes: [],
58+
startsAt: 107,
59+
length: 22,
60+
),
3861
),
3962
], iterator_to_array($parser));
4063
}

0 commit comments

Comments
 (0)