Create a node that implements given replacement strategy

@param  DOMDocument $dom
@param  array       $replacement
@return DOMNode
12565
protected static function createReplacementNode(DOMDocument $dom, array $replacement)
	{
		if ($replacement[0] === 'expression')
		{
			$newNode = $dom->createElementNS(self::XMLNS_XSL, 'xsl:value-of');
			$newNode->setAttribute('select', $replacement[1]);
		}
		elseif ($replacement[0] === 'passthrough')
		{
			$newNode = $dom->createElementNS(self::XMLNS_XSL, 'xsl:apply-templates');
			if (isset($replacement[1]))
			{
				$newNode->setAttribute('select', $replacement[1]);
			}
		}
		else
		{
			$newNode = $dom->createTextNode($replacement[1]);
		}

		return $newNode;
	}
496552
protected function createTranslationNode(\DOMDocument $dom, $id, $key, $value)
    {
        $translationNode = $dom->createElement('trans-unit');
        $translationNode->appendChild(new \DOMAttr('id', $id));
        
        /**
         * @see http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#approved
         */
        if ($value != '') {
            $translationNode->appendChild(new \DOMAttr('approved', 'yes'));
        }

        $source = $dom->createElement('source');
        $source->appendChild($dom->createCDATASection($key));
        $translationNode->appendChild($source);

        $target = $dom->createElement('target');
        $target->appendChild($dom->createCDATASection($value));
        $translationNode->appendChild($target);

        return $translationNode;
    }
1292850
public function cloneNode(\DOMDocument $domDocument)
    {
        return $domDocument->importNode($this->getDomNode()->cloneNode(false), false);
    }
61275
protected function getNextSiblingOfTypeDOMElement(\DOMNode $node)
    {
        do {
            $node = $node->nextSibling;
        } while (!($node === null || $node instanceof \DOMElement));

        return $node;
    }
749687
protected function buildNamespaceTree(\DOMDocument $dom)
    {
        $xpath = new \DOMXPath($dom);
        $namespaces = array();
        $qry = $xpath->query('//@namespace');
        for ($i = 0; $i < $qry->length; $i++) {
            if (isset($namespaces[$qry->item($i)->nodeValue])) {
                continue;
            }

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

        $namespaces = $this->generateNamespaceTree(array_keys($namespaces));
        $this->generateNamespaceElements($namespaces, $dom->documentElement);
    }
1189606
private function insertOrReplaceTag($content, $search, $replace)
    {
        $newContent =  null;

        $regexSearch = "/(\<$search\>\<\!\[CDATA\[([0-9.])+\]\]\>\<\/$search\>)/";
        if(preg_match($regexSearch, $content)) {
            $newContent = preg_replace($regexSearch, $replace, $content);
        }
        else {
            $newContent = $content . $replace;

        }

        return $newContent;
    }
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');
    }
881806
public function replaceOneValue($search, $replacement = ''): self
    {
        $array = $this->getArray();
        $key = \array_search($search, $array, true);

        if ($key !== false) {
            $array[$key] = $replacement;
        }

        return static::create(
            $array,
            $this->iteratorClass,
            false
        );
    }
