Fixes DOM document tree:
- adds package="default" to 'testsuite' elements without
package attribute
- removes outer 'testsuite' container(s)

@param DOMDocument $document
503630
protected function fixDocument(DOMDocument $document)
    {
        $rootElement = $document->firstChild;

        $xp = new DOMXPath($document);

        $nodes = $xp->query("/testsuites/testsuite/testsuite/testsuite");

        if ($nodes->length === 0) {
            $nodes = $xp->query("/testsuites/testsuite");

            foreach ($nodes as $node) {
                $children = $xp->query("./testsuite", $node);

                if ($children->length) {
                    $this->handleChildren($rootElement, $children);
                    $rootElement->removeChild($node);
                }
            }
        } else {
            $nodes = $xp->query("/testsuites/testsuite/testsuite");

            foreach ($nodes as $node) {
                $children = $xp->query("./testsuite", $node);

                if ($children->length) {
                    $this->handleChildren($rootElement, $children);
                    $rootElement->firstChild->removeChild($node);
                }
            }
        }
    }
709949
protected function normalizeNode(DOMNode $node)
	{
		if (!$node->parentNode)
		{
			// Ignore nodes that have been removed from the document
			return;
		}
		if ($node instanceof DOMElement)
		{
			$this->normalizeElement($node);
		}
		elseif ($node instanceof DOMAttr)
		{
			$this->normalizeAttribute($node);
		}
	}
698700
public function manipulate(\DOMDocument $document, LayoutInterface $layout): void
    {
        $host = '//' . $layout->getSource()->getHost();

        /** @var \DOMElement $anchor */
        foreach ($document->getElementsByTagName('a') as $anchor) {
            $this->replaceElementAttributeWithAbsoluteUri($anchor, 'href', $host);
        }

        /** @var \DOMElement $anchor */
        foreach ($document->getElementsByTagName('link') as $anchor) {
            $this->replaceElementAttributeWithAbsoluteUri($anchor, 'href', $host);
        }

        /** @var \DOMElement $anchor */
        foreach ($document->getElementsByTagName('img') as $anchor) {
            $this->replaceElementAttributeWithAbsoluteUri($anchor, 'src', $host);
        }

        /** @var \DOMElement $anchor */
        foreach ($document->getElementsByTagName('script') as $anchor) {
            $this->replaceElementAttributeWithAbsoluteUri($anchor, 'src', $host);
        }

        $xPath = new \DOMXPath($document);
        /** @var \DOMComment[] $comments */
        $comments = $xPath->query('//comment()');
        foreach ($comments as $comment) {
            $this->replaceCommentWithAbsoluteUri($comment, $host);
        }
    }
520010
protected function containerToDOM(\DomDocument $doc, \DomNode $node) : void
    {
        foreach ($this->children as $child) {
            $node->appendChild($child->toDOM($doc));
        }
    }
352120
public function getXML(\DOMDocument $document)
    {
        $xml = $document->createElement('ns2:CreditCardData');
        if (!empty($this->cardNumber)) {
            $cardNumber              = $document->createElement('ns1:CardNumber');
            $cardNumber->textContent = $this->cardNumber;
            $xml->appendChild($cardNumber);
        }
        $expMonth = $document->createElement('ns1:ExpMonth');
        $expMonth->textContent = $this->expMonth;
        $expYear = $document->createElement('ns1:ExpYear');
        $expYear->textContent = $this->expYear;

        $xml->appendChild($expMonth);
        $xml->appendChild($expYear);

        return $xml;
    }
1163126
protected function addToDocument($document)
    {
        parent::addToDocument($document);

        $this->getRangeKey()->getPropertyMetadata()->setValue(
            $document,
            $this->getRangeValue()
        );
    }
1267675
protected function encodeLiteral(DOMDocument $document)
    {
        $xpath = new DOMXPath($document);

        $xpathExpression = '//literal[not(@class="html")]';

        $elements = $xpath->query($xpathExpression);

        foreach ($elements as $element) {
            $element->textContent = str_replace(']]>', ']]]]><![CDATA[>', $element->textContent);
        }
    }
