node = $node; } /** * @return DOMXPath */ public function getXPath() { if ( $this->xpath === null ) { $this->xpath = new DOMXPath( $this->node->ownerDocument ); } return $this->xpath; } public function __toString() { if ( $this->node instanceof DOMNodeList ) { $s = ''; foreach ( $this->node as $node ) { $s .= $node->ownerDocument->saveXML( $node ); } } else { $s = $this->node->ownerDocument->saveXML( $this->node ); } return $s; } /** * @return bool|PPNode_DOM */ public function getChildren() { return $this->node->childNodes ? new self( $this->node->childNodes ) : false; } /** * @return bool|PPNode_DOM */ public function getFirstChild() { return $this->node->firstChild ? new self( $this->node->firstChild ) : false; } /** * @return bool|PPNode_DOM */ public function getNextSibling() { return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false; } /** * @param string $type * * @return bool|PPNode_DOM */ public function getChildrenOfType( $type ) { return new self( $this->getXPath()->query( $type, $this->node ) ); } /** * @return int */ public function getLength() { if ( $this->node instanceof DOMNodeList ) { return $this->node->length; } else { return false; } } /** * @param int $i * @return bool|PPNode_DOM */ public function item( $i ) { $item = $this->node->item( $i ); return $item ? new self( $item ) : false; } /** * @return string */ public function getName() { if ( $this->node instanceof DOMNodeList ) { return '#nodelist'; } else { return $this->node->nodeName; } } /** * Split a "" node into an associative array containing: * - name PPNode name * - index String index * - value PPNode value * * @throws MWException * @return array */ public function splitArg() { $xpath = $this->getXPath(); $names = $xpath->query( 'name', $this->node ); $values = $xpath->query( 'value', $this->node ); if ( !$names->length || !$values->length ) { throw new MWException( 'Invalid brace node passed to ' . __METHOD__ ); } $name = $names->item( 0 ); $index = $name->getAttribute( 'index' ); return [ 'name' => new self( $name ), 'index' => $index, 'value' => new self( $values->item( 0 ) ) ]; } /** * Split an "" node into an associative array containing name, attr, inner and close * All values in the resulting array are PPNodes. Inner and close are optional. * * @throws MWException * @return array */ public function splitExt() { $xpath = $this->getXPath(); $names = $xpath->query( 'name', $this->node ); $attrs = $xpath->query( 'attr', $this->node ); $inners = $xpath->query( 'inner', $this->node ); $closes = $xpath->query( 'close', $this->node ); if ( !$names->length || !$attrs->length ) { throw new MWException( 'Invalid ext node passed to ' . __METHOD__ ); } $parts = [ 'name' => new self( $names->item( 0 ) ), 'attr' => new self( $attrs->item( 0 ) ) ]; if ( $inners->length ) { $parts['inner'] = new self( $inners->item( 0 ) ); } if ( $closes->length ) { $parts['close'] = new self( $closes->item( 0 ) ); } return $parts; } /** * Split a "" node * @throws MWException * @return array */ public function splitHeading() { if ( $this->getName() !== 'h' ) { throw new MWException( 'Invalid h node passed to ' . __METHOD__ ); } return [ 'i' => $this->node->getAttribute( 'i' ), 'level' => $this->node->getAttribute( 'level' ), 'contents' => $this->getChildren() ]; } }