Load the HTML as a DOMDocument directly

@param \DOMDocument $domDocument
434794
public function loadDomDocument(\DOMDocument $domDocument)
    {
        $this->_dom = $domDocument;
        foreach ($this->_getNodesForCssSelector('[style]') as $node) {
            $node->setAttribute(
                'inlinestyle-original-style',
                $node->getAttribute('style')
            );
        }
    }
705511
public function toXML( DOMDocument $dom )
    {
        $blockNode = $dom->createElement( 'block' );

        foreach ( $this->attributes as $attrName => $attrValue )
        {
            switch ( $attrName )
            {
                case 'id':
                    $blockNode->setAttribute( 'id', 'id_' . $attrValue );
                    break;

                case 'action':
                    $blockNode->setAttribute( 'action', $attrValue );
                    break;

                case 'items':
                    foreach ( $this->attributes['items'] as $item )
                    {
                        $itemNode = $item->toXML( $dom );
                        if ( $itemNode )
                        {
                            $blockNode->appendChild( $itemNode );
                        }
                    }
                    break;

                case 'rotation':
                    $node = $dom->createElement( $attrName );
                    $blockNode->appendChild( $node );

                    foreach ( $attrValue as $arrayItemKey => $arrayItemValue )
                    {
                        $tmp = $dom->createElement( $arrayItemKey );
                        $tmpValue = $dom->createTextNode( $arrayItemValue );
                        $tmp->appendChild( $tmpValue );
                        $node->appendChild( $tmp );
                    }
                    break;

                case 'custom_attributes':
                    $node = $dom->createElement( $attrName );
                    $blockNode->appendChild( $node );

                    foreach ( $attrValue as $arrayItemKey => $arrayItemValue )
                    {
                        $tmp = $dom->createElement( $arrayItemKey );
                        $tmpValue = $dom->createTextNode( $arrayItemValue );
                        $tmp->appendChild( $tmpValue );
                        $node->appendChild( $tmp );
                    }
                    break;

                default:
                    $node = $dom->createElement( $attrName );
                    $nodeValue = $dom->createTextNode( $attrValue );
                    $node->appendChild( $nodeValue );
                    $blockNode->appendChild( $node );
                    break;
            }
        }

        return $blockNode;
    }
174548
public function render(\DOMDocument $domDocument)
    {
        $stylesheetXml = $domDocument->createElement("stylesheet");
        if ($this->styles3d) {
            $stylesXml = $domDocument->createElement("frame3dstyles");
            $stylesheetXml->appendChild($stylesXml);
            foreach ($this->styles3d as $style3d) {
                $style3dXml = $style3d->render($domDocument);
                $stylesXml->appendChild($style3dXml);
            }
        }
        if ($this->mood) {
            $moodXml = $this->mood->render($domDocument);
            $stylesheetXml->appendChild($moodXml);
        }
        return $stylesheetXml;
    }
756591
private function loadBlock(\DOMXPath $xpath, \DOMNode $node, &$tags)
    {
        $result = $this->loadValues(
            $xpath,
            $node,
            [
                'name',
                'default-type',
                'minOccurs',
                'maxOccurs',
                'colspan',
                'cssClass',
                'disabledCondition',
                'visibleCondition',
            ]
        );

        $result['mandatory'] = $this->getValueFromXPath('@mandatory', $xpath, $node, false);
        $result['type'] = 'block';
        $result['tags'] = $this->loadTags($tags, $xpath, $node);
        $result['params'] = $this->loadParams('x:params/x:param', $xpath, $node);
        $result['meta'] = $this->loadMeta($xpath, $node);
        $result['types'] = $this->loadTypes($tags, $xpath, $node);

        return $result;
    }
707234
public function convert(DOMDocument $document)
    {
        $xpath = new DOMXPath($document);
        $xpathExpression = '//ns:pre';
        $ns = $document->documentElement->namespaceURI;
        $xpath->registerNamespace('ns', $ns);
        $elements = $xpath->query($xpathExpression);

        foreach ($elements as $element) {
            $element->textContent = str_replace(']]>', ']]]]><![CDATA[>', $element->textContent);
        }

        return $document;
    }