1220932
public function format(Document $document)
    {        
        $this->preFormat($document);
        foreach($this->getChildren() as $child)
        {
            $child->format($document);
        }
        $this->postFormat($document);
    }
583424
public function deregisterDocument($document)
    {
        $oid = $this->getObjectIdentifier($document);
        $this->assertDocumentExists($document);
        $nodeIdentifier = $this->documentNodeMap[$oid];
        $locale = $this->documentLocaleMap[$oid];

        unset($this->nodeMap[$nodeIdentifier]);
        unset($this->nodeDocumentMap[$this->getNodeLocaleKey($nodeIdentifier, $locale)]);
        unset($this->documentMap[$oid]);
        unset($this->documentNodeMap[$oid]);
        unset($this->documentLocaleMap[$oid]);
        unset($this->hydrationState[$oid]);
    }
535949
public function manipulate(\DOMDocument $document, LayoutInterface $layout): void
    {
        $head = $document->getElementsByTagName('head')->item(0);

        if($head->getElementsByTagName('base')->length > 0){
            $head->removeChild($head->getElementsByTagName('base')->item(0));
        }
    }
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;
    }
487743
private function addFacts( &$document )
	{
		foreach ( $this->facts as $formulaFactKey => $formulaFacts )
		{
			foreach ( $formulaFacts as $factIndex => $fact )
			{
				/**
				 * @var QName $conceptQName
				 */
				$conceptQName = $fact['concept'];
				$concept = "{$conceptQName->prefix}:{$conceptQName->localName}";

				$entry = array();
				$entry[] = "<$concept ";

				if ( isset( $this->sources[ $formulaFactKey ] ) )
				{
					$attributesIterator = AttributeNodeIterator::fromNodeTest( null, null, XPath2NodeIterator::Create( $this->sources[ $formulaFactKey ] ) );
					$attributesIterator->Reset();
					$attributesIterator->MoveNext();
					foreach ( $attributesIterator as $attribute )
					{
						// Ignore the standard ones
						if ( empty( $attribute->getNamespaceURI() ) )
						{
							switch ( $attribute->getLocalName() )
							{
								case "contextRef":
								case "unitRef":
								case "decimals":
								case "precision":
									continue 2;

								default:
									break;
							}
						}
						else if ( $attribute->getNamespaceURI() == \XBRL_Constants::$standardNamespaces[ STANDARD_PREFIX_SCHEMA_INSTANCE ] )
						{
							if ( $attribute->getLocalName() == "nil" ) continue 2;
						}

						// Otherwise add an attribute
						$name = $attribute->getPrefix()
							? "{$attribute->getPrefix()}:{$attribute->getLocalName()}"
							: $attribute->getLocalName();
						$entry[] = "$name=\"{$attribute->getValue()}\"";
					}
				}

				// // Get the node if it exists
				// if ( isset( $this->vars[ $formulaFactKey ][ $factIndex ] ) )
				// {
				// 	foreach ( $this->vars[ $formulaFactKey ][ $factIndex ] as /* @var DOMXPathNavigator $var */ $var )
				// 	{
				// 		// $var will be a node iterator if the variable is bind as a sequence
				// 		if ( ! $var instanceof DOMXPathNavigator ) continue;
				// 		if ( $var->getNamespaceURI() != $fact['concept']->namespaceURI || $var->getLocalName() != $fact['concept']->localName )
				// 		{
				// 			continue;
				// 		}
                //
				// 		if ( ! $var instanceof DOMXPathNavigator || ! $var->getHasAttributes() ) continue;
				// 		// Test 46210 V-01 fails because an ID attribute should NOT be added
				// 		// Test 47212 V-01 fails because an ID attribute SHOULD be added.  What is the criteria?
				// 		if ( true )
				// 		{
				// 			$attributesIterator = AttributeNodeIterator::fromNodeTest( null, null, XPath2NodeIterator::Create( $var ) );
				// 			$attributesIterator->Reset();
				// 			foreach ( $attributesIterator as $attribute )
				// 			{
				// 				// Ignore the standard ones
				// 				if ( empty( $attribute->getNamespaceURI() ) )
				// 				{
				// 					switch ( $attribute->getLocalName() )
				// 					{
				// 						case "contextRef":
				// 						case "unitRef":
				// 						case "decimals":
				// 						case "precision":
				// 							continue 2;
                //
				// 						default:
				// 							break;
				// 					}
				// 				}
				// 				else if ( $attribute->getNamespaceURI() == \XBRL_Constants::$standardNamespaces[ STANDARD_PREFIX_SCHEMA_INSTANCE ] )
				// 				{
				// 					if ( $attribute->getLocalName() == "nil" ) continue 2;
				// 				}
                //
				// 				// Otherwise add an attribute
				// 				$name = $attribute->getPrefix()
				// 					? "{$attribute->getPrefix()}:{$attribute->getLocalName()}"
				// 					: $attribute->getLocalName();
				// 				$entry[] = "$name=\"{$attribute->getValue()}\"";
				// 			}
				// 		}
				// 	}
				// }

				$entry[] = "contextRef=\"{$fact['contextRef']}\"";

				if ( isset( $fact['unitRef'] ) )
				{
					$entry[] = "unitRef=\"{$fact['unitRef']}\"";
				}
				if ( isset( $fact['decimals'] ) )
				{
					$entry[] = "decimals=\"{$fact['decimals']}\"";
				}
				if ( isset( $fact['precision'] ) )
				{
					$entry[] = "precision=\"{$fact['precision']}\"";
				}

				if ( is_null( $fact['value'] ) )
				{
					$entry[] = "xsi:nil=\"true\"";
					$entry[] = "/>";
				}
				else
				{
					$value = htmlspecialchars( $fact['value'], ENT_XML1 | ENT_COMPAT, 'UTF-8' );
					$entry[] = ">$value</$concept>";
				}

				$document[] = "  " . implode( " ", $entry );
			}
		}
	}
