Skip to content

Commit e5252a3

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix phpGH-12170: Can't use xpath with comments in SimpleXML
2 parents b2d244a + 60b1673 commit e5252a3

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

ext/simplexml/simplexml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ PHP_METHOD(SimpleXMLElement, xpath)
13001300

13011301
for (i = 0; i < result->nodeNr; ++i) {
13021302
nodeptr = result->nodeTab[i];
1303-
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE || nodeptr->type == XML_PI_NODE) {
1303+
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE || nodeptr->type == XML_PI_NODE || nodeptr->type == XML_COMMENT_NODE) {
13041304
/**
13051305
* Detect the case where the last selector is text(), simplexml
13061306
* always accesses the text() child by default, therefore we assign

ext/simplexml/tests/bug12170.phpt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
Bug GH-12170 (Can't use xpath with comments in SimpleXML)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = <<<XML
9+
<?xml version="1.0" encoding="utf-8"?>
10+
<foo>
11+
<bar>text node</bar>
12+
<bar><!-- baz --></bar>
13+
<bar><!-- foo --></bar>
14+
</foo>
15+
XML;
16+
17+
$sxe = simplexml_load_string($xml);
18+
19+
var_dump(
20+
$sxe->xpath('//bar')
21+
);
22+
23+
foreach ($sxe->xpath('//comment()') as $comment) {
24+
var_dump($comment->getName());
25+
var_dump($comment->asXML());
26+
}
27+
28+
?>
29+
--EXPECT--
30+
array(3) {
31+
[0]=>
32+
object(SimpleXMLElement)#2 (1) {
33+
[0]=>
34+
string(9) "text node"
35+
}
36+
[1]=>
37+
object(SimpleXMLElement)#3 (1) {
38+
["comment"]=>
39+
object(SimpleXMLElement)#5 (0) {
40+
}
41+
}
42+
[2]=>
43+
object(SimpleXMLElement)#4 (1) {
44+
["comment"]=>
45+
object(SimpleXMLElement)#5 (0) {
46+
}
47+
}
48+
}
49+
string(7) "comment"
50+
string(12) "<!-- baz -->"
51+
string(7) "comment"
52+
string(12) "<!-- foo -->"

0 commit comments

Comments
 (0)