711273
private function domNodeClosingTagOptional(\DOMNode $node): bool
    {
        $tag_name = $node->nodeName;
        $nextSibling = $this->getNextSiblingOfTypeDOMElement($node);

        // https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission

        // Implemented:
        //
        // A <p> element's end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.
        // An <li> element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.
        // A <td> element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.
        // An <option> element's end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element.
        // A <tr> element's end tag may be omitted if the tr element is immediately followed by another tr element, or if there is no more content in the parent element.
        // A <th> element's end tag may be omitted if the th element is immediately followed by a td or th element, or if there is no more content in the parent element.
        // A <dt> element's end tag may be omitted if the dt element is immediately followed by another dt element or a dd element.
        // A <dd> element's end tag may be omitted if the dd element is immediately followed by another dd element or a dt element, or if there is no more content in the parent element.
        // An <rp> element's end tag may be omitted if the rp element is immediately followed by an rt or rp element, or if there is no more content in the parent element.

        // TODO:
        //
        // <html> may be omitted if first thing inside is not comment
        // <head> may be omitted if first thing inside is an element
        // <body> may be omitted if first thing inside is not space, comment, <meta>, <link>, <script>, <style> or <template>
        // <colgroup> may be omitted if first thing inside is <col>
        // <tbody> may be omitted if first thing inside is <tr>
        // An <optgroup> element's end tag may be omitted if the optgroup element is immediately followed by another optgroup element, or if there is no more content in the parent element.
        // A <colgroup> element's start tag may be omitted if the first thing inside the colgroup element is a col element, and if the element is not immediately preceded by another colgroup element whose end tag has been omitted. (It can't be omitted if the element is empty.)
        // A <colgroup> element's end tag may be omitted if the colgroup element is not immediately followed by ASCII whitespace or a comment.
        // A <caption> element's end tag may be omitted if the caption element is not immediately followed by ASCII whitespace or a comment.
        // A <thead> element's end tag may be omitted if the thead element is immediately followed by a tbody or tfoot element.
        // A <tbody> element's start tag may be omitted if the first thing inside the tbody element is a tr element, and if the element is not immediately preceded by a tbody, thead, or tfoot element whose end tag has been omitted. (It can't be omitted if the element is empty.)
        // A <tbody> element's end tag may be omitted if the tbody element is immediately followed by a tbody or tfoot element, or if there is no more content in the parent element.
        // A <tfoot> element's end tag may be omitted if there is no more content in the parent element.
        //
        // <-- However, a start tag must never be omitted if it has any attributes.

        return \in_array($tag_name, self::$optional_end_tags, true)
               ||
               (
                   $tag_name === 'li'
                   &&
                   (
                       $nextSibling === null
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           $nextSibling->tagName === 'li'
                       )
                   )
               )
               ||
               (
                   (
                       $tag_name === 'rp'
                   )
                   &&
                   (
                       $nextSibling === null
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           (
                               $nextSibling->tagName === 'rp'
                               ||
                               $nextSibling->tagName === 'rt'
                           )
                       )
                   )
               )
               ||
               (
                   $tag_name === 'tr'
                   &&
                   (
                       $nextSibling === null
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           $nextSibling->tagName === 'tr'
                       )
                   )
               )
               ||
               (
                   (
                       $tag_name === 'td'
                       ||
                       $tag_name === 'th'
                   )
                   &&
                   (
                       $nextSibling === null
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           (
                               $nextSibling->tagName === 'td'
                               ||
                               $nextSibling->tagName === 'th'
                           )
                       )
                   )
               )
               ||
               (
                   (
                       $tag_name === 'dd'
                       ||
                       $tag_name === 'dt'
                   )
                   &&
                   (
                       (
                           $nextSibling === null
                           &&
                           $tag_name === 'dd'
                       )
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           (
                               $nextSibling->tagName === 'dd'
                               ||
                               $nextSibling->tagName === 'dt'
                           )
                       )
                   )
               )
               ||
               (
                   $tag_name === 'option'
                   &&
                   (
                       $nextSibling === null
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           (
                               $nextSibling->tagName === 'option'
                               ||
                               $nextSibling->tagName === 'optgroup'
                           )
                       )
                   )
               )
               ||
               (
                   $tag_name === 'p'
                   &&
                   (
                       (
                           $nextSibling === null
                           &&
                           (
                               $node->parentNode !== null
                               &&
                               !\in_array(
                                   $node->parentNode->nodeName,
                                   [
                                       'a',
                                       'audio',
                                       'del',
                                       'ins',
                                       'map',
                                       'noscript',
                                       'video',
                                   ],
                                   true
                               )
                           )
                       )
                       ||
                       (
                           $nextSibling instanceof \DOMElement
                           &&
                           \in_array(
                               $nextSibling->tagName,
                               [
                                   'address',
                                   'article',
                                   'aside',
                                   'blockquote',
                                   'dir',
                                   'div',
                                   'dl',
                                   'fieldset',
                                   'footer',
                                   'form',
                                   'h1',
                                   'h2',
                                   'h3',
                                   'h4',
                                   'h5',
                                   'h6',
                                   'header',
                                   'hgroup',
                                   'hr',
                                   'menu',
                                   'nav',
                                   'ol',
                                   'p',
                                   'pre',
                                   'section',
                                   'table',
                                   'ul',
                               ],
                               true
                           )
                       )
                   )
               );
    }
