Skip to content

Commit 60b1673

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-12170: Can't use xpath with comments in SimpleXML
2 parents b15e9b5 + b3bce23 commit 60b1673

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP NEWS
55
- Filter:
66
. Fix explicit FILTER_REQUIRE_SCALAR with FILTER_CALLBACK (ilutov)
77

8+
- SimpleXML:
9+
. Fixed bug GH-12170 (Can't use xpath with comments in SimpleXML). (nielsdos)
10+
811
14 Sep 2023, PHP 8.3.0RC2
912

1013
- Core:

ext/simplexml/simplexml.c

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

13141314
for (i = 0; i < result->nodeNr; ++i) {
13151315
nodeptr = result->nodeTab[i];
1316-
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE || nodeptr->type == XML_PI_NODE) {
1316+
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) {
13171317
/**
13181318
* Detect the case where the last selector is text(), simplexml
13191319
* 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)