Adds a node to the xml for deprecations and the count value

@param \DOMDocument $dom Markers are extracted and a summary inserted in this object.
12298
protected function buildDeprecationList(\DOMDocument $dom)
    {
        $nodes = $this->getNodeListForTagBasedQuery($dom, 'deprecated');

        $node = new \DOMElement('deprecated');
        $dom->documentElement->appendChild($node);
        $node->setAttribute('count', (string) $nodes->length);
    }
1030695
public function addDomNode(\DOMNode $dom_node, $prepend=false)
    {
        if ($prepend) {
            array_unshift($this->nodes, $dom_node);
        } else {
            $this->nodes[] = $dom_node;
        }

        $this->length = \count($this->nodes);
        $this->setDomDocument($dom_node->ownerDocument);
    }
557808
protected function appendStatElement(\DOMDocument $document, ProjectDescriptor $project, $date)
    {
        $stat = $document->createDocumentFragment();
        $stat->appendXML(
            <<<STAT
<stat date="${date}">
    <counters>
        <files>{$this->getFilesCounter($project)}</files>
        <deprecated>{$this->getDeprecatedCounter($project)}</deprecated>
        <errors>{$this->getErrorCounter($project)}</errors>
        <markers>{$this->getMarkerCounter($project)}</markers>
    </counters>
</stat>
STAT
        );
        $document->documentElement->appendChild($stat);

        return $document;
    }
1269653
public function node(\DOMNode $node) {
        // A listing of types is at http://php.net/manual/en/dom.constants.php
        switch ($node->nodeType) {
            case XML_ELEMENT_NODE:
                $this->rules->element($node);
                break;
            case XML_TEXT_NODE:
                $this->rules->text($node);
                break;
            case XML_CDATA_SECTION_NODE:
                $this->rules->cdata($node);
                break;
            // FIXME: It appears that the parser doesn't do PI's.
            case XML_PI_NODE:
                $this->rules->processorInstruction($node);
                break;
            case XML_COMMENT_NODE:
                $this->rules->comment($node);
                break;
            // Currently we don't support embedding DTDs.
            default:
                print '<!-- Skipped -->';
                break;
        }
    }
434794
public function loadDomDocument(\DOMDocument $domDocument)
    {
        $this->_dom = $domDocument;
        foreach ($this->_getNodesForCssSelector('[style]') as $node) {
            $node->setAttribute(
                'inlinestyle-original-style',
                $node->getAttribute('style')
            );
        }
    }
708071
protected function registerNode(\DOMNode $node)
    {
        if ($node->nodeType == XML_ELEMENT_NODE) {
            /** @var \DOMElement $node */
            $localContext = $this->elementProcessor->processElement($node, $this->initialContext);

            // Register the node context
            $localContextId = spl_object_hash($localContext);
            if (empty($this->contexts[$localContextId])) {
                $this->contexts[$localContextId] = $localContext;
            }

            $this->contextMap[$node->getNodePath()] = $localContextId;
        }

        return $node;
    }
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;
    }
1350519
public function parseDom(\DOMDocument $dom)
    {
        $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName());
        $parser       = new Parser($dom, strval($this->uri));
        $parser->lang = true; // Enable experimental language parsing
        $microformats = $parser->parse();

        return new ParsingResult(
            self::FORMAT,
            isset($microformats['items']) ?
                MicroformatsFactory::createFromParserResult((array)$microformats['items']) : []
        );
    }
1356816
protected function buildXmlFromTraversable(\DOMDocument $dom, \DOMNode $node, $data)
    {
        $normalizer    = $this->getNormalizer();
        $hasAttributes = false;
        $isList        = is_array($data) && Arr::isList($data, true);

        foreach ($data as $key => $value) {
            if (!is_scalar($value) && !($value = $normalizer->ensureBuildable($value))) {
                continue;
            }

            if ($this->mapAttributes($node, $normalizer->normalize($key), $value)) {
                $hasAttributes = true;
                continue;
            }

            // ensure "node value keys" are at the bottom.
            if (null !== $this->nodeValueKey && is_array($value) && isset($value[$this->nodeValueKey])) {
                $val = $value[$this->nodeValueKey];
                unset($value[$this->nodeValueKey]);
                $value[$this->nodeValueKey] = $val;
            }

            // Set the default index key if there's no other way.
            if (is_int($key) || !$this->isValidNodeName($key)) {
                $cey = $key;
                $key = $this->indexKey;
                // In order to keep the original index for none "list-like" arrays
                // (numeric keys and none zero based indecies) add the original key
                // as index attribute
                if (is_int($cey) && !$isList) {
                    $value = [
                        '@AAA' => [$this->indexAttrKey ?: 'index' => $cey],
                        $this->nodeValueKey ?: 'value' => $value,
                    ];
                }
            }

            if (is_array($value) && !is_int($key) && $this->appendDomList($normalizer, $dom, $node, $key, $value)) {
                continue;
            }

            // if this is a non scalar value at this time, just set the
            // value on the element
            if ($this->isXMLElement($value)) {
                $element = $dom->createElement($normalizer->normalize($key));
                $node->appendChild($element);
                $this->setElementValue($dom, $element, $value);
                continue;
            }

            if ($this->isValidNodeName($key)) {
                $this->appendDOMNode($dom, $node, $normalizer->normalize($key), $value, $hasAttributes);
            }
        }
    }
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;
    }