1098406
protected function cloneAndEmpty(DOMElement $element)
    {
        $clone = $element->cloneNode(true);

        $children = [];

        // Collect child nodes first, as we can't iterate and
        // remove from \DOMNodeList directly
        foreach ($clone->childNodes as $node) {
            $children[] = $node;
        }

        foreach ($children as $node) {
            $clone->removeChild($node);
        }

        return $clone;
    }
333947
public function generateReplacement($replacementId)
    {
        $replacementMethodName = str_replace(self::PREFIX, '', $replacementId);

        if (strpos($replacementMethodName,'->') !== false) {

            $modifierAndMethod = explode('->',$replacementMethodName);
            $modifierName = $modifierAndMethod[0];
            $replacementMethodName = $modifierAndMethod[1];

            $this->validateReplacementConfiguredModifier($modifierName, $replacementMethodName);

            return (string)$this->faker->$modifierName->$replacementMethodName;
        } else {
            $this->validateReplacementConfigured($replacementMethodName);
            return (string)$this->faker->$replacementMethodName;
        }
    }
311719
public function replace($search, $replacement, &$count = null)
    {
        return $this->cloneInstance(
            function (StringObject $new) use ($search, $replacement, &$count) {
                $new->string = str_replace($search, $replacement, $new->string, $count);
            }
        );
    }
1187630
private function visitNode(\DOMNode $node)
    {
        if ($this->node !== $node) {
            $this->node->appendChild($node);
        }

        return $node;
    }
480409
protected function buildXML(DOMDocument $dom, \DOMNode $node, $data)
    {
        $normalizer = $this->getNormalizer();

        if (null === $data) {
            return;
        }

        if ((is_array($data) || $data instanceof \Traversable) && !$this->isXmlElement($data)) {
            return $this->buildXmlFromTraversable($dom, $node, $normalizer->ensureBuildable($data));
        }

        $this->setElementValue($dom, $node, $data);
    }
648402
public function replace($newNode, $clone = true)
    {
        if ($this->node->parentNode === null) {
            throw new LogicException('Can not replace element without parent node');
        }

        if ($newNode instanceof Element) {
            $newNode = $newNode->getNode();
        }

        if (!$newNode instanceof DOMNode) {
            throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of %s or DOMNode, %s given', __METHOD__, __CLASS__, (is_object($newNode) ? get_class($newNode) : gettype($newNode))));
        }

        if ($clone) {
            $newNode = $newNode->cloneNode(true);
        }

        if ($newNode->ownerDocument === null || !$this->getDocument()->is($newNode->ownerDocument)) {
            $newNode = $this->node->ownerDocument->importNode($newNode, true);
        }

        $node = $this->node->parentNode->replaceChild($newNode, $this->node);

        return new Element($node);
    }
834951
private function doFix(\DOMText $childNode, \DOMNode $node, \DOMDocument $dom)
    {
        $content = $childNode->wholeText;
        $current_node = new StateNode($childNode, $node, $dom);

        $this->stateBag->setCurrentNode($current_node);

        // run the string on all the fixers
        foreach ($this->_rules as $fixer) {
            $content = $fixer->fix($content, $this->stateBag);
        }

        // update the DOM only if the node has changed
        if ($childNode->wholeText !== $content) {
            $new_node = $dom->createTextNode($content);
            $node->replaceChild($new_node, $childNode);

            // As the node is replaced, we also update it in the StateNode
            $current_node->replaceNode($new_node);
        }
    }
586064
private function fillElement (DOMDocument $doc, DOMElement $parent, array $filler)
    {
        foreach ($filler as $name => $value) {
            if ($value instanceof DOMElement) {
                $element = $value;
            } else {
                $element = $doc->createElement($name);
                
                if (is_array($value)) {
                    $this->fillElement($doc, $element, $value);
                } else {
                    $element->appendChild(new DOMCdataSection($value));
                }
            }
            
            $parent->appendChild($element);
        }
    }