diff --git a/src/MicrodataDOMElement.php b/src/MicrodataDOMElement.php index 6ea8035..541cfe6 100644 --- a/src/MicrodataDOMElement.php +++ b/src/MicrodataDOMElement.php @@ -15,17 +15,7 @@ class MicrodataDOMElement extends \DOMElement $memory = [$this]; $pending = $this->getChildElementNodes(); - if ($this->hasAttribute('itemref')) { - $tokens = $this->tokenizeAttribute('itemref'); - - foreach ($tokens as $token) { - $references = $this->ownerDocument->xpath->query('//*[@id="'.$token.'"]'); - - if ($first = $references->item(0)) { - $pending[] = $first; - } - } - } + $pending = array_merge($pending, $this->getReferenceNodes()); while ($pending) { $current = array_pop($pending); @@ -210,4 +200,30 @@ class MicrodataDOMElement extends \DOMElement { return preg_split('/\s+/', trim($attribute)); } + + /** + * Finds the nodes that this node references through the document + * + * @see https://www.w3.org/TR/microdata/#dfn-item-properties 4th step + * + * @return array + */ + protected function getReferenceNodes(): array + { + $referenceNodes = []; + + if ($this->hasAttribute('itemref')) { + $tokens = $this->tokenizeAttribute('itemref'); + + foreach ($tokens as $token) { + $references = $this->ownerDocument->xpath->query('//*[@id="' . $token . '"]'); + + if ($first = $references->item(0)) { + $referenceNodes[] = $first; + } + } + } + + return $referenceNodes; + } }