Merge identical xsl:when elements from a list

@param  DOMElement[] $nodes
@return void
974110
protected function mergeBranches(array $nodes)
	{
		$sortedNodes = [];
		foreach ($nodes as $node)
		{
			$outerXML = $node->ownerDocument->saveXML($node);
			$innerXML = preg_replace('([^>]+>(.*)<[^<]+)s', '$1', $outerXML);

			$sortedNodes[$innerXML][] = $node;
		}

		foreach ($sortedNodes as $identicalNodes)
		{
			if (count($identicalNodes) < 2)
			{
				continue;
			}

			$expr = [];
			foreach ($identicalNodes as $i => $node)
			{
				$expr[] = $node->getAttribute('test');

				if ($i > 0)
				{
					$node->parentNode->removeChild($node);
				}
			}

			$identicalNodes[0]->setAttribute('test', implode(' or ', $expr));
		}
	}
678068
public function process(DOMElement $node, Item $item)
    {
        foreach ($this->keys as $key) {
            $item->setExtra($key, $this->getValue($node, $key));
        }
    }
247146
protected function sanitizeMixedDomNodeList(\DOMDocument $doc, &$list){

    $NewElement = $doc->createElement('text');

    $test = array(XML_ELEMENT_NODE=> FALSE, XML_TEXT_NODE=>FALSE);
    foreach($list as $node){
      $test[$node->nodeType] = TRUE;
    }

    //text only so exit
    if($test[XML_TEXT_NODE] == TRUE && $test[XML_ELEMENT_NODE] == FALSE){
      return;
    }

    //mixed group logic
    $newList = array();
    foreach($list as $k => $node){
      if (array_key_exists($node->nodeName, $this->inlineTags)||
        array_key_exists($node->nodeName, array('#text' => true))){
        unset($list[$k]);
        $NewElement->appendChild($node->cloneNode(true));
        if (!isset($node->nextSibling->nodeName)){
          $newList[$k] = $NewElement;
        }else if(array_key_exists($node->nextSibling->nodeName, $this->inlineTags)||
          array_key_exists($node->nextSibling->nodeName, array('#text' => true))){
          //do nothing
        }else{
          $newList[$k] = $NewElement;
        }
      }else{
        $newList[$k] = $node;
        $NewElement = $doc->createElement('text');
      }
    }
    $list = $newList;
  }
1143480
public function discardNodes(array $nodes)
    {
        $discardedNodeIdentifiers = [];
        foreach ($nodes as $node) {
            $this->doDiscardNode($node, $discardedNodeIdentifiers);
        }
    }
254512
public function mergeNode(ParentNode $node) {
    $child = $node->head;
    while ($child) {
      $next = $child->next;
      $this->appendChild($child);
      $child = $next;
    }
    foreach ($node->getChildProperties() as $name => $value) {
      $this->{$name} = $value;
    }
  }
313456
public function addNode(\DOMNode $node)
    {
        if ($node instanceof \DOMDocument) {
            $node = $node->documentElement;
        }

        if (null !== $this->document && $this->document !== $node->ownerDocument) {
            throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.');
        }

        if (null === $this->document) {
            $this->document = $node->ownerDocument;
        }

        // Don't add duplicate nodes in the Crawler
        if (in_array($node, $this->nodes, true)) {
            return;
        }

        $this->nodes[] = $node;
    }
479836
protected function addSequentialNode(DOMElement $element, $value): void
    {
        if (empty($element->nodeValue)) {
            $element->nodeValue = \htmlspecialchars($value);
            return;
        }

        $child = $this->element($element->nodeName);
        $child->nodeValue = \htmlspecialchars($value);
        $element->parentNode->appendChild($child);
    }
327009
public function fillChosenFields(TableNode $fields) {
    foreach ($fields->getRowsHash() as $field => $value) {
      $this->iSetChosenElement($field, $value);
    }
  }
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);
		}
	}
957729
protected function wrapOptions(array $nodes)
	{
		$ret = array();

		foreach ( $nodes as $node_element ) {
			/** @var SelectOption $option */
			$option = SelectOption::fromNodeElement($node_element, $this->getPageFactory());
			$ret[] = $option->setSelect($this);
		}

		return $ret;
	}
691476
public function after($nodes) {
    foreach ($this->nodes as $i => $node) {
      $node->after($i === 0 ? $nodes : clone $nodes);
    }
    return $this;
  }
288158
private function setAllChildren(\DOMDocument $dom, \DOMElement $node, array $attributes, $depth = 0)
    {
        static $iloop = 0;
        if ($depth == 0) {
            $iloop = 0;
        }

        foreach ($attributes as $a => $v) {
            $node->setAttribute($a, $v);
        }

        if (($iloop++ % 100) == 0) {
            usleep(1000);
        }

        for ($n = $node->firstChild; $n; $n = $n->nextSibling) {
            $this->setAllChildren($dom, $n, $attributes, $depth + 1);
        }
    }
1230951
public static function merge(TextNode $a, TextNode $b)
    {
        return new self(sprintf('%s%s', $a->getValue(), $b->getValue()));
    }
675240
def merge_text_nodes_on(self, node):
        """"""

        if not isinstance(node, ContainerNode) or not node.children:
            return

        new_children = []
        text_run = []
        for i in node.children:
            if isinstance(i, Text) and not i.translatable:
                text_run.append(i.escaped())
            else:
                if text_run:
                    new_children.append(EscapedText(''.join(text_run)))
                    text_run = []

                new_children.append(i)

        if text_run:
            new_children.append(EscapedText(''.join(text_run)))

        node.children = new_children
        for i in node.children:
            self.merge_text_nodes_on(i)
871578
public function mergeMenus(array $items)
    {
        foreach ($items as $item) {
            $menu = Yii::createObject($item['menu']);
            $this->mergeItems($menu->getItems());
        }
    }
935515
private function replaceNode(Node $node)
    {
        //Looking for parent block call
        if (!empty($inner = $node->findNode($this->name))) {
            //This construction allows child block use parent content
            $inner->nodes = $this->nodes;
        }

        $this->nodes = $node->nodes;
    }
88138
protected static function mergeNode(\SimpleXMLElement $source, \SimpleXMLElement $new)
	{
		// Update the attributes of the child node.
		foreach ($new->attributes() as $name => $value)
		{
			if (isset($source[$name]))
			{
				$source[$name] = (string) $value;
			}
			else
			{
				$source->addAttribute($name, $value);
			}
		}
	}