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}
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPNode_DOM
implements PPNode
{
30 * @var DOMElement|DOMNodeList
35 public function __construct( $node, $xpath = false ) {
42 public function getXPath() {
43 if ( $this->xpath
=== null ) {
44 $this->xpath
= new DOMXPath( $this->node
->ownerDocument
);
49 public function __toString() {
50 if ( $this->node
instanceof DOMNodeList
) {
52 foreach ( $this->node
as $node ) {
53 $s .= $node->ownerDocument
->saveXML( $node );
56 $s = $this->node
->ownerDocument
->saveXML( $this->node
);
62 * @return false|PPNode_DOM
64 public function getChildren() {
65 return $this->node
->childNodes ?
new self( $this->node
->childNodes
) : false;
69 * @return false|PPNode_DOM
71 public function getFirstChild() {
72 return $this->node
->firstChild ?
new self( $this->node
->firstChild
) : false;
76 * @return false|PPNode_DOM
78 public function getNextSibling() {
79 return $this->node
->nextSibling ?
new self( $this->node
->nextSibling
) : false;
85 * @return false|PPNode_DOM
87 public function getChildrenOfType( $type ) {
88 return new self( $this->getXPath()->query( $type, $this->node
) );
94 public function getLength() {
95 if ( $this->node
instanceof DOMNodeList
) {
96 return $this->node
->length
;
104 * @return bool|PPNode_DOM
106 public function item( $i ) {
107 $item = $this->node
->item( $i );
108 return $item ?
new self( $item ) : false;
114 public function getName() {
115 if ( $this->node
instanceof DOMNodeList
) {
118 return $this->node
->nodeName
;
123 * Split a "<part>" node into an associative array containing:
125 * - index String index
126 * - value PPNode value
128 * @throws MWException
131 public function splitArg() {
132 $xpath = $this->getXPath();
133 $names = $xpath->query( 'name', $this->node
);
134 $values = $xpath->query( 'value', $this->node
);
135 if ( !$names->length ||
!$values->length
) {
136 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
138 $name = $names->item( 0 );
139 $index = $name->getAttribute( 'index' );
141 'name' => new self( $name ),
143 'value' => new self( $values->item( 0 ) ) ];
147 * Split an "<ext>" node into an associative array containing name, attr, inner and close
148 * All values in the resulting array are PPNodes. Inner and close are optional.
150 * @throws MWException
153 public function splitExt() {
154 $xpath = $this->getXPath();
155 $names = $xpath->query( 'name', $this->node
);
156 $attrs = $xpath->query( 'attr', $this->node
);
157 $inners = $xpath->query( 'inner', $this->node
);
158 $closes = $xpath->query( 'close', $this->node
);
159 if ( !$names->length ||
!$attrs->length
) {
160 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
163 'name' => new self( $names->item( 0 ) ),
164 'attr' => new self( $attrs->item( 0 ) ) ];
165 if ( $inners->length
) {
166 $parts['inner'] = new self( $inners->item( 0 ) );
168 if ( $closes->length
) {
169 $parts['close'] = new self( $closes->item( 0 ) );
176 * @throws MWException
179 public function splitHeading() {
180 if ( $this->getName() !== 'h' ) {
181 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
184 'i' => $this->node
->getAttribute( 'i' ),
185 'level' => $this->node
->getAttribute( 'level' ),
186 'contents' => $this->getChildren()