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
25 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
26 class PPNode_DOM
implements PPNode
{
34 public function __construct( $node, $xpath = false ) {
41 public function getXPath() {
42 if ( $this->xpath
=== null ) {
43 $this->xpath
= new DOMXPath( $this->node
->ownerDocument
);
48 public function __toString() {
49 if ( $this->node
instanceof DOMNodeList
) {
51 foreach ( $this->node
as $node ) {
52 $s .= $node->ownerDocument
->saveXML( $node );
55 $s = $this->node
->ownerDocument
->saveXML( $this->node
);
61 * @return bool|PPNode_DOM
63 public function getChildren() {
64 return $this->node
->childNodes ?
new self( $this->node
->childNodes
) : false;
68 * @return bool|PPNode_DOM
70 public function getFirstChild() {
71 return $this->node
->firstChild ?
new self( $this->node
->firstChild
) : false;
75 * @return bool|PPNode_DOM
77 public function getNextSibling() {
78 return $this->node
->nextSibling ?
new self( $this->node
->nextSibling
) : false;
84 * @return bool|PPNode_DOM
86 public function getChildrenOfType( $type ) {
87 return new self( $this->getXPath()->query( $type, $this->node
) );
93 public function getLength() {
94 if ( $this->node
instanceof DOMNodeList
) {
95 return $this->node
->length
;
103 * @return bool|PPNode_DOM
105 public function item( $i ) {
106 $item = $this->node
->item( $i );
107 return $item ?
new self( $item ) : false;
113 public function getName() {
114 if ( $this->node
instanceof DOMNodeList
) {
117 return $this->node
->nodeName
;
122 * Split a "<part>" node into an associative array containing:
124 * - index String index
125 * - value PPNode value
127 * @throws MWException
130 public function splitArg() {
131 $xpath = $this->getXPath();
132 $names = $xpath->query( 'name', $this->node
);
133 $values = $xpath->query( 'value', $this->node
);
134 if ( !$names->length ||
!$values->length
) {
135 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
137 $name = $names->item( 0 );
138 $index = $name->getAttribute( 'index' );
140 'name' => new self( $name ),
142 'value' => new self( $values->item( 0 ) ) ];
146 * Split an "<ext>" node into an associative array containing name, attr, inner and close
147 * All values in the resulting array are PPNodes. Inner and close are optional.
149 * @throws MWException
152 public function splitExt() {
153 $xpath = $this->getXPath();
154 $names = $xpath->query( 'name', $this->node
);
155 $attrs = $xpath->query( 'attr', $this->node
);
156 $inners = $xpath->query( 'inner', $this->node
);
157 $closes = $xpath->query( 'close', $this->node
);
158 if ( !$names->length ||
!$attrs->length
) {
159 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
162 'name' => new self( $names->item( 0 ) ),
163 'attr' => new self( $attrs->item( 0 ) ) ];
164 if ( $inners->length
) {
165 $parts['inner'] = new self( $inners->item( 0 ) );
167 if ( $closes->length
) {
168 $parts['close'] = new self( $closes->item( 0 ) );
175 * @throws MWException
178 public function splitHeading() {
179 if ( $this->getName() !== 'h' ) {
180 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
183 'i' => $this->node
->getAttribute( 'i' ),
184 'level' => $this->node
->getAttribute( 'level' ),
185 'contents' => $this->getChildren()