Skip to content

Commit e8a73f3

Browse files
committed
fix handling multiple text nodes in one element
1 parent 8777292 commit e8a73f3

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Tokenizer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Tokenizer
2222
/** @var resource */
2323
private $handle;
2424
private int $depth = 0;
25-
private ?Text $textNode = null;
25+
private array $textNodes = [];
2626
private ?Element $current = null;
2727

2828
public function __construct(Parser $parser, string $encoding = null, int $flags = 0)
@@ -49,7 +49,7 @@ public function tokenize(): Generator
4949
{
5050
foreach ($this->parser->parse() as $data) {
5151
$this->current = null;
52-
$this->textNode = null;
52+
$this->textNodes = [];
5353

5454
if (!xml_parse($this->handle, $data, '' === $data)) {
5555
break;
@@ -60,9 +60,10 @@ public function tokenize(): Generator
6060
yield $this->current;
6161
}
6262

63-
if ($this->textNode) {
64-
$this->textNode->depth = $this->depth + 1;
65-
yield $this->textNode;
63+
if ($this->textNodes) {
64+
array_map(fn(Text $textNode) => $textNode->depth = $this->depth + 1, $this->textNodes);
65+
66+
yield from $this->textNodes;
6667
}
6768
}
6869
}
@@ -103,7 +104,7 @@ private function open($parser, string $name, array $attributes): void
103104
private function contents($parser, string $text)
104105
{
105106
if (trim($text)) {
106-
$this->textNode = new Text($text, ($this->flags & self::XML_USE_CDATA));
107+
$this->textNodes[] = new Text($text, ($this->flags & self::XML_USE_CDATA));
107108
}
108109
}
109110

0 commit comments

Comments
 (0)