3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * @deprecated since 1.34, use PPNode_Hash_{Tree,Text,Array,Attr}
25 * @phan-file-suppress PhanUndeclaredMethod
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class PPNode_DOM
implements PPNode
{
31 * @var DOMElement|DOMNodeList
36 public function __construct( $node, $xpath = false ) {
43 public function getXPath() {
44 if ( $this->xpath
=== null ) {
45 $this->xpath
= new DOMXPath( $this->node
->ownerDocument
);
50 public function __toString() {
51 if ( $this->node
instanceof DOMNodeList
) {
53 foreach ( $this->node
as $node ) {
54 $s .= $node->ownerDocument
->saveXML( $node );
57 $s = $this->node
->ownerDocument
->saveXML( $this->node
);
63 * @return false|PPNode_DOM
65 public function getChildren() {
66 return $this->node
->childNodes ?
new self( $this->node
->childNodes
) : false;
70 * @return false|PPNode_DOM
72 public function getFirstChild() {
73 return $this->node
->firstChild ?
new self( $this->node
->firstChild
) : false;
77 * @return false|PPNode_DOM
79 public function getNextSibling() {
80 return $this->node
->nextSibling ?
new self( $this->node
->nextSibling
) : false;
86 * @return false|PPNode_DOM
88 public function getChildrenOfType( $type ) {
89 return new self( $this->getXPath()->query( $type, $this->node
) );
95 public function getLength() {
96 if ( $this->node
instanceof DOMNodeList
) {
97 return $this->node
->length
;
105 * @return bool|PPNode_DOM
107 public function item( $i ) {
108 $item = $this->node
->item( $i );
109 return $item ?
new self( $item ) : false;
115 public function getName() {
116 if ( $this->node
instanceof DOMNodeList
) {
119 return $this->node
->nodeName
;
124 * Split a "<part>" node into an associative array containing:
126 * - index String index
127 * - value PPNode value
129 * @throws MWException
132 public function splitArg() {
133 $xpath = $this->getXPath();
134 $names = $xpath->query( 'name', $this->node
);
135 $values = $xpath->query( 'value', $this->node
);
136 if ( !$names->length ||
!$values->length
) {
137 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
139 $name = $names->item( 0 );
140 $index = $name->getAttribute( 'index' );
142 'name' => new self( $name ),
144 'value' => new self( $values->item( 0 ) ) ];
148 * Split an "<ext>" node into an associative array containing name, attr, inner and close
149 * All values in the resulting array are PPNodes. Inner and close are optional.
151 * @throws MWException
154 public function splitExt() {
155 $xpath = $this->getXPath();
156 $names = $xpath->query( 'name', $this->node
);
157 $attrs = $xpath->query( 'attr', $this->node
);
158 $inners = $xpath->query( 'inner', $this->node
);
159 $closes = $xpath->query( 'close', $this->node
);
160 if ( !$names->length ||
!$attrs->length
) {
161 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
164 'name' => new self( $names->item( 0 ) ),
165 'attr' => new self( $attrs->item( 0 ) ) ];
166 if ( $inners->length
) {
167 $parts['inner'] = new self( $inners->item( 0 ) );
169 if ( $closes->length
) {
170 $parts['close'] = new self( $closes->item( 0 ) );
177 * @throws MWException
180 public function splitHeading() {
181 if ( $this->getName() !== 'h' ) {
182 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
185 'i' => $this->node
->getAttribute( 'i' ),
186 'level' => $this->node
->getAttribute( 'level' ),
187 'contents' => $this->getChildren()