Collect some metadata about $dom and $content
@param \DOMDocument $dom
@param string $source
@return mixed
327154
protected function collectMetadata(\DOMDocument $dom, $source)
    {
        $metadata = array();

        $metadata['doctype'] = !!$dom->doctype;
        $metadata['fragment'] = stripos(rtrim($source), '<!DOCTYPE html>') !== 0;

        return $metadata;
    }
907282
protected function getSelectedNode(\DOMDocument $dom, string $selector): \DOMNode
    {
        $xPath = new \DOMXPath($dom);

        return $xPath->query((new CssSelectorConverter())->toXPath($selector))->item(0);
    }
796046
public static function to_string( \DOMDocument $dom ) {
		$html5 = new HTML5();
		preg_match( '/<body>(.*)<\/body>/s', $html5->saveHTML( $dom ), $match );

		return $match[1];
	}
439336
protected final function loadHTML(DOMDocument $dom, string $html)
   {
       try {
           $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
       } catch (Throwable $e) {
           throw new RuntimeException("Can't load HTML: " . $e->getMessage());
       }
   }
207352
public function getContent(Smarty_Template_Source $source)
    {
        if ($source->exists) {
            return file_get_contents($source->filepath);
        }
        throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') .
                                  " {$source->type} '{$source->name}'");
    }
592936
public function retrieveBody($dom){
		if( preg_match('/<body>(.*)<\/body>/s', $this->html5->saveHTML($dom), $match) ){
			return $match[1];
		}else{
			return null;
		}
	}
1282348
private function getNodeContent(\DOMNode $node)
    {
        $type = $node->attributes->getNamedItem('type');
        if ($type && strtolower($type->nodeValue) == 'xhtml') {
            $result = '';
            foreach ($node->childNodes as $childNode) {
                $result .= $node->ownerDocument->save($childNode);
            }
            return $result;
        } else {
            return $node->nodeValue;
        }
    }
1154951
public function canHandle(\DOMDocument $dom)
    {
        $result = false;
        $rootNodes = $dom->getElementsByTagName('rss');
        if ($rootNodes->length == 1) {
            $version = $rootNodes->item(0)->attributes->getNamedItem('version');
            $result = $version && fnmatch($this->getVersionPattern(), $version->nodeValue);
        }
        return $result;
    }
582648
public static function createFromContent($content)
    {
        $domDocument = new \DOMDocument();

        @$domDocument->loadHTML($content);

        $xpathQueryBuilder = new XpathQueryBuilder();

        $xpathQueryBuilder->setDomXpath(new \DOMXPath($domDocument));

        return $xpathQueryBuilder;
    }
740908
public function copyNodes($target, DOMNodeList $sourceNodes)
    {
        if ($sourceNodes) {
            // If $target is string, try to parse as a XPath expression.
            $targetNode = is_string($target) ? $this->getNode($target) : $target;
            if ($targetNode) {
                // Import nodes into document and then add them to the target node.
                foreach ($sourceNodes as $nodeToImport) {
                    try {
                        $nodeToAppend = $this->domDocument->importNode($nodeToImport, true);
                        $targetNode->appendChild($nodeToAppend);
                    } catch (\Exception $exception) {
                        $this->logger->error($exception->getMessage());
                        throw new Dom\Exception($exception);
                    }
                }

                return $targetNode;
            } else {
                throw new Dom\Exception("dom-missing-target-node");
            }
        } else {
            return null;
        }
    }
