diff --git a/src/MicrodataDOMElement.php b/src/MicrodataDOMElement.php index a6a1c99..e7ba087 100644 --- a/src/MicrodataDOMElement.php +++ b/src/MicrodataDOMElement.php @@ -12,15 +12,7 @@ class MicrodataDOMElement extends \DOMElement $memory[] = $this; - if ($this->hasChildNodes()) { - $childNodes = iterator_to_array($this->childNodes); - - $childNodes = array_filter($childNodes, function ($node) { - return $node instanceof \DOMElement; - }); // Get only DOMElements - - $pending = array_merge($pending, $childNodes); - } + $pending = array_merge($pending, $this->getChildElementNodes()); if ($this->hasAttribute('itemref')) { $tokens = preg_split('/\s+/', $this->getAttribute('itemref')); @@ -33,32 +25,16 @@ class MicrodataDOMElement extends \DOMElement while ($pending) { $current = array_pop($pending); - $error = false; - foreach ($memory as $memory_item) { if ($current->isSameNode($memory_item)) { - // There is MicrodataError - $error = true; - break; + continue 2; // Skip next part and continue while loop if memory contains $current } } - if ($error) { - continue; - } - $memory[] = $current; if (! $current->hasAttribute('itemscope')) { - if ($current->hasChildNodes()) { - $childNodes = iterator_to_array($current->childNodes); - - $childNodes = array_filter($childNodes, function ($node) { - return $node instanceof \DOMElement; - }); - - $pending = array_merge($pending, $childNodes); - } + $pending = array_merge($pending, $current->getChildElementNodes()); } if ($current->hasAttribute('itemprop') && /* hasPropertyNames */ $current->getPropertyNames()) { @@ -163,4 +139,17 @@ class MicrodataDOMElement extends \DOMElement { return preg_match("/^\w+:/", trim($uri)); } + + protected function getChildElementNodes() + { + $childNodes = []; + + foreach ($this->childNodes as $childNode) { + if ($childNode->nodeType == XML_ELEMENT_NODE) { + $childNodes[] = $childNode; + } + } + + return $childNodes; + } } diff --git a/src/MicrodataParser.php b/src/MicrodataParser.php index 074a34e..f265b40 100644 --- a/src/MicrodataParser.php +++ b/src/MicrodataParser.php @@ -18,14 +18,12 @@ class MicrodataParser { $result = new \stdClass; - $items = []; + $result->items = []; foreach ($this->topLevelItems as $topLevelItem) { - $items[] = $this->getObject($topLevelItem); + $result->items[] = $this->getObject($topLevelItem); } - $result->items = $items; - return $result; } @@ -53,6 +51,7 @@ class MicrodataParser foreach ($memory as $memory_item) { if ($element->isSameNode($memory_item)) { $value = 'ERROR'; + break; } }