@param \DOMElement|\DOMText|\DOMAttr $node

@return \DiDom\Element|string

@throws InvalidArgumentException if node is not DOMElement, DOMText, DOMComment, DOMCdataSection or DOMAttr
9018
protected function wrapNode($node)
    {
        switch (get_class($node)) {
            case 'DOMElement':
                return new Element($node);

            case 'DOMText':
                return $node->data;

            case 'DOMComment':
                return new Element($node);

            case 'DOMCdataSection':
                return new Element($node);

            case 'DOMAttr':
                return $node->value;
        }

        throw new InvalidArgumentException(sprintf('Unknown node type "%s"', get_class($node)));
    }
391663
protected function nodeToArray($node)
    {
        $output = array();
        switch ($node->nodeType) {
            case XML_CDATA_SECTION_NODE:
            case XML_TEXT_NODE:
                $output = trim($node->textContent);
                break;
            case XML_ELEMENT_NODE:
                for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
                    $child = $node->childNodes->item($i);
                    $value = $this->nodeToArray($child);
                    if (isset($child->tagName)) {
                        $tag = $child->tagName;
                        if (!isset($output[$tag])) {
                            $output[$tag] = array();
                        }
                        $output[$tag][] = $value;
                    } elseif ($value || $value === '0') {
                        $output = (string) $value;
                    }
                }

                // Edge case of a node containing a text node, which also has
                // attributes. this way we'll retain text and attributes for
                // this node.
                if (is_string($output) && $node->attributes->length) {
                    $output = array('@text' => $output);
                }

                if (is_array($output)) {
                    if ($node->attributes->length) {
                        $attributes = array();
                        foreach ($node->attributes as $attrName => $attrNode) {
                            $attributes[$attrName] = (string) $attrNode->value;
                        }
                        $output['@attributes'] = $attributes;
                    }
                    foreach ($output as $tag => $value) {
                        if (is_array($value) && count($value) == 1 && $tag != '@attributes') {
                            $output[$tag] = $value[0];
                        }
                    }
                }
                break;
        }

        return $output;
    }
621808
protected function replaceCallback(\DOMNode $node)
    {
        return function ($text) use ($node) {
            $attribute = '';
            if ($node instanceof \DOMText) {
                $attribute = 'nodeValue';
            } elseif ($node instanceof \DOMAttr) {
                $attribute = 'value';
            }

            if ($attribute === '') {
                throw new ParserCrawlerAfterListenerException('No callback behavior set for this node type.');
            }

            // reserved character in XML: &
            $text = str_replace('&amp;', '&', $text);
            $text = str_replace('&', '&amp;', $text);
            $text = str_replace('&lt;', '<', $text);
            $text = str_replace('&gt;', '>', $text);

            $node->$attribute = $text;
        };
    }
1170332
private function getValue(\DOMElement $node)
    {
        $value = $node->nodeValue;

        if ('boolean' === $type = $node->getAttribute('type')) {
            return 'true' === $value;
        }

        settype($value, $type);

        return $value;
    }
1077722
public static function getChildElements($node)
    {
        $ret = array();
        $nodes = $node->childNodes;
        foreach ($nodes as $node) {
            if ($node instanceof DOMElement) {
                array_push($ret, $node);
            }
        }
        return $ret;
    }
679022
public function nodeToDslText(NodeInterface $node)
    {
        switch ($node->getNodeType()) {
            case Node::TYPE_USE_CASE:
                return $this->useCaseNodeToDslText($node);
                break;
            default:
                throw new \InvalidArgumentException('Unknown node type "' . $node->getNodeType() . '"');
                break;
        }
    }
509915
public function getMethod(\DOMNode $node)
    {
        $crawler = new Crawler($node);
        $name    = $crawler->attr('name');

        // Initialize
        $method  = new Method($name);

        // Type
        $method->setType(
            preg_match('/(^(get|is)|ToString$)/', $name) ?
            Method::TYPE_ACCESSOR :
            Method::TYPE_ACTION
        );

        // Description
        $descriptions = $crawler->filterXPath('//comment');
        if (count($descriptions) !== 1) {
            throw new \Exception('Only one comment expected');
        }
        $descriptions->rewind();
        $description = $this->getInner($descriptions->current());
        $method->setDescription($description);

        // Parameters
        foreach ($crawler->filterXPath('//parameter') as $node) {
            $method->addParameter($this->getParameter($node));
        }

        // Return
        $returnNodes = $crawler->filterXPath('//return');

        if (count($returnNodes) > 1) {
            throw new \Exception("Should not be more than one return node");
        } elseif (count($returnNodes) == 1) {
            $returnNodes->rewind();
            list($type, $description) = $this->getReturn($returnNodes->current());

            $method->setReturnType($type);
            $method->setReturnDescription($description);
        }

        return $method;
    }
