Creates new document fragment.

@param $source

@return DOMDocumentWrapper | bool
164493
protected function documentFragmentCreate($source, $charset = null)
    {
        $fake              = new DOMDocumentWrapper();
        $fake->contentType = $this->contentType;
        $fake->isXML       = $this->isXML;
        $fake->isHTML      = $this->isHTML;
        $fake->isXHTML     = $this->isXHTML;
        $fake->root        = $fake->document;
        if (!$charset) {
            $charset = $this->charset;
        }
        //	$fake->documentCreate($this->charset);
        if ($source instanceof DOMNODE && !($source instanceof DOMNODELIST)) {
            $source = array($source);
        }
        if (is_array($source) || $source instanceof DOMNODELIST) {
            // dom nodes
            // load fake document
            if (!$this->documentFragmentLoadMarkup($fake, $charset)) {
                return false;
            }
            $nodes = $fake->import($source);
            foreach ($nodes as $node)
                $fake->root->appendChild($node);
        } else {
            // string markup
            $this->documentFragmentLoadMarkup($fake, $charset, $source);
        }

        return $fake;
    }
885379
public function before($source)
    {
        foreach ($this as $node) {
            if ($node instanceof Element) {
                $node->before($source);
            }
        }

        return $this;
    }
433694
public static function fromPattern($source) {
        $source = preg_replace('/([\[\]\(\)\|]|\.\.\.)/', ' $1 ', $source);
        $source = preg_split('/\s+|(\S*<.*?'.'>)/', $source, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        return new static($source, DocoptFormatError::class);
    }
496121
protected function getCustomTemplateContent(DOMNode $node)
    {
        $innerDoc = new DOMDocument();

        $rootNode = $innerDoc->createElementNS('http://docbook.org/ns/docbook', 'section');
        $innerDoc->appendChild($rootNode);

        /** @var \DOMNode $child */
        foreach ($node->childNodes as $child) {
            $newNode = $innerDoc->importNode($child, true);
            if ($newNode === false) {
                $this->logger->warning(
                    "Failed to import Custom Style content of node '{$child->getNodePath()}'"
                );
            }

            $rootNode->appendChild($newNode);
        }

        return trim($this->richTextConverter->convert($innerDoc)->saveHTML());
    }
695479
public function parse(Source $source, array $options = []): DocumentNode
    {
        $this->lexer = $this->createLexer($source, $options);

        return $this->lexDocument();
    }
1144048
public static function getDOMDocument($source) 
    {
        if ($source instanceof DOMDOCUMENT) {
            return $source;
        }
        $source = self::getDocumentID($source);
        return $source
        ? self::$documents[$id]['document']
        : null;
    }
113682
public function compile(string $source): callable
    {
        $name = $this->namePrefix . md5(self::VERSION . $source);

        if (isset($this->compiledTemplates[$name])) {
            goto returner;
        } elseif (class_exists($name) === true) {
            goto instantiator;
        }

        if ($this->loadCache($name . '.php') === false) {
            $code = $this->compiler->compile($source);
            $formattedCode = sprintf(self::$layout, $name, $code);
            $this->saveCache($name . '.php', $formattedCode);
            eval('?>' . $formattedCode);
        }

instantiator:
        $this->compiledTemplates[$name] = new $name($this->runtime, $this->dataFactory);
returner:
        return $this->compiledTemplates[$name];
    }
1115418
public function generate($document)
    {
        $lexer = new Lexer();
        $tokenStream = $lexer->tokenize($document);
        $tokenParser = new TokenParser();
        $nodeStream = $tokenParser->parse($tokenStream);
        $nodeParser = new NodeParser();
        $this->classBuilder = $nodeParser->parse($nodeStream);
        $compiler = new Compiler($this->options);
        $this->streamWriter = $compiler->compile($this->classBuilder);

        return $this;
    }
1130946
private function createDocument(?string $version = null, ?string $encoding = null): \DOMDocument
    {
        $doc = new \DOMDocument($version ?: $this->defaultVersion, $encoding ?: $this->defaultEncoding);
        $doc->formatOutput = true;

        return $doc;
    }
410653
public function getCompiledFile($source)
    {
        if (strpos($source, $root = $this->alias->get('@root')) === 0) {
            $compiled = '@data/sword' . substr($source, strlen($root));
        } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($source, $_SERVER['DOCUMENT_ROOT']) === 0) {
            $compiled = '@data/sword/' . substr($source, strlen($_SERVER['DOCUMENT_ROOT']));
        } else {
            $compiled = "@data/sword/$source";
            if (DIRECTORY_SEPARATOR === '\\') {
                $compiled = str_replace(':', '_', $compiled);
            }
        }

        $compiled = $this->alias->resolve($compiled);

        if ($this->configure->debug || !file_exists($compiled) || filemtime($source) > filemtime($compiled)) {
            $this->swordCompiler->compileFile($source, $compiled);
        }

        return $compiled;
    }
1292850
public function cloneNode(\DOMDocument $domDocument)
    {
        return $domDocument->importNode($this->getDomNode()->cloneNode(false), false);
    }
582648
public static function createFromContent($content)
    {
        $domDocument = new \DOMDocument();

        @$domDocument->loadHTML($content);

        $xpathQueryBuilder = new XpathQueryBuilder();

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

        return $xpathQueryBuilder;
    }
670774
static function simpleXmlToStdClass(SimpleXMLElement &$source)
  {
    $res = new StdClass;
    $children = false;
    foreach ($source as $key => $value) {
      if (isset($res->$key)) {
        if (!is_array($res->$key)) {
          $res->$key = array($res->$key);
        }
        $ref = &$res->$key;
        $ref[] = CPS_Response::simpleXmlToStdClass($value);
      } else {
        $res->$key = CPS_Response::simpleXmlToStdClass($value);
      }
      $children = true;
    }
    if (!$children)
      return (string)$source;
    return $res;
  }
