Skip to content

Fix GH-12170: Can't use xpath with comments in SimpleXML #12177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ PHP_METHOD(SimpleXMLElement, xpath)

for (i = 0; i < result->nodeNr; ++i) {
nodeptr = result->nodeTab[i];
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE || nodeptr->type == XML_PI_NODE) {
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) {
/**
* Detect the case where the last selector is text(), simplexml
* always accesses the text() child by default, therefore we assign
Expand Down
52 changes: 52 additions & 0 deletions ext/simplexml/tests/bug12170.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
Bug GH-12170 (Can't use xpath with comments in SimpleXML)
--EXTENSIONS--
simplexml
--FILE--
<?php

$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>text node</bar>
<bar><!-- baz --></bar>
<bar><!-- foo --></bar>
</foo>
XML;

$sxe = simplexml_load_string($xml);

var_dump(
$sxe->xpath('//bar')
);

foreach ($sxe->xpath('//comment()') as $comment) {
var_dump($comment->getName());
var_dump($comment->asXML());
}

?>
--EXPECT--
array(3) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(9) "text node"
}
[1]=>
object(SimpleXMLElement)#3 (1) {
["comment"]=>
object(SimpleXMLElement)#5 (0) {
}
}
[2]=>
object(SimpleXMLElement)#4 (1) {
["comment"]=>
object(SimpleXMLElement)#5 (0) {
}
}
}
string(7) "comment"
string(12) "<!-- baz -->"
string(7) "comment"
string(12) "<!-- foo -->"