1251130
public function toDOM(\DOMDocument $doc): \DOMNode
    {
        $paragraph = $doc->createElement('p');

        $this->customAttributesToDOM($paragraph);
        $this->containerToDOM($doc, $paragraph);

        return $paragraph;
    }
654951
public function process(\DOMDocument $xml)
    {
        $xpath = new \DOMXPath($xml);
        $nodes = $xpath->query(
            '/project/file/class/docblock/tag[@name="method"]'
        );

        /** @var \DOMElement $node */
        foreach ($nodes as $node) {
            $class = $node->parentNode->parentNode;

            $method = new \DOMElement('method');
            $class->appendChild($method);

            // set basic information
            $method->setAttribute('final', 'false');
            $method->setAttribute('static', 'false');
            $method->setAttribute('visibility', 'public');
            $method->setAttribute('line', $node->getAttribute('line'));
            $method->appendChild(
                new \DOMElement('name', $node->getAttribute('method_name'))
            );

            // fill docblock
            $docblock = new \DOMElement('docblock');
            $method->appendChild($docblock);
            $docblock->appendChild(
                new \DOMElement('description', $node->getAttribute('description'))
            );
            $docblock->appendChild(new \DOMElement('long-description'));

            // for each argument; create an @param tag and an argument element
            /** @var \DOMElement $argument */
            foreach ($node->getElementsByTagName('argument') as $argument) {
                $param_tag = new \DOMElement('tag');
                $docblock->appendChild($param_tag);
                $param_tag->setAttribute('name', 'param');
                $param_tag->setAttribute(
                    'type',
                    $argument->getElementsByTagName('type')->item(0)->nodeValue
                );
                $param_tag->setAttribute(
                    'variable',
                    $argument->getElementsByTagName('name')->item(0)->nodeValue
                );
                $param_tag->setAttribute('line', $node->getAttribute('line'));

                $types = explode(
                    '|',
                    $argument->getElementsByTagName('type')->item(0)->nodeValue
                );

                foreach ($types as $type) {
                    $type_element = new \DOMElement('type', $type);
                    $param_tag->appendChild($type_element);
                }

                $argument_element = $argument->cloneNode(true);
                $method->appendChild($argument_element);
            }

            // add a tag 'magic'
            $magic_tag = new \DOMElement('tag');
            $docblock->appendChild($magic_tag);
            $magic_tag->setAttribute('name', 'magic');
            $magic_tag->setAttribute('line', $node->getAttribute('line'));

            // add a @return tag
            $return_tag = new \DOMElement('tag');
            $docblock->appendChild($return_tag);
            $return_tag->setAttribute('name', 'return');
            $return_tag->setAttribute('line', $node->getAttribute('line'));
            $return_tag->setAttribute('type', $node->getAttribute('type'));

            // add type sub elements to the param
            foreach (explode('|', $node->getAttribute('type')) as $type) {
                $type_element = new \DOMElement('type', $type);
                $return_tag->appendChild($type_element);
            }

            // remove the tag, it is unneeded
            $node->parentNode->removeChild($node);
            $docblock->appendChild($node);
        }

        return $xml;
    }
46412
private function convertDocument(\DOMDocument $xml)
    {
        return $this->loader->load($xml->saveXML(), [LoaderInterface::FROM_STRING => true]);
    }
798927
public function render(\DOMDocument $domDocument)
    {
        $this->loadFeatures($this->features);

        $scriptXml  = $domDocument->createElement("script");
        $scriptText = $this->buildScriptText();

        $scriptComment = $domDocument->createComment($scriptText);
        $scriptXml->appendChild($scriptComment);

        return $scriptXml;
    }
