Skip to content

Commit 747335f

Browse files
committed
Fix GH-12170: Can't use xpath with comments in SimpleXML
Closes GH-12177.
1 parent d0b76d7 commit 747335f

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

NEWS

+3
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
28 Sep 2023, PHP 8.1.24
912

1013
- Core:

ext/simplexml/simplexml.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ PHP_METHOD(SimpleXMLElement, xpath)
13221322

13231323
for (i = 0; i < result->nodeNr; ++i) {
13241324
nodeptr = result->nodeTab[i];
1325-
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE || nodeptr->type == XML_PI_NODE) {
1325+
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) {
13261326
/**
13271327
* Detect the case where the last selector is text(), simplexml
13281328
* always accesses the text() child by default, therefore we assign

ext/simplexml/tests/bug12170.phpt

+52
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)