768006
protected function checkElementNode(DOMElement $element, Tag $tag)
	{
		$xpath = new DOMXPath($element->ownerDocument);

		// If current node is not an <xsl:attribute/> element, we exclude descendants
		// with an <xsl:attribute/> ancestor so that content such as:
		//   <script><xsl:attribute name="id"><xsl:value-of/></xsl:attribute></script>
		// would not trigger a false-positive due to the presence of an <xsl:value-of/>
		// element in a <script>
		$predicate = ($element->localName === 'attribute') ? '' : '[not(ancestor::xsl:attribute)]';

		// Test the select expression of <xsl:value-of/> nodes
		$query = './/xsl:value-of' . $predicate;
		foreach ($xpath->query($query, $element) as $valueOf)
		{
			$this->checkSelectNode($valueOf->getAttributeNode('select'), $tag);
		}

		// Reject all <xsl:apply-templates/> nodes
		$query = './/xsl:apply-templates' . $predicate;
		foreach ($xpath->query($query, $element) as $applyTemplates)
		{
			throw new UnsafeTemplateException('Cannot allow unfiltered data in this context', $applyTemplates);
		}
	}
868872
private function appendDOMNode(\DOMDocument $dom, $node, $name, $value = null, $hasAttributes = false)
    {
        $element = $dom->createElement($name);

        if ($hasAttributes && $this->nodeValueKey === $name) {
            $this->setElementValue($dom, $node, $value);

            return;
        }

        $this->setElementValue($dom, $element, $value);
        $node->appendChild($element);
    }
619272
protected function buildPackageTree(\DOMDocument $dom)
    {
        $xpath = new \DOMXPath($dom);
        $packages = array('global' => true);
        $qry = $xpath->query('//@package');
        for ($i = 0; $i < $qry->length; $i++) {
            if (isset($packages[$qry->item($i)->nodeValue])) {
                continue;
            }

            $packages[$qry->item($i)->nodeValue] = true;
        }

        $packages = $this->generateNamespaceTree(array_keys($packages));
        $this->generateNamespaceElements($packages, $dom->documentElement, 'package');
    }
1139674
private function createTextNodeWithTypeAttribute(\DOMDocument $dom, \DOMNode $node, $value, $type = 'int')
    {
        $text = $dom->createTextNode($value);
        $attr = $dom->createAttribute('type');
        $attr->value = $type;

        $node->appendChild($text);
        $node->appendChild($attr);
    }
332159
public function appendChildFromXmlString($xml)
    {
        $dom = new \DOMDocument;
        $dom->loadXML($xml);

        $element = simplexml_import_dom($dom);

        $this->appendChildNode($element);
    }
610136
protected function addRootNodes(\DOMDocument $dom, $targetLanguage = null)
    {
        $xliff = $dom->appendChild($dom->createElement('xliff'));
        $xliff->appendChild(new \DOMAttr('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'));
        $xliff->appendChild(new \DOMAttr('version', '1.2'));

        $fileNode = $xliff->appendChild($dom->createElement('file'));
        $fileNode->appendChild(new \DOMAttr('source-language', 'en'));
        $fileNode->appendChild(new \DOMAttr('datatype', 'plaintext'));
        $fileNode->appendChild(new \DOMAttr('original', 'file.ext'));

        if (!is_null($targetLanguage)) {
            $fileNode->appendChild(new \DOMAttr('target-language', $targetLanguage));
        }

        $bodyNode =  $fileNode->appendChild($dom->createElement('body'));

        return $bodyNode;
    }
1124782
private function prepDocument(DOMDocument $dom)
    {
        $this->logger->info('[PrepDocument] Preparing document for parsing...');

        foreach ($dom->shiftingAwareGetElementsByTagName('br') as $br) {
            $next = $br->nextSibling;

            /*
             * Whether 2 or more <br> elements have been found and replaced with a
             * <p> block.
             */
            $replaced = false;

            /*
             * If we find a <br> chain, remove the <br>s until we hit another element
             * or non-whitespace. This leaves behind the first <br> in the chain
             * (which will be replaced with a <p> later).
             */
            while (($next = NodeUtility::nextElement($next)) && ($next->nodeName === 'br')) {
                $this->logger->debug('[PrepDocument] Removing chain of BR nodes...');

                $replaced = true;
                $brSibling = $next->nextSibling;
                $next->parentNode->removeChild($next);
                $next = $brSibling;
            }

            /*
             * If we removed a <br> chain, replace the remaining <br> with a <p>. Add
             * all sibling nodes as children of the <p> until we hit another <br>
             * chain.
             */

            if ($replaced) {
                $p = $dom->createElement('p');
                $br->parentNode->replaceChild($p, $br);

                $next = $p->nextSibling;
                while ($next) {
                    // If we've hit another <br><br>, we're done adding children to this <p>.
                    if ($next->nodeName === 'br') {
                        $nextElem = NodeUtility::nextElement($next->nextSibling);
                        if ($nextElem && $nextElem->nodeName === 'br') {
                            break;
                        }
                    }

                    if (!$next->isPhrasingContent()) {
                        break;
                    }

                    $this->logger->debug('[PrepDocument] Replacing BR with a P node...');

                    // Otherwise, make this node a child of the new <p>.
                    $sibling = $next->nextSibling;
                    $p->appendChild($next);
                    $next = $sibling;
                }

                while ($p->lastChild && $p->lastChild->isWhitespace()) {
                    $p->removeChild($p->lastChild);
                }

                if ($p->parentNode->tagName === 'p') {
                    NodeUtility::setNodeTag($p->parentNode, 'div');
                }
            }
        }

        // Replace font tags with span
        $fonts = $dom->getElementsByTagName('font');
        $length = $fonts->length;
        for ($i = 0; $i < $length; $i++) {
            $this->logger->debug('[PrepDocument] Converting font tag into a span tag.');
            $font = $fonts->item($length - 1 - $i);
            NodeUtility::setNodeTag($font, 'span');
        }
    }