564920
public static function getText(DOMElement $node)
    {
        $s = '';
        for ($i = 0; $i < $node->childNodes->length; $i++) {
            $child = $node->childNodes->item($i);

            switch ($child->nodeType) {
                case XML_CDATA_SECTION_NODE:
                case XML_TEXT_NODE: {
                    $s .= $child->nodeValue;
                }
            }
        }

        return $s;
    }
1278760
public function addNode(ElementtypeStructureNode $node)
    {
        $dsId = $node->getDsId();
        $parentDsId = $node->getParentDsId();

        if (array_key_exists($dsId, $this->dsIdMap)) {
            $msg = "Duplicate node in element structure tree: ds_id=$dsId";
            throw new InvalidArgumentException($msg);
        }

        // add node by node id and ds id to node map
        $this->dsIdMap[$dsId] = $node;
        $this->childrenMap[$parentDsId][] = $dsId;
        $this->parentMap[$dsId] = $parentDsId;

        if (!$this->rootDsId && $node->isRoot()) {
            $this->rootDsId = $dsId;
        }

        return $this;
    }
42477
protected function getParentElementId(DOMNode $node)
	{
		$parentNode = $node->parentNode;
		while (isset($parentNode))
		{
			if ($parentNode->nodeName === 'element')
			{
				return $parentNode->getAttribute('id');
			}
			$parentNode = $parentNode->parentNode;
		}
	}
1219436
private function normalizeStyleAttributes(\DOMElement $node)
    {
        $normalizedOriginalStyle = preg_replace_callback(
            '/[A-z\\-]+(?=\\:)/S',
            function (array $m) {
                return strtolower($m[0]);
            },
            $node->getAttribute('style')
        );

        // in order to not overwrite existing style attributes in the HTML, we
        // have to save the original HTML styles
        $nodePath = $node->getNodePath();
        if (!isset($this->styleAttributesForNodes[$nodePath])) {
            $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationsBlock($normalizedOriginalStyle);
            $this->visitedNodes[$nodePath] = $node;
        }

        $node->setAttribute('style', $normalizedOriginalStyle);
    }
1345820
public function getTypeForDOMNode( $node )
	{
		$ns = $node->namespaceURI;
		if ( ! $ns )
		{
			if ( $node instanceof \DOMElement )
			{
				// Get the default namespace for the DOMNode
				$doc = $node->ownerDocument;
				/**
				 * @var DOMNode $firstChild
				 */
				$firstChild = $doc->documentElement;
				$ns = $firstChild->getAttributeNode('xmlns')
					? $firstChild->getAttributeNode('xmlns')->nodeValue
					: "";
			}
			else if ( $node instanceof \DOMAttr )
			{
				$parent = $node->parentNode;
				if ( $parent )
				{
					$ns = $parent->namespaceURI;
				}
			}
		}

		$prefix = $this->getPrefixForNamespace( $ns );
		$types;

		if ( is_a( $node, "DOMElement" ) )
		{
			$el = $this->getElement( $node->localName, $prefix );
			if ( $el === false ) throw new SchemaException( "Unable to locate element '{$node->localName}' in the schema types" );
			$types = isset( $el['types'] ) ? $el['types'] : array();
		}
		else if ( is_a( $node, "DOMAttr" ) )
		{
			$attr = $this->getAttribute( $node->localName, $prefix );
			if ( $attr === false ) throw new SchemaException( "Unable to locate attribute '{$node->localName}' in the schema types" );
			$types = isset( $attr['types'] ) ? $attr['types'] : array();
		}
		else if ( is_a( $node, "DOMDocument" ) )
		{
			throw new SchemaException("Getting the type for a Document node is not supported");
		}
		else if ( is_a( $node, "DOMComment" ) )
		{
			// BMS 2018-04-09 Test candidates changed.
			return "xs:string";
		}
		else if ( is_a( $node, "DOMProcessingInstruction" ) )
		{
			throw new SchemaException("Getting the type for a Processing Instruction node is not supported");
		}
		else if ( is_a( $node, "DOMText" ) )
		{
			// BMS 2018-04-09 Test candidates changed.
			return "xs:string";
		}
		else if ( is_a( $node, "DOMNameSpaceNode" ) )
		{
			throw new SchemaException("Getting the type for a Namespace node is not supported");
		}

		// Get the first simple type
		$type = null;
		foreach ( $types as $xmlType )
		{
			if ( is_array( $xmlType ) )
			{
				if ( isset( $xmlType['class'] ) && $xmlType['class'] == "complex" )
				{
					continue;
				}

				if ( isset( $xmlType['contentType'] ) && $xmlType['contentType'] == "extension" && isset( $xmlType['base'] ) )
				{
					return $xmlType['parent'];
				}

				if ( isset( $xmlType['contentType'] ) && $xmlType['contentType'] == "restriction" && isset( $xmlType['base'] ) )
				{
					return $xmlType['parent'];
				}
			}

			return $xmlType;
		}

		return false;
	}
