DomIterator keys were off-by-one

This commit is contained in:
Matias Griese 2021-12-08 12:49:13 +02:00
parent 5552ea2d70
commit 9df7b35c65
2 changed files with 5 additions and 5 deletions

View File

@ -156,7 +156,7 @@ final class DOMLettersIterator implements Iterator
{
$this->current = $this->start;
$this->offset = -1;
$this->key = -1;
$this->key = 0;
$this->letters = [];
$this->next();

View File

@ -28,7 +28,7 @@ final class DOMWordsIterator implements Iterator
private $offset = -1;
/** @var int|null */
private $key;
/** @var array<int,string>|null */
/** @var array<int,array<int,int|string>>|null */
private $words;
/**
@ -94,7 +94,7 @@ final class DOMWordsIterator implements Iterator
if ($this->current->nodeType === XML_TEXT_NODE || $this->current->nodeType === XML_CDATA_SECTION_NODE) {
if ($this->offset === -1) {
$this->words = preg_split("/[\n\r\t ]+/u", $this->current->textContent, -1, PREG_SPLIT_NO_EMPTY) ?: [];
$this->words = preg_split("/[\n\r\t ]+/", $this->current->textContent, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_OFFSET_CAPTURE) ?: [];
}
$this->offset++;
@ -133,7 +133,7 @@ final class DOMWordsIterator implements Iterator
*/
public function current(): ?string
{
return $this->words ? $this->words[$this->offset] : null;
return $this->words ? (string)$this->words[$this->offset][0] : null;
}
/**
@ -150,7 +150,7 @@ final class DOMWordsIterator implements Iterator
{
$this->current = $this->start;
$this->offset = -1;
$this->key = -1;
$this->key = 0;
$this->words = [];
$this->next();