1311895
public function render(\DOMDocument $domDocument)
    {
        $domElement = $domDocument->createElement("custom_ui");

        $settings = $this->getSettings();
        foreach ($settings as $setting => $value) {
            if ($value === null) {
                continue;
            }

            $settingDomElement = $domDocument->createElement($setting);
            $settingDomElement->setAttribute("visible", ($value ? 1 : 0));
            $domElement->appendChild($settingDomElement);
        }

        return $domElement;
    }
801721
private function createRawDomDocument($html)
    {
        $domDocument = new \DOMDocument();
        $domDocument->strictErrorChecking = false;
        $domDocument->formatOutput = true;
        $libXmlState = \libxml_use_internal_errors(true);
        $domDocument->loadHTML($this->prepareHtmlForDomConversion($html));
        \libxml_clear_errors();
        \libxml_use_internal_errors($libXmlState);

        $this->domDocument = $domDocument;
    }
1289669
public function writeXml(DOMDocument $dom = null)
    {
        if (null === $dom) {
            $imp = new DOMImplementation();
            $dtd  = $imp->createDocumentType(
                static::DOCUMENT_TYPE_QUALIFIED_NAME,
                static::DOCUMENT_TYPE_PUBLIC_ID,
                static::DOCUMENT_TYPE_SYSTEM_ID
            );
            $dom = $imp->createDocument('', '', $dtd);
            $dom->encoding = 'UTF-8';
            $dom->xmlVersion = '1.0';
            $dom->formatOutput = true;
        }

        $descriptor = $dom->createElement('workflow');

        $metaAttributes = $this->getMetaAttributes();
        $metaElementBase = $dom->createElement('meta');
        foreach ($metaAttributes as $metaAttributeName => $metaAttributeValue) {
            $metaAttributeNameEncode = XmlUtil::encode($metaAttributeName);
            $metaAttributeValueEnEncode = XmlUtil::encode($metaAttributeValue);

            $metaElement = clone $metaElementBase;
            $metaElement->setAttribute('name', $metaAttributeNameEncode);
            $metaValueElement = $dom->createTextNode($metaAttributeValueEnEncode);
            $metaElement->appendChild($metaValueElement);

            $descriptor->appendChild($metaElement);
        }

        $registers = $this->getRegisters();
        if ($registers->count() > 0) {
            $registersElement = $dom->createElement('registers');
            foreach ($registers as $register) {
                $registerElement = $register->writeXml($dom);
                $registersElement->appendChild($registerElement);
            }

            $descriptor->appendChild($registersElement);
        }


        $timerFunctions = $this->getTriggerFunctions();
        if (count($timerFunctions) > 0) {
            $timerFunctionsElement = $dom->createElement('trigger-functions');
            $timerFunctionElementBase = $dom->createElement('trigger-function');
            foreach ($timerFunctions as $timerFunctionId => $timerFunction) {
                $timerFunctionElement = clone $timerFunctionElementBase;
                $timerFunctionElement->setAttribute('id', $timerFunctionId);

                $functionElement = $timerFunction->writeXml($dom);

                $timerFunctionElement->appendChild($functionElement);

                $timerFunctionsElement->appendChild($timerFunctionElement);
            }
            $descriptor->appendChild($timerFunctionsElement);
        }


        $globalConditions = $this->getGlobalConditions();
        if (null !== $globalConditions) {
            $globalConditionsElement = $dom->createElement('global-conditions');
            $globalConditionElement = $globalConditions->writeXml($dom);
            $globalConditionsElement->appendChild($globalConditionElement);
            $descriptor->appendChild($globalConditionsElement);
        }


        $initialActionsElement = $dom->createElement('initial-actions');
        $initialActions = $this->getInitialActions();
        foreach ($initialActions as $initialAction) {
            $initialActionElement = $initialAction->writeXml($dom);
            $initialActionsElement->appendChild($initialActionElement);
        }
        $descriptor->appendChild($initialActionsElement);


        $globalActions = $this->getGlobalActions();
        if ($globalActions->count() > 0) {
            $globalActionsElement = $dom->createElement('global-actions');
            foreach ($globalActions as $globalAction) {
                $globalActionElement = $globalAction->writeXml($dom);

                $globalActionsElement->appendChild($globalActionElement);
            }

            $descriptor->appendChild($globalActionsElement);
        }

        $commonActions = $this->getCommonActions();
        if (count($commonActions) > 0) {
            $commonActionsElement = $dom->createElement('common-actions');
            foreach ($commonActions as $commonAction) {
                $commonActionElement = $commonAction->writeXml($dom);
                $commonActionsElement->appendChild($commonActionElement);
            }

            $descriptor->appendChild($commonActionsElement);
        }


        $stepsElement = $dom->createElement('steps');
        $steps = $this->getSteps();
        foreach ($steps as $step) {
            $stepElement = $step->writeXml($dom);
            $stepsElement->appendChild($stepElement);
        }

        $descriptor->appendChild($stepsElement);

        $splits = $this->getSplits();
        if ($splits->count() > 0) {
            $splitsElement = $dom->createElement('splits');
            foreach ($splits as $split) {
                $splitElement = $split->writeXml($dom);
                $splitsElement->appendChild($splitElement);
            }

            $descriptor->appendChild($splitsElement);
        }

        $joins = $this->getJoins();
        if ($joins->count() > 0) {
            $joinsElement = $dom->createElement('joins');
            foreach ($joins as $join) {
                $joinElement = $join->writeXml($dom);
                $joinsElement->appendChild($joinElement);
            }

            $descriptor->appendChild($joinsElement);
        }

        $dom->appendChild($descriptor);
        return $dom;
    }