497555
public function convert(DOMDocument $document)
    {
        $this->processTag($document, 'ezembed', false);
        $this->processTag($document, 'ezembedinline', true);

        return $document;
    }
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;
    }
1320924
public function setDocument(Document $document): self
    {
        $this->_data = $document;

        $metadata = $this->_getMetadata($document);

        $this->setMetadata($metadata);

        return $this;
    }
1249907
protected function processTemplate(DOMDocument $document, DOMXPath $xpath, DOMElement $template)
    {
        $content = null;
        $templateName = $template->getAttribute('name');
        $templateType = $template->hasAttribute('type') ? $template->getAttribute('type') : 'tag';
        $parameters = [
            'name' => $templateName,
            'params' => $this->extractTemplateConfiguration($template, $xpath),
        ];

        $contentNodes = $xpath->query('./docbook:ezcontent', $template);
        $innerContent = '';
        foreach ($contentNodes as $contentNode) {
            $innerContent .= $this->getCustomTemplateContent($contentNode);
        }
        if (!empty($innerContent)) {
            $parameters['content'] = $innerContent;
        }

        if ($template->hasAttribute('ezxhtml:align')) {
            $parameters['align'] = $template->getAttribute('ezxhtml:align');
        }

        $content = $this->renderer->renderTemplate(
            $templateName,
            $templateType,
            $parameters,
            $template->localName === 'eztemplateinline'
        );

        if (isset($content)) {
            // If current tag is wrapped inside another template tag we can't use CDATA section
            // for its content as these can't be nested.
            // CDATA section will be used only for content of root wrapping tag, content of tags
            // inside it will be added as XML fragments.
            if ($this->isWrapped($template)) {
                $fragment = $document->createDocumentFragment();
                $fragment->appendXML(htmlspecialchars($content));
                $template->parentNode->replaceChild($fragment, $template);
            } else {
                $payload = $document->createElement('ezpayload');
                $payload->appendChild($document->createCDATASection($content));
                $template->appendChild($payload);
            }
        }
    }
560572
protected function expandParagraph(DOMDocument $document, DOMElement $paragraph)
    {
        $paragraph->parentNode->replaceChild(
            $this->expandElement($document, $paragraph),
            $paragraph
        );
    }