/** * Move supported document head elements into platform document object, return all * unsupported tags in a string. * * @param string $input * @param string $location * @param int $priority * @return string */ public function parseAssetsFunc($input, $location = 'head', $priority = 0) { $doc = new \DOMDocument(); $doc->loadHTML('<html><head>' . $input . '</head><body></body></html>'); $raw = []; /** @var \DomElement $element */ foreach ($doc->getElementsByTagName('head')->item(0)->childNodes as $element) { $result = ['tag' => $element->tagName, 'content' => $element->textContent]; foreach ($element->attributes as $attribute) { $result[$attribute->name] = $attribute->value; } $success = Document::addHeaderTag($result, $location, $priority); if (!$success) { $raw[] = $doc->saveHTML($element); } } return implode("\n", $raw); }
/** * Move supported document head elements into platform document object, return all * unsupported tags in a string. * * @param string $input * @param string $location * @param int $priority * @return string */ public function parseAssetsFunc($input, $location = 'head', $priority = 0) { if ($location == 'head') { $scope = 'head'; } else { $scope = 'body'; } $html = "<html><{$scope}>{$input}</{$scope}></html>"; $internal = libxml_use_internal_errors(true); $doc = new \DOMDocument(); $doc->loadHTML($html); foreach (libxml_get_errors() as $error) { $this->dealXmlError($error, $html); } libxml_clear_errors(); libxml_use_internal_errors($internal); $raw = []; /** @var \DomElement $element */ foreach ($doc->getElementsByTagName($scope)->item(0)->childNodes as $element) { if (empty($element->tagName)) { continue; } $result = ['tag' => $element->tagName, 'content' => $element->textContent]; foreach ($element->attributes as $attribute) { $result[$attribute->name] = $attribute->value; } $success = Document::addHeaderTag($result, $location, (int) $priority); if (!$success) { $raw[] = $doc->saveHTML($element); } } return implode("\n", $raw); }