1177338
public function parseDocument(\DOMDocument $document)
    {
        foreach ($this->registry[DocumentExtension::class] as $extension) {
            $doc = $extension->parseDocument($this, $document);
            if ($doc) {
                return $doc;
            }
        }

        return null;
    }
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);
    }
1180344
public function loadXMLString(string $xmlString): DOMDocument
    {
        $document = new DOMDocument();

        libxml_use_internal_errors(true);
        libxml_clear_errors();

        // Options:
        // - substitute entities
        // - disable network access
        // - relax parser limits for document size/complexity
        $success = $document->loadXML($xmlString, LIBXML_NOENT | LIBXML_NONET | LIBXML_PARSEHUGE);
        if (!$success) {
            throw new InvalidXmlException('$xmlString', libxml_get_errors());
        }

        return $document;
    }
411805
public function transform($xslDocument)
    {
        if ($xslDocument->domDocument) {
            $processor = new \XsltProcessor();
            $processor->importStylesheet($xslDocument->domDocument);
            $htmlDomDocument = new self;
            $htmlDomDocument->domDocument = $processor->transformToDoc($this->domDocument);

            if (!$htmlDomDocument->domDocument) {
                throw new Dom\Exception('The XSL transformation has failed.');
            }
        } else {
            throw new Dom\Exception('There is no XSL template to transform with.');
        }

        try {
            return $htmlDomDocument->toString(false);
        } catch (\Exception $exception) {
            $this->logger->error($exception->getMessage());
            throw new Dom\Exception("dom-transform-failed");
        }
    }
1255466
public function process(\DOMDocument $xml)
    {
        $ignoreQry = '//tag[@name=\'' . $this->tag . '\']';

        $xpath = new \DOMXPath($xml);
        $nodes = $xpath->query($ignoreQry);

        /** @var \DOMElement $node */
        foreach ($nodes as $node) {
            $remove = $node->parentNode->parentNode;

            // sometimes the parent node of the entity-to-be-removed is already
            // gone; for instance when a File docblock contains an @internal and
            // the underlying class also contains an @internal.
            // Because the File Docblock is found sooner, it is removed first.
            // Without the following check the application would fatal since
            // it cannot find, and then remove, this node from the parent.
            if (!isset($remove->parentNode)) {
                continue;
            }

            $remove->parentNode->removeChild($remove);
        }

        return $xml;
    }