514130
protected function createImageSource(Document $document)
    {
        $src = $this->getAttribute('src');

        return is_string($src) ? $this->createSource($document) : $src;
    }
1230610
public function loadHTML($source, $options = 0)
    {
        // Enables libxml errors handling
        $internalErrorsOptionValue = libxml_use_internal_errors();
        if ($internalErrorsOptionValue === false) {
            libxml_use_internal_errors(true);
        }

        $source = trim($source);

        $autoAddHtmlAndBodyTags = !defined('LIBXML_HTML_NOIMPLIED') || ($options & LIBXML_HTML_NOIMPLIED) === 0;
        $autoAddDoctype = !defined('LIBXML_HTML_NODEFDTD') || ($options & LIBXML_HTML_NODEFDTD) === 0;

        $allowDuplicateIDs = ($options & self::ALLOW_DUPLICATE_IDS) !== 0;

        // Add body tag if missing
        if ($autoAddHtmlAndBodyTags && $source !== '' && preg_match('/\<!DOCTYPE.*?\>/', $source) === 0 && preg_match('/\<html.*?\>/', $source) === 0 && preg_match('/\<body.*?\>/', $source) === 0 && preg_match('/\<head.*?\>/', $source) === 0) {
            $source = '<body>' . $source . '</body>';
        }

        // Add DOCTYPE if missing
        if ($autoAddDoctype && strtoupper(substr($source, 0, 9)) !== '<!DOCTYPE') {
            $source = "<!DOCTYPE html>\n" . $source;
        }

        // Adds temporary head tag
        $charsetTag = '<meta data-html5-dom-document-internal-attribute="charset-meta" http-equiv="content-type" content="text/html; charset=utf-8" />';
        $matches = [];
        preg_match('/\<head.*?\>/', $source, $matches);
        $removeHeadTag = false;
        $removeHtmlTag = false;
        if (isset($matches[0])) { // has head tag
            $insertPosition = strpos($source, $matches[0]) + strlen($matches[0]);
            $source = substr($source, 0, $insertPosition) . $charsetTag . substr($source, $insertPosition);
        } else {
            $matches = [];
            preg_match('/\<html.*?\>/', $source, $matches);
            if (isset($matches[0])) { // has html tag
                $source = str_replace($matches[0], $matches[0] . '<head>' . $charsetTag . '</head>', $source);
            } else {
                $source = '<head>' . $charsetTag . '</head>' . $source;
                $removeHtmlTag = true;
            }
            $removeHeadTag = true;
        }

        // Preserve html entities
        $source = preg_replace('/&([a-zA-Z]*);/', 'html5-dom-document-internal-entity1-$1-end', $source);
        $source = preg_replace('/&#([0-9]*);/', 'html5-dom-document-internal-entity2-$1-end', $source);

        $result = parent::loadHTML('<?xml encoding="utf-8" ?>' . $source, $options);
        if ($internalErrorsOptionValue === false) {
            libxml_use_internal_errors(false);
        }
        if ($result === false) {
            return false;
        }
        $this->encoding = 'utf-8';
        foreach ($this->childNodes as $item) {
            if ($item->nodeType === XML_PI_NODE) {
                $this->removeChild($item);
                break;
            }
        }
        $metaTagElement = $this->getElementsByTagName('meta')->item(0);
        if ($metaTagElement !== null) {
            if ($metaTagElement->getAttribute('data-html5-dom-document-internal-attribute') === 'charset-meta') {
                $headElement = $metaTagElement->parentNode;
                $htmlElement = $headElement->parentNode;
                $metaTagElement->parentNode->removeChild($metaTagElement);
                if ($removeHeadTag && $headElement !== null && $headElement->parentNode !== null && ($headElement->firstChild === null || ($headElement->childNodes->length === 1 && $headElement->firstChild instanceof \DOMText))) {
                    $headElement->parentNode->removeChild($headElement);
                }
                if ($removeHtmlTag && $htmlElement !== null && $htmlElement->parentNode !== null && $htmlElement->firstChild === null) {
                    $htmlElement->parentNode->removeChild($htmlElement);
                }
            }
        }

        if (!$allowDuplicateIDs) {
            $matches = [];
            preg_match_all('/\sid[\s]*=[\s]*(["\'])(.*?)\1/', $source, $matches);
            if (!empty($matches[2]) && max(array_count_values($matches[2])) > 1) {
                $elementIDs = [];
                $walkChildren = function($element) use (&$walkChildren, &$elementIDs) {
                    foreach ($element->childNodes as $child) {
                        if ($child instanceof \DOMElement) {
                            if ($child->attributes->length > 0) { // Performance optimization
                                $id = $child->getAttribute('id');
                                if ($id !== '') {
                                    if (isset($elementIDs[$id])) {
                                        throw new \Exception('A DOM node with an ID value "' . $id . '" already exists!');
                                    } else {
                                        $elementIDs[$id] = true;
                                    }
                                }
                            }
                            $walkChildren($child);
                        }
                    }
                };
                $walkChildren($this);
            }
        }

        $this->loaded = true;
        return true;
    }
1177347
protected function processNoDataDocument($source): NoDataDocument
    {
        $document = new NoDataDocument();

        $this->hydrateObject($document, $source);

        return $document;
    }
1089628
public function setSource($source)
    {
        if (is_file($source)) {
            $info = getimagesize($source);

            $this->mime         = $info['mime'];
            $this->source       = $source;
            $this->sourceWidth  = $info[0];
            $this->sourceHeight = $info[1];
        }

        return $this;
    }