26159
private function protectTags(HtmlDomParser $dom): HtmlDomParser
    {
        // init
        $counter = 0;

        foreach ($dom->find('script, style') as $element) {

            // skip external links
            if ($element->tag === 'script' || $element->tag === 'style') {
                $attributes = $element->getAllAttributes();
                if (isset($attributes['src'])) {
                    continue;
                }
            }

            $this->protectedChildNodes[$counter] = $element->text();
            $element->getNode()->nodeValue = '<' . $this->protectedChildNodesHelper . ' data-' . $this->protectedChildNodesHelper . '="' . $counter . '"></' . $this->protectedChildNodesHelper . '>';

            ++$counter;
        }

        foreach ($dom->find('code, nocompress') as $element) {
            if ($element->isRemoved()) {
                continue;
            }

            $this->protectedChildNodes[$counter] = $element->parentNode()->innerHtml();
            $element->getNode()->parentNode->nodeValue = '<' . $this->protectedChildNodesHelper . ' data-' . $this->protectedChildNodesHelper . '="' . $counter . '"></' . $this->protectedChildNodesHelper . '>';

            ++$counter;
        }

        foreach ($dom->find('//comment()') as $element) {
            $text = $element->text();

            // skip normal comments
            if (!$this->isConditionalComment($text)) {
                continue;
            }

            $this->protectedChildNodes[$counter] = '<!--' . $text . '-->';

            /* @var $node \DOMComment */
            $node = $element->getNode();
            $child = new \DOMText('<' . $this->protectedChildNodesHelper . ' data-' . $this->protectedChildNodesHelper . '="' . $counter . '"></' . $this->protectedChildNodesHelper . '>');
            /** @noinspection UnusedFunctionResultInspection */
            $element->getNode()->parentNode->replaceChild($child, $node);

            ++$counter;
        }

        return $dom;
    }
238974
public static function getMetas(\DOMDocument $html)
    {
        $metas = [];

        foreach ($html->getElementsByTagName('meta') as $meta) {
            $name = trim(strtolower($meta->getAttribute('property') ?: $meta->getAttribute('name')));
            $value = $meta->getAttribute('content') ?: $meta->getAttribute('value');

            $metas[] = [$name, $value, $meta];
        }

        return $metas;
    }
1318224
public function parse($source)
    {
        $result = false;
        foreach ($this->parsers as $parser) {
            try {
                $result = $parser->parse($source);
                if ($result instanceof \DOMDocument) {
                    break;
                }
            } catch (\Exception $e) {
                $this->logException($e);
            }
        }
        if (!($result instanceof \DOMDocument)) {
            throw new \DOMException('Unable to parse source');
        }
        return $result;
    }
438408
public static function getJSNodes(DOMDocument $dom)
	{
		$regexp = '/^(?:data-s9e-livepreview-postprocess$|on)/i';
		$nodes  = array_merge(
			self::getAttributesByRegexp($dom, $regexp),
			self::getElementsByRegexp($dom, '/^script$/i')
		);

		return $nodes;
	}
237310
public function parseDom(\DOMDocument $dom)
    {
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
        $items = [];

        // Find and process all JSON-LD documents
        $xpath      = new \DOMXPath($dom);
        $jsonLDDocs = $xpath->query('//*[local-name(.) = "script"][@type = "application/ld+json"]');
        $this->logger->debug('Processing '.$jsonLDDocs->length.' JSON-LD documents');

        // Run through all JSON-LD documents
        foreach ($jsonLDDocs as $jsonLDDoc) {
            $jsonLDDocSource = preg_replace(self::JSON_COMMENT_PATTERN, '', $jsonLDDoc->textContent);
            $i               = $this->parseDocument($jsonLDDocSource);
            $items           = array_merge($items, $i);
        }

        return new ParsingResult(self::FORMAT, $items);
    }
308847
public function saveHTML( $dom ) {
		if ( $this->parser instanceof \DOMDocument ) {
			libxml_use_internal_errors( true );
			$html = $dom->saveHTML();
			$this->errors = libxml_get_errors();
			libxml_clear_errors();
		} else {
			$html = $this->parser->saveHTML( $dom );
		}

		$html = \Pressbooks\Sanitize\strip_container_tags( $html );

		$replace_pairs = [
			'<div><!-- pb_fixme -->' => '',
			'<!-- pb_fixme --></div>' => '',
		];
		$html = strtr( $html, $replace_pairs );

		return $html;
	}
910134
function get(dom) {
      return dom.tagName && __TAG_IMPL[getAttribute(dom, IS_DIRECTIVE) ||
        getAttribute(dom, IS_DIRECTIVE) || dom.tagName.toLowerCase()]
    }