882236
protected function transpileDirectiveNode(Node $node) : string
    {
        $children = $node->getChildren();

        if (count($children) === 0) {
            return '';
        }

        $directiveName = $children[0]->getValue();
        $expression = count($children) === 2 ? $children[1]->getValue() : '';

        if (!isset($this->directiveTranspilers[$directiveName])) {
            throw new RuntimeException(
                sprintf(
                    'No transpiler registered for directive "%s"',
                    $directiveName
                )
            );
        }

        return $this->directiveTranspilers[$directiveName]($expression);
    }
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;
    }
360088
protected function initializeNode(\DOMElement $node)
    {
        if (!isset($node->tagName)) {
            return;
        }

        $readability = $this->dom->createAttribute('readability');
        // this is our contentScore
        $readability->value = 0;
        $node->setAttributeNode($readability);

        // using strtoupper just in case
        switch (strtoupper($node->tagName)) {
            case 'ARTICLE':
                $readability->value += 15;
                // no break
            case 'DIV':
                $readability->value += 5;
                break;
            case 'PRE':
            case 'CODE':
            case 'TD':
            case 'BLOCKQUOTE':
            case 'FIGURE':
                $readability->value += 3;
                break;
            case 'SECTION':
                // often misused
                // $readability->value += 2;
                break;
            case 'OL':
            case 'UL':
            case 'DL':
            case 'DD':
            case 'DT':
            case 'LI':
                $readability->value -= 2 * round($this->getLinkDensity($node), 0, PHP_ROUND_HALF_UP);
                break;
            case 'ASIDE':
            case 'FOOTER':
            case 'HEADER':
            case 'ADDRESS':
            case 'FORM':
            case 'BUTTON':
            case 'TEXTAREA':
            case 'INPUT':
            case 'NAV':
                $readability->value -= 3;
                break;
            case 'H1':
            case 'H2':
            case 'H3':
            case 'H4':
            case 'H5':
            case 'H6':
            case 'TH':
            case 'HGROUP':
                $readability->value -= 5;
                break;
        }

        $readability->value += $this->getWeight($node);
    }
321064
protected function restoreItemFromXml(DOMElement $node)
    {
        $item = array('attributes' => array());

        if ($node->hasAttributes()) {
            foreach ($node->attributes as $attr) {
                switch ($attr->name) {
                    case 'action':
                        $item['action'] = $attr->value;
                        break;
                    default:
                        $item['attributes'][$attr->name] = $attr->value;
                }
            }
        }

        foreach ($node->childNodes as $node) {
            if ($node->nodeType !== XML_ELEMENT_NODE) {
                continue;
            }

            switch ($node->nodeName) {
                case 'object_id':
                    $item['contentId'] = $node->nodeValue;
                    break;
                case 'node_id':
                    $item['locationId'] = $node->nodeValue;
                    break;
                case 'priority':
                    $item[$node->nodeName] = $node->nodeValue;
                    break;
                case 'ts_publication':
                    $item['publicationDate'] = $node->nodeValue;
                    break;
                case 'ts_visible':
                    $item['visibilityDate'] = $node->nodeValue;
                    break;
                case 'ts_hidden':
                    $item['hiddenDate'] = $node->nodeValue;
                    break;
                case 'rotation_until':
                    $item['rotationUntilDate'] = $node->nodeValue;
                    break;
                case 'moved_to':
                    $item['movedTo'] = $node->nodeValue;
                    break;
            }
        }

        return new Parts\Item($item);
    }
206231
protected function extendedAttrRequired(DOMNode $node, DOMAttr $attr)
	{
		$id = $node->getAttribute('id');

		$message = $attr->value;
		$this->js []= "addValidator('required', '#$id', '$message');";

	}